Project

General

Profile

Download (9.2 KB) Statistics
| Branch: | Tag: | Revision:
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
import java.text.ParseException;
12

    
13
import org.apache.log4j.Logger;
14
import org.junit.Assert;
15
import org.junit.Before;
16
import org.junit.Test;
17

    
18
import eu.etaxonomy.cdm.model.location.Point.Direction;
19
import eu.etaxonomy.cdm.model.location.Point.Sexagesimal;
20
import eu.etaxonomy.cdm.test.unit.EntityTestBase;
21

    
22
/**
23
 * @author a.mueller
24
 * @since 04.06.2010
25
 */
26
public class PointTest extends EntityTestBase {
27

    
28
	@SuppressWarnings("unused")
29
	private static final Logger logger = Logger.getLogger(PointTest.class);
30

    
31
	private Point point1;
32
	private Point point2;
33

    
34
	private Integer errorRadius;
35
	private Double longitude1;
36
	private Double latitude1;
37
	private Double longitude2;
38
	private Double latitude2;
39

    
40
	private ReferenceSystem referenceSystem;
41

    
42
	@Before
43
	public void setUp() throws Exception {
44
		longitude1 = 23.123556;
45
		latitude1 = -13.975556;
46

    
47
		longitude2 = 28.48556;
48
		latitude2 = 12.656;
49

    
50
		errorRadius = 5;
51
		referenceSystem = ReferenceSystem.GOOGLE_EARTH();
52

    
53
		point1 = Point.NewInstance(longitude1, latitude1, referenceSystem, errorRadius);
54
		point2 = Point.NewInstance();
55
	}
56

    
57
//********************** TESTS *****************************
58

    
59
	@Test
60
	public void testNewInstance(){
61
		Assert.assertNotNull("ReferenceSystem must not be null", referenceSystem);
62
		Assert.assertNotNull("Point1 must not be null", point1);
63
		Assert.assertNotNull("Point2 must not be null", point2);
64
		Assert.assertEquals("", longitude1, point1.getLongitude());
65

    
66
		Assert.assertEquals("", latitude1, point1.getLatitude());
67
		Assert.assertEquals("", errorRadius, point1.getErrorRadius());
68
		Assert.assertEquals("", referenceSystem, point1.getReferenceSystem());
69

    
70
		Assert.assertNull("LongitudeSexagesimal should be null", point2.getLongitudeSexagesimal());
71
		Assert.assertNull("LatitudeSexagesimal should be null", point2.getLatitudeSexagesimal());
72
	}
73

    
74
	@Test
75
	public void testGetSetLongitude(){
76
		point2.setLongitude(5.888);
77
		Assert.assertEquals(Double.valueOf(5.888), point2.getLongitude());
78
		point2.setLongitude(null);
79
		Assert.assertEquals(null, point2.getLongitude());
80
	}
81

    
82
	@Test
83
	public void testGetSetLatitude(){
84
		point2.setLatitude(-34.987);
85
		Assert.assertEquals(Double.valueOf(-34.987), point2.getLatitude());
86
		point2.setLatitude(null);
87
		Assert.assertEquals(null, point2.getLatitude());
88
	}
89

    
90
	@Test
91
	public void testGetSetErrorRadius(){
92
		point2.setErrorRadius(7);
93
		Assert.assertEquals(Integer.valueOf(7), point2.getErrorRadius());
94
		point2.setErrorRadius(null);
95
		Assert.assertEquals(null, point2.getErrorRadius());
96
	}
97

    
98
	@Test
99
	public void testGetSetReferenceSystem(){
100
		ReferenceSystem newRefSystem = ReferenceSystem.NewInstance();
101
		point2.setReferenceSystem(newRefSystem);
102
		Assert.assertEquals(newRefSystem, point2.getReferenceSystem());
103
		point2.setReferenceSystem(null);
104
		Assert.assertEquals(null, point2.getReferenceSystem());
105
	}
106

    
107
	@Test
108
	public void testGetLongitudeSexagesimal(){
109
		Assert.assertEquals("23\u00B07'24.801\"E", point1.getLongitudeSexagesimal().toString(true, false));
110

    
111

    
112
		point2.setLongitudeSexagesimal(Sexagesimal.NewInstance(5, 22, null, Direction.WEST));
113
		Assert.assertEquals((Integer)22, point2.getLongitudeSexagesimal().minutes);
114
		Assert.assertEquals((Integer)0, point2.getLongitudeSexagesimal().seconds);
115

    
116
		Double latitudeDouble = -45.57389326;
117
		point1.setLatitudeSexagesimal(Sexagesimal.valueOf(latitudeDouble, true));
118
		//Not true because of rounding errors
119
//		Assert.assertEquals("latitudeDouble must be equal", latitudeDouble, point1.getLatitude());
120

    
121
		Sexagesimal sexagesimal1 = Sexagesimal.NewInstance(0, 0, 0, Direction.WEST);
122
		Sexagesimal sexagesimal2 = Sexagesimal.NewInstance(2, 2, 2, Direction.WEST);
123
		Assert.assertNotSame("", sexagesimal1, sexagesimal2);
124
	}
125

    
126
	@Test
127
	public void testParsing(){
128
		try {
129
			Assert.assertEquals("", longitude1, point1.getLongitude());
130
			Assert.assertTrue("", latitude1.equals(point1.getLatitude()));
131
			point1.setLatitudeByParsing("35\u00B034'20\"S");
132
			Assert.assertEquals("", longitude1, point1.getLongitude());
133
			Assert.assertFalse("", latitude1.equals(point1.getLatitude()));
134
			Assert.assertEquals("", Double.valueOf("-35.57222222222222"), point1.getLatitude());
135
		} catch (ParseException e) {
136
			Assert.fail("No parsing error should occur");
137
		}
138
		try {
139
			point1.setLongitudeByParsing("112\u00B034.34'N");
140
			Assert.assertEquals("", "112.57233", point1.getLongitude().toString().substring(0,9));
141
		} catch (ParseException e) {
142
			Assert.fail("No parsing error should occur");
143
		}
144
		try {
145
			point1.setLatitudeByParsing("112\u00B034.34'S");
146
			Assert.fail("Latitude can not be > 90");
147
		} catch (ParseException e) {
148
			Assert.assertTrue("Latitude can not be > 90", true);
149
		}
150
		try {
151
			point1.setLongitudeByParsing("45\u00B034.34'S");
152
			Assert.fail("Longitude can not be S");
153
		} catch (ParseException e) {
154
			Assert.assertTrue("Longitude can not be S", true);
155
		}
156
		//#2962 (rounding of tertiers)
157
		try {
158
			point1.setLatitudeByParsing("37\u00B07'44\"N");
159
			Assert.assertEquals("Result should be 37\u00B07'44\"N not 37\u00B07'44.999\"N", "37\u00B07'44\"N", point1.getLatitudeSexagesimal().toString());
160

    
161
			point1.setLatitudeByParsing("37\u00B07'45\"N");
162
			Assert.assertEquals("Result should be 37\u00B07'45\"N not 37\u00B07'45.\"N", "37\u00B07'45\"N", point1.getLatitudeSexagesimal().toString());
163

    
164
		} catch (ParseException e) {
165
			Assert.fail("No parsing error should occur");
166
		}
167

    
168

    
169
//		Assert.assertTrue("Southern must be negative", conversionResults.convertedCoord < 0);
170
//		Assert.assertFalse("Southern must be latitude", conversionResults.isLongitude);
171
//
172
//		conversionResults = coordinateConverter.tryConvert("35\u00B034.744");
173
//		Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
174
//		Assert.assertNull("Longitude must be undefined", conversionResults.isLongitude);
175
//
176
//		conversionResults = coordinateConverter.tryConvert("95\u00B034.744");
177
//		Assert.assertTrue("Longitude must be defined", conversionResults.isLongitude);
178
//
179
//
180
//		conversionResults = coordinateConverter.tryConvert("-35\u00B034'55.67S");
181
//		Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
182
//
183
//		conversionResults = coordinateConverter.tryConvert("35\u00B011'34.744SN");
184
//		Assert.assertTrue(conversionResults.conversionComments, conversionResults.patternRecognised);
185
//
186
//		conversionResults = coordinateConverter.tryConvert("35\u00B011'34.744SW");
187
//		Assert.assertTrue("Western must be longitude", conversionResults.isLongitude);
188
//
189
//		conversionResults = coordinateConverter.tryConvert("35D11M34.744S");
190
//		Assert.assertNull("isLongitude must be undefined. S stands for second.", conversionResults.isLongitude);
191

    
192
	}
193

    
194

    
195
	@Test
196
	public void testDoubleParsing(){
197
		try {
198
			Assert.assertEquals("", longitude1, point1.getLongitude());
199
			Assert.assertTrue("", latitude1.equals(point1.getLatitude()));
200
			point1.setLatitudeByParsing("33.474");
201
			Assert.assertEquals("", longitude1, point1.getLongitude());
202
			Assert.assertFalse("", latitude1.equals(point1.getLatitude()));
203
			Assert.assertEquals("", Double.valueOf("33.474"), point1.getLatitude());
204
			point1.setLatitudeByParsing("-39,474");
205
			Assert.assertEquals("", Double.valueOf("-39.474"), point1.getLatitude());
206
		} catch (ParseException e) {
207
			Assert.fail("No parsing error should occur");
208
		}
209

    
210
		try {
211
			point1.setLongitudeByParsing("-120.4");
212
			Assert.assertEquals("", "-120.4", point1.getLongitude().toString());
213
			point1.setLongitudeByParsing("53,4");
214
			Assert.assertEquals("", "53.4", point1.getLongitude().toString());
215
		} catch (ParseException e) {
216
			Assert.fail("No parsing error should occur");
217
		}
218
		try {
219
			point1.setLatitudeByParsing("112.456");
220
			Assert.fail("Latitude can not be > 90");
221
		} catch (ParseException e) {
222
			Assert.assertTrue("Latitude can not be > 90", true);
223
		}
224

    
225
		try {
226
			point1.setLongitudeByParsing("191");
227
			Assert.fail("Longitude can be > 180°");
228
		} catch (ParseException e) {
229
			Assert.assertTrue("Longitude can not > 180°", true);
230
		}
231
		try {
232
			point1.setLatitudeByParsing("2\u00B039'38,5956\"S");
233
		} catch (ParseException e) {
234
			Assert.fail("String '2°39'38,5956\"S'should be parsable");
235
		}
236
}
237

    
238
	/**
239
	 * I don't exactly know what should happen here.
240
	 * Please see https://dev.e-taxonomy.eu/redmine/issues/2267#comment:3 on why this test was created
241
	 *
242
	 * @throws ParseException
243
	 */
244
	@Test
245
	public void testParsingHexagesimalAndDecimalMixed() throws ParseException{
246
		String example = "35\u00B034'55.67\"S";
247
		point1.setLatitudeByParsing(example);
248
		Assert.assertEquals(example, point1.getLatitudeSexagesimal().toString());
249
	}
250

    
251
	@Test
252
	public void testStaticParsing(){
253
		try{
254
			Point.parseLatitude("1");
255
		}catch (NullPointerException e){
256
			Assert.fail("No NullPointerException should occur");
257
		} catch (ParseException e) {
258
			Assert.fail("No parsing error should occur");
259
		}
260
	}
261
}
(2-2/3)