001: /*
002: * Copyright (C) 2003-2007 Stephen Ostermiller
003: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * See COPYING.TXT for details.
016: */
017: package com.Ostermiller.util;
018:
019: import java.io.ByteArrayInputStream;
020: import java.util.*;
021:
022: /**
023: * UberProperties regression test.
024: */
025: class UberPropertiesTests {
026: private final static String[] TESTS = new String[] {
027: "A" + "one=\n" + "A" + "two= \n" + "A" + "three=\t",
028: "B" + "one=1\n B" + "two = two \nB" + "three 3\n" + "B"
029: + "four: 4",
030: "C" + "on\\\n" + "e=on\\\n" + "e\n" + "C" + "t"
031: + "w \\\n o=t" + "w \\\n o\nCth\\\n r" + "e"
032: + "e=t" + "h \\\n" + "r" + "e" + "e",
033: "Done=one\nDone=two\nDone=three",
034: "#Comment\n" + "name value\n!Comment\n"
035: + "name value\n# Comment\\n"
036: + "name value\n #name value\n\t \t!name value",
037: "#\n# That was a comment\n\nname:value\n"
038: + "name=value\n"
039: + "name value\n name = value \n name = value \n name = value ",
040: "# empty properties\n" + "name\n" + "name=\n"
041: + "name:\n name\n name ",
042: "# 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 :",
043: "name::value starts with colon\n"
044: + "name=:value starts with colon\n"
045: + "name :value starts with colon\nname:value ends with colon:\n"
046: + "name=value ends with colon:\n"
047: + "name value ends with colon:\n"
048: + "name:=value starts with equal\n"
049: + "name==value starts with equal\n"
050: + "name =value starts with equal\nname:value ends with equal=\n"
051: + "name=value ends with equal=\n"
052: + "name value ends with equal=\n"
053: + "name:!value starts with exclamation\n"
054: + "name=!value starts with exclamation\n"
055: + "name !value starts with exclamation\n"
056: + "name:#value starts with pound\n"
057: + "name=#value starts with pound\n"
058: + "name #value starts with pound\n"
059: + "name=value ends with colon :\n"
060: + "name=value ends with equal =",
061: "@!#$%^name value!@#$%^&*(){}",
062: "\n\n\n\n#comment\n\n \n\t \n ",
063: "# escapes\n\\ \\=\\:name=value\\ \\=\\:\n\\u3443\\0233name value\\u3432\\0213",
064: "name",
065: "name ",
066: "name =",
067: "",
068: "#comment",
069: "name= ",
070: "name= value",
071: "name=value ",
072: "name\\\n" + "still" + "name value\n" + "name\\\n still"
073: + "name value\n" + "name\\\n" + "still"
074: + "name\\\n" + "still" + "name value\n"
075: + "name\\\n\\\n \\\n" + "still" + "name value\n"
076: + "name\\\n#still" + "name value\n"
077: + "name\\\n!still" + "name value",
078: "# empty property\n" + "name\\\n\n#comment", };
079:
080: /**
081: * Main method for tests
082: * @param args command line arguments (ignored)
083: */
084: public static void main(String[] args) {
085: try {
086: for (String element : TESTS) {
087: byte[] bytes = element.getBytes("ISO-8859-1");
088: Properties p = new Properties();
089: p.load(new ByteArrayInputStream(bytes));
090: UberProperties up = new UberProperties();
091: up.load(new ByteArrayInputStream(bytes));
092: String results = compare(up, p);
093: if (results != null) {
094: System.err.println(results);
095: System.err.println(element);
096: System.exit(1);
097: }
098: CircularByteBuffer cbb = new CircularByteBuffer(
099: CircularByteBuffer.INFINITE_SIZE);
100: up.save(cbb.getOutputStream());
101: cbb.getOutputStream().close();
102: UberProperties up2 = new UberProperties();
103: up2.load(cbb.getInputStream());
104: results = compare(up, up2);
105: if (results != null) {
106: System.err.println(results);
107: System.err.println(element);
108: System.exit(1);
109: }
110: }
111: } catch (Exception x) {
112: x.printStackTrace();
113: System.exit(1);
114: }
115: }
116:
117: private static String compare(UberProperties uberProps,
118: Properties props) {
119: String[] upNames = uberProps.propertyNames();
120: Enumeration<?> pNamesEnum = props.propertyNames();
121: ArrayList<String> pNamesList = new ArrayList<String>();
122: while (pNamesEnum.hasMoreElements()) {
123: Object o = pNamesEnum.nextElement();
124: if (o instanceof String) {
125: pNamesList.add((String) o);
126: }
127: }
128: String[] pNames = pNamesList.toArray(new String[0]);
129: if (upNames.length != pNames.length) {
130: return ("Number of properties do not match: UberProperties: "
131: + upNames.length + " Normal:" + pNames.length);
132: }
133: for (String element : pNames) {
134: String upValue = uberProps.getProperty(element);
135: String pValue = props.getProperty(element);
136: if (upValue == null) {
137: return "UberProperties does not contain property: '"
138: + element + "'";
139: }
140: if (!upValue.equals(pValue)) {
141: return ("Values for '" + element
142: + "' do not match:\n '" + pValue + "'\n '"
143: + upValue + "'");
144: }
145: }
146: return null;
147: }
148:
149: private static String compare(UberProperties up1, UberProperties up2) {
150: String[] up1Names = up1.propertyNames();
151: String[] up2Names = up2.propertyNames();
152: if (up1Names.length != up2Names.length) {
153: return ("Number of properties do not match: UberProperties: "
154: + up1Names.length + " Normal:" + up2Names.length);
155: }
156: for (String element : up1Names) {
157: String up1Value = up1.getProperty(element);
158: String up2Value = up2.getProperty(element);
159: if (up2Value == null) {
160: return "Second does not contain property: '" + element
161: + "'";
162: }
163: if (!up1Value.equals(up2Value)) {
164: return ("Values for '" + element
165: + "' do not match:\n '" + up1Value + "'\n '"
166: + up2Value + "'");
167: }
168: }
169: return null;
170: }
171: }
|