KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > UberPropertiesTests


1 /*
2  * UberProperties regression test.
3  * Copyright (C) 2003 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.util.*;
21 import java.io.*;
22
23 class UberPropertiesTests {
24     private final static String JavaDoc[] TESTS = new String JavaDoc[]{
25         "Aone=\nAtwo= \nAthree=\t",
26         "Bone=1\n Btwo = two \nBthree 3\nBfour: 4",
27         "Con\\\ne=on\\\ne\nCtw \\\n o=tw \\\n o\nCth\\\n ree=th \\\nree",
28         "Done=one\nDone=two\nDone=three",
29         "#Comment\nname value\n!Comment\nname value\n# Comment\\nname value\n #name value\n\t \t!name value",
30         "#\n# That was a comment\n\nname:value\nname=value\nname value\n name = value \n name = value \n name = value ",
31         "# empty properties\nname\nname=\nname:\n name\n name ",
32         "# property names of length zero\n:value value\n:value\n=value\n :value\n =value\n:value : has colon\n:value ends with equal =\n:value ends with colon :",
33         "name::value starts with colon\nname=:value starts with colon\nname :value starts with colon\nname:value ends with colon:\nname=value ends with colon:\nname value ends with colon:\nname:=value starts with equal\nname==value starts with equal\nname =value starts with equal\nname:value ends with equal=\nname=value ends with equal=\nname value ends with equal=\nname:!value starts with exclamation\nname=!value starts with exclamation\nname !value starts with exclamation\nname:#value starts with pound\nname=#value starts with pound\nname #value starts with pound\nname=value ends with colon :\nname=value ends with equal =",
34         "@!#$%^name value!@#$%^&*(){}",
35         "\n\n\n\n#comment\n\n \n\t \n ",
36         "# escapes\n\\ \\=\\:name=value\\ \\=\\:\n\\u3443\\0233name value\\u3432\\0213",
37         "name",
38         "name ",
39         "name =",
40         "",
41         "#comment",
42         "name= ",
43         "name= value",
44         "name=value ",
45         "name\\\nstillname value\nname\\\n stillname value\nname\\\nstillname\\\nstillname value\nname\\\n\\\n \\\nstillname value\nname\\\n#stillname value\nname\\\n!stillname value",
46         //"# empty property\nname\\",
47
//"# empty property\nname\\\n",
48
"# empty property\nname\\\n\n#comment",
49         //"name= \\\nvalue\nname: \\\nvalue\nname:\\\nvalue\nname=\\\nvalue\nname=\\",
50
};
51
52     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
53         for (int i=0; i<TESTS.length; i++){
54             byte[] bytes = TESTS[i].getBytes("ISO-8859-1");
55             Properties p = new Properties();
56             p.load(new ByteArrayInputStream(bytes));
57             UberProperties up = new UberProperties();
58             up.load(new ByteArrayInputStream(bytes));
59             String JavaDoc results = compare(up, p);
60             if (results != null){
61                 System.err.println(results);
62                 System.err.println(TESTS[i]);
63                 System.exit(1);
64             }
65             CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
66             up.save(cbb.getOutputStream());
67             cbb.getOutputStream().close();
68             UberProperties up2 = new UberProperties();
69             up2.load(cbb.getInputStream());
70             results = compare(up, up2);
71             if (results != null){
72                 System.err.println(results);
73                 System.err.println(TESTS[i]);
74                 System.exit(1);
75             }
76         }
77     }
78
79     private static String JavaDoc compare(UberProperties uberProps, Properties props){
80         String JavaDoc[] upNames = uberProps.propertyNames();
81         Enumeration pNamesEnum = props.propertyNames();
82         ArrayList<String JavaDoc> pNamesList = new ArrayList<String JavaDoc>();
83         while (pNamesEnum.hasMoreElements()){
84             pNamesList.add((String JavaDoc)pNamesEnum.nextElement());
85         }
86         String JavaDoc[] pNames = (String JavaDoc[])pNamesList.toArray(new String JavaDoc[0]);
87         if (upNames.length != pNames.length){
88             return ("Number of properties do not match: Uber: " + upNames.length + " Normal:" + pNames.length);
89         }
90         for (int i=0; i<pNames.length; i++){
91             String JavaDoc upValue = uberProps.getProperty(pNames[i]);
92             String JavaDoc pValue = props.getProperty(pNames[i]);
93             if (upValue == null) {
94                 return "UberProperties does not contain property: '" + pNames[i] + "'";
95             }
96             if (!upValue.equals(pValue)){
97                 return ("Values for '" + pNames[i] + "' do not match:\n '" + pValue + "'\n '" + upValue + "'");
98             }
99         }
100         return null;
101     }
102
103     private static String JavaDoc compare(UberProperties up1, UberProperties up2){
104         String JavaDoc[] up1Names = up1.propertyNames();
105         String JavaDoc[] up2Names = up2.propertyNames();
106         if (up1Names.length != up2Names.length){
107             return ("Number of properties do not match: Uber: " + up1Names.length + " Normal:" + up2Names.length);
108         }
109         for (int i=0; i<up1Names.length; i++){
110             String JavaDoc up1Value = up1.getProperty(up1Names[i]);
111             String JavaDoc up2Value = up2.getProperty(up1Names[i]);
112             if (up2Value == null) {
113                 return "Second does not contain property: '" + up1Names[i] + "'";
114             }
115             if (!up1Value.equals(up2Value)){
116                 return ("Values for '" + up1Names[i] + "' do not match:\n '" + up1Value + "'\n '" + up2Value + "'");
117             }
118         }
119         return null;
120     }
121 }
122
Popular Tags