ShapefileDataStore outStore = new ShapefileDataStore(shapeFile.toURI().toURL()); outStore.createSchema(contourSchema); Transaction transaction = new DefaultTransaction("create"); SimpleFeatureStore featureStore = (SimpleFeatureStore) outStore.getFeatureSource(); featureStore.setTransaction(transaction);
/** Create a transaction for being used in this walker */ public void startTransaction() { if (transaction != null) { throw new IllegalStateException("Transaction already open!"); } this.transaction = new DefaultTransaction("MosaicCreationTransaction" + System.nanoTime()); }
@Test public void testLockUnlockFilter() throws Exception { SimpleFeatureLocking fl; fl = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED); final FeatureLock lock = FeatureLockFactory.generate(10 * 60 * 1000); Transaction t = new DefaultTransaction(); t.addAuthorization(lock.getAuthorization()); fl.setTransaction(t); fl.setFeatureLock(lock); SimpleFeatureLocking fl2; fl2 = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED); fl.setFeatureLock(lock); fl2.setTransaction(new DefaultTransaction()); assertEquals(1, fl.lockFeatures(fidFilter)); assertEquals(0, fl2.lockFeatures(fidFilter)); fl.unLockFeatures(fidFilter); assertEquals(1, fl2.lockFeatures(fidFilter)); }
@Test public void testLockUnlockFilter() throws Exception { SimpleFeatureLocking fl; fl = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED); final FeatureLock lock = FeatureLockFactory.generate(10 * 60 * 1000); Transaction t = new DefaultTransaction(); t.addAuthorization(lock.getAuthorization()); fl.setTransaction(t); fl.setFeatureLock(lock); SimpleFeatureLocking fl2; fl2 = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED); fl.setFeatureLock(lock); fl2.setTransaction(new DefaultTransaction()); assertEquals(1, fl.lockFeatures(fidFilter)); assertEquals(0, fl2.lockFeatures(fidFilter)); fl.unLockFeatures(fidFilter); assertEquals(1, fl2.lockFeatures(fidFilter)); }
@Test public void testLockUnlockQuery() throws Exception { SimpleFeatureLocking fl; fl = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED); final FeatureLock lock = FeatureLockFactory.generate(10 * 60 * 1000); Transaction t = new DefaultTransaction(); t.addAuthorization(lock.getAuthorization()); fl.setTransaction(t); fl.setFeatureLock(lock); SimpleFeatureLocking fl2; fl2 = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED); fl.setFeatureLock(lock); fl2.setTransaction(new DefaultTransaction()); Query q = new Query(RENAMED, fidFilter); assertEquals(1, fl.lockFeatures(q)); assertEquals(0, fl2.lockFeatures(q)); fl.unLockFeatures(q); assertEquals(1, fl2.lockFeatures(q)); }
@Test public void testLockUnlockQuery() throws Exception { SimpleFeatureLocking fl; fl = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED); final FeatureLock lock = FeatureLockFactory.generate(10 * 60 * 1000); Transaction t = new DefaultTransaction(); t.addAuthorization(lock.getAuthorization()); fl.setTransaction(t); fl.setFeatureLock(lock); SimpleFeatureLocking fl2; fl2 = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED); fl.setFeatureLock(lock); fl2.setTransaction(new DefaultTransaction()); Query q = new Query(RENAMED, fidFilter); assertEquals(1, fl.lockFeatures(q)); assertEquals(0, fl2.lockFeatures(q)); fl.unLockFeatures(q); assertEquals(1, fl2.lockFeatures(q)); } }
@Before public void setUp() { defaultTransaction = new DefaultTransaction(); anotherTransaction = new DefaultTransaction(); }
/** * Builds a transaction object around a user provided connection. The returned transaction * allows the store to work against an externally managed transaction, such as in J2EE * enviroments. It is the duty of the caller to ensure the connection is to the same database * managed by this {@link JDBCDataStore}. * * <p>Calls to {@link Transaction#commit()}, {@link Transaction#rollback()} and {@link * Transaction#close()} will not result in corresponding calls to the provided {@link * Connection} object. * * @param conn The externally managed connection */ public Transaction buildTransaction(Connection cx) { DefaultTransaction tx = new DefaultTransaction(); State state = new JDBCTransactionState(cx, this, true); tx.putState(this, state); return tx; }
@Before public void setUp() throws SQLException { cx = new TracingMockConnection(); configureMetadata(); dataStore = new JDBCDataStore(); dataStore.setFilterFactory(CommonFactoryFinder.getFilterFactory2()); dataStore.setSQLDialect(createBasicSQLDialect()); MockDataSource dataSource = new MockDataSource(); dataSource.setupConnection(cx); dataStore.setDataSource(dataSource); featureType = Mockito.mock(SimpleFeatureType.class); Mockito.when(featureType.getTypeName()).thenReturn(SAMPLE_FEATURE_NAME); Mockito.when(featureType.getName()).thenReturn(new NameImpl(SAMPLE_FEATURE_NAME)); tx = new DefaultTransaction(); }
public void testRemoveFeature() throws Exception { SimpleFeature f1 = SimpleFeatureBuilder.build(type, new Object[] {"one", geom}, null); SimpleFeatureStore store = (SimpleFeatureStore) ds.getFeatureSource("default"); store.setTransaction(new DefaultTransaction()); List<FeatureId> fid = store.addFeatures(DataUtilities.collection(f1)); count(store, 2); FeatureId identifier = fid.iterator().next(); String next = identifier.getID(); FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory(null); Filter f = filterFactory.id(Collections.singleton(filterFactory.featureId(next))); store.removeFeatures(f); count(store, 1); // assertEquals("Number of known feature as obtained from getCount",3, // store.getCount(Query.ALL)); }
public void testUUIDAsPrimaryKey() throws Exception { try (Transaction transaction = new DefaultTransaction()) { SimpleFeatureStore featureStore = (SimpleFeatureStore) dataStore.getFeatureSource(tname("uuidt")); featureStore.setTransaction(transaction); featureStore.addFeatures(createFeatureCollection()); transaction.commit(); assertEquals(3, featureStore.getCount(Query.ALL)); } }
public void testNoCommit() throws IOException { try (Transaction tx = new DefaultTransaction(); FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriterAppend(tname("ft1"), tx)) { SimpleFeature feature = writer.next(); feature.setAttribute(aname("intProperty"), Integer.valueOf(100)); writer.write(); tx.rollback(); } SimpleFeatureCollection fc = dataStore.getFeatureSource(tname("ft1")).getFeatures(); assertEquals(3, fc.size()); }
public void testAddFeature() throws Exception { SimpleFeature f1 = SimpleFeatureBuilder.build(type, new Object[] {"one", geom}, null); SimpleFeature f2 = SimpleFeatureBuilder.build(type, new Object[] {"two", geom}, null); SimpleFeatureStore store = (SimpleFeatureStore) ds.getFeatureSource("default"); store.setTransaction(new DefaultTransaction()); store.addFeatures(DataUtilities.collection(f1)); store.addFeatures(DataUtilities.collection(f2)); count(store, 3); // assertEquals("Number of known feature as obtained from getCount",3, // store.getCount(Query.ALL)); }
public void testCommit() throws IOException { // JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("ft1")); try (Transaction tx = new DefaultTransaction(); FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriterAppend(tname("ft1"), tx)) { SimpleFeature feature = writer.next(); feature.setAttribute(aname("intProperty"), Integer.valueOf(100)); writer.write(); writer.close(); tx.commit(); } SimpleFeatureCollection fc = dataStore.getFeatureSource(tname("ft1")).getFeatures(); assertEquals(4, fc.size()); }
public void testGetFeatureReaderLake() throws IOException, IllegalFilterException { try (Transaction t = new DefaultTransaction(); FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(new Query(tname("lake")), t)) { assertNotNull(reader); assertTrue(reader.hasNext()); assertNotNull(reader.next()); assertFalse(reader.hasNext()); } }
public void testLockFeaturesWithInvalidFilter() throws Exception { FeatureLock lock = FeatureLockFactory.generate(tname("ft1"), 60 * 60 * 1000); try (Transaction tx = new DefaultTransaction()) { store.setTransaction(tx); store.setFeatureLock(lock); // lock features FilterFactory ff = dataStore.getFilterFactory(); PropertyIsEqualTo f = ff.equals(ff.property(aname("invalidProperty")), ff.literal(1)); try { store.lockFeatures(f); fail("Should have failed with an exception, the filter is not valid"); } catch (Exception e) { // fine } } }
public void testInsertFeatures() throws Exception { try (Transaction transaction = new DefaultTransaction()) { SimpleFeatureStore featureStore = (SimpleFeatureStore) dataStore.getFeatureSource(tname("guid")); featureStore.setTransaction(transaction); assertEquals(featureStore.getCount(Query.ALL), 2); SimpleFeatureType type = dataStore.getSchema(tname("guid")); SimpleFeature feature = SimpleFeatureBuilder.build(type, new Object[] {uuid3}, "guid.3"); SimpleFeatureCollection collection = DataUtilities.collection(feature); featureStore.addFeatures(collection); transaction.commit(); assertEquals(featureStore.getCount(Query.ALL), 3); } }
public void testLockFeaturesWithInvalidQuery() throws Exception { FeatureLock lock = FeatureLockFactory.generate(tname("ft1"), 60 * 60 * 1000); try (Transaction tx = new DefaultTransaction()) { store.setTransaction(tx); store.setFeatureLock(lock); // lock features FilterFactory ff = dataStore.getFilterFactory(); PropertyIsEqualTo f = ff.equals(ff.property(aname("invalidProperty")), ff.literal(1)); try { store.lockFeatures(new Query(store.getSchema().getTypeName(), f)); fail("Should have failed with an exception, the filter is not valid"); } catch (Exception e) { // fine } } }
public void testGetFeatureReaderFilterPrePost() throws IOException, IllegalFilterException { FilterFactory factory = CommonFactoryFinder.getFilterFactory(null); FilterFunction_geometryType geomTypeExpr = new FilterFunction_geometryType(); geomTypeExpr.setParameters( (List) Collections.singletonList(factory.property(aname("geom")))); PropertyIsEqualTo filter = factory.equals(geomTypeExpr, factory.literal("Polygon")); try (Transaction t = new DefaultTransaction()) { try (FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(new Query(tname("road"), filter), t)) { assertNotNull(reader); assertFalse(reader.hasNext()); } filter = factory.equals(geomTypeExpr, factory.literal("LineString")); try (FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(new Query(tname("road"), filter), t)) { assertTrue(reader.hasNext()); } } }
public void testGetFeatureReaderFilterWithAttributesNotRequested() throws Exception { // this is here to avoid http://jira.codehaus.org/browse/GEOT-1069 // to come up again SimpleFeatureType type = dataStore.getSchema(tname("river")); FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); PropertyIsEqualTo f = ff.equals(ff.property(aname("flow")), ff.literal(4.5)); Query q = new Query(tname("river")); q.setPropertyNames(new String[] {aname("geom")}); q.setFilter(f); // with GEOT-1069 an exception is thrown here try (Transaction t = new DefaultTransaction(); FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(q, t)) { assertTrue(reader.hasNext()); assertEquals(1, reader.getFeatureType().getAttributeCount()); reader.next(); assertFalse(reader.hasNext()); } }