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
.Ignore
;
20 import org
.junit
.Test
;
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
;
31 public class PointTest
{
32 @SuppressWarnings("unused")
33 private static final Logger logger
= Logger
.getLogger(PointTest
.class);
38 private Integer errorRadius
;
39 private Double longitude1
;
40 private Double latitude1
;
41 private Double longitude2
;
42 private Double latitude2
;
44 private ReferenceSystem referenceSystem
;
48 * @throws java.lang.Exception
51 public static void setUpBeforeClass() throws Exception
{
52 if (ReferenceSystem
.WGS84() == null){
53 new DefaultTermInitializer().initialize();
58 * @throws java.lang.Exception
61 public void setUp() throws Exception
{
62 longitude1
= 23.123556;
63 latitude1
= -13.975556;
65 longitude2
= 28.48556;
69 referenceSystem
= ReferenceSystem
.GOOGLE_EARTH();
71 point1
= Point
.NewInstance(longitude1
, latitude1
, referenceSystem
, errorRadius
);
72 point2
= Point
.NewInstance();
78 //********************** TESTS *****************************
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());
87 Assert
.assertEquals("", latitude1
, point1
.getLatitude());
88 Assert
.assertEquals("", errorRadius
, point1
.getErrorRadius());
89 Assert
.assertEquals("", referenceSystem
, point1
.getReferenceSystem());
91 Assert
.assertNull("LongitudeSexagesimal should be null", point2
.getLongitudeSexagesimal());
92 Assert
.assertNull("LatitudeSexagesimal should be null", point2
.getLatitudeSexagesimal());
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());
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());
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());
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());
129 public void testGetLongitudeSexagesimal(){
130 Assert
.assertEquals("23\u00B07'24.801\"E", point1
.getLongitudeSexagesimal().toString(true, false));
133 point2
.setLongitudeSexagesimal(Sexagesimal
.NewInstance(5, 22, null, Direction
.WEST
));
134 Assert
.assertEquals((Integer
)22, point2
.getLongitudeSexagesimal().minutes
);
135 Assert
.assertEquals((Integer
)0, point2
.getLongitudeSexagesimal().seconds
);
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());
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
);
150 public void testParsing(){
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");
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");
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);
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);
179 //#2962 (rounding of tertiers)
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());
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());
187 } catch (ParseException e
) {
188 Assert
.fail("No parsing error should occur");
198 // Assert.assertTrue("Southern must be negative", conversionResults.convertedCoord < 0);
199 // Assert.assertFalse("Southern must be latitude", conversionResults.isLongitude);
201 // conversionResults = coordinateConverter.tryConvert("35\u00B034.744");
202 // Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
203 // Assert.assertNull("Longitude must be undefined", conversionResults.isLongitude);
205 // conversionResults = coordinateConverter.tryConvert("95\u00B034.744");
206 // Assert.assertTrue("Longitude must be defined", conversionResults.isLongitude);
209 // conversionResults = coordinateConverter.tryConvert("-35\u00B034'55.67S");
210 // Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
212 // conversionResults = coordinateConverter.tryConvert("35\u00B011'34.744SN");
213 // Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
215 // conversionResults = coordinateConverter.tryConvert("35\u00B011'34.744SW");
216 // Assert.assertTrue("Western must be longitude", conversionResults.isLongitude);
218 // conversionResults = coordinateConverter.tryConvert("35D11M34.744S");
219 // Assert.assertNull("isLongitude must be undefined. S stands for second.", conversionResults.isLongitude);
225 public void testDoubleParsing(){
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");
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");
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);
255 point1
.setLongitudeByParsing("191");
256 Assert
.fail("Longitude can be > 180°");
257 } catch (ParseException e
) {
258 Assert
.assertTrue("Longitude can not > 180°", true);
261 point1
.setLatitudeByParsing("2\u00B039'38,5956\"S");
262 } catch (ParseException e
) {
263 Assert
.fail("String '2°39'38,5956\"S'should be parsable");
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
271 * @throws ParseException
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());
281 public void testStaticParsing(){
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");