001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.metadata;
017:
018: import java.util.Collection;
019: import java.util.HashSet;
020: import java.util.Set;
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024: import org.geotools.util.SimpleInternationalString;
025: import org.opengis.metadata.citation.Citation;
026: import org.geotools.metadata.iso.citation.CitationImpl;
027: import org.geotools.metadata.iso.citation.Citations;
028: import org.opengis.util.InternationalString;
029:
030: /**
031: * Tests the {@link PropertyAccessor} class.
032: *
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/test/java/org/geotools/metadata/PropertyAccessorTest.java $
034: * @version $Id: PropertyAccessorTest.java 25193 2007-04-18 13:37:38Z desruisseaux $
035: * @author Martin Desruisseaux
036: */
037: public class PropertyAccessorTest extends TestCase {
038: /**
039: * Run the suit from the command line.
040: */
041: public static void main(final String[] args) {
042: junit.textui.TestRunner.run(suite());
043: }
044:
045: /**
046: * Returns the test suite.
047: */
048: public static Test suite() {
049: return new TestSuite(PropertyAccessorTest.class);
050: }
051:
052: /**
053: * Constructs a test case with the given name.
054: */
055: public PropertyAccessorTest(final String name) {
056: super (name);
057: }
058:
059: /**
060: * Creates a property accessor for the given citation.
061: */
062: private static PropertyAccessor createPropertyAccessor(
063: final Citation citation) {
064: final Class implementation = citation.getClass();
065: final Class type = PropertyAccessor.getType(implementation,
066: "org.opengis.metadata");
067: assertNotNull(type);
068: return new PropertyAccessor(implementation, type);
069: }
070:
071: /**
072: * Tests the constructor.
073: */
074: public void testConstructor() {
075: final Citation citation = Citations.EPSG;
076: PropertyAccessor accessor;
077: assertNull("No dummy interface expected.", PropertyAccessor
078: .getType(citation.getClass(), "org.opengis.dummy"));
079: accessor = createPropertyAccessor(citation);
080: assertEquals("Count of 'get' methods.", 14, accessor.count());
081: }
082:
083: /**
084: * Tests the {@code indexOf} and {code name} methods.
085: */
086: public void testName() {
087: final Citation citation = Citations.EPSG;
088: final PropertyAccessor accessor = createPropertyAccessor(citation);
089: assertEquals("Non-existent property", -1, accessor
090: .indexOf("dummy"));
091: assertEquals("getTitle() property", "title", accessor
092: .name(accessor.indexOf("title")));
093: assertEquals("getTitle() property", "title", accessor
094: .name(accessor.indexOf("TITLE")));
095: assertEquals("getISBN() property", "ISBN", accessor
096: .name(accessor.indexOf("ISBN")));
097: assertNull(accessor.name(-1));
098: }
099:
100: /**
101: * Tests the get method.
102: */
103: public void testGet() {
104: Citation citation = Citations.EPSG;
105: final PropertyAccessor accessor = createPropertyAccessor(citation);
106: final int index = accessor.indexOf("identifiers");
107: assertTrue(String.valueOf(index), index >= 0);
108: final Object identifiers = accessor.get(index, citation);
109: assertNotNull(identifiers);
110: assertTrue(identifiers instanceof Collection);
111: assertTrue(((Collection) identifiers).contains("EPSG"));
112: }
113:
114: /**
115: * Tests the shallow equals and copy methods.
116: */
117: public void testEquals() {
118: Citation citation = Citations.EPSG;
119: final PropertyAccessor accessor = createPropertyAccessor(citation);
120: assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF,
121: true));
122: assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF,
123: false));
124: assertTrue(accessor.shallowEquals(citation, Citations.EPSG,
125: false));
126:
127: citation = new CitationImpl();
128: assertTrue(accessor.shallowCopy(Citations.EPSG, citation, true));
129: assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF,
130: true));
131: assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF,
132: false));
133: assertTrue(accessor.shallowEquals(citation, Citations.EPSG,
134: false));
135:
136: final int index = accessor.indexOf("identifiers");
137: final Object source = accessor.get(index, Citations.EPSG);
138: final Object target = accessor.get(index, citation);
139: assertNotNull(source);
140: assertNotNull(target);
141: assertNotSame(source, target);
142: assertEquals(source, target);
143: assertTrue(((Collection) target).contains("EPSG"));
144:
145: assertSame(target, accessor.set(index, citation, null));
146: final Object value = accessor.get(index, citation);
147: assertNotNull(value);
148: assertTrue(((Collection) value).isEmpty());
149:
150: try {
151: accessor.shallowCopy(citation, Citations.EPSG, true);
152: fail("Citations.EPSG should be unmodifiable.");
153: } catch (UnmodifiableMetadataException e) {
154: // This is the expected exception.
155: }
156: }
157:
158: /**
159: * Tests the hash code computation.
160: */
161: public void testHashCode() {
162: final CitationImpl citation = new CitationImpl();
163: final PropertyAccessor accessor = createPropertyAccessor(citation);
164: int hashCode = accessor.hashCode(citation);
165: assertEquals("Empty metadata.", 0, hashCode);
166:
167: final String ISBN = "Dummy ISBN";
168: citation.setISBN(ISBN);
169: hashCode = accessor.hashCode(citation);
170: assertEquals("Metadata with a single String value.", ISBN
171: .hashCode(), hashCode);
172:
173: final Set set = new HashSet();
174: assertEquals("By Set.hashCode() contract.", 0, set.hashCode());
175: assertTrue(set.add(ISBN));
176: assertEquals("Expected Metadata.hashCode() == Set.hashCode().",
177: set.hashCode(), hashCode);
178:
179: final InternationalString title = new SimpleInternationalString(
180: "Dummy title");
181: citation.setTitle(title);
182: hashCode = accessor.hashCode(citation);
183: assertEquals("Metadata with two values.", ISBN.hashCode()
184: + title.hashCode(), hashCode);
185: assertTrue(set.add(title));
186: assertEquals("Expected Metadata.hashCode() == Set.hashCode().",
187: set.hashCode(), hashCode);
188: assertEquals("CitationsImpl.hashCode() should delegate.",
189: hashCode, citation.hashCode());
190:
191: final Collection values = citation.asMap().values();
192: assertEquals(hashCode, new HashSet(values).hashCode());
193: assertTrue(values.containsAll(set));
194: assertTrue(set.containsAll(values));
195: }
196: }
|