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.geometry;
017:
018: // JUnit dependencies
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import org.opengis.geometry.DirectPosition;
023:
024: /**
025: * Tests the {@link GeneralEnvelope} class.
026: *
027: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/geometry/GeneralEnvelopeTest.java $
028: * @version $Id: GeneralEnvelopeTest.java 26151 2007-07-04 18:54:48Z desruisseaux $
029: * @author Martin Desruisseaux
030: */
031: public class GeneralEnvelopeTest extends TestCase {
032: /**
033: * Run the suite from the command line.
034: */
035: public static void main(String[] args) {
036: junit.textui.TestRunner.run(suite());
037: }
038:
039: /**
040: * Returns the test suite.
041: */
042: public static Test suite() {
043: return new TestSuite(GeneralEnvelopeTest.class);
044: }
045:
046: /**
047: * Constructs a test case with the given name.
048: */
049: public GeneralEnvelopeTest(final String name) {
050: super (name);
051: }
052:
053: /**
054: * Tests {@link GeneralEnvelope#equals} method.
055: */
056: public void testEquals() {
057: /*
058: * Initialize an empty envelope. The new envelope is empty
059: * but not null because initialized to 0, not NaN.
060: */
061: final GeneralEnvelope e1 = new GeneralEnvelope(4);
062: assertTrue(e1.isEmpty());
063: assertFalse(e1.isNull());
064: assertEquals(e1.getLowerCorner(), e1.getUpperCorner());
065: /*
066: * Initialize with arbitrary coordinate values.
067: * Should not be empty anymore.
068: */
069: for (int i = e1.getDimension(); --i >= 0;) {
070: e1.setRange(i, i * 5 + 2, i * 6 + 5);
071: }
072: assertFalse(e1.isNull());
073: assertFalse(e1.isEmpty());
074: assertFalse(e1.getLowerCorner().equals(e1.getUpperCorner()));
075: /*
076: * Creates a new envelope initialized with the same
077: * coordinate values. The two envelope should be equals.
078: */
079: final GeneralEnvelope e2 = new GeneralEnvelope(e1);
080: assertPositionEquals(e1.getLowerCorner(), e2.getLowerCorner());
081: assertPositionEquals(e1.getUpperCorner(), e2.getUpperCorner());
082: assertTrue(e1.contains(e2, true));
083: assertFalse(e1.contains(e2, false));
084: assertNotSame(e1, e2);
085: assertEquals(e1, e2);
086: assertTrue(e1.equals(e2, 1E-4, true));
087: assertTrue(e1.equals(e2, 1E-4, false));
088: assertEquals(e1.hashCode(), e2.hashCode());
089: /*
090: * Offset slightly one coordinate value. Should not be equals anymore,
091: * except when comparing with a tolerance value.
092: */
093: e2
094: .setRange(2, e2.getMinimum(2) + 3E-5,
095: e2.getMaximum(2) - 3E-5);
096: assertTrue(e1.contains(e2, true));
097: assertFalse(e1.contains(e2, false));
098: assertFalse(e1.equals(e2));
099: assertTrue(e1.equals(e2, 1E-4, true));
100: assertTrue(e1.equals(e2, 1E-4, false));
101: assertFalse(e1.hashCode() == e2.hashCode());
102: /*
103: * Apply a greater offset. Should not be equals,
104: * even when comparing with a tolerance value.
105: */
106: e2.setRange(1, e2.getMinimum(1) + 3, e2.getMaximum(1) - 3);
107: assertTrue(e1.contains(e2, true));
108: assertFalse(e1.contains(e2, false));
109: assertFalse(e1.equals(e2));
110: assertFalse(e1.equals(e2, 1E-4, true));
111: assertFalse(e1.equals(e2, 1E-4, false));
112: assertFalse(e1.hashCode() == e2.hashCode());
113: }
114:
115: /**
116: * Compares the specified corners.
117: */
118: private static void assertPositionEquals(final DirectPosition p1,
119: final DirectPosition p2) {
120: assertNotSame(p1, p2);
121: assertEquals(p1, p2);
122: assertEquals(p1.hashCode(), p2.hashCode());
123: }
124: }
|