organizing imports
[cdmlib.git] / cdmlib-model / src / test / java / eu / etaxonomy / cdm / model / location / PointTest.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.model.location;
11
12
13 import java.text.ParseException;
14
15 import org.apache.log4j.Logger;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20
21 import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
22 import eu.etaxonomy.cdm.model.location.Point.Direction;
23 import eu.etaxonomy.cdm.model.location.Point.Sexagesimal;
24
25 /**
26 * @author a.mueller
27 * @date 04.06.2010
28 *
29 */
30 public class PointTest {
31 @SuppressWarnings("unused")
32 private static final Logger logger = Logger.getLogger(PointTest.class);
33
34 private Point point1;
35 private Point point2;
36
37 private Integer errorRadius;
38 private Double longitude1;
39 private Double latitude1;
40 private Double longitude2;
41 private Double latitude2;
42
43 private ReferenceSystem referenceSystem;
44
45
46 /**
47 * @throws java.lang.Exception
48 */
49 @BeforeClass
50 public static void setUpBeforeClass() throws Exception {
51 if (ReferenceSystem.WGS84() == null){
52 new DefaultTermInitializer().initialize();
53 }
54 }
55
56 /**
57 * @throws java.lang.Exception
58 */
59 @Before
60 public void setUp() throws Exception {
61 longitude1 = 23.123556;
62 latitude1 = -13.975556;
63
64 longitude2 = 28.48556;
65 latitude2 = 12.656;
66
67 errorRadius = 5;
68 referenceSystem = ReferenceSystem.GOOGLE_EARTH();
69
70 point1 = Point.NewInstance(longitude1, latitude1, referenceSystem, errorRadius);
71 point2 = Point.NewInstance();
72
73
74
75 }
76
77 //********************** TESTS *****************************
78
79 @Test
80 public void testNewInstance(){
81 Assert.assertNotNull("ReferenceSystem must not be null", referenceSystem);
82 Assert.assertNotNull("Point1 must not be null", point1);
83 Assert.assertNotNull("Point2 must not be null", point2);
84 Assert.assertEquals("", longitude1, point1.getLongitude());
85
86 Assert.assertEquals("", latitude1, point1.getLatitude());
87 Assert.assertEquals("", errorRadius, point1.getErrorRadius());
88 Assert.assertEquals("", referenceSystem, point1.getReferenceSystem());
89
90 Assert.assertNull("LongitudeSexagesimal should be null", point2.getLongitudeSexagesimal());
91 Assert.assertNull("LatitudeSexagesimal should be null", point2.getLatitudeSexagesimal());
92 }
93
94 @Test
95 public void testGetSetLongitude(){
96 point2.setLongitude(5.888);
97 Assert.assertEquals(Double.valueOf(5.888), point2.getLongitude());
98 point2.setLongitude(null);
99 Assert.assertEquals(null, point2.getLongitude());
100 }
101
102 @Test
103 public void testGetSetLatitude(){
104 point2.setLatitude(-34.987);
105 Assert.assertEquals(Double.valueOf(-34.987), point2.getLatitude());
106 point2.setLatitude(null);
107 Assert.assertEquals(null, point2.getLatitude());
108 }
109
110 @Test
111 public void testGetSetErrorRadius(){
112 point2.setErrorRadius(7);
113 Assert.assertEquals(Integer.valueOf(7), point2.getErrorRadius());
114 point2.setErrorRadius(null);
115 Assert.assertEquals(null, point2.getErrorRadius());
116 }
117
118 @Test
119 public void testGetSetReferenceSystem(){
120 ReferenceSystem newRefSystem = ReferenceSystem.NewInstance();
121 point2.setReferenceSystem(newRefSystem);
122 Assert.assertEquals(newRefSystem, point2.getReferenceSystem());
123 point2.setReferenceSystem(null);
124 Assert.assertEquals(null, point2.getReferenceSystem());
125 }
126
127 @Test
128 public void testGetLongitudeSexagesimal(){
129 Assert.assertEquals("23\u00B07'24.801\"E", point1.getLongitudeSexagesimal().toString(true, false));
130
131
132 point2.setLongitudeSexagesimal(Sexagesimal.NewInstance(5, 22, null, Direction.WEST));
133 Assert.assertEquals((Integer)22, point2.getLongitudeSexagesimal().minutes);
134 Assert.assertEquals((Integer)0, point2.getLongitudeSexagesimal().seconds);
135
136 Double latitudeDouble = -45.57389326;
137 point1.setLatitudeSexagesimal(Sexagesimal.valueOf(latitudeDouble, true));
138 //Not true because of rounding errors
139 // Assert.assertEquals("latitudeDouble must be equal", latitudeDouble, point1.getLatitude());
140
141 Sexagesimal sexagesimal1 = Sexagesimal.NewInstance(0, 0, 0, Direction.WEST);
142 Sexagesimal sexagesimal2 = Sexagesimal.NewInstance(2, 2, 2, Direction.WEST);
143 Assert.assertNotSame("", sexagesimal1, sexagesimal2);
144
145
146 }
147
148 @Test
149 public void testParsing(){
150 try {
151 Assert.assertEquals("", longitude1, point1.getLongitude());
152 Assert.assertTrue("", latitude1.equals(point1.getLatitude()));
153 point1.setLatitudeByParsing("35\u00B034'20\"S");
154 Assert.assertEquals("", longitude1, point1.getLongitude());
155 Assert.assertFalse("", latitude1.equals(point1.getLatitude()));
156 Assert.assertEquals("", Double.valueOf("-35.57222222222222"), point1.getLatitude());
157 } catch (ParseException e) {
158 Assert.fail("No parsing error should occur");
159 }
160 try {
161 point1.setLongitudeByParsing("112\u00B034.34'N");
162 Assert.assertEquals("", "112.57233", point1.getLongitude().toString().substring(0,9));
163 } catch (ParseException e) {
164 Assert.fail("No parsing error should occur");
165 }
166 try {
167 point1.setLatitudeByParsing("112\u00B034.34'S");
168 Assert.fail("Latitude can not be > 90");
169 } catch (ParseException e) {
170 Assert.assertTrue("Latitude can not be > 90", true);
171 }
172 try {
173 point1.setLongitudeByParsing("45\u00B034.34'S");
174 Assert.fail("Longitude can not be S");
175 } catch (ParseException e) {
176 Assert.assertTrue("Longitude can not be S", true);
177 }
178 //#2962 (rounding of tertiers)
179 try {
180 point1.setLatitudeByParsing("37\u00B07'44\"N");
181 Assert.assertEquals("Result should be 37\u00B07'44\"N not 37\u00B07'44.999\"N", "37\u00B07'44\"N", point1.getLatitudeSexagesimal().toString());
182
183 point1.setLatitudeByParsing("37\u00B07'45\"N");
184 Assert.assertEquals("Result should be 37\u00B07'45\"N not 37\u00B07'45.\"N", "37\u00B07'45\"N", point1.getLatitudeSexagesimal().toString());
185
186 } catch (ParseException e) {
187 Assert.fail("No parsing error should occur");
188 }
189
190
191
192
193
194
195
196
197 // Assert.assertTrue("Southern must be negative", conversionResults.convertedCoord < 0);
198 // Assert.assertFalse("Southern must be latitude", conversionResults.isLongitude);
199 //
200 // conversionResults = coordinateConverter.tryConvert("35\u00B034.744");
201 // Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
202 // Assert.assertNull("Longitude must be undefined", conversionResults.isLongitude);
203 //
204 // conversionResults = coordinateConverter.tryConvert("95\u00B034.744");
205 // Assert.assertTrue("Longitude must be defined", conversionResults.isLongitude);
206 //
207 //
208 // conversionResults = coordinateConverter.tryConvert("-35\u00B034'55.67S");
209 // Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
210 //
211 // conversionResults = coordinateConverter.tryConvert("35\u00B011'34.744SN");
212 // Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
213 //
214 // conversionResults = coordinateConverter.tryConvert("35\u00B011'34.744SW");
215 // Assert.assertTrue("Western must be longitude", conversionResults.isLongitude);
216 //
217 // conversionResults = coordinateConverter.tryConvert("35D11M34.744S");
218 // Assert.assertNull("isLongitude must be undefined. S stands for second.", conversionResults.isLongitude);
219
220 }
221
222
223 @Test
224 public void testDoubleParsing(){
225 try {
226 Assert.assertEquals("", longitude1, point1.getLongitude());
227 Assert.assertTrue("", latitude1.equals(point1.getLatitude()));
228 point1.setLatitudeByParsing("33.474");
229 Assert.assertEquals("", longitude1, point1.getLongitude());
230 Assert.assertFalse("", latitude1.equals(point1.getLatitude()));
231 Assert.assertEquals("", Double.valueOf("33.474"), point1.getLatitude());
232 point1.setLatitudeByParsing("-39,474");
233 Assert.assertEquals("", Double.valueOf("-39.474"), point1.getLatitude());
234 } catch (ParseException e) {
235 Assert.fail("No parsing error should occur");
236 }
237
238 try {
239 point1.setLongitudeByParsing("-120.4");
240 Assert.assertEquals("", "-120.4", point1.getLongitude().toString());
241 point1.setLongitudeByParsing("53,4");
242 Assert.assertEquals("", "53.4", point1.getLongitude().toString());
243 } catch (ParseException e) {
244 Assert.fail("No parsing error should occur");
245 }
246 try {
247 point1.setLatitudeByParsing("112.456");
248 Assert.fail("Latitude can not be > 90");
249 } catch (ParseException e) {
250 Assert.assertTrue("Latitude can not be > 90", true);
251 }
252
253 try {
254 point1.setLongitudeByParsing("191");
255 Assert.fail("Longitude can be > 180°");
256 } catch (ParseException e) {
257 Assert.assertTrue("Longitude can not > 180°", true);
258 }
259 try {
260 point1.setLatitudeByParsing("2\u00B039'38,5956\"S");
261 } catch (ParseException e) {
262 Assert.fail("String '2°39'38,5956\"S'should be parsable");
263 }
264 }
265
266 /**
267 * I don't exactly know what should happen here.
268 * Please see http://dev.e-taxonomy.eu/trac/ticket/2267#comment:3 on why this test was created
269 *
270 * @throws ParseException
271 */
272 @Test
273 public void testParsingHexagesimalAndDecimalMixed() throws ParseException{
274 String example = "35\u00B034'55.67\"S";
275 point1.setLatitudeByParsing(example);
276 Assert.assertEquals(example, point1.getLatitudeSexagesimal().toString());
277 }
278
279 @Test
280 public void testStaticParsing(){
281 try{
282 Point.parseLatitude("1");
283 }catch (NullPointerException e){
284 Assert.fail("No NullPointerException should occur");
285 } catch (ParseException e) {
286 Assert.fail("No parsing error should occur");
287 }
288 }
289
290
291
292 }