1 18 package com.Ostermiller.util; 19 20 import java.util.*; 21 import java.io.*; 22 23 class UberPropertiesTests { 24 private final static String [] TESTS = new String []{ 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\\\n\n#comment", 49 }; 51 52 public static void main(String [] args) throws Exception { 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 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 compare(UberProperties uberProps, Properties props){ 80 String [] upNames = uberProps.propertyNames(); 81 Enumeration pNamesEnum = props.propertyNames(); 82 ArrayList<String > pNamesList = new ArrayList<String >(); 83 while (pNamesEnum.hasMoreElements()){ 84 pNamesList.add((String )pNamesEnum.nextElement()); 85 } 86 String [] pNames = (String [])pNamesList.toArray(new String [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 upValue = uberProps.getProperty(pNames[i]); 92 String 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 compare(UberProperties up1, UberProperties up2){ 104 String [] up1Names = up1.propertyNames(); 105 String [] 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 up1Value = up1.getProperty(up1Names[i]); 111 String 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 |