| /trunk/test-suite/src/test/java/org/hibernatespatial/testsuite/TestStoreRetrieve.java |
|---|
| New file |
| 0,0 → 1,167 |
| /* |
| * $Id: TestStoreRetrieve.java 242 2010-09-22 20:40:07Z maesenka $ |
| * |
| * This file is part of Hibernate Spatial, an extension to the |
| * hibernate ORM solution for geographic data. |
| * |
| * Copyright © 2007-2010 Geovise BVBA |
| * |
| * This library is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Lesser General Public |
| * License as published by the Free Software Foundation; either |
| * version 2.1 of the License, or (at your option) any later version. |
| * |
| * This library is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * Lesser General Public License for more details. |
| * |
| * You should have received a copy of the GNU Lesser General Public |
| * License along with this library; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| * |
| * For more information, visit: http://www.hibernatespatial.org/ |
| */ |
| package org.hibernatespatial.testsuite; |
| import com.vividsolutions.jts.geom.Geometry; |
| import com.vividsolutions.jts.io.ParseException; |
| import org.hibernate.Criteria; |
| import org.hibernate.Session; |
| import org.hibernate.Transaction; |
| import org.hibernatespatial.test.*; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| /** |
| * This testsuite-suite class verifies whether the <code>Geometry</code>s retrieved |
| * are equal to the <code>Geometry</code>s stored. |
| */ |
| public class TestStoreRetrieve extends SpatialFunctionalTestCase { |
| private static Logger LOGGER = LoggerFactory.getLogger(TestStoreRetrieve.class); |
| public TestStoreRetrieve(String string){ |
| super(string); |
| } |
| protected Logger getLogger(){ |
| return LOGGER; |
| } |
| public void test_store_retrieve() throws ParseException { |
| Map<Integer, GeomEntity> stored = new HashMap<Integer, GeomEntity>(); |
| storeTestObjects(stored); |
| retrieveAndCompare(stored); |
| } |
| public void test_store_retrieve_null_geometry() { |
| storeNullGeometry(); |
| retrieveNullGeometry(); |
| } |
| private void retrieveAndCompare(Map<Integer, GeomEntity> stored) { |
| int id = -1; |
| Transaction tx = null; |
| Session session = null; |
| try { |
| session = openSession(); |
| tx = session.beginTransaction(); |
| for (GeomEntity storedEntity : stored.values()) { |
| id = storedEntity.getId(); |
| GeomEntity retrievedEntity = (GeomEntity) session.get(GeomEntity.class, id); |
| Geometry retrievedGeometry = retrievedEntity.getGeom(); |
| Geometry storedGeometry = storedEntity.getGeom(); |
| String msg = createFailureMessage(storedEntity.getId(), storedGeometry, retrievedGeometry); |
| assertTrue(msg, geometryEquality.test(storedGeometry, retrievedGeometry)); |
| } |
| tx.commit(); |
| } catch (Exception e) { |
| if (tx != null) tx.rollback(); |
| throw new RuntimeException(String.format("Failure on case: %d", id), e); |
| } |
| finally { |
| if (session != null) session.close(); |
| } |
| } |
| private String createFailureMessage(int id, Geometry storedGeometry, Geometry retrievedGeometry) { |
| String expectedText = (storedGeometry != null ? storedGeometry.toText() : "NULL"); |
| String retrievedText = (retrievedGeometry != null ? retrievedGeometry.toText() : "NULL"); |
| return String.format("Equality testsuite-suite failed for %d.\nExpected: %s\nReceived:%s", id, expectedText, retrievedText); |
| } |
| private void storeTestObjects(Map<Integer, GeomEntity> stored) { |
| Session session = null; |
| Transaction tx = null; |
| int id = -1; |
| try { |
| session = openSession(); |
| // Every testsuite-suite instance is committed seperately |
| // to improve feedback in case of failure |
| for (TestDataElement element : testData) { |
| id = element.id; |
| tx = session.beginTransaction(); |
| GeomEntity entity = GeomEntity.createFrom(element); |
| stored.put(entity.getId(), entity); |
| session.save(entity); |
| tx.commit(); |
| } |
| } catch (Exception e) { |
| if (tx != null) tx.rollback(); |
| throw new RuntimeException("Failed storing testsuite-suite object with id:" + id, e); |
| } finally { |
| if (session != null) session.close(); |
| } |
| } |
| private void storeNullGeometry() { |
| GeomEntity entity = null; |
| Session session = null; |
| Transaction tx = null; |
| try { |
| session = openSession(); |
| tx = session.beginTransaction(); |
| entity = new GeomEntity(); |
| entity.setId(1); |
| entity.setType("NULL OBJECT"); |
| session.save(entity); |
| tx.commit(); |
| } catch (Exception e) { |
| if (tx != null) tx.rollback(); |
| throw new RuntimeException("Failed storing testsuite-suite object with id:" + entity.getId(), e); |
| } finally { |
| if (session != null) session.close(); |
| } |
| } |
| private void retrieveNullGeometry() { |
| Transaction tx = null; |
| Session session = null; |
| try { |
| session = openSession(); |
| tx = session.beginTransaction(); |
| Criteria criteria = session.createCriteria(GeomEntity.class); |
| List<GeomEntity> retrieved = criteria.list(); |
| assertEquals("Expected exactly one result", 1, retrieved.size()); |
| GeomEntity entity = retrieved.get(0); |
| assertNull(entity.getGeom()); |
| tx.commit(); |
| } catch(Exception e){ |
| if (tx != null) tx.rollback(); |
| throw new RuntimeException(e); |
| } finally { |
| if (session != null) session.close(); |
| } |
| } |
| } |
| /trunk/test-suite/src/test/java/org/hibernatespatial/testsuite/TestSpatialFunctions.java |
|---|
| New file |
| 0,0 → 1,297 |
| /* |
| * $Id: TestSpatialFunctions.java 193 2010-03-26 15:56:02Z maesenka $ |
| * |
| * This file is part of Hibernate Spatial, an extension to the |
| * hibernate ORM solution for geographic data. |
| * |
| * Copyright © 2007-2010 Geovise BVBA |
| * |
| * This library is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Lesser General Public |
| * License as published by the Free Software Foundation; either |
| * version 2.1 of the License, or (at your option) any later version. |
| * |
| * This library is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * Lesser General Public License for more details. |
| * |
| * You should have received a copy of the GNU Lesser General Public |
| * License along with this library; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| * |
| * For more information, visit: http://www.hibernatespatial.org/ |
| */ |
| package org.hibernatespatial.testsuite; |
| import com.vividsolutions.jts.geom.Geometry; |
| import org.hibernate.Query; |
| import org.hibernate.Session; |
| import org.hibernate.Transaction; |
| import org.hibernate.type.CustomType; |
| import org.hibernatespatial.GeometryUserType; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| import java.sql.SQLException; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| import static junit.framework.Assert.assertTrue; |
| import static org.junit.Assert.*; |
| /** |
| * @author Karel Maesen, Geovise BVBA |
| */ |
| public class TestSpatialFunctions extends SpatialFunctionalTestCase { |
| private static Logger LOGGER = LoggerFactory.getLogger(TestSpatialFunctions.class); |
| public TestSpatialFunctions(String string) { |
| super(string); |
| } |
| public void prepareTest(){ |
| super.prepareTest(); |
| try { |
| dataSourceUtils.insertTestData(testData); |
| } catch (SQLException e) { |
| throw new RuntimeException(e); |
| } |
| } |
| protected Logger getLogger(){ |
| return LOGGER; |
| } |
| public void test_dimension() throws SQLException { |
| Map<Integer, Integer> dbexpected = expectationsFactory.getDimension(); |
| String hql = "SELECT id, dimension(geom) FROM GeomEntity"; |
| retrieveHQLResultsAndCompare(dbexpected, hql); |
| } |
| public void test_astext() throws SQLException { |
| Map<Integer, String> dbexpected = expectationsFactory.getAsText(); |
| String hql = "SELECT id, astext(geom) from GeomEntity"; |
| retrieveHQLResultsAndCompare(dbexpected, hql); |
| } |
| public void test_asbinary() throws SQLException { |
| Map<Integer, byte[]> dbexpected = expectationsFactory.getAsBinary(); |
| String hql = "SELECT id, asbinary(geom) from GeomEntity"; |
| retrieveHQLResultsAndCompare(dbexpected, hql); |
| } |
| public void test_geometrytype() throws SQLException { |
| Map<Integer, String> dbexpected = expectationsFactory.getGeometryType(); |
| String hql = "SELECT id, geometrytype(geom) from GeomEntity"; |
| retrieveHQLResultsAndCompare(dbexpected, hql); |
| } |
| public void test_srid() throws SQLException { |
| Map<Integer, Integer> dbexpected = expectationsFactory.getSrid(); |
| String hql = "SELECT id, srid(geom) from GeomEntity"; |
| retrieveHQLResultsAndCompare(dbexpected, hql); |
| } |
| public void test_issimple() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getIsSimple(); |
| String hql = "SELECT id, issimple(geom) from GeomEntity"; |
| retrieveHQLResultsAndCompare(dbexpected, hql); |
| } |
| public void test_isempty() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getIsEmpty(); |
| String hql = "SELECT id, isEmpty(geom) from GeomEntity"; |
| retrieveHQLResultsAndCompare(dbexpected, hql); |
| } |
| public void test_boundary() throws SQLException { |
| Map<Integer, Geometry> dbexpected = expectationsFactory.getBoundary(); |
| String hql = "SELECT id, boundary(geom) from GeomEntity"; |
| retrieveHQLResultsAndCompare(dbexpected, hql); |
| } |
| public void test_envelope() throws SQLException { |
| Map<Integer, Geometry> dbexpected = expectationsFactory.getEnvelope(); |
| String hql = "SELECT id, envelope(geom) from GeomEntity"; |
| retrieveHQLResultsAndCompare(dbexpected, hql); |
| } |
| public void test_within() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getWithin(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, within(geom, :filter) from GeomEntity where within(geom, :filter) = true and srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| private Map<String, Object> createQueryParams(String filterParamName, Object value) { |
| Map<String, Object> params = new HashMap<String, Object>(); |
| params.put(filterParamName, value); |
| return params; |
| } |
| public void test_equals() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getEquals(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, equals(geom, :filter) from GeomEntity where equals(geom, :filter) = true and srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_crosses() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getCrosses(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, crosses(geom, :filter) from GeomEntity where crosses(geom, :filter) = true and srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_contains() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getContains(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, contains(geom, :filter) from GeomEntity where contains(geom, :filter) = true and srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_disjoint() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getDisjoint(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, disjoint(geom, :filter) from GeomEntity where disjoint(geom, :filter) = true and srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_intersects() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getIntersects(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, intersects(geom, :filter) from GeomEntity where intersects(geom, :filter) = true and srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_overlaps() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getOverlaps(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, overlaps(geom, :filter) from GeomEntity where overlaps(geom, :filter) = true and srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_touches() throws SQLException { |
| String hql = "SELECT id, touches(geom, :filter) from GeomEntity where touches(geom, :filter) = true and srid(geom) = 4326"; |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getTouches(expectationsFactory.getTestPolygon()); |
| Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_relate() throws SQLException { |
| String matrix = "T*T***T**"; |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getRelate(expectationsFactory.getTestPolygon(), matrix); |
| String hql = "SELECT id, relate(geom, :filter, :matrix) from GeomEntity where relate(geom, :filter, :matrix) = true and srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon()); |
| params.put("matrix", matrix); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| matrix = "FF*FF****"; |
| dbexpected = expectationsFactory.getRelate(expectationsFactory.getTestPolygon(), matrix); |
| params.put("matrix", matrix); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_distance() throws SQLException { |
| Map<Integer, Double> dbexpected = expectationsFactory.getDistance(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, distance(geom, :filter) from GeomEntity where srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_buffer() throws SQLException { |
| Map<Integer, Geometry> dbexpected = expectationsFactory.getBuffer(Double.valueOf(1.0)); |
| String hql = "SELECT id, buffer(geom, :distance) from GeomEntity where srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("distance", Double.valueOf(1.0)); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_convexhull() throws SQLException { |
| Map<Integer, Geometry> dbexpected = expectationsFactory.getConvexHull(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, convexhull(geomunion(geom, :polygon)) from GeomEntity where srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("polygon", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_intersection() throws SQLException { |
| Map<Integer, Geometry> dbexpected = expectationsFactory.getIntersection(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, intersection(geom, :polygon) from GeomEntity where srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("polygon", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_difference() throws SQLException { |
| Map<Integer, Geometry> dbexpected = expectationsFactory.getDifference(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, difference(geom, :polygon) from GeomEntity where srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("polygon", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_symdifference() throws SQLException { |
| Map<Integer, Geometry> dbexpected = expectationsFactory.getSymDifference(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, symdifference(geom, :polygon) from GeomEntity where srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("polygon", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public void test_geomunion() throws SQLException { |
| Map<Integer, Geometry> dbexpected = expectationsFactory.getGeomUnion(expectationsFactory.getTestPolygon()); |
| String hql = "SELECT id, geomunion(geom, :polygon) from GeomEntity where srid(geom) = 4326"; |
| Map<String, Object> params = createQueryParams("polygon", expectationsFactory.getTestPolygon()); |
| retrieveHQLResultsAndCompare(dbexpected, hql, params); |
| } |
| public <T> void retrieveHQLResultsAndCompare(Map<Integer, T> dbexpected, String hql) { |
| retrieveHQLResultsAndCompare(dbexpected, hql, null); |
| } |
| protected <T> void retrieveHQLResultsAndCompare(Map<Integer, T> dbexpected, String hql, Map<String, Object> params) { |
| Map<Integer, T> hsreceived = new HashMap<Integer, T>(); |
| doInSession(hql, hsreceived, params); |
| compare(dbexpected, hsreceived); |
| } |
| private <T> void doInSession(String hql, Map<Integer, T> result, Map<String, Object> params) { |
| Session session = null; |
| Transaction tx = null; |
| try { |
| session = openSession(); |
| tx = session.beginTransaction(); |
| Query query = session.createQuery(hql); |
| setParameters(params, query); |
| addQueryResults(result, query); |
| } finally { |
| if (tx != null) tx.rollback(); |
| if (session != null) session.close(); |
| } |
| } |
| private void setParameters(Map<String, Object> params, Query query) { |
| if (params == null) return; |
| for (String param : params.keySet()) { |
| Object value = params.get(param); |
| if (value instanceof Geometry) { |
| query.setParameter(param, value, new CustomType(new GeometryUserType())); |
| } else { |
| query.setParameter(param, value); |
| } |
| } |
| } |
| } |
| /trunk/test-suite/src/test/java/org/hibernatespatial/testsuite/TestSpatialRestrictions.java |
|---|
| New file |
| 0,0 → 1,163 |
| /* |
| * $Id: TestSpatialRestrictions.java 242 2010-09-22 20:40:07Z maesenka $ |
| * |
| * This file is part of Hibernate Spatial, an extension to the |
| * hibernate ORM solution for geographic data. |
| * |
| * Copyright © 2007-2010 Geovise BVBA |
| * |
| * This library is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Lesser General Public |
| * License as published by the Free Software Foundation; either |
| * version 2.1 of the License, or (at your option) any later version. |
| * |
| * This library is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * Lesser General Public License for more details. |
| * |
| * You should have received a copy of the GNU Lesser General Public |
| * License along with this library; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| * |
| * For more information, visit: http://www.hibernatespatial.org/ |
| */ |
| package org.hibernatespatial.testsuite; |
| import org.hibernate.Criteria; |
| import org.hibernate.Session; |
| import org.hibernate.Transaction; |
| import org.hibernate.criterion.Criterion; |
| import org.hibernatespatial.criterion.SpatialRestrictions; |
| import org.hibernatespatial.test.GeomEntity; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| import java.sql.SQLException; |
| import java.util.List; |
| import java.util.Map; |
| import static junit.framework.Assert.assertEquals; |
| import static org.junit.Assert.fail; |
| /** |
| * Created by IntelliJ IDEA. |
| * User: maesenka |
| * Date: Mar 18, 2010 |
| * Time: 10:02:24 PM |
| * To change this template use File | Settings | File Templates. |
| */ |
| public class TestSpatialRestrictions extends SpatialFunctionalTestCase{ |
| private static Logger LOGGER = LoggerFactory.getLogger(TestSpatialRestrictions.class); |
| public TestSpatialRestrictions(String string) { |
| super(string); |
| } |
| public void prepareTest(){ |
| super.prepareTest(); |
| try { |
| dataSourceUtils.insertTestData(testData); |
| } catch (SQLException e) { |
| throw new RuntimeException(e); |
| } |
| } |
| protected Logger getLogger(){ |
| return LOGGER; |
| } |
| public void test_within() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getWithin(expectationsFactory.getTestPolygon()); |
| Criterion spatialCriterion = SpatialRestrictions.within("geom", expectationsFactory.getTestPolygon()); |
| retrieveAndCompare(dbexpected, spatialCriterion); |
| } |
| public void test_filter() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getFilter(expectationsFactory.getTestPolygon()); |
| Criterion spatialCriterion = SpatialRestrictions.filter("geom", expectationsFactory.getTestPolygon()); |
| retrieveAndCompare(dbexpected, spatialCriterion); |
| } |
| public void test_contains() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getContains(expectationsFactory.getTestPolygon()); |
| Criterion spatialCriterion = SpatialRestrictions.contains("geom", expectationsFactory.getTestPolygon()); |
| retrieveAndCompare(dbexpected, spatialCriterion); |
| } |
| public void test_crosses() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getCrosses(expectationsFactory.getTestPolygon()); |
| Criterion spatialCriterion = SpatialRestrictions.crosses("geom", expectationsFactory.getTestPolygon()); |
| retrieveAndCompare(dbexpected, spatialCriterion); |
| } |
| public void test_touches() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getTouches(expectationsFactory.getTestPolygon()); |
| Criterion spatialCriterion = SpatialRestrictions.touches("geom", expectationsFactory.getTestPolygon()); |
| retrieveAndCompare(dbexpected, spatialCriterion); |
| } |
| public void test_disjoint() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getDisjoint(expectationsFactory.getTestPolygon()); |
| Criterion spatialCriterion = SpatialRestrictions.disjoint("geom", expectationsFactory.getTestPolygon()); |
| retrieveAndCompare(dbexpected, spatialCriterion); |
| } |
| public void test_eq() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getEquals(expectationsFactory.getTestPolygon()); |
| Criterion spatialCriterion = SpatialRestrictions.eq("geom", expectationsFactory.getTestPolygon()); |
| retrieveAndCompare(dbexpected, spatialCriterion); |
| } |
| public void test_intersects() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getIntersects(expectationsFactory.getTestPolygon()); |
| Criterion spatialCriterion = SpatialRestrictions.intersects("geom", expectationsFactory.getTestPolygon()); |
| retrieveAndCompare(dbexpected, spatialCriterion); |
| } |
| public void test_overlaps() throws SQLException { |
| Map<Integer, Boolean> dbexpected = expectationsFactory.getOverlaps(expectationsFactory.getTestPolygon()); |
| Criterion spatialCriterion = SpatialRestrictions.overlaps("geom", expectationsFactory.getTestPolygon()); |
| retrieveAndCompare(dbexpected, spatialCriterion); |
| } |
| private void retrieveAndCompare(Map<Integer, Boolean> dbexpected, Criterion spatialCriterion) { |
| Session session = null; |
| Transaction tx = null; |
| try { |
| session = openSession(); |
| tx = session.beginTransaction(); |
| Criteria criteria = session.createCriteria(GeomEntity.class); |
| criteria.add(spatialCriterion); |
| compare(dbexpected, criteria.list()); |
| tx.commit(); |
| } catch(Exception e){ |
| tx.rollback(); |
| } |
| finally { |
| if (session != null) session.close(); |
| } |
| } |
| private void compare(Map<Integer, Boolean> dbexpected, List list) { |
| int cnt = 0; |
| for (Integer id : dbexpected.keySet()) { |
| if (dbexpected.get(id)) { |
| cnt++; |
| if (!findInList(id, (List<GeomEntity>) list)) |
| fail(String.format("Expected object with id= %d, but not found in result", id)); |
| } |
| } |
| assertEquals(cnt, list.size()); |
| LOGGER.info(String.format("Found %d objects within testsuite-suite polygon.", cnt)); |
| } |
| private boolean findInList(Integer id, List<GeomEntity> list) { |
| for (GeomEntity entity : list) { |
| if (entity.getId() == id) return true; |
| } |
| return false; |
| } |
| } |
| /trunk/test-suite/src/test/java/org/hibernatespatial/testsuite/SpatialFunctionalTestCase.java |
|---|
| New file |
| 0,0 → 1,110 |
| package org.hibernatespatial.testsuite; |
| import com.vividsolutions.jts.geom.Geometry; |
| import org.hibernate.Criteria; |
| import org.hibernate.Query; |
| import org.hibernate.cfg.Configuration; |
| import org.hibernate.testing.junit.functional.FunctionalTestCase; |
| import org.hibernatespatial.test.*; |
| import org.slf4j.Logger; |
| import java.util.List; |
| import java.util.Map; |
| import static org.junit.Assert.assertArrayEquals; |
| /** |
| * @author Karel Maesen, Geovise BVBA |
| * creation-date: Sep 30, 2010 |
| */ |
| public abstract class SpatialFunctionalTestCase extends FunctionalTestCase { |
| protected TestData testData; |
| protected DataSourceUtils dataSourceUtils; |
| protected GeometryEquality geometryEquality; |
| protected AbstractExpectationsFactory expectationsFactory; |
| public SpatialFunctionalTestCase(String string) { |
| super(string); |
| } |
| public void prepareTest(){ |
| try { |
| TestSupportFactory tsFactory = TestSupportFactories.instance().getTestSupportFactory(getDialect()); |
| Configuration cfg = getCfg(); |
| dataSourceUtils = tsFactory.createDataSourceUtil(cfg); |
| expectationsFactory = tsFactory.createExpectationsFactory(dataSourceUtils); |
| testData = tsFactory.createTestData(this); |
| geometryEquality = tsFactory.createGeometryEquality(); |
| } catch (Exception e) { |
| throw new RuntimeException(e); |
| } |
| } |
| public String getBaseForMappings() { |
| return "org/hibernatespatial/test/"; |
| } |
| public String[] getMappings() { |
| return new String[]{"GeomEntity.hbm.xml"}; |
| } |
| abstract protected Logger getLogger(); |
| /** |
| * Adds the query results to a Map. |
| * |
| * Each row is added as a Map entry with the first column the key, |
| * and the second the value. It is assumed that the first column is an |
| * identifier of a type assignable to Integer. |
| * |
| * @param result map of |
| * @param query the source Query |
| * @param <T> type of the second column in the query results |
| */ |
| protected <T> void addQueryResults(Map<Integer, T> result, Query query) { |
| List<Object[]> rows = (List<Object[]>) query.list(); |
| if (rows.size() == 0) { |
| getLogger().warn("No results returned for query!!"); |
| } |
| for (Object[] row : rows) { |
| Integer id = (Integer) row[0]; |
| T val = (T) row[1]; |
| result.put(id, val); |
| } |
| } |
| protected <T> void compare(Map<Integer, T> expected, Map<Integer, T> received) { |
| for (Integer id : expected.keySet()) { |
| getLogger().debug("Case :" + id); |
| getLogger().debug("expected: " + expected.get(id)); |
| getLogger().debug("received: " + received.get(id)); |
| compare(id, expected.get(id), received.get(id)); |
| } |
| } |
| protected void compare(Integer id, Object expected, Object received) { |
| assertTrue(expected != null && received != null); |
| if (expected instanceof byte[]) { |
| assertArrayEquals("Failure on testsuite-suite for case " + id, (byte[]) expected, (byte[]) received); |
| } else if (expected instanceof Geometry) { |
| if (!(received instanceof Geometry)) |
| fail("Expected a Geometry, but received an object of type " + received.getClass().getCanonicalName()); |
| assertTrue("Failure on testsuite-suite for case " + id, geometryEquality.test((Geometry) expected, (Geometry) received)); |
| } else { |
| if (expected instanceof Long) { |
| assertEquals("Failure on testsuite-suite for case " + id, ((Long) expected).intValue(), received); |
| } else { |
| assertEquals("Failure on testsuite-suite for case " + id, expected, received); |
| } |
| } |
| } |
| } |
| /trunk/test-suite/src/test/java/org/hibernatespatial/testsuite/TestSupportFactories.java |
|---|
| New file |
| 0,0 → 1,48 |
| package org.hibernatespatial.testsuite; |
| import org.hibernate.dialect.Dialect; |
| import org.hibernatespatial.test.TestSupportFactory; |
| /** |
| * @author Karel Maesen, Geovise BVBA |
| * creation-date: Sep 30, 2010 |
| */ |
| public class TestSupportFactories { |
| private static TestSupportFactories instance = new TestSupportFactories(); |
| public static TestSupportFactories instance(){ |
| return instance; |
| } |
| private TestSupportFactories(){} |
| public TestSupportFactory getTestSupportFactory(Dialect dialect) throws ClassNotFoundException, InstantiationException, IllegalAccessException { |
| if (dialect == null) throw new IllegalArgumentException("Dialect argument is required."); |
| String testSupportFactoryClassName = getSupportFactoryClassName(dialect); |
| return instantiate(testSupportFactoryClassName); |
| } |
| private TestSupportFactory instantiate(String testSupportFactoryClassName) throws ClassNotFoundException, IllegalAccessException, InstantiationException { |
| ClassLoader cloader = getClassLoader(); |
| Class<TestSupportFactory> cl = (Class<TestSupportFactory>)(cloader.loadClass(testSupportFactoryClassName)); |
| return cl.newInstance(); |
| } |
| private ClassLoader getClassLoader() { |
| return this.getClass().getClassLoader(); |
| } |
| private static String getSupportFactoryClassName(Dialect dialect){ |
| String canonicalName = dialect.getClass().getCanonicalName(); |
| if ("org.hibernatespatial.postgis.PostgisDialect".equals(canonicalName)) { |
| return "org.hibernatespatial.postgis.PostgisTestSupportFactory"; |
| } |
| throw new IllegalArgumentException("Dialect not known in test suite"); |
| } |
| } |
| /trunk/test-suite/src/test/resources/log4j.properties |
|---|
| New file |
| 0,0 → 1,49 |
| ### direct log messages to stdout ### |
| log4j.appender.stdout=org.apache.log4j.ConsoleAppender |
| log4j.appender.stdout.Target=System.out |
| log4j.appender.stdout.layout=org.apache.log4j.PatternLayout |
| log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n |
| ### direct messages to file hibernate.log ### |
| #log4j.appender.file=org.apache.log4j.FileAppender |
| #log4j.appender.file.File=hibernate.log |
| #log4j.appender.file.layout=org.apache.log4j.PatternLayout |
| #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n |
| ### set log levels - for more verbose logging change 'info' to 'debug' ### |
| log4j.rootLogger=debug, stdout |
| log4j.logger.org.hibernatespatial.testsuite-suite=debug |
| log4j.logger.org.hibernate=info |
| #log4j.logger.org.hibernate=debug |
| ### log HQL query parser activity |
| #log4j.logger.org.hibernate.hql.ast.AST=debug |
| ### log just the SQL |
| #log4j.logger.org.hibernate.SQL=debug |
| ### log JDBC bind parameters ### |
| log4j.logger.org.hibernate.type=info |
| #log4j.logger.org.hibernate.type=debug |
| ### log schema export/update ### |
| log4j.logger.org.hibernate.tool.hbm2ddl=debug |
| ### log HQL parse trees |
| #log4j.logger.org.hibernate.hql=debug |
| ### log cache activity ### |
| #log4j.logger.org.hibernate.cache=debug |
| ### log transaction activity |
| #log4j.logger.org.hibernate.transaction=debug |
| ### log JDBC resource acquisition |
| #log4j.logger.org.hibernate.jdbc=debug |
| ### enable the following line if you want to track down connection ### |
| ### leakages when using DriverManagerConnectionProvider ### |
| #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace |
| Property changes: |
| Added: svn:executable |
| + |
| Added: svn:keywords |
| + Id |
| /trunk/test-suite/src/test/resources/hibernate.properties |
|---|
| New file |
| 0,0 → 1,22 |
| hibernate.dialect ${db.dialect} |
| hibernate.connection.driver_class ${jdbc.driver} |
| hibernate.connection.url ${jdbc.url} |
| hibernate.connection.username ${jdbc.user} |
| hibernate.connection.password ${jdbc.pass} |
| hibernate.connection.isolation ${jdbc.isolation} |
| hibernate.test.validateDataCleanup=false |
| hibernate.connection.pool_size 5 |
| hibernate.show_sql true |
| hibernate.format_sql true |
| hibernate.max_fetch_depth 5 |
| hibernate.cache.region_prefix hibernate.test |
| hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider |
| hibernate.jdbc.batch_size 0 |
| /trunk/test-suite/pom.xml |
|---|
| New file |
| 0,0 → 1,32 |
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| <modelVersion>4.0.0</modelVersion> |
| <parent> |
| <groupId>org.hibernatespatial</groupId> |
| <artifactId>hibernate-spatial-maven</artifactId> |
| <version>1.1-SNAPSHOT</version> |
| <relativePath>../hibernate-spatial-maven</relativePath> |
| </parent> |
| <artifactId>test-suite</artifactId> |
| <packaging>jar</packaging> |
| <name>test-suite</name> |
| <url>http://maven.apache.org</url> |
| <properties> |
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| </properties> |
| <dependencies> |
| <dependency> |
| <groupId>${project.groupId}</groupId> |
| <artifactId>hibernate-spatial</artifactId> |
| <version>${project.version}</version> |
| <classifier>tests</classifier> |
| </dependency> |
| </dependencies> |
| </project> |