001: package org.igfay.jfig;
002:
003: import java.io.File;
004: import java.util.List;
005: import java.util.Properties;
006:
007: import junit.framework.TestCase;
008:
009: import org.apache.log4j.Logger;
010: import org.igfay.util.Metric;
011: import org.igfay.util.PropertyUtility;
012:
013: /**
014: * JFigTester
015: * Note: Start with args -Dconfig.filename=bconrad.config.xml
016: *@author bconrad
017: *@created April 3, 2001
018: */
019: public class JFigTester extends TestCase implements JFigListener {
020: private static Logger log = Logger.getLogger(JFigTester.class);
021: private static String testValue;
022: private static boolean isReprocessed = false;
023:
024: public JFigTester() {
025: super (null);
026: }
027:
028: public JFigTester(String arg0) {
029: super (arg0);
030: }
031:
032: public static void main(String args[]) {
033: while (true) {//for (int i = 0; i < 20; i++) {
034: try {
035: JFig.initialize();
036: JFig.getInstance().print();
037: } catch (JFigException e) {
038: log.debug("Exception " + e.getMessage(), e);
039: }
040:
041: }
042: }
043:
044: public static void xtestSubsection() {
045: log.info("---------------");
046: String originalFilename = PropertyUtility
047: .getProperty("config.filename");
048: PropertyUtility.setProperty("config.filename", "sub.xml");
049: JFig.getInstance().print();
050: assertTrue(true);
051: PropertyUtility
052: .setProperty("config.filename", originalFilename);
053: }
054:
055: public static void testClasspath() {
056: log.info("---------------");
057: String originalFilename = PropertyUtility
058: .getProperty("config.filename");
059: PropertyUtility.setProperty("config.filename",
060: "inclasspath.config.xml");
061: JFig.getInstance().print();
062: assertTrue(true);
063: PropertyUtility
064: .setProperty("config.filename", originalFilename);
065: }
066:
067: public static void testConfigLocationFile() {
068: log.info("---------------");
069: String originalFilename = PropertyUtility
070: .getProperty("config.filename");
071: PropertyUtility.setProperty("config.location", "file");
072: PropertyUtility
073: .setProperty("config.filename",
074: "C:/JavaTools/eclipse_work/jfig/etc/bconrad.config.xml");
075: JFig.getInstance().print();
076: assertTrue(true);
077: PropertyUtility.setProperty("config.location", "classpath");
078: PropertyUtility
079: .setProperty("config.filename", originalFilename);
080: }
081:
082: public static String getTestValue() {
083: log.info("---------------");
084: if (testValue == null) {
085: testValue = JFig.getInstance().getValue("bc", "instance",
086: "");
087: }
088: return testValue;
089: }
090:
091: public static void testBasic() {
092: log.info("---------------");
093: try {
094: JFig.getInstance().reprocessConfiguration();
095: } catch (JFigException e) {
096: log.debug("Exception " + e.getMessage(), e);
097: }
098: String value = JFig.getInstance().getValue("myproject",
099: "instance", "xdefault");
100: log.info("value: " + value);
101: assertTrue("development".equalsIgnoreCase(value));
102: }
103:
104: public static void testDefault() {
105: log.info("---------------");
106: String defaultValue = "xdefault";
107: String value = JFig.getInstance().getValue("xmyProject",
108: "xinstance", defaultValue);
109: log.info("value: " + value);
110: assertTrue(value.equalsIgnoreCase(defaultValue));
111: }
112:
113: public static void testProperties() {
114: log.info("---------------");
115: String propertyName = "authentication.isCheckingApprovalPermission";
116: String value = System.getProperty(propertyName);
117:
118: log.info("value: " + value);
119: assertTrue("false".equalsIgnoreCase(value));
120: }
121:
122: public static void testSubstitution() {
123: log.info("---------------");
124: String expectedResult = "d:/Development/myproject/";
125: String value = JFig.getInstance().getValue("paths",
126: "myprojectHome", "");
127:
128: log.info("value: " + value);
129: assertTrue(expectedResult.equalsIgnoreCase(value));
130: }
131:
132: public static void testDoubleSubstitution() {
133: log.info("---------------");
134: String expectedResult = "d:/Development/myproject/downloads/";
135: JFig.getInstance().print();
136: String value = JFig.getInstance().getValue("paths",
137: "downloadDirectory", "");
138:
139: log.info("value: " + value);
140: assertTrue(expectedResult.equalsIgnoreCase(value));
141: }
142:
143: public static void testPrint() {
144: log.info("---------------");
145: JFig.getInstance().print();
146: assertTrue(true);
147: }
148:
149: public static void testGetValuesStartingWith() {
150: log.info("---------------");
151: String dummySection = "dummySection";
152: String dummyValue = "dummyValue.";
153: int numberOfValues = 3;
154: for (int i = 0; i < numberOfValues; i++) {
155: JFig.getInstance().setConfigurationValue(dummySection,
156: dummyValue + i, "" + i);
157: }
158: JFig.getInstance().print();
159:
160: List list = JFig.getInstance().getValuesStartingWith(
161: dummySection, dummyValue);
162: log.debug("size" + list.size());
163: for (int i = 0; i < list.size(); i++) {
164: log.debug(i + " " + list.get(i));
165: }
166: assertTrue(list.size() == numberOfValues);
167: }
168:
169: public static void testUsingProperties() {
170: log.info("---------------");
171: String unexpectedDefault = "default";
172: String homeDir = JFig.getInstance().getValue("paths",
173: "homeDir", unexpectedDefault);
174: log.debug("homeDir " + homeDir);
175: assertTrue(homeDir.indexOf("user.home") < 0);
176:
177: String documentDir = JFig.getInstance().getValue("paths",
178: "documentDir", unexpectedDefault);
179: log.debug("documentDir " + documentDir);
180: assertTrue(homeDir.indexOf(unexpectedDefault) < 0);
181: }
182:
183: public static void testSettingProperties() {
184: log.info("---------------");
185:
186: String expectedValue = "beagle";
187: Properties pr = System.getProperties();
188: String value = pr.getProperty("riley");
189: log
190: .debug("value " + value + " expectedValue "
191: + expectedValue);
192: assertTrue(value.indexOf(expectedValue) == 0);
193: }
194:
195: public static void testSectionAsProperties() {
196: log.info("---------------");
197: Properties properties = JFig.getInstance()
198: .getSectionAsProperties("myProject");
199: String value = properties.getProperty("instance");
200: log.debug("value " + value);
201: assertTrue(value != null);
202:
203: properties = JFig.getInstance().getSectionAsProperties(
204: "logging");
205: value = properties.getProperty("logDirectory");
206: log.debug("value " + value);
207: assertTrue(value == null);
208: }
209:
210: public static void testNullValue() {
211: log.info("---------------");
212:
213: String value;
214: try {
215: value = JFig.getInstance().getValue("logging",
216: "logDirectory");
217: log.debug("value " + value);
218: assertTrue(false);
219: } catch (JFigException e) {
220: assertTrue(true);
221: }
222: }
223:
224: public static void testReprocessAndNotify() {
225: log.info("---------------");
226: JFigTester tester = new JFigTester();
227: JFig.getInstance().addConfigEventListener(tester);
228: // verify that we have not yet reprocessed
229: assertTrue(!isReprocessed);
230:
231: // set a dummy key/value and verify that it set
232: String dummySection = "dummySection";
233: String dummyValue = "dummyValue";
234: JFig.getInstance().setConfigurationValue(dummySection,
235: dummySection, dummyValue);
236: String value = JFig.getInstance().getValue(dummySection,
237: dummySection, dummySection);
238: assertTrue(value.equalsIgnoreCase(dummyValue));
239:
240: // reprocess and verify
241: try {
242: JFig.getInstance().reprocessConfiguration();
243: assertTrue(isReprocessed);
244: } catch (JFigException e) {
245: log.warn("JFigException " + e.getMessage());
246: assertTrue(false);
247: }
248:
249: // verify that dummy value is unset
250: value = JFig.getInstance().getValue(dummySection, dummySection,
251: dummySection);
252: assertTrue(!value.equalsIgnoreCase(dummyValue));
253: }
254:
255: public void testException() {
256: log.info("---------------");
257: String dummySection = "dummySection";
258: String dummyValue = "dummyValue";
259:
260: try {
261: // test exception
262: String value = JFig.getInstance().getValue(dummySection,
263: dummyValue);
264: log.info("value: " + value);
265: } catch (JFigException e) {
266: log.info("catch JFigException e:\n " + e);
267: assertTrue(true);
268: return;
269: }
270: // should not get here
271: assertTrue(false);
272: }
273:
274: public static void testConfigFileNotFound() {
275: log.info("---------------");
276: String originalFilename = PropertyUtility
277: .getProperty("config.filename");
278: JFig.setInstance(null);
279: PropertyUtility.setProperty("config.filename",
280: "youcantfindme.xml");
281: JFig.getInstance().print();
282: String defaultValue = "xdefault";
283: String value = JFig.getInstance().getValue("myproject",
284: "instance", defaultValue);
285: log.info("value: " + value);
286: assertTrue(value.equalsIgnoreCase(defaultValue));
287:
288: PropertyUtility
289: .setProperty("config.filename", originalFilename);
290: }
291:
292: public static void testReprocessConfigFileNotFound() {
293: log.info("---------------");
294: String originalFilename = PropertyUtility
295: .getProperty("config.filename");
296: JFigIF instance = JFig.getInstance();
297: PropertyUtility.setProperty("config.filename",
298: "youcantfindme.xml");
299: // reprocess and verify...check for null for JUnit testing
300: try {
301: // reprocessConfiguration should throw exception
302: JFig.getInstance().reprocessConfiguration();
303: assertTrue(false);
304: } catch (JFigException e) {
305: log.warn("JFigException " + e.getMessage());
306: // original saved instance should be unchanged.
307: assertTrue(JFig.getInstance() == instance);
308: }
309: PropertyUtility
310: .setProperty("config.filename", originalFilename);
311: }
312:
313: public static void testInitializeException() {
314: log.info("---------------");
315: String originalFilename = PropertyUtility
316: .getProperty("config.filename");
317: PropertyUtility.setProperty("config.filename",
318: "youcantfindme.xml");
319: try {
320: JFig.initialize();
321: assertTrue(false);
322: } catch (JFigException e) {
323: log.warn("JFigException " + e.getMessage());
324: assertTrue(true);
325: }
326: PropertyUtility
327: .setProperty("config.filename", originalFilename);
328: }
329:
330: public static void testInitializeIncludeException() {
331: log.info("---------------");
332: String originalFilename = PropertyUtility
333: .getProperty("config.filename");
334: PropertyUtility.setProperty("config.filename",
335: "includeNotFound.xml");
336: try {
337: JFig.initialize();
338: assertTrue(false);
339: } catch (JFigException e) {
340: log.warn("JFigException " + e.getMessage());
341: assertTrue(true);
342: }
343: PropertyUtility
344: .setProperty("config.filename", originalFilename);
345: try {
346: // reprocessConfiguration should throw exception
347: JFig.initialize().reprocessConfiguration();
348: } catch (JFigException e) {
349: log.warn("JFIgException", e);
350: }
351:
352: }
353:
354: public static void testConvert() {
355: log.info("---------------");
356: JFigConverter jsh = new JFigConverter();
357: String string = "abcd-[authentication]{isCheckingApprovalPermission}-efg"
358: + " userHome: $user.home$ endUserHome";
359:
360: try {
361: String newString = jsh.convert(string);
362: log.info("newString:" + newString);
363: assertTrue(true);
364: } catch (JFigException e) {
365: assertTrue(false);
366: }
367: }
368:
369: public static void testConvertFile() {
370: log.info("---------------");
371: JFigConverter jfc = new JFigConverter();
372: File inFile = new File("c:/jfig/etc/myproject.template1");
373: File outFile = new File(
374: "c:/jfig/etc/myproject.template.convert");
375: try {
376: log.info("inFile: " + inFile.getName());
377:
378: String newString = jfc.convert(inFile, outFile);
379: log.info("newString:\n" + newString);
380: assertTrue(true);
381: } catch (JFigException e) {
382: assertTrue(false);
383: }
384: }
385:
386: public static void testConvertFileEdh() {
387: log.info("---------------");
388: String originalFilename = PropertyUtility
389: .getProperty("config.filename");
390: PropertyUtility
391: .setProperty("config.filename", "edh.config.xml");
392: // reprocess and verify...check for null for JUnit testing
393: try {
394: // reprocessConfiguration should throw exception
395: JFig.initialize().reprocessConfiguration();
396: } catch (JFigException e) {
397: log.warn("JFIgException", e);
398: }
399: JFigConverter jfc = new JFigConverter();
400: File inFile = new File("c:/jfig/etc/edh.in.properties");
401: File outFile = new File("c:/jfig/etc/out.properties");
402: try {
403:
404: String newString = jfc.convert(inFile, outFile);
405: log.info("newString:\n" + newString);
406: assertTrue(true);
407: } catch (JFigException e) {
408: assertTrue(false);
409: }
410: PropertyUtility
411: .setProperty("config.filename", originalFilename);
412: try {
413: // reprocessConfiguration should throw exception
414: JFig.initialize().reprocessConfiguration();
415: } catch (JFigException e) {
416: log.warn("JFIgException", e);
417: }
418:
419: }
420:
421: public static void testSchmoopsie() {
422: log.info("---------------");
423: String originalFilename = PropertyUtility
424: .getProperty("config.filename");
425: PropertyUtility.setProperty("config.filename",
426: "schmoopsie.config.xml");
427: try {
428: JFig.initialize();
429: JFig.getInstance().print();
430: assertTrue(true);
431: } catch (JFigException e) {
432: log.warn("JFigException " + e.getMessage());
433: assertTrue(false);
434: }
435: PropertyUtility
436: .setProperty("config.filename", originalFilename);
437: }
438:
439: public static void xtestPerformance() {
440:
441: log.info("---------------");
442: // JFig.setProjectName("test");
443: JFig.getInstance();
444: log.info("---------------");
445: double iterator = 1000000;
446:
447: Metric metric0 = new Metric(Metric.DEFAULT, "loopOnly", true);
448: for (int i = 0; i < iterator; i++) {
449: }
450: metric0.endAndPrint();
451:
452: Metric metric = new Metric(Metric.DEFAULT, "getValue", true);
453: long start = System.currentTimeMillis();
454: for (int i = 0; i < iterator; i++) {
455: JFig.getInstance().getValue("bc", "instance", "");
456: }
457: metric.endAndPrint();
458: log.info("elapse " + metric.getElapsedTime());
459:
460: start = System.currentTimeMillis();
461: Metric metric2 = new Metric(Metric.DEFAULT, "saveValue", true);
462: for (int i = 0; i < iterator; i++) {
463: getTestValue();
464: }
465: metric2.endAndPrint();
466: double average = metric2.getElapsedTime() / iterator;
467: log.info("average " + average + " elapse "
468: + metric2.getElapsedTime());
469:
470: Metric metric3 = new Metric(Metric.DEFAULT, "oneTime", true);
471: for (int i = 0; i < 0; i++) {
472: JFig.getInstance().getValue("bc", "instance", "");
473: // this takes 10ms
474: }
475: metric3.endAndPrint();
476: log.info("elapse " + (start - System.currentTimeMillis()));
477:
478: log.info("elapse " + metric3.getElapsedTime());
479:
480: Metric metric4 = new Metric(Metric.DEFAULT, "oneTimeNoLoop",
481: true);
482: start = System.currentTimeMillis();
483: JFig.getInstance().getValue("bc", "instance", "");
484: long elapse = start - System.currentTimeMillis();
485: metric4.endAndPrint();
486: log.info("elapse " + metric4.getElapsedTime());
487: }
488:
489: public static void xtestPerformanceSimple() {
490:
491: // JFig.setProjectName("test");
492: log.info("---------------");
493: double iterations = 1000000;
494:
495: Metric metric = new Metric(Metric.DEFAULT, "getValue", true);
496: long start = System.currentTimeMillis();
497: for (int i = 0; i < iterations; i++) {
498: JFig.getInstance().getValue("bc", "instance", "");
499: }
500: metric.endAndPrint();
501: double average = metric.getElapsedTime() / iterations;
502: log.info("average " + average + " elapse "
503: + metric.getElapsedTime() + " iterations "
504: + iterations);
505:
506: }
507:
508: /**
509: * @see gov.llnl.ais.util.JFig.JFigListener#JFigurationUpdate(gov.llnl.ais.util.JFig.JFigEvent)
510: */
511: public void configurationUpdate(JFigEvent event) {
512: isReprocessed = true;
513: }
514:
515: }
|