3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.model
.location
;
13 import java
.text
.ParseException
;
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
;
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
;
30 public class PointTest
{
31 @SuppressWarnings("unused")
32 private static final Logger logger
= Logger
.getLogger(PointTest
.class);
37 private Integer errorRadius
;
38 private Double longitude1
;
39 private Double latitude1
;
40 private Double longitude2
;
41 private Double latitude2
;
43 private ReferenceSystem referenceSystem
;
47 * @throws java.lang.Exception
50 public static void setUpBeforeClass() throws Exception
{
51 if (ReferenceSystem
.WGS84() == null){
52 new DefaultTermInitializer().initialize();
57 * @throws java.lang.Exception
60 public void setUp() throws Exception
{
61 longitude1
= 23.123556;
62 latitude1
= -13.975556;
64 longitude2
= 28.48556;
68 referenceSystem
= ReferenceSystem
.GOOGLE_EARTH();
70 point1
= Point
.NewInstance(longitude1
, latitude1
, referenceSystem
, errorRadius
);
71 point2
= Point
.NewInstance();
77 //********************** TESTS *****************************
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());
86 Assert
.assertEquals("", latitude1
, point1
.getLatitude());
87 Assert
.assertEquals("", errorRadius
, point1
.getErrorRadius());
88 Assert
.assertEquals("", referenceSystem
, point1
.getReferenceSystem());
90 Assert
.assertNull("LongitudeSexagesimal should be null", point2
.getLongitudeSexagesimal());
91 Assert
.assertNull("LatitudeSexagesimal should be null", point2
.getLatitudeSexagesimal());
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());
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());
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());
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());
128 public void testGetLongitudeSexagesimal(){
129 Assert
.assertEquals("23\u00B07'24.801\"E", point1
.getLongitudeSexagesimal().toString(true, false));
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
);
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());
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
);
149 public void testParsing(){
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");
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");
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);
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);
178 //#2962 (rounding of tertiers)
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());
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());
186 } catch (ParseException e
) {
187 Assert
.fail("No parsing error should occur");
197 // Assert.assertTrue("Southern must be negative", conversionResults.convertedCoord < 0);
198 // Assert.assertFalse("Southern must be latitude", conversionResults.isLongitude);
200 // conversionResults = coordinateConverter.tryConvert("35\u00B034.744");
201 // Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
202 // Assert.assertNull("Longitude must be undefined", conversionResults.isLongitude);
204 // conversionResults = coordinateConverter.tryConvert("95\u00B034.744");
205 // Assert.assertTrue("Longitude must be defined", conversionResults.isLongitude);
208 // conversionResults = coordinateConverter.tryConvert("-35\u00B034'55.67S");
209 // Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
211 // conversionResults = coordinateConverter.tryConvert("35\u00B011'34.744SN");
212 // Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
214 // conversionResults = coordinateConverter.tryConvert("35\u00B011'34.744SW");
215 // Assert.assertTrue("Western must be longitude", conversionResults.isLongitude);
217 // conversionResults = coordinateConverter.tryConvert("35D11M34.744S");
218 // Assert.assertNull("isLongitude must be undefined. S stands for second.", conversionResults.isLongitude);
224 public void testDoubleParsing(){
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");
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");
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);
254 point1
.setLongitudeByParsing("191");
255 Assert
.fail("Longitude can be > 180°");
256 } catch (ParseException e
) {
257 Assert
.assertTrue("Longitude can not > 180°", true);
260 point1
.setLatitudeByParsing("2\u00B039'38,5956\"S");
261 } catch (ParseException e
) {
262 Assert
.fail("String '2°39'38,5956\"S'should be parsable");
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
270 * @throws ParseException
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());
280 public void testStaticParsing(){
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");