Subversion Repositories hibernate-spatial

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
244 maesenka 1
/*
2
 * $Id: TestSpatialFunctions.java 193 2010-03-26 15:56:02Z maesenka $
3
 *
4
 * This file is part of Hibernate Spatial, an extension to the
5
 * hibernate ORM solution for geographic data.
6
 *
7
 * Copyright © 2007-2010 Geovise BVBA
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 *
23
 * For more information, visit: http://www.hibernatespatial.org/
24
 */
25
 
26
package org.hibernatespatial.testsuite;
27
 
28
import com.vividsolutions.jts.geom.Geometry;
29
import org.hibernate.Query;
30
import org.hibernate.Session;
31
import org.hibernate.Transaction;
32
import org.hibernate.type.CustomType;
33
import org.hibernatespatial.GeometryUserType;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36
 
37
import java.sql.SQLException;
38
import java.util.HashMap;
39
import java.util.List;
40
import java.util.Map;
41
 
42
import static junit.framework.Assert.assertTrue;
43
import static org.junit.Assert.*;
44
 
45
/**
46
 * @author Karel Maesen, Geovise BVBA
47
 */
48
public class TestSpatialFunctions extends SpatialFunctionalTestCase {
49
 
50
    private static Logger LOGGER = LoggerFactory.getLogger(TestSpatialFunctions.class);
51
 
52
    public TestSpatialFunctions(String string) {
53
        super(string);
54
    }
55
 
56
    public void prepareTest(){
57
        super.prepareTest();
58
        try {
59
            dataSourceUtils.insertTestData(testData);
60
        } catch (SQLException e) {
61
            throw new RuntimeException(e);
62
        }
63
    }
64
 
65
 
66
    public void test_dimension() throws SQLException {
67
        Map<Integer, Integer> dbexpected = expectationsFactory.getDimension();
68
        String hql = "SELECT id, dimension(geom) FROM GeomEntity";
69
        retrieveHQLResultsAndCompare(dbexpected, hql);
70
    }
71
 
72
    public void test_astext() throws SQLException {
73
        Map<Integer, String> dbexpected = expectationsFactory.getAsText();
74
        String hql = "SELECT id, astext(geom) from GeomEntity";
75
        retrieveHQLResultsAndCompare(dbexpected, hql);
76
    }
77
 
78
    public void test_asbinary() throws SQLException {
79
        Map<Integer, byte[]> dbexpected = expectationsFactory.getAsBinary();
80
        String hql = "SELECT id, asbinary(geom) from GeomEntity";
81
        retrieveHQLResultsAndCompare(dbexpected, hql);
82
    }
83
 
84
 
85
    public void test_geometrytype() throws SQLException {
86
        Map<Integer, String> dbexpected = expectationsFactory.getGeometryType();
87
        String hql = "SELECT id, geometrytype(geom) from GeomEntity";
88
        retrieveHQLResultsAndCompare(dbexpected, hql);
89
    }
90
 
91
    public void test_srid() throws SQLException {
92
        Map<Integer, Integer> dbexpected = expectationsFactory.getSrid();
93
        String hql = "SELECT id, srid(geom) from GeomEntity";
94
        retrieveHQLResultsAndCompare(dbexpected, hql);
95
    }
96
 
97
    public void test_issimple() throws SQLException {
98
        Map<Integer, Boolean> dbexpected = expectationsFactory.getIsSimple();
99
        String hql = "SELECT id, issimple(geom) from GeomEntity";
100
        retrieveHQLResultsAndCompare(dbexpected, hql);
101
    }
102
 
103
    public void test_isempty() throws SQLException {
104
        Map<Integer, Boolean> dbexpected = expectationsFactory.getIsEmpty();
105
        String hql = "SELECT id, isEmpty(geom) from GeomEntity";
106
        retrieveHQLResultsAndCompare(dbexpected, hql);
107
    }
108
 
109
 
110
    public void test_boundary() throws SQLException {
111
        Map<Integer, Geometry> dbexpected = expectationsFactory.getBoundary();
112
        String hql = "SELECT id, boundary(geom) from GeomEntity";
113
        retrieveHQLResultsAndCompare(dbexpected, hql);
114
    }
115
 
116
 
117
    public void test_envelope() throws SQLException {
118
        Map<Integer, Geometry> dbexpected = expectationsFactory.getEnvelope();
119
        String hql = "SELECT id, envelope(geom) from GeomEntity";
120
        retrieveHQLResultsAndCompare(dbexpected, hql);
121
    }
122
 
123
    public void test_within() throws SQLException {
124
        Map<Integer, Boolean> dbexpected = expectationsFactory.getWithin(expectationsFactory.getTestPolygon());
125
        String hql = "SELECT id, within(geom, :filter) from GeomEntity where within(geom, :filter) = true and srid(geom) = 4326";
126
        Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon());
127
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
128
    }
129
 
