Search in sources :

Example 1 with SamplePoint

use of org.datanucleus.samples.jtsgeometry.SamplePoint in project tests by datanucleus.

the class JtsGeometryMappingTest method testUserDataMappingWithString.

public void testUserDataMappingWithString() throws SQLException, ParseException {
    if (!runTestsForDatastore()) {
        return;
    }
    Point point = (Point) wktReader.read("POINT(10 10)");
    point.setSRID(-1);
    Object userData = "UserDataString";
    point.setUserData(userData);
    SamplePoint samplePoint;
    SamplePoint samplePoint_read;
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        samplePoint = new SamplePoint(12001, "UserDataWithString", point);
        em.persist(samplePoint);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    em = emf.createEntityManager();
    tx = em.getTransaction();
    try {
        tx.begin();
        samplePoint_read = (SamplePoint) em.find(SamplePoint.class, new Long(12001));
        assertEquals(samplePoint, samplePoint_read);
        assertNotNull(samplePoint_read.getGeom().getUserData());
        assertEquals(String.class, samplePoint_read.getGeom().getUserData().getClass());
        assertEquals(userData, samplePoint_read.getGeom().getUserData());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : SamplePoint(org.datanucleus.samples.jtsgeometry.SamplePoint) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) SamplePoint(org.datanucleus.samples.jtsgeometry.SamplePoint) Point(com.vividsolutions.jts.geom.Point)

Example 2 with SamplePoint

use of org.datanucleus.samples.jtsgeometry.SamplePoint in project tests by datanucleus.

the class JtsGeometryMappingTest method testPointMapping.

public void testPointMapping() throws SQLException, ParseException {
    if (!runTestsForDatastore()) {
        return;
    }
    Point point = (Point) wktReader.read("POINT(10 10)");
    point.setSRID(-1);
    SamplePoint samplePoint;
    SamplePoint samplePoint_read;
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        samplePoint = new SamplePoint(1001, "Point 1", point);
        em.persist(samplePoint);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    em = emf.createEntityManager();
    tx = em.getTransaction();
    try {
        tx.begin();
        samplePoint_read = (SamplePoint) em.find(SamplePoint.class, new Long(1001));
        assertEquals(samplePoint, samplePoint_read);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : SamplePoint(org.datanucleus.samples.jtsgeometry.SamplePoint) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) SamplePoint(org.datanucleus.samples.jtsgeometry.SamplePoint) Point(com.vividsolutions.jts.geom.Point)

Example 3 with SamplePoint

use of org.datanucleus.samples.jtsgeometry.SamplePoint in project tests by datanucleus.

the class JtsGeometryMappingTest method testUserDataMappingWithObject.

public void testUserDataMappingWithObject() throws SQLException, ParseException {
    if (!runTestsForDatastore()) {
        return;
    }
    Point point = (Point) wktReader.read("POINT(10 10)");
    point.setSRID(-1);
    Object userData = new Object();
    point.setUserData(userData);
    SamplePoint samplePoint;
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        samplePoint = new SamplePoint(11001, "UserDataWithObject", point);
        em.persist(samplePoint);
        tx.commit();
        fail("Persisted spatial field with non-serializable user-data but should have failed");
    } catch (Exception e) {
    // Expected : should fail since Object is not Serializable
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : SamplePoint(org.datanucleus.samples.jtsgeometry.SamplePoint) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) SamplePoint(org.datanucleus.samples.jtsgeometry.SamplePoint) Point(com.vividsolutions.jts.geom.Point) ParseException(com.vividsolutions.jts.io.ParseException) SQLException(java.sql.SQLException) PersistenceException(javax.persistence.PersistenceException)

Example 4 with SamplePoint

use of org.datanucleus.samples.jtsgeometry.SamplePoint in project tests by datanucleus.

the class JtsGeometrySpatialTest method testAsText.

public void testAsText() throws SQLException, ParseException {
    SamplePoint point1 = getSamplePoint(1);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        String wkt = "POINT(10 10)";
        Query query = pm.newQuery(SamplePoint.class, "geom != null && geom.toText() == :wkt");
        List list = (List) query.execute(wkt);
        assertEquals("Wrong number of geometries with a given wkt returned", 1, list.size());
        assertTrue("Point 1 should be in the list of geometries with a given wkt", list.contains(point1));
        query = pm.newQuery(SamplePoint.class, "id == :id");
        query.setResultClass(String.class);
        query.setResult("geom.toText()");
        list = (List) query.execute(new Long(point1.getId()));
        assertEquals("Wrong number of geometries with a given id returned", 1, list.size());
        String wkt_read = (String) list.get(0);
        assertEquals("WKT of Point 1 should be equal to a given point's wkt", wkt, wkt_read);
    } finally {
        tx.commit();
    }
}
Also used : SamplePoint(org.datanucleus.samples.jtsgeometry.SamplePoint) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) LineString(com.vividsolutions.jts.geom.LineString) SampleLineString(org.datanucleus.samples.jtsgeometry.SampleLineString) MultiLineString(org.postgis.MultiLineString)

Aggregations

SamplePoint (org.datanucleus.samples.jtsgeometry.SamplePoint)4 Point (com.vividsolutions.jts.geom.Point)3 EntityManager (javax.persistence.EntityManager)3 EntityTransaction (javax.persistence.EntityTransaction)3 LineString (com.vividsolutions.jts.geom.LineString)1 ParseException (com.vividsolutions.jts.io.ParseException)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 PersistenceManager (javax.jdo.PersistenceManager)1 Query (javax.jdo.Query)1 Transaction (javax.jdo.Transaction)1 PersistenceException (javax.persistence.PersistenceException)1 SampleLineString (org.datanucleus.samples.jtsgeometry.SampleLineString)1 MultiLineString (org.postgis.MultiLineString)1