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