130
    private Map<String, Object> createQueryParams(String filterParamName, Object value) {
131
        Map<String, Object> params = new HashMap<String, Object>();
132
        params.put(filterParamName, value);
133
        return params;
134
    }
135
 
136
    public void test_equals() throws SQLException {
137
        Map<Integer, Boolean> dbexpected = expectationsFactory.getEquals(expectationsFactory.getTestPolygon());
138
        String hql = "SELECT id, equals(geom, :filter) from GeomEntity where equals(geom, :filter) = true and srid(geom) = 4326";
139
        Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon());
140
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
141
    }
142
 
143
    public void test_crosses() throws SQLException {
144
        Map<Integer, Boolean> dbexpected = expectationsFactory.getCrosses(expectationsFactory.getTestPolygon());
145
        String hql = "SELECT id, crosses(geom, :filter) from GeomEntity where crosses(geom, :filter) = true and srid(geom) = 4326";
146
        Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon());
147
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
148
 
149
    }
150
 
151
    public void test_contains() throws SQLException {
152
        Map<Integer, Boolean> dbexpected = expectationsFactory.getContains(expectationsFactory.getTestPolygon());
153
        String hql = "SELECT id, contains(geom, :filter) from GeomEntity where contains(geom, :filter) = true and srid(geom) = 4326";
154
        Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon());
155
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
156
    }
157
 
158
 
159
    public void test_disjoint() throws SQLException {
160
        Map<Integer, Boolean> dbexpected = expectationsFactory.getDisjoint(expectationsFactory.getTestPolygon());
161
        String hql = "SELECT id, disjoint(geom, :filter) from GeomEntity where disjoint(geom, :filter) = true and srid(geom) = 4326";
162
        Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon());
163
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
164
    }
165
 
166
    public void test_intersects() throws SQLException {
167
        Map<Integer, Boolean> dbexpected = expectationsFactory.getIntersects(expectationsFactory.getTestPolygon());
168
        String hql = "SELECT id, intersects(geom, :filter) from GeomEntity where intersects(geom, :filter) = true and srid(geom) = 4326";
169
        Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon());
170
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
171
    }
172
 
173
    public void test_overlaps() throws SQLException {
174
        Map<Integer, Boolean> dbexpected = expectationsFactory.getOverlaps(expectationsFactory.getTestPolygon());
175
        String hql = "SELECT id, overlaps(geom, :filter) from GeomEntity where overlaps(geom, :filter) = true and srid(geom) = 4326";
176
        Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon());
177
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
178
    }
179
 
180
    public void test_touches() throws SQLException {
181
        String hql = "SELECT id, touches(geom, :filter) from GeomEntity where touches(geom, :filter) = true and srid(geom) = 4326";
182
        Map<Integer, Boolean> dbexpected = expectationsFactory.getTouches(expectationsFactory.getTestPolygon());
183
        Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon());
184
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
185
    }
186
 
187
    public void test_relate() throws SQLException {
188
        String matrix = "T*T***T**";
189
        Map<Integer, Boolean> dbexpected = expectationsFactory.getRelate(expectationsFactory.getTestPolygon(), matrix);
190
        String hql = "SELECT id, relate(geom, :filter, :matrix) from GeomEntity where relate(geom, :filter, :matrix) = true and srid(geom) = 4326";
191
        Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon());
192
        params.put("matrix", matrix);
193
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
194
 
195
        matrix = "FF*FF****";
196
        dbexpected = expectationsFactory.getRelate(expectationsFactory.getTestPolygon(), matrix);
197
        params.put("matrix", matrix);
198
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
199
 
200
    }
201
 
202
    public void test_distance() throws SQLException {
203
        Map<Integer, Double> dbexpected = expectationsFactory.getDistance(expectationsFactory.getTestPolygon());
204
        String hql = "SELECT id, distance(geom, :filter) from GeomEntity where srid(geom) = 4326";
205
        Map<String, Object> params = createQueryParams("filter", expectationsFactory.getTestPolygon());
206
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
207
    }
208
 
209
    public void test_buffer() throws SQLException {
210
        Map<Integer, Geometry> dbexpected = expectationsFactory.getBuffer(Double.valueOf(1.0));
211
        String hql = "SELECT id, buffer(geom, :distance) from GeomEntity where srid(geom) = 4326";
212
        Map<String, Object> params = createQueryParams("distance", Double.valueOf(1.0));
213
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
214
 
215
    }
216
 
217
    public void test_convexhull() throws SQLException {
218
        Map<Integer, Geometry> dbexpected = expectationsFactory.getConvexHull(expectationsFactory.getTestPolygon());
219
        String hql = "SELECT id, convexhull(geomunion(geom, :polygon)) from GeomEntity where srid(geom) = 4326";
220
        Map<String, Object> params = createQueryParams("polygon", expectationsFactory.getTestPolygon());
221
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
222
 
223
    }
224
 
