Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/lutris/util/tests/ConfigTests.java


1   /*
2    * Enhydra Java Application Server Project
3    * 
4    * The contents of this file are subject to the Enhydra Public License
5    * Version 1.1 (the "License"); you may not use this file except in
6    * compliance with the License. You may obtain a copy of the License on
7    * the Enhydra web site ( http://www.enhydra.org/ ).
8    * 
9    * Software distributed under the License is distributed on an "AS IS"
10   * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
11   * the License for the specific terms governing rights and limitations
12   * under the License.
13   * 
14   * The Initial Developer of the Enhydra Application Server is Lutris
15   * Technologies, Inc. The Enhydra Application Server and portions created
16   * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17   * All Rights Reserved.
18   * 
19   * Contributor(s):
20   * 
21   * $Id: ConfigTests.java,v 1.10.8.1 2000/10/19 17:58:53 jasona Exp $
22   */
23  
24  
25  
26  
27  
28  package com.lutris.util.tests;
29  import com.lutris.util.*;
30  import java.io.*;
31  
32  /**
33   * Class that tests the Confi object.
34   */
35  public class ConfigTests {
36      private static final    String  NL = "\n";
37      private static       int    ival;
38      private static       int[]  ivals;
39      private static       long  lval;
40      private static       long[]  lvals;
41      private static       String  sval;
42      private static       String[]  svals;
43      private static       boolean  bval;
44      private static       boolean[]  bvals;
45      private static       double  dval;
46      private static       double[]  dvals;
47  
48      private static final String Test1 =
49          "#" + NL +
50          "# Correct data." + NL +
51          "#" + NL +
52          "int.a = 1" + NL +
53          "int.b = [1, 2, 3]" + NL +
54          "int.c = \"123\"" + NL +
55          "long.a = 100000000000000" + NL +
56          "long.b = [0, 200000000000000]" + NL +
57          "long.c = \"300000000000000\"" + NL +
58          "boolean.a = true" + NL +
59          "boolean.b = false" + NL +
60          "boolean.c = [true, false]" + NL +
61          "boolean.d = [\"true\", \"false\"]" + NL +
62          "string.a = \"Hello World\"" + NL +
63          "string.b = Hello-World" + NL +
64          "string.c = [\"Hello\", \"World\", \"\"]" + NL +
65          "string.d = \"\007\"" + NL +
66          "string.e = \"There is a \" + " + NL + "\"newline in here.\"" + NL +
67          "string.f = \"Another\\" + NL + " newline.\"" + NL +
68          "string.g = \"PartA\" + \"PartB\" + PartC" + NL +
69          "double.a = 1.000000000000001" + NL +
70          "double.b = [0, 200000000000.0002, 3.1415927, -2.2E+12]" + NL +
71          "double.c = \"3000000.000000003\"" + NL +
72          "" + NL +
73          "" + NL +
74          "#" + NL +
75          "# Comment" + NL +
76          "#" + NL +
77          "comment.a = \"Hello world\" # Trailing comment." + NL;
78      
79      private static
80      void beginTest(String name, String reason)
81      {
82    System.out.println("TEST: " + name + "  --  " + reason);
83      }
84  
85      private static
86      void goodTest(String name)
87      {
88    System.out.println("SUCCEED: " + name);
89      }
90  
91      private static
92      void failTest(String name, String reason)
93      throws Exception
94      {
95    throw new Exception("FAIL: " + name + ": " + reason);
96      }
97      
98      static
99      void testGoodStreamRead()
100     throws Exception
101     {
102   beginTest("testGoodStreamRead", "Read good input from stream.");
103   ByteArrayInputStream sis = new ByteArrayInputStream(Test1.getBytes());
104   try {
105       ConfigFile cf = new ConfigFile(sis);
106       Config conf = cf.getConfig();
107   }
108   catch (Exception e) {
109       failTest("testGoodStreamRead", e.toString());
110   }
111   goodTest("testGoodStreamRead");
112     }
113     
114     static
115     void testGoodFileRead()
116     throws Exception
117     {
118   String FileName = "./Config.test.xx";
119   beginTest("testGoodFileRead", "Read good input from file.");
120   try {
121       FileOutputStream fos = new FileOutputStream(FileName);
122       BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
123       bw.write(Test1, 0, Test1.length());
124       bw.flush(); bw.close();
125       bw = null;
126       fos = null;
127   }
128   catch (Exception e) {
129       failTest("testGoodFileRead", "Failed to write out test data.");
130   }
131   try {
132       FileInputStream cfis = new FileInputStream(FileName);
133       ConfigFile cf = new ConfigFile(cfis);
134       Config conf = cf.getConfig();
135   }
136   catch (Exception e) {
137       failTest("testGoodFileRead", e.toString());
138   }
139   goodTest("testGoodStreamRead");
140     }
141     
142     static
143     void testGoodInput()
144     throws Exception
145     {
146   Config conf = null;
147   beginTest("testGoodInput", "Read good input from stream.");
148   ByteArrayInputStream sis = new ByteArrayInputStream(Test1.getBytes());
149   try {
150       ConfigFile cf = new ConfigFile(sis);
151       conf = cf.getConfig();
152   }
153   catch (Exception e) {
154       failTest("testGoodInput", e.toString());
155   }
156   
157   int    thisCase = 0;
158   boolean    xfail;
159   
160   try {
161       thisCase = 1;
162       ival = conf.getInt("int.a");
163       if (ival != 1) throw new Exception("Wrong value");
164       
165       thisCase = 2;
166       ivals = conf.getInts("int.a");
167       if (ivals.length != 1) throw new Exception("Wrong count");
168       if (ivals[0] != 1) throw new Exception("Wrong value");
169       
170       thisCase = 3;
171       ivals = conf.getInts("int.b");
172       if (ivals.length != 3) throw new Exception("Wrong count");
173       if (ivals[0] != 1) throw new Exception("Wrong value");
174       if (ivals[1] != 2) throw new Exception("Wrong value");
175       if (ivals[2] != 3) throw new Exception("Wrong value");
176       
177       thisCase = 4;
178       xfail = false;
179       try {
180     ival = conf.getInt("int.b");
181     xfail = true;
182       } catch (ConfigException e) {}
183       if (xfail) throw new Exception("Expected ConfigException");
184 
185       thisCase = 5;
186       ival = conf.getInt("int.c");
187       if (ival != 123) throw new Exception("Wrong value");
188       
189       thisCase = 6;
190       ivals = conf.getInts("int.c");
191       if (ivals.length != 1) throw new Exception("Wrong count");
192       if (ivals[0] != 123) throw new Exception("Wrong value");
193       
194       //
195       // Test Longs.
196       // 
197       thisCase = 7;
198       lval = conf.getLong("long.a");
199       if (lval != 100000000000000L)
200     throw new Exception("Wrong value");
201       
202       thisCase = 8;
203       lvals = conf.getLongs("long.a");
204       if (lvals.length != 1) throw new Exception("Wrong count");
205       if (lvals[0] != 100000000000000L)
206     throw new Exception("Wrong value");
207       
208       thisCase = 9;
209       lvals = conf.getLongs("long.b");
210       if (lvals.length != 2) throw new Exception("Wrong count");
211       if (lvals[0] != 0L) throw new Exception("Wrong value");
212       if (lvals[1] != 200000000000000L)
213     throw new Exception("Wrong value");
214       
215       thisCase = 10;
216       xfail = false;
217       try {
218     lval = conf.getLong("long.b");
219     xfail = true;
220       } catch (ConfigException e) {}
221       if (xfail) throw new Exception("Expected ConfigException");
222 
223       thisCase = 11;
224       lval = conf.getLong("long.c");
225       if (lval != 300000000000000L)
226     throw new Exception("Wrong value");
227       
228       thisCase = 12;
229       lvals = conf.getLongs("long.c");
230       if (lvals.length != 1) throw new Exception("Wrong count");
231       if (lvals[0] != 300000000000000L)
232     throw new Exception("Wrong value");
233       
234       //
235       // Booleans
236       // 
237       thisCase = 13;
238       bval = conf.getBoolean("boolean.a");
239       if (!bval) throw new Exception("Wrong value");
240 
241       thisCase = 14;
242       bvals = conf.getBooleans("boolean.a");
243       if (bvals.length != 1) throw new Exception("Wrong count");
244       if (!(bvals[0])) throw new Exception("Wrong value");
245 
246       thisCase = 15;
247       bval = conf.getBoolean("boolean.b");
248       if (bval) throw new Exception("Wrong value");
249       
250       thisCase = 16;
251       bvals = conf.getBooleans("boolean.b");
252       if (bvals.length != 1) throw new Exception("Wrong count");
253       if (bvals[0]) throw new Exception("Wrong value");
254   
255       thisCase = 17;
256       bvals = conf.getBooleans("boolean.c");
257       if (bvals.length != 2) throw new Exception("Wrong count");
258       if (!(bvals[0])) throw new Exception("Wrong value");
259       if (bvals[1]) throw new Exception("Wrong value");
260       
261       thisCase = 18;
262       xfail = false;
263       try {
264     bval = conf.getBoolean("boolean.c");
265     xfail = true;
266       } catch (ConfigException e) {}
267       if (xfail) throw new Exception("Expected ConfigException");
268       
269       thisCase = 19;
270       bvals = conf.getBooleans("boolean.d");
271       if (bvals.length != 2) throw new Exception("Wrong count");
272       if (!(bvals[0])) throw new Exception("Wrong value");
273       if (bvals[1]) throw new Exception("Wrong value");
274       
275       thisCase = 20;
276       xfail = false;
277       try {
278     bval = conf.getBoolean("boolean.d");
279     xfail = true;
280       } catch (ConfigException e) {}
281       if (xfail) throw new Exception("Expected ConfigException");
282       
283       thisCase = 21;
284       sval = conf.getString("string.a");
285       if (!sval.equals("Hello World"))
286     throw new Exception("Wrong value");
287 
288       thisCase = 22;
289       svals = conf.getStrings("string.a");
290       if (svals.length != 1) throw new Exception("Wrong count");
291       if (!svals[0].equals("Hello World"))
292     throw new Exception("Wrong value");
293 
294       thisCase = 23;
295       sval = conf.getString("string.b");
296       if (!sval.equals("Hello-World"))
297     throw new Exception("Wrong value");
298 
299       thisCase = 24;
300       svals = conf.getStrings("string.b");
301       if (svals.length != 1) throw new Exception("Wrong count");
302       if (!svals[0].equals("Hello-World"))
303     throw new Exception("Wrong value");
304       
305       thisCase = 25;
306       svals = conf.getStrings("string.c");
307       if (svals.length != 3) throw new Exception("Wrong count");
308       if (!svals[0].equals("Hello"))
309     throw new Exception("Wrong value");
310       if (!svals[1].equals("World"))
311     throw new Exception("Wrong value");
312       if (!svals[2].equals(""))
313     throw new Exception("Wrong value");
314 
315       thisCase = 26;
316       xfail = false;
317       try {
318     sval = conf.getString("string.c");
319     xfail = true;
320       } catch (ConfigException e) {}
321       if (xfail) throw new Exception("Expected ConfigException");
322 
323       thisCase = 27;
324       sval = conf.getString("string.d");
325       if (!sval.equals("\007"))
326     throw new Exception("Wrong value");
327 
328       thisCase = 28;
329       svals = conf.getStrings("string.d");
330       if (svals.length != 1) throw new Exception("Wrong count");
331       if (!svals[0].equals("\007"))
332     throw new Exception("Wrong value");
333 
334       thisCase = 29;
335       sval = conf.getString("string.e");
336       if (!sval.equals("There is a newline in here."))
337     throw new Exception("Wrong value");
338 
339       thisCase = 30;
340       svals = conf.getStrings("string.e");
341       if (svals.length != 1) throw new Exception("Wrong count");
342       if (!svals[0].equals("There is a newline in here."))
343     throw new Exception("Wrong value");
344 
345       thisCase = 31;
346       sval = conf.getString("string.f");
347       if (!sval.equals("Another" + NL + " newline."))
348     throw new Exception("Wrong value");
349 
350       thisCase = 32;
351       svals = conf.getStrings("string.f");
352       if (svals.length != 1) throw new Exception("Wrong count");
353       if (!svals[0].equals("Another" + NL + " newline."))
354     throw new Exception("Wrong value");
355 
356       thisCase = 33;
357       sval = conf.getString("string.g");
358       if (!sval.equals("PartAPartBPartC"))
359     throw new Exception("Wrong value");
360 
361       thisCase = 34;
362       svals = conf.getStrings("string.g");
363       if (svals.length != 1) throw new Exception("Wrong count");
364       if (!svals[0].equals("PartAPartBPartC"))
365     throw new Exception("Wrong value");
366 
367       //
368       // Doubles
369       // 
370       thisCase = 35;
371       dval = conf.getDouble("double.a");
372       if (dval != Double.valueOf("1.000000000000001").doubleValue())
373     throw new Exception("Wrong value");
374 
375       thisCase = 36;
376       dvals = conf.getDoubles("double.a");
377       if (dvals.length != 1) throw new Exception("Wrong count");
378       if (dvals[0] != Double.valueOf("1.000000000000001").doubleValue())
379     throw new Exception("Wrong value");
380 
381       thisCase = 37;
382       dvals = conf.getDoubles("double.b");
383       if (dvals.length != 4) throw new Exception("Wrong count");
384       if (dvals[0] != Double.valueOf("0").doubleValue())
385     throw new Exception("Wrong value");
386       if (dvals[1] != Double.valueOf("200000000000.0002").doubleValue())
387     throw new Exception("Wrong value");
388       if (dvals[2] != Double.valueOf("3.1415927").doubleValue())
389     throw new Exception("Wrong value");
390       if (dvals[3] != Double.valueOf("-2.2E+12").doubleValue())
391     throw new Exception("Wrong value");
392 
393       thisCase = 38;
394       xfail = false;
395       try {
396     dval = conf.getDouble("double.b");
397     xfail = true;
398       } catch (ConfigException e) {}
399       if (xfail) throw new Exception("Expected ConfigException");
400 
401       thisCase = 39;
402       dval = conf.getDouble("double.c");
403       if (dval != Double.valueOf("3000000.000000003").doubleValue())
404     throw new Exception("Wrong value");
405 
406       thisCase = 40;
407       dvals = conf.getDoubles("double.c");
408       if (dvals.length != 1) throw new Exception("Wrong count");
409       if (dvals[0] != Double.valueOf("3000000.000000003").doubleValue())
410     throw new Exception("Wrong value");
411       
412       //
413       // Misc.
414       //
415       thisCase = 41;
416       sval = conf.getString("comment.a");
417       if (!sval.equals("Hello world")) 
418     throw new Exception("Wrong value");
419       
420       thisCase = 42;
421       bval = conf.containsKey("comment.a");
422       if (!bval) throw new Exception("Wrong value");
423       
424       thisCase = 43;
425       bval = conf.containsKey("IDontExist");
426       if (bval) throw new Exception("Wrong value");
427       
428       thisCase = 44;
429       ival = conf.containsCount("int.a");
430       if (ival != 1) throw new Exception("Wrong value");
431 
432       thisCase = 45;
433       ival = conf.containsCount("double.b");
434       if (ival != 4) throw new Exception("Wrong value");
435 
436       thisCase = 46;
437       ival = conf.containsCount("NotHere");
438       if (ival >= 0) throw new Exception("Wrong value");
439   }
440   catch (Exception e) {
441       failTest("testGoodInput (case " + thisCase + ")", e.toString());
442   }
443   goodTest("testGoodInput");
444     }
445 
446     public static void runTests()
447     {
448   int i;
449   String comma = "";
450   try {
451       testGoodFileRead();
452       testGoodStreamRead();
453       testGoodInput();
454   }
455   catch (Exception e) {
456       System.out.println("Test FAILED: " + e);
457       e.printStackTrace(System.out);
458 
459       System.out.println("ival=" + ival);
460       System.out.print("ivals=");
461       if (ivals == null) System.out.println("null");
462       else {
463     System.out.print("{");
464     comma = "";
465     for (i=0; i<ivals.length; i++) {
466         System.out.print(comma + ivals[i]);
467         comma = ", ";
468     }
469     System.out.println("}");
470       }
471 
472       System.out.println("lval=" + lval);
473       System.out.print("lvals=");
474       if (lvals == null) System.out.println("null");
475       else {
476     System.out.print("{");
477     comma = "";
478     for (i=0; i<lvals.length; i++) {
479         System.out.print(comma + lvals[i]);
480         comma = ", ";
481     }
482     System.out.println("}");
483       }
484 
485       System.out.println("sval=" + sval);
486       System.out.print("svals=");
487       if (svals == null) System.out.println("null");
488       else {
489     System.out.print("{");
490     comma = "";
491     for (i=0; i<svals.length; i++) {
492         System.out.print(comma + svals[i]);
493         comma = ", ";
494     }
495     System.out.println("}");
496       }
497 
498       System.out.println("bval=" + bval);
499       System.out.print("bvals=");
500       if (bvals == null) System.out.println("null");
501       else {
502     System.out.print("{");
503     comma = "";
504     for (i=0; i<bvals.length; i++) {
505         System.out.print(comma + bvals[i]);
506         comma = ", ";
507     }
508     System.out.println("}");
509       }
510 
511       System.out.println("dval=" + dval);
512       System.out.print("dvals=");
513       if (dvals == null) System.out.println("null");
514       else {
515     System.out.print("{");
516     comma = "";
517     for (i=0; i<dvals.length; i++) {
518         System.out.print(comma + dvals[i]);
519         comma = ", ";
520     }
521     System.out.println("}");
522       }
523       return;
524   }
525   System.out.println("Test OK!");
526     }
527 
528     public static void main (String [] args)
529     {
530         runTests();
531     }
532 }