use of org.geotoolkit.storage.DataStoreFactory in project geotoolkit by Geomatys.
the class PostgresVersioningTest method testDistinctSchema.
@Test
public void testDistinctSchema() throws DataStoreException, VersioningException, FileNotFoundException, IOException {
reload(true);
List<Version> versions;
Feature feature;
ResourceId fid;
Version v0;
Version v1;
Version v2;
FeatureIterator ite;
Query qb = new Query();
final FeatureType refType = FTYPE_SIMPLE;
// ------------------- initialize public2 schema --------------------
// / creation 2eme table
PostgresStore store2;
final ParameterValueGroup params2 = params.clone();
params2.parameter("schema").setValue("public2");
store2 = (PostgresStore) DataStores.open(params2);
// -------------- create schema in public2 schema --------------------
try {
((DataStoreFactory) store2.getProvider()).create(params2);
} catch (Exception ex) {
// schema public2 already exist
}
for (GenericName n : store2.getNames()) {
VersionControl vc = store2.getVersioning(n.toString());
vc.dropVersioning();
store2.deleteFeatureType(n.toString());
}
assertTrue(store2.getNames().isEmpty());
// delete historisation functions, he must create them himself
store2.dropHSFunctions();
// -------------- create table in public schema --------------------
store.createFeatureType(refType);
assertEquals(1, store.getNames().size());
assertTrue(store2.getNames().isEmpty());
// get version control
final VersionControl vcP1 = store.getVersioning(refType.getName().toString());
assertNotNull(vcP1);
assertTrue(vcP1.isEditable());
assertFalse(vcP1.isVersioned());
// -------------------- start versioning in public schema ---------------
vcP1.startVersioning();
assertTrue(vcP1.isVersioned());
versions = vcP1.list();
assertTrue(versions.isEmpty());
// --------------------- table creation in public2 schema ---------------
store2.createFeatureType(refType);
assertEquals(1, store2.getNames().size());
// get version control
final VersionControl vcP2 = store2.getVersioning(refType.getName().toString());
assertNotNull(vcP2);
assertTrue(vcP2.isEditable());
assertFalse(vcP2.isVersioned());
// -------------------- start versioning in public schema ---------------
vcP2.startVersioning();
assertTrue(vcP2.isVersioned());
versions = vcP2.list();
assertTrue(versions.isEmpty());
/* insert, update and delete some elements in public schema and verify
* public2 schema stay empty, to verify the 2th schema are actions distincts.*/
// make an insert ///////////////////////////////////////////////////////
final Point firstPoint = GF.createPoint(new Coordinate(56, 45));
feature = refType.newInstance();
feature.setPropertyValue("id", "0");
feature.setPropertyValue("boolean", Boolean.TRUE);
feature.setPropertyValue("integer", 14);
feature.setPropertyValue("point", firstPoint);
feature.setPropertyValue("string", "someteststring");
store.addFeatures(refType.getName().toString(), Collections.singleton(feature));
// ensure test table in public2 schema is empty
qb = new Query();
qb.setTypeName(refType.getName());
assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
// ensure history test table in public2 schema is empty
assertTrue(vcP2.list().isEmpty());
// make an update ///////////////////////////////////////////////////////
// get feature to update
// get identifier
qb = new Query();
qb.setTypeName(refType.getName());
ite = store.createSession(true).getFeatureCollection(qb).iterator();
try {
feature = ite.next();
fid = FeatureExt.getId(feature);
} finally {
ite.close();
}
try {
// wait a bit just to have some space between version dates
Thread.sleep(1000);
} catch (InterruptedException ex) {
fail(ex.getMessage());
}
final Point secondPoint = GF.createPoint(new Coordinate(-12, 21));
final Map<String, Object> updates = new HashMap<>();
updates.put("boolean", Boolean.FALSE);
updates.put("integer", -3);
updates.put("point", secondPoint);
updates.put("string", "anothertextupdated");
store.updateFeatures(refType.getName().toString(), fid, updates);
// ensure test table in public2 schema is empty
qb = new Query();
qb.setTypeName(refType.getName());
assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
// ensure history test table in public2 schema is empty
assertTrue(vcP2.list().isEmpty());
// make a remove ///////////////////////////////////////////////////////
store.removeFeatures(refType.getName().toString(), fid);
// ensure test table in public2 schema is empty
qb = new Query();
qb.setTypeName(refType.getName());
assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
// ensure history test table in public2 schema is empty
assertTrue(vcP2.list().isEmpty());
// get all versions organized in increase dates order.
versions = vcP1.list();
assertEquals(3, versions.size());
v0 = versions.get(0);
v1 = versions.get(1);
v2 = versions.get(2);
vcP1.revert(v1.getDate());
// ensure test table in public2 schema is empty
qb = new Query();
qb.setTypeName(refType.getName());
assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
// ensure history test table in public2 schema is empty
assertTrue(vcP2.list().isEmpty());
vcP1.trim(v1.getDate());
// ensure test table in public2 schema is empty
qb = new Query();
qb.setTypeName(refType.getName());
assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
// ensure history test table in public2 schema is empty
assertTrue(vcP2.list().isEmpty());
}
Aggregations