225
    public void test_intersection() throws SQLException {
226
        Map<Integer, Geometry> dbexpected = expectationsFactory.getIntersection(expectationsFactory.getTestPolygon());
227
        String hql = "SELECT id, intersection(geom, :polygon) from GeomEntity where srid(geom) = 4326";
228
        Map<String, Object> params = createQueryParams("polygon", expectationsFactory.getTestPolygon());
229
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
230
    }
231
 
232
    public void test_difference() throws SQLException {
233
        Map<Integer, Geometry> dbexpected = expectationsFactory.getDifference(expectationsFactory.getTestPolygon());
234
        String hql = "SELECT id, difference(geom, :polygon) from GeomEntity where srid(geom) = 4326";
235
        Map<String, Object> params = createQueryParams("polygon", expectationsFactory.getTestPolygon());
236
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
237
    }
238
 
239
    public void test_symdifference() throws SQLException {
240
        Map<Integer, Geometry> dbexpected = expectationsFactory.getSymDifference(expectationsFactory.getTestPolygon());
241
        String hql = "SELECT id, symdifference(geom, :polygon) from GeomEntity where srid(geom) = 4326";
242
        Map<String, Object> params = createQueryParams("polygon", expectationsFactory.getTestPolygon());
243
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
244
    }
245
 
246
    public void test_geomunion() throws SQLException {
247
        Map<Integer, Geometry> dbexpected = expectationsFactory.getGeomUnion(expectationsFactory.getTestPolygon());
248
        String hql = "SELECT id, geomunion(geom, :polygon) from GeomEntity where srid(geom) = 4326";
249
        Map<String, Object> params = createQueryParams("polygon", expectationsFactory.getTestPolygon());
250
        retrieveHQLResultsAndCompare(dbexpected, hql, params);
251
    }
252
 
253
    public <T> void retrieveHQLResultsAndCompare(Map<Integer, T> dbexpected, String hql) {
254
        Map<Integer, T> hsreceived = new HashMap<Integer, T>();
255
        doInSession(hql, hsreceived, null);
256
        compare(dbexpected, hsreceived);
257
    }
258
 
259
    protected <T> void retrieveHQLResultsAndCompare(Map<Integer, T> dbexpected, String hql, Map<String, Object> params) {
260
        Map<Integer, T> hsreceived = new HashMap<Integer, T>();
261
        doInSession(hql, hsreceived, params);
262
        compare(dbexpected, hsreceived);
263
    }
264
 
265
    protected <T> void compare(Map<Integer, T> expected, Map<Integer, T> received) {
266
        for (Integer id : expected.keySet()) {
267
            LOGGER.debug("Case :" + id);
268
            LOGGER.debug("expected: " + expected.get(id));
269
            LOGGER.debug("received: " + received.get(id));
270
            compare(id, expected.get(id), received.get(id));
271
        }
272
    }
273
 
274
 
275
    protected void compare(Integer id, Object expected, Object received) {
276
        assertTrue(expected != null && received != null);
277
        if (expected instanceof byte[]) {
278
            assertArrayEquals("Failure on testsuite-suite for case " + id, (byte[]) expected, (byte[]) received);
279
 
280
        } else if (expected instanceof Geometry) {
281
            if (!(received instanceof Geometry))
282
                fail("Expected a Geometry, but received an object of type " + received.getClass().getCanonicalName());
283
            assertTrue("Failure on testsuite-suite for case " + id, geometryEquality.test((Geometry) expected, (Geometry) received));
284
 
285
        } else {
286
            if (expected instanceof Long) {
287
                assertEquals("Failure on testsuite-suite for case " + id, ((Long) expected).intValue(), received);
288
            } else {
289
                assertEquals("Failure on testsuite-suite for case " + id, expected, received);
290
            }
291
        }
292
    }
293
 
294
    private <T> void doInSession(String hql, Map<Integer, T> result, Map<String, Object> params) {
295
        Session session = null;
296
        Transaction tx = null;
297
        try {
298
            session = openSession();
299
            tx = session.beginTransaction();
300
            Query query = session.createQuery(hql);
301
            setParameters(params, query);
302
            addQueryResults(result, query);
303
        } finally {
304
            if (tx != null) tx.rollback();
305
            if (session != null) session.close();
306
        }
307
    }
308
 
309
    private <T> void addQueryResults(Map<Integer, T> result, Query query) {
310
        List<Object[]> rows = (List<Object[]>) query.list();
311
        if (rows.size() == 0) {
312
            LOGGER.warn("No results returned for query!!");
313
        }
314
        for (Object[] row : rows) {
315
            Integer id = (Integer) row[0];
316
            T val = (T) row[1];
317
            result.put(id, val);
318
        }
319
    }
320
 
321
    private void setParameters(Map<String, Object> params, Query query) {
322
        if (params == null) return;
323
        for (String param : params.keySet()) {
324
            Object value = params.get(param);
325
            if (value instanceof Geometry) {
326
                query.setParameter(param, value, new CustomType(new GeometryUserType()));
327
            } else {
328
                query.setParameter(param, value);
329
            }
330
        }
331
    }
332
 
333
}