1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.domain.geoloc.entity;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Random;
28  
29  import junit.framework.Assert;
30  
31  import org.junit.Test;
32  import org.springframework.beans.factory.annotation.Required;
33  
34  import com.gisgraphy.domain.geoloc.service.fulltextsearch.AbstractIntegrationHttpSolrTestCase;
35  import com.gisgraphy.domain.repository.ICityDao;
36  import com.gisgraphy.domain.repository.ICountryDao;
37  import com.gisgraphy.test.GeolocTestHelper;
38  
39  public class GisFeatureTest extends AbstractIntegrationHttpSolrTestCase {
40  
41      private ICountryDao countryDao;
42  
43      private ICityDao cityDao;
44  
45      @Test
46      public void testAddAlternateNamesShouldAddChildrenAndNotReplace() {
47  	GisFeature gisFeature = GeolocTestHelper
48  		.createGisFeatureWithAlternateNames("toto", 3);
49  	List<AlternateName> alternateNames = new ArrayList<AlternateName>();
50  	AlternateName a1 = new AlternateName();
51  	AlternateName a2 = new AlternateName();
52  	alternateNames.add(a1);
53  	alternateNames.add(a2);
54  	gisFeature.addAlternateNames(alternateNames);
55  	assertEquals(5, gisFeature.getAlternateNames().size());
56      }
57  
58      @Test
59      public void testAddAlternateNamesShouldDoADoubleSet() {
60  	GisFeature gisFeature = GeolocTestHelper
61  		.createGisFeatureWithAlternateNames("toto", 3);
62  	List<AlternateName> alternateNames = new ArrayList<AlternateName>();
63  	AlternateName a1 = new AlternateName();
64  	AlternateName a2 = new AlternateName();
65  	alternateNames.add(a1);
66  	alternateNames.add(a2);
67  	gisFeature.addAlternateNames(alternateNames);
68  	assertEquals(5, gisFeature.getAlternateNames().size());
69  	for (AlternateName alternateName : gisFeature.getAlternateNames()) {
70  	    assertEquals(gisFeature, alternateName.getGisFeature());
71  	}
72      }
73  
74      @Test
75      public void testSetFeatureClassShouldAlwaysSetInUpperCase() {
76  	GisFeature gisFeature = new GisFeature();
77  	gisFeature.setFeatureClass("a");
78  	assertEquals("A", gisFeature.getFeatureClass());
79      }
80  
81      @Test
82      public void testSetFeatureCodeShouldAlwaysSetInUpperCase() {
83  	GisFeature gisFeature = new GisFeature();
84  	gisFeature.setFeatureCode("a");
85  	assertEquals("A", gisFeature.getFeatureCode());
86      }
87  
88      @Test
89      public void testSetFeatureClassWithNullValueShouldNotThrow() {
90  	GisFeature gisFeature = new GisFeature();
91  	try {
92  	    gisFeature.setFeatureClass(null);
93  	} catch (RuntimeException e) {
94  	    fail("setting a null feture class should not throw");
95  	}
96      }
97  
98      @Test
99      public void testSetFeatureCodeWithNullShouldnotThrow() {
100 	GisFeature gisFeature = new GisFeature();
101 	try {
102 	    gisFeature.setFeatureCode(null);
103 	} catch (RuntimeException e) {
104 	    fail("setting a null feature code should not throw");
105 	}
106     }
107 
108     @Test
109     public void testGetCountryShouldReturnTheCountryObject() {
110 	Country country = GeolocTestHelper.createCountryForFrance();
111 	Country savedCountry = this.countryDao.save(country);
112 	assertNotNull(savedCountry.getId());
113 
114 	GisFeature gisFeature = GeolocTestHelper.createCity("cityGisFeature",
115 		null, null, new Random().nextLong());
116 	City paris = new City(gisFeature);
117 	paris.setCountryCode("FR");
118 
119 	// save city
120 	City savedParis = this.cityDao.save(paris);
121 
122 	// chek city is well saved
123 	City retrievedParis = this.cityDao.get(savedParis.getId());
124 	assertNotNull(retrievedParis);
125 	assertEquals(paris.getId(), retrievedParis.getId());
126 
127 	Country retrievedCountry = savedParis.getCountry();
128 	assertNotNull(retrievedCountry);
129 	assertEquals(savedCountry, retrievedCountry);
130 
131     }
132 
133     @Test
134     public void testGetCountryShouldReturnNullIfNoCountryCodeisSpecified() {
135 	Country country = GeolocTestHelper.createCountryForFrance();
136 	Country savedCountry = this.countryDao.save(country);
137 	assertNotNull(savedCountry.getId());
138 
139 	GisFeature gisFeature = GeolocTestHelper.createCity("cityGisFeature",
140 		null, null, new Random().nextLong());
141 	City paris = new City(gisFeature);
142 	paris.setCountryCode(null);
143 
144 	// save city
145 	City savedParis = this.cityDao.save(paris);
146 
147 	// chek city is well saved
148 	City retrievedParis = this.cityDao.get(savedParis.getId());
149 	assertNotNull(retrievedParis);
150 	assertEquals(paris.getId(), retrievedParis.getId());
151 
152 	Country retrievedCountry = savedParis.getCountry();
153 	assertNull(retrievedCountry);
154 
155     }
156 
157     @Test
158     public void testGetCountryShouldReturnNullIfUnknowCountryCode() {
159 	Country country = GeolocTestHelper.createCountryForFrance();
160 	Country savedCountry = this.countryDao.save(country);
161 	assertNotNull(savedCountry.getId());
162 
163 	GisFeature gisFeature = GeolocTestHelper.createCity("cityGisFeature",
164 		null, null, new Random().nextLong());
165 	City paris = new City(gisFeature);
166 	paris.setCountryCode("ER");
167 
168 	// save city
169 	City savedParis = this.cityDao.save(paris);
170 
171 	// chek city is well saved
172 	City retrievedParis = this.cityDao.get(savedParis.getId());
173 	assertNotNull(retrievedParis);
174 	assertEquals(paris.getId(), retrievedParis.getId());
175 
176 	Country retrievedCountry = savedParis.getCountry();
177 	assertNull(retrievedCountry);
178 
179     }
180 
181     @Test
182     public void testDistanceShouldRetrunCorrectDistance() {
183 	GisFeature point1 = new GisFeature();
184 	point1.setLocation(GeolocTestHelper.createPoint(48.867F, 2.333F));
185 
186 	GisFeature point2 = new GisFeature();
187 	point2.setLocation(GeolocTestHelper.createPoint(49.017F, 2.467F));
188 
189 	assertEquals(Math.round(point1.distanceTo(point2.getLocation())), Math
190 		.round(point2.distanceTo(point1.getLocation())));
191 	assertEquals(22, Math
192 		.round(point2.distanceTo(point1.getLocation()) / 1000));
193     }
194 
195     public void testDistanceShouldHaveCorrectParameters() {
196 	GisFeature point1 = new GisFeature();
197 	point1.setLocation(GeolocTestHelper.createPoint(0F, 0F));
198 
199 	try {
200 	    point1.distanceTo(null);
201 	    fail("Distance for a null feature must throws");
202 	} catch (RuntimeException e) {
203 	}
204 
205 	GisFeature point2 = new GisFeature();
206 	// point2.setLocation(this.geolocTestHelper.createPoint(1F, 1F));
207 
208 	try {
209 	    point1.distanceTo(point2.getLocation());
210 	    fail("Distance with a null location must throws");
211 	} catch (RuntimeException e) {
212 	}
213 
214 	point1.setLocation(null);
215 	try {
216 	    point1.distanceTo(point2.getLocation());
217 	    fail("Distance with a null location must throws");
218 	} catch (RuntimeException e) {
219 	}
220 
221     }
222 
223     @Test
224     public void testPopulateAcityShouldsetZipCode() {
225 	City city1 = GeolocTestHelper.createCity("name", 1.5F, 1.6F, 2L);
226 	city1.addZipCode(new ZipCode("10000"));
227 	City city2 = new City();
228 	city2.populate(city1);
229 	assertEquals("Populate a city with a city should set the zipcode",
230 		city1.getZipCodes().get(0), city2.getZipCodes().get(0));
231 
232     }
233 
234     @Test
235     public void testToStringShouldContainsTheClassName() {
236 	City city1 = GeolocTestHelper.createCity("name", 1.5F, 1.6F, 2L);
237 	city1.addZipCode(new ZipCode("10000"));
238 	assertTrue(city1.toString().startsWith(City.class.getSimpleName()));
239     }
240     
241     @Test
242     public void testAddZipCodesShouldDoADoubleSet(){
243 	GisFeature gisFeature = new GisFeature();
244 	gisFeature.setFeatureId(3L);
245 	ZipCode zipCode1 = new ZipCode("zip1");
246 	ZipCode zipCode2 = new ZipCode("zip2");
247 	List<ZipCode> zipCodes = new ArrayList<ZipCode>();
248 	zipCodes.add(zipCode1);
249 	zipCodes.add(zipCode2);
250 	gisFeature.addZipCodes(zipCodes);
251 	Assert.assertEquals("all the zipcodes of the list should be added",zipCodes.size(), gisFeature.getZipCodes().size());
252 	Assert.assertTrue("zipCode1 is missing", gisFeature.getZipCodes().contains(zipCode1));
253 	Assert.assertTrue("zipCode2 is missing", gisFeature.getZipCodes().contains(zipCode2));
254 	Assert.assertEquals("A double set should be done, gisfeature should be set in the the zipCode entity",
255 		gisFeature.getFeatureId(), gisFeature.getZipCodes().get(0).getGisFeature().getFeatureId() );
256     }
257     
258     @Test
259     public void testAddZipCodeShouldDoADoubleSet(){
260 	GisFeature gisFeature = new GisFeature();
261 	gisFeature.setFeatureId(3L);
262 	ZipCode zipCode1 = new ZipCode("zip2");
263 	gisFeature.addZipCode(zipCode1);
264 	Assert.assertEquals("all the zipcodes of the list should be added",1, gisFeature.getZipCodes().size());
265 	Assert.assertTrue("zipCode1 is missing", gisFeature.getZipCodes().contains(zipCode1));
266 	Assert.assertEquals("A double set should be done, gisfeature should be set in the the zipCode entity",
267 		gisFeature.getFeatureId(), gisFeature.getZipCodes().get(0).getGisFeature().getFeatureId() );
268     }
269 
270     @Required
271     public void setCityDao(ICityDao cityDao) {
272 	this.cityDao = cityDao;
273     }
274 
275     @Required
276     public void setCountryDao(ICountryDao countryDao) {
277 	this.countryDao = countryDao;
278     }
279 
280 }