001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.util;
043:
044: import java.lang.ref.*;
045: import java.util.*;
046: import org.openide.ErrorManager;
047: import junit.framework.*;
048: import org.netbeans.junit.*;
049: import org.openide.util.Enumerations;
050: import java.io.*;
051: import java.util.Enumeration;
052: import java.io.Serializable;
053: import org.openide.util.io.NbObjectOutputStream;
054: import org.openide.util.io.NbObjectInputStream;
055:
056: public class UtilitiesTranslateTest extends NbTestCase {
057:
058: public UtilitiesTranslateTest(java.lang.String testName) {
059: super (testName);
060: }
061:
062: public static void main(java.lang.String[] args) {
063: if (args.length == 1) {
064: junit.textui.TestRunner.run(new UtilitiesTranslateTest(
065: args[0]));
066: } else {
067: junit.textui.TestRunner.run(new NbTestSuite(
068: UtilitiesTranslateTest.class));
069: }
070: }
071:
072: /** Setups the info.
073: */
074: protected void setUp() {
075: System.setProperty("org.openide.util.Lookup", "-");
076: Utilities.initForLoader(new CL("UtilitiesTranslate.txt"), null);
077: }
078:
079: /** Checks whether the . is treated is normal character.
080: */
081: public void testDotIsNotEscaped() {
082: String dot = "combo";
083:
084: assertEquals("Error! Dot is not escaped", Utilities
085: .translate(dot), dot);
086: }
087:
088: /** Tests fail result.
089: */
090: public void testNoTransformation() {
091: String unknown = "unknown.pkg.UnknownName";
092:
093: String res = Utilities.translate(unknown);
094:
095: assertTrue("Should be the same instance", unknown == res);
096: }
097:
098: /** Checks class transformation.
099: */
100: public void testClassTransformation() {
101: String c = "org.netbeans.api.MyClass";
102: String res = Utilities.translate(c);
103:
104: assertTrue("Not equal", !res.equals(c));
105: assertTrue("Not the same", res != c);
106: assertEquals("Result ok", "org.nb.api.MyClass", res);
107: }
108:
109: /** Checks package transformation.
110: */
111: public void testPackageTransformation() {
112: String c = "SomeClass";
113: String p = "org.openide.util";
114:
115: String res = Utilities.translate(p + '.' + c);
116:
117: assertTrue("Ends with the same name", res.endsWith(c));
118: assertTrue("Begins with package", res.startsWith("org.nb.util"));
119: assertEquals("Length is good", res.length(), 1 + c.length()
120: + "org.nb.util".length());
121: }
122:
123: /** Test longer transform takes preceedence over shorter one.
124: */
125: public void testMoreExplicitTransform() {
126: String c = A.class.getName();
127:
128: String res = Utilities.translate(c);
129:
130: assertEquals("A converts to Ahoj", Ahoj.class.getName(), res);
131: }
132:
133: /** Checks transformation to the class with the same name.
134: */
135: public void testSameNameTransform() {
136: String n = "org.openide.TheSame";
137:
138: String res = Utilities.translate(n);
139:
140: assertTrue("Equal strings", res.equals(n));
141: assertTrue("But not the same", res != n);
142: }
143:
144: /** Test (de)serialization of classes with different name
145: */
146: public void testDeserialization() throws Exception {
147: ByteArrayOutputStream os = new ByteArrayOutputStream();
148:
149: NbObjectOutputStream oos = new NbObjectOutputStream(os);
150:
151: A a = new A();
152: a.value = "Ahoj";
153:
154: oos.writeObject(a);
155: oos.close();
156:
157: ByteArrayInputStream is = new ByteArrayInputStream(os
158: .toByteArray());
159: NbObjectInputStream ois = new NbObjectInputStream(is);
160:
161: Object res = ois.readObject();
162:
163: assertEquals("Must be class Ahoj", res.getClass(), Ahoj.class);
164:
165: Ahoj ahoj = (Ahoj) res;
166:
167: assertEquals("Must contain right values", ahoj.value, a.value);
168: }
169:
170: /** Test that we are able to read the original format.
171: */
172: public void testReadOldFormat() {
173: Utilities.initForLoader(new CL("UtilitiesTranslateOrig.txt"),
174: null);
175: }
176:
177: /** Translate whole original format.
178: */
179: public void testTranslateOldFormat() throws Exception {
180: Utilities.initForLoader(new CL("UtilitiesTranslateOrig.txt"),
181: null);
182:
183: InputStream is = getClass().getResourceAsStream(
184: "UtilitiesTranslateOrig.txt");
185: BufferedReader r = new BufferedReader(new InputStreamReader(is));
186:
187: for (;;) {
188: String line = r.readLine();
189: if (line == null) {
190: break;
191: }
192:
193: int space = line.indexOf(' ');
194: assertTrue("Line does not have spaces" + line, space >= 0);
195:
196: String o = line.substring(0, space).trim() + ".Ahoj";
197: String n = line.substring(space).trim() + ".Ahoj";
198:
199: String trans = Utilities.translate(o);
200:
201: assertEquals("Translatated as expected", n, trans);
202: }
203: }
204:
205: /** Test with empty classloader.
206: */
207: public void testNoConvesions() {
208: Utilities.initForLoader(new CL(null), null);
209:
210: Utilities.translate("something.strange");
211: Utilities.translate("anything");
212: }
213:
214: public void testEmptyFile() {
215: Utilities.initForLoader(new CL("UtilitiesTranslateEmpty.txt"),
216: null);
217:
218: Utilities.translate("something.strange");
219: Utilities.translate("anything");
220: }
221:
222: /** Test to fix bug 29878
223: */
224: public void testBug29878() {
225: Utilities.initForLoader(new CL("UtilitiesTranslate29878.txt"),
226: null);
227: Utilities
228: .translate("org.netbeans.modules.apisupport.APIModule");
229: }
230:
231: /** Fake classloader that returns provides file from getResources method.
232: */
233: private static final class CL extends ClassLoader {
234: private String file;
235:
236: public CL(String file) {
237: super (java.lang.Object.class.getClassLoader());
238: this .file = file;
239: }
240:
241: protected Enumeration findResources(String res)
242: throws IOException {
243: if (file != null) {
244: return Enumerations.singleton(getClass().getResource(
245: file));
246: } else {
247: return Enumerations.empty();
248: }
249: }
250:
251: } // end of CL
252:
253: /** A test to serialize and deserialize different class with same fields.
254: */
255: private static final class A implements Serializable {
256: final static long serialVersionUID = 1;
257: public String value;
258: }
259:
260: private static final class Ahoj implements Serializable {
261: final static long serialVersionUID = 1;
262: public String value;
263: }
264:
265: /** Useful code to write down current content of a registry
266: *
267: * /
268: public static void main(String[] args) throws Exception {
269: // String r = org.openide.util.Utilities.translate ("org.netbeans.modules.apisupport.APIModule");
270: // System.out.println("result: " + r);
271:
272:
273: Enumeration en;
274: ClassLoader c = org.openide.TopManager.getDefault().currentClassLoader();
275: en = c.getResources("META-INF/netbeans/translate.names");
276:
277:
278: PrintStream out = new PrintStream (new FileOutputStream ("/tmp/allnames.txt"));
279: while (en.hasMoreElements()) {
280: URL url = (URL)en.nextElement ();
281:
282: out.print ("# ");
283: out.println (url.toString());
284: out.println ();
285:
286: org.openide.filesystems.FileUtil.copy(url.openStream(), out);
287:
288: out.println ();
289: out.println ();
290: }
291:
292: out.close ();
293: }
294: */
295:
296: }
|