PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/geotools-9.2/modules/extension/validation/src/test/java/org/geotools/validation/attributes/GazetteerNameValidationOnlineTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 383 lines | 252 code | 61 blank | 70 comment | 14 complexity | 17effe2c73644956b13739a89022c8f6 MD5 | raw file
  1. /*
  2. * GeoTools - The Open Source Java GIS Toolkit
  3. * http://geotools.org
  4. *
  5. * (C) 2004-2008, Open Source Geospatial Foundation (OSGeo)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation;
  10. * version 2.1 of the License.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. package org.geotools.validation.attributes;
  18. import java.io.BufferedReader;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.net.ConnectException;
  22. import java.net.HttpURLConnection;
  23. import java.net.URL;
  24. import java.util.Collection;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import javax.xml.parsers.DocumentBuilderFactory;
  29. import junit.framework.TestCase;
  30. import org.geotools.data.simple.SimpleFeatureCollection;
  31. import org.geotools.feature.IllegalAttributeException;
  32. import org.geotools.geometry.jts.ReferencedEnvelope;
  33. import org.opengis.feature.GeometryAttribute;
  34. import org.opengis.feature.Property;
  35. import org.opengis.feature.simple.SimpleFeature;
  36. import org.opengis.feature.simple.SimpleFeatureType;
  37. import org.opengis.feature.type.AttributeDescriptor;
  38. import org.opengis.feature.type.Name;
  39. import org.opengis.feature.type.PropertyDescriptor;
  40. import org.opengis.filter.identity.FeatureId;
  41. import org.opengis.referencing.crs.CoordinateReferenceSystem;
  42. import org.w3c.dom.Document;
  43. import org.w3c.dom.Element;
  44. import org.w3c.dom.Node;
  45. import org.w3c.dom.NodeList;
  46. import org.xml.sax.InputSource;
  47. import com.vividsolutions.jts.geom.Geometry;
  48. /**
  49. * GazetteerNameValidationTest purpose.
  50. * <p>
  51. * Description of GazetteerNameValidationTest ...
  52. * </p>
  53. *
  54. * @author dzwiers, Refractions Research, Inc.
  55. * @author $Author: sploreg $ (last modification)
  56. *
  57. *
  58. * @source $URL$
  59. * @version $Id$
  60. */
  61. public class GazetteerNameValidationOnlineTest extends TestCase {
  62. public GazetteerNameValidationOnlineTest(){super("");}
  63. public GazetteerNameValidationOnlineTest(String s){super(s);}
  64. public void XtestValidate() {
  65. class TestFeature implements SimpleFeature {
  66. Map attrs = new HashMap();
  67. public SimpleFeatureCollection getParent(){return null;}
  68. public void setParent(SimpleFeatureCollection collection){}
  69. public SimpleFeatureType getFeatureType(){return null;}
  70. public String getID(){return "";}
  71. public FeatureId getIdentifier(){return null;}
  72. public Object[] getAttributes(Object[] attributes){return attrs.entrySet().toArray();}
  73. // used
  74. public Object getAttribute(String xPath){return attrs.get(xPath);}
  75. public Object getAttribute(int index){return attrs.entrySet().toArray()[index];}
  76. public void setAttribute(int position, Object val) throws IllegalAttributeException, ArrayIndexOutOfBoundsException{}
  77. public int getNumberOfAttributes(){return attrs.size();}
  78. public void setAttributes(Object[] attributes) throws IllegalAttributeException{}
  79. // used
  80. public void setAttribute(String xPath, Object attribute)throws IllegalAttributeException{attrs.put(xPath,attribute);}
  81. public void setDefaultGeometry(Geometry geometry) throws IllegalAttributeException{}
  82. public ReferencedEnvelope getBounds(){return null;}
  83. public Object getDefaultGeometryValue() {
  84. return null;
  85. }
  86. public SimpleFeatureType getType() {
  87. return null;
  88. }
  89. public List getTypes() {
  90. return null;
  91. }
  92. public Object getValue(String name) {
  93. return null;
  94. }
  95. public Object getValue(int index) {
  96. return null;
  97. }
  98. public List getValues() {
  99. return null;
  100. }
  101. public Object operation(String name, Object parameters) {
  102. return null;
  103. }
  104. public void setDefaultGeometryValue(Object geometry) {
  105. }
  106. public void setValue(String name, Object value) {
  107. }
  108. public void setValue(int index, Object value) {
  109. }
  110. public void setValues(List values) {
  111. }
  112. public void setValues(Object[] values) {
  113. }
  114. public CoordinateReferenceSystem getCRS() {
  115. return null;
  116. }
  117. public Geometry getDefaultGeometry() {
  118. return null;
  119. }
  120. public Object getUserData(Object key) {
  121. return null;
  122. }
  123. public void putUserData(Object key, Object value) {
  124. }
  125. public void setCRS(CoordinateReferenceSystem crs) {
  126. }
  127. public void setDefaultGeometry(GeometryAttribute geometryAttribute) {
  128. }
  129. public Collection associations() {
  130. return null;
  131. }
  132. public Collection attributes() {
  133. return null;
  134. }
  135. public Object get() {
  136. return null;
  137. }
  138. public List get(Name name) {
  139. return null;
  140. }
  141. public AttributeDescriptor getDescriptor() {
  142. return null;
  143. }
  144. public void set(Object newValue) throws IllegalArgumentException {
  145. }
  146. public boolean nillable() {
  147. return false;
  148. }
  149. public Object operation(Name name, List parameters) {
  150. return null;
  151. }
  152. public PropertyDescriptor descriptor() {
  153. return null;
  154. }
  155. public Name name() {
  156. return null;
  157. }
  158. public List getAttributes() {
  159. return null;
  160. }
  161. public Collection<? extends Property> getValue() {
  162. return null;
  163. }
  164. public void setValue( List values ) {
  165. }
  166. public void setValue( Object values ) {
  167. }
  168. public Object getAttribute(Name name) {
  169. return null;
  170. }
  171. public int getAttributeCount() {
  172. return 0;
  173. }
  174. public void setAttribute(Name name, Object value) {
  175. }
  176. public void setAttributes(List<Object> values) {
  177. }
  178. public void setDefaultGeometry(Object geometry) {
  179. }
  180. public GeometryAttribute getDefaultGeometryProperty() {
  181. return null;
  182. }
  183. public void setDefaultGeometryProperty(
  184. GeometryAttribute geometryAttribute) {
  185. }
  186. public Collection<Property> getProperties(Name name) {
  187. return null;
  188. }
  189. public Collection<Property> getProperties(String name) {
  190. return null;
  191. }
  192. public Property getProperty(Name name) {
  193. return null;
  194. }
  195. public Property getProperty(String name) {
  196. return null;
  197. }
  198. public void setValue(Collection<Property> values) {
  199. }
  200. public Name getName() {
  201. return null;
  202. }
  203. public Map<Object, Object> getUserData() {
  204. return null;
  205. }
  206. public boolean isNillable() {
  207. return false;
  208. }
  209. public Collection<Property> getProperties() {
  210. return null;
  211. }
  212. public void validate() {
  213. }
  214. }
  215. SimpleFeature f = new TestFeature();
  216. try{f.setAttribute("CityName","Vancouver");}catch(Exception e){}
  217. GazetteerNameValidation gnv = new GazetteerNameValidation();
  218. gnv.setAttributeName("CityName");
  219. try{gnv.setGazetteer(new URL("http://cgdi-dev.geoconnections.org/cgi-bin/prototypes/cgdigaz/cgdigaz.cgi?version=1.0&request=GetPlacenameGeometry&wildcards=false&geomtype=bbox"));}catch(Exception e){}
  220. // ValidationResults results = new RoadValidationResults();
  221. // if(!gnv.validate(f,null,results)){
  222. // fail("Did not validate.");
  223. // }
  224. }
  225. public void testURLConnection(){
  226. String place = "Vancouver";
  227. try{
  228. URL gazetteerURL = new URL("http://cgdi-dev.geoconnections.org/cgi-bin/prototypes/cgdigaz/cgdigaz.cgi?version=1.0&request=GetPlacenameGeometry&wildcards=false&geomtype=bbox&placename="+place);
  229. HttpURLConnection gazetteerConnection = (HttpURLConnection)gazetteerURL.openConnection();
  230. // gazetteerConnection.setConnectTimeout(100);
  231. if(!("OK".equals(gazetteerConnection.getResponseMessage())))
  232. throw new Exception("An error occured creating the connection to the Gazetteer.");
  233. InputStream gazetteerInputStream = gazetteerConnection.getInputStream();
  234. InputStreamReader gazetteerInputStreamReader = new InputStreamReader(gazetteerInputStream);
  235. BufferedReader gazetteerBufferedReader = new BufferedReader(gazetteerInputStreamReader);
  236. InputSource gazetteerInputSource = new InputSource(gazetteerBufferedReader);
  237. DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
  238. dfactory.setNamespaceAware(true);
  239. // TODO turn on validation
  240. dfactory.setValidating(false);
  241. dfactory.setIgnoringComments(true);
  242. dfactory.setCoalescing(true);
  243. dfactory.setIgnoringElementContentWhitespace(true);
  244. Document serviceDoc = dfactory.newDocumentBuilder().parse(gazetteerInputSource);
  245. Element elem = serviceDoc.getDocumentElement();
  246. // elem == SimpleFeatureCollection at this point
  247. elem = getChildElement(elem,"queryInfo");
  248. if(elem==null)
  249. throw new NullPointerException("Invalid DOM tree returned by gazetteer.");
  250. // this number is the number of instances found.
  251. int number = Integer.parseInt(getChildText(elem,"numberOfResults"));
  252. if(number>0){
  253. // found vancouver!
  254. }
  255. else {
  256. // did not find vancouver
  257. // (but out plugin worked so we still pass the test
  258. }
  259. }
  260. catch ( ConnectException timedOut ){
  261. return; // ignore server must be down
  262. } catch(Exception e){
  263. e.printStackTrace();
  264. fail(e.toString());
  265. }
  266. }
  267. /**
  268. * getChildElement purpose.
  269. *
  270. * <p>
  271. * Used to help with XML manipulations. Returns the first child element of
  272. * the specified name.
  273. * </p>
  274. *
  275. * @param root The root element to look for children in.
  276. * @param name The name of the child element to look for.
  277. *
  278. * @return The child element found, null if not found.
  279. *
  280. * @see getChildElement(Element,String,boolean)
  281. */
  282. public static Element getChildElement(Element root, String name) {
  283. Node child = root.getFirstChild();
  284. while (child != null) {
  285. if (child.getNodeType() == Node.ELEMENT_NODE) {
  286. if (name.equals(child.getNodeName())) {
  287. return (Element) child;
  288. }
  289. }
  290. child = child.getNextSibling();
  291. }
  292. return null;
  293. }
  294. /**
  295. * getChildText purpose.
  296. *
  297. * <p>
  298. * Used to help with XML manipulations. Returns the first child text value
  299. * of the specified element name.
  300. * </p>
  301. *
  302. * @param root The root element to look for children in.
  303. * @param childName The name of the attribute to look for.
  304. *
  305. * @return The value if the child was found, the null otherwise.
  306. */
  307. public static String getChildText(Element root, String childName) {
  308. Element elem = getChildElement(root, childName);
  309. if (elem != null) {
  310. Node child;
  311. NodeList childs = elem.getChildNodes();
  312. int nChilds = childs.getLength();
  313. for (int i = 0; i < nChilds; i++) {
  314. child = childs.item(i);
  315. if (child.getNodeType() == Node.TEXT_NODE) {
  316. return child.getNodeValue();
  317. }
  318. }
  319. }
  320. return null;
  321. }
  322. }