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