/**
* Test two transactions one removing feature, and one adding a feature.
*/
@Test
public void testGetFeatureWriterTransaction() throws Exception {
Transaction t1 = new DefaultTransaction();
Transaction t2 = new DefaultTransaction();
FeatureWriter<SimpleFeatureType, SimpleFeature> writer1 = data.getFeatureWriter(
getRoadTypeName(), rd1Filter, t1);
FeatureWriter<SimpleFeatureType, SimpleFeature> writer2 = data.getFeatureWriterAppend(
getRoadTypeName(), t2);
SimpleFeatureType road = data.getSchema(getRoadTypeName());
FeatureReader<SimpleFeatureType, SimpleFeature> reader;
SimpleFeature feature;
SimpleFeature[] ORIGIONAL = roadFeatures;
SimpleFeature[] REMOVE = new SimpleFeature[ORIGIONAL.length - 1];
SimpleFeature[] ADD = new SimpleFeature[ORIGIONAL.length + 1];
SimpleFeature[] FINAL = new SimpleFeature[ORIGIONAL.length];
int i;
int index;
index = 0;
for (i = 0; i < ORIGIONAL.length; i++) {
feature = ORIGIONAL[i];
if (!feature.getID().equals(roadFeatures[0].getID())) {
REMOVE[index++] = feature;
}
}
for (i = 0; i < ORIGIONAL.length; i++) {
ADD[i] = ORIGIONAL[i];
}
ADD[i] = newRoad;
for (i = 0; i < REMOVE.length; i++) {
FINAL[i] = REMOVE[i];
}
FINAL[i] = newRoad;
// start of with ORIGINAL
final Query allRoadsQuery = new Query(getRoadTypeName());
reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
assertTrue(covers(reader, ORIGIONAL));
// writer 1 removes road.rd1 on t1
// -------------------------------
// - tests transaction independence from DataStore
while (writer1.hasNext()) {
feature = (SimpleFeature) writer1.next();
assertEquals(roadFeatures[0].getID(), feature.getID());
writer1.remove();
}
// still have ORIGIONAL and t1 has REMOVE
reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
assertTrue(covers(reader, ORIGIONAL));
reader = data.getFeatureReader(allRoadsQuery, t1);
assertTrue(covers(reader, REMOVE));
// close writer1
// --------------
// ensure that modification is left up to transaction commmit
writer1.close();
// We still have ORIGIONAL and t1 has REMOVE
reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
assertTrue(covers(reader, ORIGIONAL));
reader = data.getFeatureReader(allRoadsQuery, t1);
assertTrue(covers(reader, REMOVE));
// writer 2 adds road.rd4 on t2
// ----------------------------
// - tests transaction independence from each other
feature = (SimpleFeature) writer2.next();
feature.setAttributes(newRoad.getAttributes());
writer2.write();
// We still have ORIGIONAL and t2 has ADD
reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
assertTrue(covers(reader, ORIGIONAL));
reader = data.getFeatureReader(allRoadsQuery, t2);
assertTrue(coversLax(reader, ADD));
// close writer2
// -------------
// ensure that modification is left up to transaction commmit
writer2.close();
// Still have ORIGIONAL and t2 has ADD
reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
assertTrue(covers(reader, ORIGIONAL));
reader = data.getFeatureReader(allRoadsQuery, t2);
assertTrue(coversLax(reader, ADD));
// commit t1
// ---------
// -ensure that delayed writing of transactions takes place
//
t1.commit();
// We now have REMOVE, as does t1 (which has not additional diffs)
// t2 will have FINAL
reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
assertTrue(covers(reader, REMOVE));
reader = data.getFeatureReader(allRoadsQuery, t1);
assertTrue(covers(reader, REMOVE));
reader = data.getFeatureReader(allRoadsQuery, t2);
assertTrue(coversLax(reader, FINAL));
// commit t2
// ---------
// -ensure that everyone is FINAL at the end of the day
t2.commit();
// We now have Number( remove one and add one)
reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
assertTrue(coversLax(reader, FINAL));