KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > UtilitiesTranslateTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
30 import java.io.Serializable JavaDoc;
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 JavaDoc testName) {
37         super(testName);
38     }
39
40     public static void main(java.lang.String JavaDoc[] 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     /** Setups the info.
49      */

50     protected void setUp () {
51         System.setProperty ("org.openide.util.Lookup", "-");
52         Utilities.initForLoader (new CL ("UtilitiesTranslate.txt"), null);
53     }
54     
55     /** Checks whether the . is treated is normal character.
56      */

57     public void testDotIsNotEscaped () {
58         String JavaDoc dot = "combo";
59         
60         assertEquals ("Error! Dot is not escaped", Utilities.translate (dot), dot);
61     }
62     
63     /** Tests fail result.
64      */

65     public void testNoTransformation () {
66         String JavaDoc unknown = "unknown.pkg.UnknownName";
67         
68         String JavaDoc res = Utilities.translate(unknown);
69         
70         assertTrue ("Should be the same instance", unknown == res);
71     }
72     
73     /** Checks class transformation.
74      */

75     public void testClassTransformation () {
76         String JavaDoc c = "org.netbeans.api.MyClass";
77         String JavaDoc 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     /** Checks package transformation.
85      */

86     public void testPackageTransformation () {
87         String JavaDoc c = "SomeClass";
88         String JavaDoc p = "org.openide.util";
89         
90         String JavaDoc 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     /** Test longer transform takes preceedence over shorter one.
98      */

99     public void testMoreExplicitTransform () {
100         String JavaDoc c = A.class.getName ();
101         
102         String JavaDoc res = Utilities.translate (c);
103         
104         assertEquals ("A converts to Ahoj", Ahoj.class.getName (), res);
105     }
106         
107     /** Checks transformation to the class with the same name.
108      */

109     public void testSameNameTransform () {
110         String JavaDoc n = "org.openide.TheSame";
111         
112         String JavaDoc res = Utilities.translate (n);
113         
114         assertTrue ("Equal strings", res.equals (n));
115         assertTrue ("But not the same", res != n);
116     }
117     
118     /** Test (de)serialization of classes with different name
119      */

120     public void testDeserialization () throws Exception JavaDoc {
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 JavaDoc 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     /** Test that we are able to read the original format.
144      */

145     public void testReadOldFormat () {
146         Utilities.initForLoader (new CL ("UtilitiesTranslateOrig.txt"), null);
147     }
148     
149     /** Translate whole original format.
150      */

151     public void testTranslateOldFormat () throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc o = line.substring (0, space).trim () + ".Ahoj";
167             String JavaDoc n = line.substring (space).trim () + ".Ahoj";
168             
169             String JavaDoc trans = Utilities.translate (o);
170             
171             assertEquals ("Translatated as expected", n, trans);
172         }
173     }
174     
175     /** Test with empty classloader.
176      */

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     /** Test to fix bug 29878
192      */

193     public void testBug29878 () {
194         Utilities.initForLoader (new CL ("UtilitiesTranslate29878.txt"), null);
195         Utilities.translate ("org.netbeans.modules.apisupport.APIModule");
196     }
197     
198     /** Fake classloader that returns provides file from getResources method.
199      */

200     private static final class CL extends ClassLoader JavaDoc {
201         private String JavaDoc file;
202         
203         public CL (String JavaDoc file) {
204             super (java.lang.Object JavaDoc.class.getClassLoader());
205             this.file = file;
206         }
207      
208         
209         protected Enumeration JavaDoc findResources (String JavaDoc res) throws IOException {
210             if (file != null) {
211                 return Enumerations.singleton (getClass ().getResource (file));
212             } else {
213                 return Enumerations.empty ();
214             }
215         }
216         
217     } // end of CL
218

219     
220     /** A test to serialize and deserialize different class with same fields.
221      */

222     private static final class A implements Serializable JavaDoc {
223         final static long serialVersionUID = 1;
224         public String JavaDoc value;
225     }
226     
227     private static final class Ahoj implements Serializable JavaDoc {
228         final static long serialVersionUID = 1;
229         public String JavaDoc value;
230     }
231     
232     /** Useful code to write down current content of a registry
233      *
234      * /
235     public static void main(String[] args) throws Exception {
236 // String r = org.openide.util.Utilities.translate ("org.netbeans.modules.apisupport.APIModule");
237 // System.out.println("result: " + r);
238         
239         
240         Enumeration en;
241         ClassLoader c = org.openide.TopManager.getDefault().currentClassLoader();
242         en = c.getResources("META-INF/netbeans/translate.names");
243         
244         
245         PrintStream out = new PrintStream (new FileOutputStream ("/tmp/allnames.txt"));
246         while (en.hasMoreElements()) {
247             URL url = (URL)en.nextElement ();
248             
249             out.print ("# ");
250             out.println (url.toString());
251             out.println ();
252             
253             org.openide.filesystems.FileUtil.copy(url.openStream(), out);
254             
255             out.println ();
256             out.println ();
257         }
258         
259         out.close ();
260     }
261     */

262     
263 }
264
Popular Tags