76 add(p, n, load(f), f.getPath());
77 }
78 }
79 }
80 finally {
81 propTables = new Map[p.size()];
82 p.copyInto(propTables);
83 propTableNames = new String[n.size()];
84 n.copyInto(propTableNames);
85 updateEnvTable();
86 }
87 }
88
89 /**
90 * Create a context from a specified set of named tables.
91 * @param tables An array of tables giving the properties to be read
92 * @param tableNames An array of names, one for each table in the tables array,
93 * that will be used to identify the source of the properties in any
94 * environments that are created
95 */
96 public TestEnvContext(Map[] tables, String[] tableNames) {
97 Vector<String> n = new Vector<>();
98 Vector<Map<String, String>> p = new Vector<>();
99 for (int i = 0; i < tables.length; i++) {
100 add(p, n, tables[i], tableNames[i]);
101 }
102 propTables = new Map[p.size()];
103 p.copyInto(propTables);
104 propTableNames = new String[n.size()];
105 n.copyInto(propTableNames);
106 updateEnvTable();
107 }
108
109 /**
110 * Create a context from a named table.
111 * @param table A table giving the properties to be read
112 * @param tableName The name that will be used to identify the source
113 * of the properties in any environments that are created.
114 */
115 public TestEnvContext(Map table, String tableName) {
116 Vector<String> n = new Vector<>();
117 Vector<Map<String, String>> p = new Vector<>();
118 add(p, n, table, tableName);
119 propTables = new Map[p.size()];
120 p.copyInto(propTables);
121 propTableNames = new String[n.size()];
122 n.copyInto(propTableNames);
123 updateEnvTable();
124 }
125
126 /**
127 * Get a environment from this set of environments.
128 * @param name the name of the desired environment
129 * @return the specified environment, or null if not found
130 * @throws TestEnvironment.Fault if there is a problem creating
131 * the specified environment
132 */
133 public TestEnvironment getEnv(String name) throws TestEnvironment.Fault {
134 if (isValidEnv(name))
135 return new TestEnvironment(name, propTables, propTableNames);
218 new Object[] {f, e.getMessage()});
219 }
220 */
221 /*
222 catch (InstantiationException e) {
223 throw new Fault(i18n, "tec.interviewClassInstantiation",
224 new Object[] {f, e.getMessage()});
225 }
226 */
227 }
228
229 private void add(Vector<Map<String, String>> pv, Vector<String> nv, Map<String, String> p, String n) {
230 if (p != null) {
231 pv.addElement(p);
232 nv.addElement(n);
233 }
234 }
235
236 private void updateEnvTable() {
237 // the tables given to the constructor ...
238 Map[] tables = propTables;
239 String[] tableNames = propTableNames;
240
241 // defaults given to TestEnvironment
242 Map[] defaultTables = TestEnvironment.defaultPropTables;
243 String[] defaultTableNames = TestEnvironment.defaultPropTableNames;
244
245 // if there are defaults, merge them with the TestEnvContext tables
246 // for the purposes of determining the EnvTable
247 if (defaultTables != null && defaultTables.length > 0) {
248 tables = DynamicArray.join(defaultTables, tables);
249 tableNames = DynamicArray.join(defaultTableNames, tableNames);
250 }
251
252 Vector<String> allVec = new Vector<>();
253 Vector<String> menuExcludeVec = new Vector<>();
254
255 // scan all the property tables, looking for entries
256 final String ENV_DOT = "env.";
257 final String DOT_DESCRIPTION = ".description";
258 final String DOT_FINDER = ".finder";
259 final String DOT_SCRIPT = ".script";
260 final String DOT_SCRIPT_DOT = ".script.";
261 final String DOT_INHERITS = ".inherits";
262 final String DOT_MENU = ".menu";
263 final String DOT_TESTSUITE = ".testsuite";
264
265 if (debug)
266 System.err.println(getClass().getName() + ": trace");
267
268 for (int i = 0; i < tables.length; i++) {
269 if (debug)
270 System.err.println("Checking " + tableNames[i] + " for environments...");
271
272 Map table = tables[i];
273 for (Iterator ii = table.keySet().iterator(); ii.hasNext(); ) {
274 String prop = (String) (ii.next());
275 String name = null;
276
277 if (debug)
278 System.err.println("Checking property " + prop);
279
280 if (!prop.startsWith(ENV_DOT))
281 continue;
282
283 if (prop.endsWith(DOT_INHERITS)) {
284 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_INHERITS.length());
285 }
286 else if (prop.endsWith(DOT_MENU)) {
287 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_MENU.length());
288 String value = (String) (table.get(prop));
289 if ("false".equals(value))
290 sortedInsert(menuExcludeVec, name);
291 }
292 else if (prop.endsWith(DOT_DESCRIPTION)) {
293 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_DESCRIPTION.length());
294 }
295 else if (prop.endsWith(DOT_FINDER)) {
296 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_FINDER.length());
297 }
298 else if (prop.endsWith(DOT_SCRIPT)) {
299 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_SCRIPT.length());
300 }
301 else if (prop.endsWith(DOT_TESTSUITE)) {
302 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_TESTSUITE.length());
303 }
304 else {
305 int lastDot = prop.lastIndexOf('.');
306 int scriptStartIndex = lastDot - DOT_SCRIPT_DOT.length() + 1;
307 if (scriptStartIndex > 0 &&
308 prop.regionMatches(scriptStartIndex, DOT_SCRIPT_DOT, 0, DOT_SCRIPT_DOT.length())) {
324 Vector<String> menuVec = new Vector<String>(allVec);
325 for (int i = 0; i < menuExcludeVec.size(); i++)
326 menuVec.removeElement(menuExcludeVec.elementAt(i));
327 envMenuNames = new String[menuVec.size()];
328 menuVec.copyInto(envMenuNames);
329 }
330
331 private void sortedInsert(Vector<String> v, String s) {
332 for (int i = 0; i < v.size(); i++) {
333 int c = s.compareTo(v.elementAt(i));
334 if (c > 0) {
335 v.insertElementAt(s, i);
336 return;
337 }
338 else if (c == 0)
339 return;
340 }
341 v.addElement(s);
342 }
343
344 private Map[] propTables;
345 private String[] propTableNames;
346 private String[] envNames;
347 private String[] envMenuNames;
348
349 private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(TestEnvContext.class);
350 private static boolean debug = Boolean.getBoolean("debug." + TestEnvContext.class.getName());
351 }
|
76 add(p, n, load(f), f.getPath());
77 }
78 }
79 }
80 finally {
81 propTables = new Map[p.size()];
82 p.copyInto(propTables);
83 propTableNames = new String[n.size()];
84 n.copyInto(propTableNames);
85 updateEnvTable();
86 }
87 }
88
89 /**
90 * Create a context from a specified set of named tables.
91 * @param tables An array of tables giving the properties to be read
92 * @param tableNames An array of names, one for each table in the tables array,
93 * that will be used to identify the source of the properties in any
94 * environments that are created
95 */
96 public TestEnvContext(Map<String, String>[] tables, String[] tableNames) {
97 Vector<String> n = new Vector<>();
98 Vector<Map<String, String>> p = new Vector<>();
99 for (int i = 0; i < tables.length; i++) {
100 add(p, n, tables[i], tableNames[i]);
101 }
102 propTables = new Map[p.size()];
103 p.copyInto(propTables);
104 propTableNames = new String[n.size()];
105 n.copyInto(propTableNames);
106 updateEnvTable();
107 }
108
109 /**
110 * Create a context from a named table.
111 * @param table A table giving the properties to be read
112 * @param tableName The name that will be used to identify the source
113 * of the properties in any environments that are created.
114 */
115 public TestEnvContext(Map<String, String> table, String tableName) {
116 Vector<String> n = new Vector<>();
117 Vector<Map<String, String>> p = new Vector<>();
118 add(p, n, table, tableName);
119 propTables = new Map[p.size()];
120 p.copyInto(propTables);
121 propTableNames = new String[n.size()];
122 n.copyInto(propTableNames);
123 updateEnvTable();
124 }
125
126 /**
127 * Get a environment from this set of environments.
128 * @param name the name of the desired environment
129 * @return the specified environment, or null if not found
130 * @throws TestEnvironment.Fault if there is a problem creating
131 * the specified environment
132 */
133 public TestEnvironment getEnv(String name) throws TestEnvironment.Fault {
134 if (isValidEnv(name))
135 return new TestEnvironment(name, propTables, propTableNames);
218 new Object[] {f, e.getMessage()});
219 }
220 */
221 /*
222 catch (InstantiationException e) {
223 throw new Fault(i18n, "tec.interviewClassInstantiation",
224 new Object[] {f, e.getMessage()});
225 }
226 */
227 }
228
229 private void add(Vector<Map<String, String>> pv, Vector<String> nv, Map<String, String> p, String n) {
230 if (p != null) {
231 pv.addElement(p);
232 nv.addElement(n);
233 }
234 }
235
236 private void updateEnvTable() {
237 // the tables given to the constructor ...
238 Map<String, String>[] tables = propTables;
239 String[] tableNames = propTableNames;
240
241 // defaults given to TestEnvironment
242 Map<String, String>[] defaultTables = TestEnvironment.defaultPropTables;
243 String[] defaultTableNames = TestEnvironment.defaultPropTableNames;
244
245 // if there are defaults, merge them with the TestEnvContext tables
246 // for the purposes of determining the EnvTable
247 if (defaultTables != null && defaultTables.length > 0) {
248 tables = DynamicArray.join(defaultTables, tables);
249 tableNames = DynamicArray.join(defaultTableNames, tableNames);
250 }
251
252 Vector<String> allVec = new Vector<>();
253 Vector<String> menuExcludeVec = new Vector<>();
254
255 // scan all the property tables, looking for entries
256 final String ENV_DOT = "env.";
257 final String DOT_DESCRIPTION = ".description";
258 final String DOT_FINDER = ".finder";
259 final String DOT_SCRIPT = ".script";
260 final String DOT_SCRIPT_DOT = ".script.";
261 final String DOT_INHERITS = ".inherits";
262 final String DOT_MENU = ".menu";
263 final String DOT_TESTSUITE = ".testsuite";
264
265 if (debug)
266 System.err.println(getClass().getName() + ": trace");
267
268 for (int i = 0; i < tables.length; i++) {
269 if (debug)
270 System.err.println("Checking " + tableNames[i] + " for environments...");
271
272 Map<String, String> table = tables[i];
273 for (Iterator<String> ii = table.keySet().iterator(); ii.hasNext(); ) {
274 String prop = (ii.next());
275 String name = null;
276
277 if (debug)
278 System.err.println("Checking property " + prop);
279
280 if (!prop.startsWith(ENV_DOT))
281 continue;
282
283 if (prop.endsWith(DOT_INHERITS)) {
284 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_INHERITS.length());
285 }
286 else if (prop.endsWith(DOT_MENU)) {
287 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_MENU.length());
288 String value = (table.get(prop));
289 if ("false".equals(value))
290 sortedInsert(menuExcludeVec, name);
291 }
292 else if (prop.endsWith(DOT_DESCRIPTION)) {
293 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_DESCRIPTION.length());
294 }
295 else if (prop.endsWith(DOT_FINDER)) {
296 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_FINDER.length());
297 }
298 else if (prop.endsWith(DOT_SCRIPT)) {
299 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_SCRIPT.length());
300 }
301 else if (prop.endsWith(DOT_TESTSUITE)) {
302 name = prop.substring(ENV_DOT.length(), prop.length() - DOT_TESTSUITE.length());
303 }
304 else {
305 int lastDot = prop.lastIndexOf('.');
306 int scriptStartIndex = lastDot - DOT_SCRIPT_DOT.length() + 1;
307 if (scriptStartIndex > 0 &&
308 prop.regionMatches(scriptStartIndex, DOT_SCRIPT_DOT, 0, DOT_SCRIPT_DOT.length())) {
324 Vector<String> menuVec = new Vector<String>(allVec);
325 for (int i = 0; i < menuExcludeVec.size(); i++)
326 menuVec.removeElement(menuExcludeVec.elementAt(i));
327 envMenuNames = new String[menuVec.size()];
328 menuVec.copyInto(envMenuNames);
329 }
330
331 private void sortedInsert(Vector<String> v, String s) {
332 for (int i = 0; i < v.size(); i++) {
333 int c = s.compareTo(v.elementAt(i));
334 if (c > 0) {
335 v.insertElementAt(s, i);
336 return;
337 }
338 else if (c == 0)
339 return;
340 }
341 v.addElement(s);
342 }
343
344 private Map<String, String>[] propTables;
345 private String[] propTableNames;
346 private String[] envNames;
347 private String[] envMenuNames;
348
349 private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(TestEnvContext.class);
350 private static boolean debug = Boolean.getBoolean("debug." + TestEnvContext.class.getName());
351 }
|