1 19 20 package org.openide.util; 21 22 import java.lang.ref.*; 23 import java.util.*; 24 import org.openide.ErrorManager; 25 import junit.framework.*; 26 import org.netbeans.junit.*; 27 import org.openide.util.Enumerations; 28 import java.io.*; 29 import java.util.Enumeration ; 30 import java.io.Serializable ; 31 import org.openide.util.io.NbObjectOutputStream; 32 import org.openide.util.io.NbObjectInputStream; 33 34 public class UtilitiesTranslateTest extends NbTestCase { 35 36 public UtilitiesTranslateTest(java.lang.String testName) { 37 super(testName); 38 } 39 40 public static void main(java.lang.String [] args) { 41 if (args.length == 1) { 42 junit.textui.TestRunner.run(new UtilitiesTranslateTest (args[0])); 43 } else { 44 junit.textui.TestRunner.run(new NbTestSuite(UtilitiesTranslateTest.class)); 45 } 46 } 47 48 50 protected void setUp () { 51 System.setProperty ("org.openide.util.Lookup", "-"); 52 Utilities.initForLoader (new CL ("UtilitiesTranslate.txt"), null); 53 } 54 55 57 public void testDotIsNotEscaped () { 58 String dot = "combo"; 59 60 assertEquals ("Error! Dot is not escaped", Utilities.translate (dot), dot); 61 } 62 63 65 public void testNoTransformation () { 66 String unknown = "unknown.pkg.UnknownName"; 67 68 String res = Utilities.translate(unknown); 69 70 assertTrue ("Should be the same instance", unknown == res); 71 } 72 73 75 public void testClassTransformation () { 76 String c = "org.netbeans.api.MyClass"; 77 String res = Utilities.translate (c); 78 79 assertTrue ("Not equal", !res.equals (c)); 80 assertTrue ("Not the same", res != c); 81 assertEquals ("Result ok", "org.nb.api.MyClass", res); 82 } 83 84 86 public void testPackageTransformation () { 87 String c = "SomeClass"; 88 String p = "org.openide.util"; 89 90 String res = Utilities.translate (p + '.' + c); 91 92 assertTrue ("Ends with the same name", res.endsWith (c)); 93 assertTrue ("Begins with package", res.startsWith ("org.nb.util")); 94 assertEquals ("Length is good", res.length (), 1 + c.length () + "org.nb.util".length ()); 95 } 96 97 99 public void testMoreExplicitTransform () { 100 String c = A.class.getName (); 101 102 String res = Utilities.translate (c); 103 104 assertEquals ("A converts to Ahoj", Ahoj.class.getName (), res); 105 } 106 107 109 public void testSameNameTransform () { 110 String n = "org.openide.TheSame"; 111 112 String res = Utilities.translate (n); 113 114 assertTrue ("Equal strings", res.equals (n)); 115 assertTrue ("But not the same", res != n); 116 } 117 118 120 public void testDeserialization () throws Exception { 121 ByteArrayOutputStream os = new ByteArrayOutputStream (); 122 123 NbObjectOutputStream oos = new NbObjectOutputStream (os); 124 125 A a = new A (); 126 a.value = "Ahoj"; 127 128 oos.writeObject (a); 129 oos.close (); 130 131 ByteArrayInputStream is = new ByteArrayInputStream (os.toByteArray()); 132 NbObjectInputStream ois = new NbObjectInputStream (is); 133 134 Object res = ois.readObject (); 135 136 assertEquals ("Must be class Ahoj", res.getClass (), Ahoj.class); 137 138 Ahoj ahoj = (Ahoj)res; 139 140 assertEquals ("Must contain right values", ahoj.value, a.value); 141 } 142 143 145 public void testReadOldFormat () { 146 Utilities.initForLoader (new CL ("UtilitiesTranslateOrig.txt"), null); 147 } 148 149 151 public void testTranslateOldFormat () throws Exception { 152 Utilities.initForLoader (new CL ("UtilitiesTranslateOrig.txt"), null); 153 154 InputStream is = getClass ().getResourceAsStream ("UtilitiesTranslateOrig.txt"); 155 BufferedReader r = new BufferedReader (new InputStreamReader (is)); 156 157 for (;;) { 158 String line = r.readLine (); 159 if (line == null) { 160 break; 161 } 162 163 int space = line.indexOf (' '); 164 assertTrue ("Line does not have spaces" + line, space >= 0); 165 166 String o = line.substring (0, space).trim () + ".Ahoj"; 167 String n = line.substring (space).trim () + ".Ahoj"; 168 169 String trans = Utilities.translate (o); 170 171 assertEquals ("Translatated as expected", n, trans); 172 } 173 } 174 175 177 public void testNoConvesions () { 178 Utilities.initForLoader (new CL (null), null); 179 180 Utilities.translate ("something.strange"); 181 Utilities.translate ("anything"); 182 } 183 184 public void testEmptyFile () { 185 Utilities.initForLoader (new CL ("UtilitiesTranslateEmpty.txt"), null); 186 187 Utilities.translate ("something.strange"); 188 Utilities.translate ("anything"); 189 } 190 191 193 public void testBug29878 () { 194 Utilities.initForLoader (new CL ("UtilitiesTranslate29878.txt"), null); 195 Utilities.translate ("org.netbeans.modules.apisupport.APIModule"); 196 } 197 198 200 private static final class CL extends ClassLoader { 201 private String file; 202 203 public CL (String file) { 204 super (java.lang.Object .class.getClassLoader()); 205 this.file = file; 206 } 207 208 209 protected Enumeration findResources (String res) throws IOException { 210 if (file != null) { 211 return Enumerations.singleton (getClass ().getResource (file)); 212 } else { 213 return Enumerations.empty (); 214 } 215 } 216 217 } 219 220 222 private static final class A implements Serializable { 223 final static long serialVersionUID = 1; 224 public String value; 225 } 226 227 private static final class Ahoj implements Serializable { 228 final static long serialVersionUID = 1; 229 public String value; 230 } 231 232 262 263 } 264 | Popular Tags |