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.HashMap;
020: import java.util.HashSet;
021: import java.util.Map;
022: import java.util.Set;
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026: import org.opengis.metadata.citation.Citation;
027: import org.geotools.metadata.iso.citation.CitationImpl;
028: import org.geotools.metadata.iso.citation.Citations;
029:
030: /**
031: * Tests the {@link MetadataStandard} class.
032: *
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/test/java/org/geotools/metadata/MetadataStandardTest.java $
034: * @version $Id: MetadataStandardTest.java 25175 2007-04-16 13:40:57Z desruisseaux $
035: * @author Martin Desruisseaux
036: */
037: public class MetadataStandardTest 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(MetadataStandardTest.class);
050: }
051:
052: /**
053: * Constructs a test case with the given name.
054: */
055: public MetadataStandardTest(final String name) {
056: super (name);
057: }
058:
059: /**
060: * Tests the shallow equals and copy methods.
061: */
062: public void testEquals() {
063: final MetadataStandard std = MetadataStandard.ISO_19115;
064: Citation citation = Citations.EPSG;
065: assertFalse(std
066: .shallowEquals(citation, Citations.GEOTIFF, true));
067: assertFalse(std.shallowEquals(citation, Citations.GEOTIFF,
068: false));
069: assertTrue(std.shallowEquals(citation, Citations.EPSG, false));
070:
071: citation = new CitationImpl();
072: std.shallowCopy(Citations.EPSG, citation, true);
073: assertFalse(std
074: .shallowEquals(citation, Citations.GEOTIFF, true));
075: assertFalse(std.shallowEquals(citation, Citations.GEOTIFF,
076: false));
077: assertTrue(std.shallowEquals(citation, Citations.EPSG, false));
078:
079: try {
080: std.shallowCopy(citation, Citations.EPSG, true);
081: fail("Citations.EPSG should be unmodifiable.");
082: } catch (UnmodifiableMetadataException e) {
083: // This is the expected exception.
084: }
085: }
086:
087: /**
088: * Tests the {@link PropertyMap} implementation.
089: */
090: public void testMap() {
091: final Citation citation = new CitationImpl(Citations.EPSG);
092: final Map map = MetadataStandard.ISO_19115.asMap(citation);
093: assertFalse(map.isEmpty());
094: assertTrue(map.size() > 1);
095:
096: final Set keys = map.keySet();
097: assertTrue("Property exists and should be defined.", keys
098: .contains("title"));
099: assertFalse(
100: "Property exists but undefined for Citations.EPSG.",
101: keys.contains("ISBN"));
102: assertFalse("Property do not exists.", keys.contains("dummy"));
103:
104: final String s = keys.toString();
105: assertTrue(s.indexOf("title") >= 0);
106: assertTrue(s.indexOf("identifiers") >= 0);
107: assertFalse(s.indexOf("ISBN") >= 0);
108:
109: final Object identifiers = map.get("identifiers");
110: assertTrue(identifiers instanceof Collection);
111: assertTrue(((Collection) identifiers).contains("EPSG"));
112:
113: final Map copy = new HashMap(map);
114: assertEquals(map, copy);
115:
116: // Note: AbstractCollection do not defines hashCode(); we have to wraps in a HashSet.
117: final int hashCode = citation.hashCode();
118: assertEquals("hashCode() should be as in a Set.", hashCode,
119: new HashSet(map.values()).hashCode());
120: assertEquals("hashCode() should be as in a Set.", hashCode,
121: new HashSet(copy.values()).hashCode());
122:
123: map.remove("identifiers");
124: final int newHashCode = citation.hashCode();
125: assertFalse(map.equals(copy));
126: assertFalse(hashCode == newHashCode);
127: assertEquals(newHashCode, new HashSet(map.values()).hashCode());
128: }
129: }
|