001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, 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.feature.visitor;
017:
018: import junit.framework.TestCase;
019:
020: /*
021: So far CalcResult contains the following conversion methods which call
022: getValue() and then convert to the type we want. The ones marked below with a
023: "!" haven't really found an implementation yet and no tests have been
024: written. They should work under normal circumstances, but will likely explode:
025: public int toInt()
026: public double toDouble();
027: public String toString();
028: public long toLong();
029: public float toFloat();
030: ! public Geometry toGeometry();
031: ! public Point toPoint();
032: ! public Set toSet();
033: ! public List toList();
034: ! public Object[] toArray();
035: ! public Map toMap();
036: */
037:
038: //TODO: add more tests as needed
039: /**
040: * Purpose: these tests ensure that the output of CalcResult converts as expected.
041: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/feature/visitor/CalcResultTest.java $
042: */
043: public class CalcResultTest extends TestCase {
044: MockCalcResult result = new MockCalcResult();
045: Integer val1 = new Integer(4);
046: Long val2 = new Long(5);
047: Float val3 = new Float(6.0);
048: Double val4 = new Double(7.0);
049: String val5 = new String("8");
050: String val6 = new String("Random text of arbitrary complexity.");
051:
052: public void testInt() {
053: //int --> int
054: result.setValue(val1);
055: assertEquals((int) 4, result.toInt());
056:
057: //long --> int
058: result.setValue(val2);
059: assertEquals((int) 5, result.toInt());
060:
061: //float --> int
062: result.setValue(val3);
063: assertEquals((int) 6, result.toInt());
064:
065: //double --> int
066: result.setValue(val4);
067: assertEquals((int) 7, result.toInt());
068:
069: //string --> int (a string that looks like a number, should we add this functionality?)
070: result.setValue(val5);
071: assertEquals((int) 0, result.toInt());
072:
073: //string --> int (a real string which clearly isn't an int, so we expect nothing good)
074: result.setValue(val6);
075: assertEquals((int) 0, result.toInt());
076: }
077:
078: public void testLong() {
079: //int --> long
080: result.setValue(val1);
081: assertEquals((long) 4, result.toLong());
082:
083: //long --> long
084: result.setValue(val2);
085: assertEquals((long) 5, result.toLong());
086:
087: //float --> long
088: result.setValue(val3);
089: assertEquals((long) 6, result.toLong());
090:
091: //double --> long
092: result.setValue(val4);
093: assertEquals((long) 7, result.toLong());
094:
095: //string --> long (a string that looks like a number, should we add this functionality?)
096: result.setValue(val5);
097: assertEquals((long) 0, result.toLong());
098:
099: //string --> long (a real string which clearly isn't an int, so we expect nothing good)
100: result.setValue(val6);
101: assertEquals((long) 0, result.toLong());
102: }
103:
104: public void testFloat() {
105: //int --> float
106: result.setValue(val1);
107: assertEquals((float) 4, result.toFloat(), 0);
108:
109: //long --> float
110: result.setValue(val2);
111: assertEquals((float) 5, result.toFloat(), 0);
112:
113: //float --> float
114: result.setValue(val3);
115: assertEquals((float) 6, result.toFloat(), 0);
116:
117: //double --> float
118: result.setValue(val4);
119: assertEquals((float) 7, result.toFloat(), 0);
120:
121: //string --> float (a string that looks like a number, should we add this functionality?)
122: result.setValue(val5);
123: assertEquals((float) 0, result.toFloat(), 0);
124:
125: //string --> float (a real string which clearly isn't an int, so we expect nothing good)
126: result.setValue(val6);
127: assertEquals((float) 0, result.toFloat(), 0);
128: }
129:
130: public void testDouble() {
131: //int --> double
132: result.setValue(val1);
133: assertEquals((double) 4, result.toDouble(), 0);
134:
135: //long --> double
136: result.setValue(val2);
137: assertEquals((double) 5, result.toDouble(), 0);
138:
139: //float --> double
140: result.setValue(val3);
141: assertEquals((double) 6, result.toDouble(), 0);
142:
143: //double --> double
144: result.setValue(val4);
145: assertEquals((double) 7, result.toDouble(), 0);
146:
147: //string --> double (a string that looks like a number, should we add this functionality?)
148: result.setValue(val5);
149: assertEquals((double) 0, result.toDouble(), 0);
150:
151: //string --> double (a real string which clearly isn't an int, so we expect nothing good)
152: result.setValue(val6);
153: assertEquals((double) 0, result.toDouble(), 0);
154: }
155:
156: public void testString() {
157: //int --> string
158: result.setValue(val1);
159: assertEquals("4", result.toString());
160:
161: //long --> string
162: result.setValue(val2);
163: assertEquals("5", result.toString());
164:
165: //float --> string
166: result.setValue(val3);
167: assertEquals("6.0", result.toString());
168:
169: //double --> string
170: result.setValue(val4);
171: assertEquals("7.0", result.toString());
172:
173: //string --> string
174: result.setValue(val5);
175: assertEquals("8", result.toString());
176:
177: //string --> string
178: result.setValue(val6);
179: assertEquals("Random text of arbitrary complexity.", result
180: .toString());
181: }
182:
183: /**
184: * A simple results set which allows us to test the AbstractCalcResult type
185: * conversions
186: */
187: public class MockCalcResult extends AbstractCalcResult {
188: Object value;
189:
190: public MockCalcResult() {
191: }
192:
193: public Object getValue() {
194: return value;
195: }
196:
197: public void setValue(Object value) {
198: this.value = value;
199: }
200: }
201: }
|