001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.util;
018:
019: // J2SE dependencies
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.ObjectInputStream;
024: import java.io.ObjectOutputStream;
025: import java.util.Locale;
026:
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030:
031: import org.opengis.util.GenericName;
032:
033: /**
034: * Tests the various {@link InternationalString} implementations.
035: *
036: * @author Martin Desruisseaux
037: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/test/java/org/geotools/util/InternationalStringTest.java $
038: * @version $Id: InternationalStringTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
039: */
040: public final class InternationalStringTest extends TestCase {
041: /**
042: * Run the suit from the command line.
043: */
044: public static void main(final String[] args) {
045: org.geotools.util.logging.Logging.GEOTOOLS
046: .forceMonolineConsoleOutput();
047: junit.textui.TestRunner.run(suite());
048: }
049:
050: /**
051: * Returns the test suite.
052: */
053: public static Test suite() {
054: return new TestSuite(InternationalStringTest.class);
055: }
056:
057: /**
058: * Constructs a test case with the given name.
059: */
060: public InternationalStringTest(final String name) {
061: super (name);
062: }
063:
064: /**
065: * Tests the {@link SimpleInternationalString} implementation.
066: */
067: public void testSimple() throws IOException, ClassNotFoundException {
068: final String message = "This is an unlocalized message";
069: final SimpleInternationalString toTest = new SimpleInternationalString(
070: message);
071: assertSame("Construction:", message, toTest.toString());
072: basicTests(toTest);
073: }
074:
075: /**
076: * Tests the {@link SimpleInternationalString} implementation.
077: */
078: public void testGrowable() throws IOException,
079: ClassNotFoundException {
080: final String message = "This is an unlocalized message";
081: final String messageEn = "This is a localized message";
082: final String messageFr = "Voici un message";
083: final String messageFrCa = "Caribou!";
084: GrowableInternationalString toTest = new GrowableInternationalString();
085: basicTests(toTest);
086: toTest.add(Locale.ENGLISH, message);
087: assertSame("Addition:", message, toTest.toString());
088: basicTests(toTest);
089:
090: toTest = new GrowableInternationalString(message);
091: assertSame("Construction:", message, toTest.toString());
092: basicTests(toTest);
093: toTest.add(Locale.ENGLISH, messageEn);
094: basicTests(toTest);
095: toTest.add(Locale.FRENCH, messageFr);
096: basicTests(toTest);
097: assertEquals("Unlocalized message:", message, toTest
098: .toString(null));
099: assertEquals("English message:", messageEn, toTest
100: .toString(Locale.ENGLISH));
101: assertEquals("French message:", messageFr, toTest
102: .toString(Locale.FRENCH));
103: assertEquals("French message:", messageFr, toTest
104: .toString(Locale.CANADA_FRENCH));
105: assertEquals("Other language:", message, toTest
106: .toString(Locale.CHINESE));
107: toTest.add(Locale.CANADA_FRENCH, messageFrCa);
108: basicTests(toTest);
109: assertEquals("Unlocalized message:", message, toTest
110: .toString(null));
111: assertEquals("English message:", messageEn, toTest
112: .toString(Locale.ENGLISH));
113: assertEquals("French message:", messageFr, toTest
114: .toString(Locale.FRENCH));
115: assertEquals("French message:", messageFrCa, toTest
116: .toString(Locale.CANADA_FRENCH));
117: assertEquals("Other language:", message, toTest
118: .toString(Locale.CHINESE));
119: }
120:
121: /**
122: * Tests the {@link GenericName} implementation.
123: */
124: public void testName() throws IOException, ClassNotFoundException {
125: final GenericName name = NameFactory
126: .create("codespace:subspace:name");
127: basicTests(name);
128: assertEquals("toString:", "codespace:subspace:name", name
129: .toString());
130: assertEquals("toString:", "codespace:subspace", name.getScope()
131: .toString());
132: assertEquals("toString:", "codespace", name.getScope()
133: .getScope().toString());
134: assertSame("asScopedName", name, name.asScopedName());
135: assertSame("asLocalName", name, name.asLocalName()
136: .asScopedName());
137: }
138:
139: /**
140: * Performs basic test on the given object.
141: */
142: private void basicTests(final Comparable toTest)
143: throws IOException, ClassNotFoundException {
144: assertEquals("CompareTo: ", 0, toTest.compareTo(toTest));
145: assertEquals("Equals:", toTest, toTest);
146: if (toTest instanceof CharSequence) {
147: // TODO: Uncomment when we will be allowed to use J2SE 1.5
148: // assertEquals("CharSequence:", toTest.toString(),
149: // new StringBuffer((CharSequence) toTest).toString());
150: }
151: /*
152: * Tests serialization
153: */
154: final ByteArrayOutputStream out = new ByteArrayOutputStream();
155: final ObjectOutputStream objectOut = new ObjectOutputStream(out);
156: objectOut.writeObject(toTest);
157: objectOut.close();
158:
159: final ByteArrayInputStream in = new ByteArrayInputStream(out
160: .toByteArray());
161: final ObjectInputStream objectIn = new ObjectInputStream(in);
162: final Object object = objectIn.readObject();
163: objectIn.close();
164:
165: assertEquals("Serialization:", toTest.getClass(), object
166: .getClass());
167: assertEquals("Serialization:", toTest, object);
168: assertEquals("Hash code:", toTest.hashCode(), object.hashCode());
169: }
170: }
|