KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > igfay > jfig > JFigTester


1 package org.igfay.jfig;
2
3 import java.io.File JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 import junit.framework.TestCase;
8
9 import org.apache.log4j.Logger;
10 import org.igfay.util.Metric;
11 import org.igfay.util.PropertyUtility;
12
13 /**
14  * JFigTester
15  * Note: Start with args -Dconfig.filename=bconrad.config.xml
16  *@author bconrad
17  *@created April 3, 2001
18  */

19 public class JFigTester extends TestCase implements JFigListener {
20     private static Logger log = Logger.getLogger(JFigTester.class);
21     private static String JavaDoc testValue;
22     private static boolean isReprocessed = false;
23
24     public JFigTester() {
25         super(null);
26     }
27
28     public JFigTester(String JavaDoc arg0) {
29         super(arg0);
30     }
31
32    public static void main(String JavaDoc args[]) {
33        while(true) {//for (int i = 0; i < 20; i++) {
34
try {
35             JFig.initialize();
36             JFig.getInstance().print();
37         } catch (JFigException e) {
38             log.debug("Exception "+e.getMessage(), e);
39         }
40         
41     }
42    }
43
44
45
46    public static void xtestSubsection() {
47     log.info("---------------");
48     String JavaDoc originalFilename = PropertyUtility.getProperty("config.filename");
49     PropertyUtility.setProperty("config.filename","sub.xml");
50     JFig.getInstance().print();
51     assertTrue(true);
52     PropertyUtility.setProperty("config.filename",originalFilename);
53    }
54    
55    public static void testClasspath() {
56         log.info("---------------");
57         String JavaDoc originalFilename = PropertyUtility.getProperty("config.filename");
58         PropertyUtility.setProperty("config.filename","inclasspath.config.xml");
59         JFig.getInstance().print();
60         assertTrue(true);
61         PropertyUtility.setProperty("config.filename",originalFilename);
62       }
63       
64       
65    public static void testConfigLocationFile() {
66         log.info("---------------");
67         String JavaDoc originalFilename = PropertyUtility.getProperty("config.filename");
68         PropertyUtility.setProperty("config.location", "file");
69         PropertyUtility.setProperty("config.filename","C:/JavaTools/eclipse_work/jfig/etc/bconrad.config.xml");
70         JFig.getInstance().print();
71         assertTrue(true);
72         PropertyUtility.setProperty("config.location", "classpath");
73         PropertyUtility.setProperty("config.filename",originalFilename);
74       }
75       
76       
77    public static String JavaDoc getTestValue() {
78         log.info("---------------");
79         if (testValue == null) {
80             testValue = JFig.getInstance().getValue("bc", "instance", "");
81         }
82         return testValue;
83     }
84
85     public static void testBasic() {
86         log.info("---------------");
87         try {
88             JFig.getInstance().reprocessConfiguration();
89         } catch (JFigException e) {
90             log.debug("Exception "+e.getMessage(), e);
91         }
92         String JavaDoc value = JFig.getInstance().getValue("myproject", "instance", "xdefault");
93         log.info("value: " + value);
94         assertTrue("development".equalsIgnoreCase(value));
95     }
96
97
98     public static void testDefault() {
99         log.info("---------------");
100         String JavaDoc defaultValue = "xdefault";
101         String JavaDoc value = JFig.getInstance().getValue("xmyProject", "xinstance", defaultValue);
102         log.info("value: " + value);
103         assertTrue(value. equalsIgnoreCase(defaultValue));
104     }
105
106
107     public static void testProperties() {
108         log.info("---------------");
109         String JavaDoc propertyName = "authentication.isCheckingApprovalPermission";
110         String JavaDoc value = System.getProperty(propertyName);
111
112         log.info("value: " + value);
113         assertTrue("false".equalsIgnoreCase(value));
114     }
115
116
117     public static void testSubstitution() {
118         log.info("---------------");
119         String JavaDoc expectedResult = "d:/Development/myproject/";
120         String JavaDoc value = JFig.getInstance().getValue("paths", "myprojectHome", "");
121         
122         log.info("value: " + value);
123         assertTrue(expectedResult.equalsIgnoreCase(value));
124     }
125
126
127     public static void testDoubleSubstitution() {
128         log.info("---------------");
129         String JavaDoc expectedResult = "d:/Development/myproject/downloads/";
130         JFig.getInstance().print();
131         String JavaDoc value = JFig.getInstance().getValue("paths", "downloadDirectory", "");
132         
133         log.info("value: " + value);
134         assertTrue(expectedResult.equalsIgnoreCase(value));
135     }
136
137
138     public static void testPrint() {
139         log.info("---------------");
140         JFig.getInstance().print();
141         assertTrue(true);
142     }
143     
144
145     public static void testGetValuesStartingWith() {
146         log.info("---------------");
147         String JavaDoc dummySection = "dummySection";
148         String JavaDoc dummyValue = "dummyValue.";
149         int numberOfValues = 3;
150         for (int i=0;i<numberOfValues;i++) {
151             JFig.getInstance().setConfigurationValue(dummySection, dummyValue+i, ""+i);
152         }
153         JFig.getInstance().print();
154
155         List JavaDoc list = JFig.getInstance().getValuesStartingWith(dummySection, dummyValue);
156         log.debug("size"+list.size());
157         for (int i=0; i<list.size(); i++) {
158             log.debug(i+" "+list.get(i));
159         }
160         assertTrue(list.size() == numberOfValues);
161     }
162
163     public static void testUsingProperties() {
164         log.info("---------------");
165         String JavaDoc unexpectedDefault = "default";
166         String JavaDoc homeDir = JFig.getInstance().getValue("paths", "homeDir", unexpectedDefault);
167         log.debug("homeDir "+homeDir);
168         assertTrue(homeDir.indexOf("user.home") < 0);
169
170         String JavaDoc documentDir = JFig.getInstance().getValue("paths", "documentDir", unexpectedDefault);
171         log.debug("documentDir "+documentDir);
172         assertTrue(homeDir.indexOf(unexpectedDefault) < 0);
173     }
174
175     public static void testSettingProperties() {
176         log.info("---------------");
177
178         String JavaDoc expectedValue = "beagle";
179         Properties JavaDoc pr = System.getProperties();
180         String JavaDoc value = pr.getProperty("riley");
181         log.debug("value "+value+" expectedValue "+expectedValue);
182         assertTrue(value.indexOf(expectedValue) == 0);
183     }
184
185     public static void testSectionAsProperties() {
186         log.info("---------------");
187         Properties JavaDoc properties = JFig.getInstance().getSectionAsProperties("myProject");
188         String JavaDoc value = properties.getProperty("instance");
189         log.debug("value "+value);
190         assertTrue(value != null);
191
192
193         properties = JFig.getInstance().getSectionAsProperties("logging");
194         value = properties.getProperty("logDirectory");
195         log.debug("value "+value);
196         assertTrue(value == null);
197     }
198
199     public static void testNullValue() {
200         log.info("---------------");
201         
202         String JavaDoc value;
203         try {
204             value = JFig.getInstance().getValue("logging", "logDirectory");
205             log.debug("value "+value);
206             assertTrue(false);
207         } catch (JFigException e) {
208             assertTrue(true);
209         }
210     }
211
212     public static void testReprocessAndNotify() {
213         log.info("---------------");
214         JFigTester tester = new JFigTester();
215         JFig.getInstance().addConfigEventListener(tester);
216         // verify that we have not yet reprocessed
217
assertTrue(!isReprocessed);
218         
219         // set a dummy key/value and verify that it set
220
String JavaDoc dummySection = "dummySection";
221         String JavaDoc dummyValue = "dummyValue";
222         JFig.getInstance().setConfigurationValue(dummySection, dummySection, dummyValue);
223         String JavaDoc value = JFig.getInstance().getValue(dummySection, dummySection, dummySection);
224         assertTrue(value.equalsIgnoreCase(dummyValue));
225         
226         // reprocess and verify
227
try {
228             JFig.getInstance().reprocessConfiguration();
229             assertTrue(isReprocessed);
230         } catch(JFigException e) {
231             log.warn("JFigException "+e.getMessage());
232             assertTrue(false);
233         }
234
235         // verify that dummy value is unset
236
value = JFig.getInstance().getValue(dummySection, dummySection, dummySection);
237         assertTrue(!value.equalsIgnoreCase(dummyValue));
238     }
239
240     public void testException() {
241         log.info("---------------");
242         String JavaDoc dummySection = "dummySection";
243         String JavaDoc dummyValue = "dummyValue";
244     
245         try {
246             // test exception
247
String JavaDoc value = JFig.getInstance().getValue(dummySection, dummyValue);
248             log.info("value: " + value);
249         } catch (JFigException e) {
250             log.info("catch JFigException e:\n " + e);
251             assertTrue(true);
252             return;
253         }
254         // should not get here
255
assertTrue(false);
256     }
257     
258     
259     public static void testConfigFileNotFound() {
260         log.info("---------------");
261         String JavaDoc originalFilename = PropertyUtility.getProperty("config.filename");
262         JFig.setInstance(null);
263         PropertyUtility.setProperty("config.filename","youcantfindme.xml");
264         JFig.getInstance().print();
265         String JavaDoc defaultValue = "xdefault";
266         String JavaDoc value = JFig.getInstance().getValue("myproject", "instance", defaultValue);
267         log.info("value: " + value);
268         assertTrue(value. equalsIgnoreCase(defaultValue));
269         
270         PropertyUtility.setProperty("config.filename",originalFilename);
271     }
272
273     public static void testReprocessConfigFileNotFound() {
274         log.info("---------------");
275         String JavaDoc originalFilename = PropertyUtility.getProperty("config.filename");
276         JFigIF instance = JFig.getInstance();
277         PropertyUtility.setProperty("config.filename","youcantfindme.xml");
278         // reprocess and verify...check for null for JUnit testing
279
try {
280             // reprocessConfiguration should throw exception
281
JFig.getInstance().reprocessConfiguration();
282             assertTrue(false);
283         } catch(JFigException e) {
284             log.warn("JFigException "+e.getMessage());
285             // original saved instance should be unchanged.
286
assertTrue(JFig.getInstance() == instance);
287         }
288         PropertyUtility.setProperty("config.filename",originalFilename);
289     }
290
291     public static void testInitializeException() {
292         log.info("---------------");
293         String JavaDoc originalFilename = PropertyUtility.getProperty("config.filename");
294         PropertyUtility.setProperty("config.filename","youcantfindme.xml");
295         try {
296             JFig.initialize();
297             assertTrue(false);
298         } catch(JFigException e) {
299             log.warn("JFigException "+e.getMessage());
300             assertTrue(true);
301         }
302         PropertyUtility.setProperty("config.filename",originalFilename);
303     }
304     public static void testInitializeIncludeException() {
305         log.info("---------------");
306         String JavaDoc originalFilename = PropertyUtility.getProperty("config.filename");
307         PropertyUtility.setProperty("config.filename","includeNotFound.xml");
308         try {
309             JFig.initialize();
310             assertTrue(false);
311         } catch(JFigException e) {
312             log.warn("JFigException "+e.getMessage());
313             assertTrue(true);
314         }
315         PropertyUtility.setProperty("config.filename",originalFilename);
316         try {
317             // reprocessConfiguration should throw exception
318
JFig.initialize().reprocessConfiguration();
319         } catch(JFigException e) {
320             log.warn("JFIgException",e);
321         }
322         
323     }
324     
325     public static void testConvert() {
326         log.info("---------------");
327         JFigConverter jsh = new JFigConverter();
328         String JavaDoc string = "abcd-[authentication]{isCheckingApprovalPermission}-efg"
329         + " userHome: $user.home$ endUserHome";
330
331         try {
332             String JavaDoc newString = jsh.convert(string);
333             log.info("newString:"+newString);
334             assertTrue(true);
335         } catch (JFigException e) {
336             assertTrue(false);
337         }
338     }
339
340     public static void testConvertFile() {
341         log.info("---------------");
342         JFigConverter jfc = new JFigConverter();
343         File JavaDoc inFile = new File JavaDoc("c:/jfig/etc/myproject.template1");
344         File JavaDoc outFile = new File JavaDoc("c:/jfig/etc/myproject.template.convert");
345         try {
346             log.info("inFile: "+inFile.getName());
347
348             String JavaDoc newString = jfc.convert(inFile, outFile);
349             log.info("newString:\n"+newString);
350             assertTrue(true);
351         } catch (JFigException e) {
352             assertTrue(false);
353         }
354     }
355     
356
357     public static void testConvertFileEdh() {
358         log.info("---------------");
359         String JavaDoc originalFilename = PropertyUtility.getProperty("config.filename");
360         PropertyUtility.setProperty("config.filename","edh.config.xml");
361         // reprocess and verify...check for null for JUnit testing
362
try {
363             // reprocessConfiguration should throw exception
364
JFig.initialize().reprocessConfiguration();
365         } catch(JFigException e) {
366             log.warn("JFIgException",e);
367         }
368         JFigConverter jfc = new JFigConverter();
369         File JavaDoc inFile = new File JavaDoc("c:/jfig/etc/edh.in.properties");
370         File JavaDoc outFile = new File JavaDoc("c:/jfig/etc/out.properties");
371         try {
372
373             String JavaDoc newString = jfc.convert(inFile, outFile);
374             log.info("newString:\n"+newString);
375             assertTrue(true);
376         } catch (JFigException e) {
377             assertTrue(false);
378         }
379         PropertyUtility.setProperty("config.filename",originalFilename);
380         try {
381             // reprocessConfiguration should throw exception
382
JFig.initialize().reprocessConfiguration();
383         } catch(JFigException e) {
384             log.warn("JFIgException",e);
385         }
386         
387     }
388     
389
390     public static void testSchmoopsie() {
391         log.info("---------------");
392         String JavaDoc originalFilename = PropertyUtility.getProperty("config.filename");
393         PropertyUtility.setProperty("config.filename","schmoopsie.config.xml");
394         try {
395             JFig.initialize();
396             JFig.getInstance().print();
397             assertTrue(true);
398         } catch(JFigException e) {
399             log.warn("JFigException "+e.getMessage());
400             assertTrue(false);
401         }
402         PropertyUtility.setProperty("config.filename",originalFilename);
403     }
404     public static void xtestPerformance() {
405
406         log.info("---------------");
407 // JFig.setProjectName("test");
408
JFig.getInstance();
409         log.info("---------------");
410         double iterator = 1000000;
411
412
413         Metric metric0 = new Metric(Metric.DEFAULT, "loopOnly", true);
414         for (int i=0; i< iterator; i++) {
415         }
416         metric0.endAndPrint();
417
418         Metric metric = new Metric(Metric.DEFAULT, "getValue", true);
419         long start = System.currentTimeMillis();
420         for (int i=0; i< iterator; i++) {
421             JFig.getInstance().getValue("bc", "instance", "");
422         }
423         metric.endAndPrint();
424         log.info("elapse "+metric.getElapsedTime());
425
426         start = System.currentTimeMillis();
427         Metric metric2 = new Metric(Metric.DEFAULT, "saveValue", true);
428         for (int i=0; i< iterator; i++) {
429             getTestValue();
430         }
431         metric2.endAndPrint();
432         double average = metric2.getElapsedTime()/iterator;
433         log.info("average "+average + " elapse "+ metric2.getElapsedTime());
434
435
436         Metric metric3 = new Metric(Metric.DEFAULT, "oneTime", true);
437         for (int i=0; i< 0; i++) {
438             JFig.getInstance().getValue("bc", "instance", "");
439             // this takes 10ms
440
}
441         metric3.endAndPrint();
442         log.info("elapse "+ (start - System.currentTimeMillis()) );
443
444         log.info("elapse "+ metric3.getElapsedTime());
445
446         Metric metric4 = new Metric(Metric.DEFAULT, "oneTimeNoLoop", true);
447        start = System.currentTimeMillis();
448         JFig.getInstance().getValue("bc", "instance", "");
449         long elapse = start - System.currentTimeMillis();
450         metric4.endAndPrint();
451         log.info("elapse "+ metric4.getElapsedTime() );
452     }
453
454     public static void xtestPerformanceSimple() {
455
456 // JFig.setProjectName("test");
457
log.info("---------------");
458         double iterations = 1000000;
459
460
461         Metric metric = new Metric(Metric.DEFAULT, "getValue", true);
462         long start = System.currentTimeMillis();
463         for (int i=0; i< iterations; i++) {
464             JFig.getInstance().getValue("bc", "instance", "");
465         }
466         metric.endAndPrint();
467         double average = metric.getElapsedTime()/iterations;
468         log.info("average "+average + " elapse "+ metric.getElapsedTime()+" iterations "+iterations);
469
470
471     }
472
473     /**
474      * @see gov.llnl.ais.util.JFig.JFigListener#JFigurationUpdate(gov.llnl.ais.util.JFig.JFigEvent)
475      */

476     public void configurationUpdate(JFigEvent event) {
477         isReprocessed = true;
478     }
479
480 }
481
Popular Tags