0001: /*
0002: * GeoTools - OpenSource mapping toolkit
0003: * http://geotools.org
0004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
0005: *
0006: * This library is free software; you can redistribute it and/or
0007: * modify it under the terms of the GNU Lesser General Public
0008: * License as published by the Free Software Foundation; either
0009: * version 2.1 of the License, or (at your option) any later version.
0010: *
0011: * This library is distributed in the hope that it will be useful,
0012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * Lesser General Public License for more details.
0015: */
0016: package org.geotools.caching;
0017:
0018: import com.vividsolutions.jts.geom.Coordinate;
0019: import com.vividsolutions.jts.geom.Envelope;
0020: import com.vividsolutions.jts.geom.Geometry;
0021: import com.vividsolutions.jts.geom.GeometryFactory;
0022: import com.vividsolutions.jts.geom.MultiLineString;
0023:
0024: import org.geotools.caching.impl.InMemoryDataCache;
0025:
0026: import org.geotools.data.DataStore;
0027: import org.geotools.data.DataTestCase;
0028: import org.geotools.data.DataUtilities;
0029: import org.geotools.data.DefaultQuery;
0030: import org.geotools.data.DefaultTransaction;
0031: import org.geotools.data.DiffFeatureReader;
0032: import org.geotools.data.EmptyFeatureReader;
0033: import org.geotools.data.EmptyFeatureWriter;
0034: import org.geotools.data.FeatureEvent;
0035: import org.geotools.data.FeatureListener;
0036: import org.geotools.data.FeatureListenerManager;
0037: import org.geotools.data.FeatureLock;
0038: import org.geotools.data.FeatureLockFactory;
0039: import org.geotools.data.FeatureLocking;
0040: import org.geotools.data.FeatureReader;
0041: import org.geotools.data.FeatureSource;
0042: import org.geotools.data.FeatureStore;
0043: import org.geotools.data.FeatureWriter;
0044: import org.geotools.data.FilteringFeatureReader;
0045: import org.geotools.data.FilteringFeatureWriter;
0046: import org.geotools.data.InProcessLockingManager;
0047: import org.geotools.data.Query;
0048: import org.geotools.data.Transaction;
0049: import org.geotools.data.TransactionStateDiff;
0050: import org.geotools.data.memory.MemoryDataStore;
0051:
0052: import org.geotools.feature.AttributeType;
0053: import org.geotools.feature.Feature;
0054: import org.geotools.feature.FeatureCollection;
0055: import org.geotools.feature.FeatureIterator;
0056: import org.geotools.feature.FeatureType;
0057: import org.geotools.feature.IllegalAttributeException;
0058: import org.geotools.feature.SimpleFeature;
0059:
0060: import org.geotools.filter.FidFilter;
0061: import org.geotools.filter.FidFilterImpl;
0062: import org.geotools.filter.FilterFactory;
0063: import org.geotools.filter.FilterFactoryFinder;
0064:
0065: import org.opengis.filter.Filter;
0066:
0067: import java.io.IOException;
0068:
0069: import java.net.URI;
0070:
0071: import java.util.ArrayList;
0072: import java.util.Iterator;
0073: import java.util.List;
0074: import java.util.NoSuchElementException;
0075:
0076: /**
0077: * DOCUMENT ME!
0078: *
0079: * @author Jody Garnett, Refractions Research
0080: * @source $URL:
0081: * http://svn.geotools.org/geotools/trunk/gt/modules/library/main/src/test/java/org/geotools/data/memory/MemoryDataStoreTest.java $
0082: */
0083: public class CacheDataStoreTest extends DataTestCase {
0084: InMemoryDataCache data;
0085:
0086: /**
0087: * Constructor for MemoryDataStoreTest.
0088: *
0089: * @param arg0
0090: */
0091: public CacheDataStoreTest(String arg0) {
0092: super (arg0);
0093: }
0094:
0095: /*
0096: * @see TestCase#setUp()
0097: */
0098: protected void setUp() throws Exception {
0099: super .setUp();
0100:
0101: MemoryDataStore ds = new MemoryDataStore();
0102: ds.addFeatures(roadFeatures);
0103: ds.addFeatures(riverFeatures);
0104: data = new InMemoryDataCache(ds);
0105: }
0106:
0107: /*
0108: * @see TestCase#tearDown()
0109: */
0110: protected void tearDown() throws Exception {
0111: data = null;
0112: super .tearDown();
0113: }
0114:
0115: public void testFixture() throws Exception {
0116: FeatureType type = DataUtilities.createType(
0117: "namespace.typename",
0118: "name:String,id:0,geom:MultiLineString");
0119: assertEquals("namespace", new URI("namespace"), type
0120: .getNamespace());
0121: assertEquals("typename", "typename", type.getTypeName());
0122: assertEquals("attributes", 3, type.getAttributeCount());
0123:
0124: AttributeType[] a = type.getAttributeTypes();
0125: assertEquals("a1", "name", a[0].getName());
0126: assertEquals("a1", String.class, a[0].getType());
0127:
0128: assertEquals("a2", "id", a[1].getName());
0129: assertEquals("a2", Integer.class, a[1].getType());
0130:
0131: assertEquals("a3", "geom", a[2].getName());
0132: assertEquals("a3", MultiLineString.class, a[2].getType());
0133: }
0134:
0135: public void testMemoryDataStore() throws Exception {
0136: DataStore store = new MemoryDataStore();
0137: InMemoryDataCache cache = new InMemoryDataCache(store);
0138: }
0139:
0140: /*
0141: * Test for void MemoryDataStore(FeatureCollection)
0142: */
0143: public void testMemoryDataStoreFeatureCollection() {
0144: DataStore store = new MemoryDataStore(DataUtilities
0145: .collection(roadFeatures));
0146: }
0147:
0148: /*
0149: * Test for void MemoryDataStore(FeatureReader)
0150: */
0151: public void testMemoryDataStoreFeatureArray() throws IOException {
0152: DataStore store = new MemoryDataStore(roadFeatures);
0153: }
0154:
0155: /*
0156: * Test for void MemoryDataStore(FeatureReader)
0157: */
0158: public void testMemoryDataStoreFeatureReader() throws IOException {
0159: FeatureReader reader = DataUtilities.reader(roadFeatures);
0160: DataStore store = new MemoryDataStore(reader);
0161: }
0162:
0163: public void testGetFeatureTypes() throws IOException {
0164: String[] names = data.getTypeNames();
0165: assertEquals(2, names.length);
0166: assertTrue(contains(names, "road"));
0167: assertTrue(contains(names, "river"));
0168: }
0169:
0170: boolean contains(Object[] array, Object expected) {
0171: if ((array == null) || (array.length == 0)) {
0172: return false;
0173: }
0174:
0175: for (int i = 0; i < array.length; i++) {
0176: if (array[i].equals(expected)) {
0177: return true;
0178: }
0179: }
0180:
0181: return false;
0182: }
0183:
0184: /**
0185: * Like contain but based on match rather than equals
0186: *
0187: * @param array DOCUMENT ME!
0188: * @param expected DOCUMENT ME!
0189: * @return DOCUMENT ME!
0190: */
0191: boolean containsLax(Feature[] array, Feature expected) {
0192: if ((array == null) || (array.length == 0)) {
0193: return false;
0194: }
0195:
0196: FeatureType type = expected.getFeatureType();
0197:
0198: for (int i = 0; i < array.length; i++) {
0199: if (match(array[i], expected)) {
0200: return true;
0201: }
0202: }
0203:
0204: return false;
0205: }
0206:
0207: /**
0208: * Compare based on attributes not getID allows comparison of Diff contents
0209: *
0210: * @param expected DOCUMENT ME!
0211: * @param actual DOCUMENT ME!
0212: * @return DOCUMENT ME!
0213: */
0214: boolean match(Feature expected, Feature actual) {
0215: FeatureType type = expected.getFeatureType();
0216:
0217: for (int i = 0; i < type.getAttributeCount(); i++) {
0218: Object av = actual.getAttribute(i);
0219: Object ev = expected.getAttribute(i);
0220:
0221: if ((av == null) && (ev != null)) {
0222: return false;
0223: } else if ((ev == null) && (av != null)) {
0224: return false;
0225: } else if (av instanceof Geometry && ev instanceof Geometry) {
0226: Geometry ag = (Geometry) av;
0227: Geometry eg = (Geometry) ev;
0228:
0229: if (!ag.equals(eg)) {
0230: return false;
0231: }
0232: } else if (!av.equals(ev)) {
0233: return false;
0234: }
0235: }
0236:
0237: return true;
0238: }
0239:
0240: public void testGetSchema() throws IOException {
0241: assertSame(roadType, data.getSchema("road"));
0242: assertSame(riverType, data.getSchema("river"));
0243: }
0244:
0245: void assertCovers(String msg, FeatureCollection c1,
0246: FeatureCollection c2) {
0247: if (c1 == c2) {
0248: return;
0249: }
0250:
0251: assertNotNull(msg, c1);
0252: assertNotNull(msg, c2);
0253: assertEquals(msg + " size", c1.size(), c2.size());
0254:
0255: Feature f;
0256:
0257: for (FeatureIterator i = c1.features(); i.hasNext();) {
0258: f = i.next();
0259: assertTrue(msg + " " + f.getID(), c2.contains(f));
0260: }
0261: }
0262:
0263: /* public void testGetFeatureReader() throws IOException, IllegalAttributeException {
0264: FeatureReader reader = data.getFeatureReader("road");
0265: assertCovered(roadFeatures, reader);
0266: assertEquals(false, reader.hasNext());
0267: } */
0268:
0269: /* public void testGetFeatureReaderMutability() throws IOException, IllegalAttributeException {
0270: FeatureReader reader = data.getFeatureReader("road");
0271: Feature feature;
0272: while( reader.hasNext() ) {
0273: feature = (Feature) reader.next();
0274: feature.setAttribute("name", null);
0275: }
0276: reader.close();
0277: reader = data.getFeatureReader("road");
0278: while( reader.hasNext() ) {
0279: feature = (Feature) reader.next();
0280: assertNotNull(feature.getAttribute("name"));
0281: }
0282: reader.close();
0283: try {
0284: reader.next();
0285: fail("next should fail with an IOException");
0286: } catch (IOException expected) {
0287: }
0288: } */
0289:
0290: /*public void testGetFeatureReaderConcurancy() throws NoSuchElementException, IOException,
0291: IllegalAttributeException {
0292: FeatureReader reader1 = data.get;
0293: FeatureReader reader2 = data.getFeatureReader("road");
0294: FeatureReader reader3 = data.getFeatureReader("river");
0295: Feature feature1;
0296: Feature feature2;
0297: Feature feature3;
0298: while( reader1.hasNext() || reader2.hasNext() || reader3.hasNext() ) {
0299: assertTrue(contains(roadFeatures, reader1.next()));
0300: assertTrue(contains(roadFeatures, reader2.next()));
0301: if (reader3.hasNext()) {
0302: assertTrue(contains(riverFeatures, reader3.next()));
0303: }
0304: }
0305: try {
0306: reader1.next();
0307: fail("next should fail with an IOException");
0308: } catch (IOException expected) {
0309: }
0310: try {
0311: reader2.next();
0312: fail("next should fail with an IOException");
0313: } catch (IOException expected) {
0314: }
0315: try {
0316: reader3.next();
0317: fail("next should fail with an IOException");
0318: } catch (IOException expected) {
0319: }
0320: reader1.close();
0321: reader2.close();
0322: reader3.close();
0323: } */
0324: public void testGetFeatureReaderFilterAutoCommit()
0325: throws NoSuchElementException, IOException,
0326: IllegalAttributeException {
0327: FeatureType type = data.getSchema("road");
0328: FeatureReader reader;
0329:
0330: reader = data.getFeatureReader(new DefaultQuery("road"),
0331: Transaction.AUTO_COMMIT);
0332: assertFalse(reader instanceof FilteringFeatureReader);
0333: assertEquals(type, reader.getFeatureType());
0334: assertEquals(roadFeatures.length, count(reader));
0335:
0336: reader = data.getFeatureReader(new DefaultQuery("road",
0337: Filter.EXCLUDE), Transaction.AUTO_COMMIT);
0338: assertTrue(reader instanceof EmptyFeatureReader);
0339:
0340: assertEquals(type, reader.getFeatureType());
0341: assertEquals(0, count(reader));
0342:
0343: reader = data.getFeatureReader(new DefaultQuery("road",
0344: rd1Filter), Transaction.AUTO_COMMIT);
0345: assertTrue(reader instanceof FilteringFeatureReader);
0346: assertEquals(type, reader.getFeatureType());
0347: assertEquals(1, count(reader));
0348: }
0349:
0350: public void testGetFeatureReaderFilterTransaction()
0351: throws NoSuchElementException, IOException,
0352: IllegalAttributeException {
0353: Transaction t = new DefaultTransaction();
0354: FeatureType type = data.getSchema("road");
0355: FeatureReader reader;
0356:
0357: reader = data.getFeatureReader(new DefaultQuery("road",
0358: Filter.EXCLUDE), t);
0359: assertTrue(reader instanceof EmptyFeatureReader);
0360: assertEquals(type, reader.getFeatureType());
0361: assertEquals(0, count(reader));
0362:
0363: reader = data.getFeatureReader(new DefaultQuery("road"), t);
0364: assertTrue(reader instanceof DiffFeatureReader);
0365: assertEquals(type, reader.getFeatureType());
0366: assertEquals(roadFeatures.length, count(reader));
0367:
0368: reader = data.getFeatureReader(new DefaultQuery("road",
0369: rd1Filter), t);
0370: // assertTrue(reader instanceof DiffFeatureReader);//Currently wrapped by a filtering
0371: // feature reader
0372: assertEquals(type, reader.getFeatureType());
0373: assertEquals(1, count(reader));
0374:
0375: TransactionStateDiff state = (TransactionStateDiff) t
0376: .getState(data);
0377: FeatureWriter writer = state.writer("road", Filter.INCLUDE);
0378: Feature feature;
0379:
0380: while (writer.hasNext()) {
0381: feature = writer.next();
0382:
0383: if (feature.getID().equals("road.rd1")) {
0384: writer.remove();
0385: }
0386: }
0387:
0388: reader = data.getFeatureReader(new DefaultQuery("road",
0389: Filter.EXCLUDE), t);
0390: assertEquals(0, count(reader));
0391:
0392: reader = data.getFeatureReader(new DefaultQuery("road"), t);
0393: assertEquals(roadFeatures.length - 1, count(reader));
0394:
0395: reader = data.getFeatureReader(new DefaultQuery("road",
0396: rd1Filter), t);
0397: assertEquals(0, count(reader));
0398:
0399: t.rollback();
0400: reader = data.getFeatureReader(new DefaultQuery("road",
0401: Filter.EXCLUDE), t);
0402: assertEquals(0, count(reader));
0403:
0404: reader = data.getFeatureReader(new DefaultQuery("road"), t);
0405: assertEquals(roadFeatures.length, count(reader));
0406:
0407: reader = data.getFeatureReader(new DefaultQuery("road",
0408: rd1Filter), t);
0409: assertEquals(1, count(reader));
0410: }
0411:
0412: /**
0413: * When a data store is loaded with a reader, it would be nice if the memory
0414: * data store preserved feature order, so that features are always rendered
0415: * the same way (rendering is different if order changes and features do overlap)
0416: */
0417: public void testOrderPreservationRoad() throws Exception {
0418: assertOrderSame(roadFeatures);
0419: }
0420:
0421: public void testOrderPreservationRiver() throws Exception {
0422: assertOrderSame(riverFeatures);
0423: }
0424:
0425: public void testOrderPreservationMemFetures() throws Exception {
0426: SimpleFeature[] dynFeatures = new SimpleFeature[3];
0427: dynFeatures[0] = (SimpleFeature) roadType.create(new Object[] {
0428: new Integer(1),
0429: line(new int[] { 1, 1, 2, 2, 4, 2, 5, 1 }), "r1", });
0430: dynFeatures[1] = (SimpleFeature) roadType.create(new Object[] {
0431: new Integer(2),
0432: line(new int[] { 3, 0, 3, 2, 3, 3, 3, 4 }), "r2" });
0433: dynFeatures[2] = (SimpleFeature) roadType.create(new Object[] {
0434: new Integer(3), line(new int[] { 3, 2, 4, 2, 5, 3 }),
0435: "r3" });
0436: assertOrderSame(dynFeatures);
0437: }
0438:
0439: void assertOrderSame(Feature[] features) throws Exception {
0440: // init using readers
0441: FeatureReader reader = DataUtilities.reader(features);
0442: DataStore store1 = new MemoryDataStore(reader);
0443: assertReaderOrderSame(features, store1);
0444:
0445: // init using array directly
0446: DataStore store2 = new MemoryDataStore(features);
0447: assertReaderOrderSame(features, store2);
0448: }
0449:
0450: private void assertReaderOrderSame(Feature[] features,
0451: DataStore store) throws IOException,
0452: IllegalAttributeException {
0453: FeatureReader r1 = store.getFeatureReader(new DefaultQuery(
0454: features[0].getFeatureType().getTypeName()),
0455: Transaction.AUTO_COMMIT);
0456: FeatureReader r2 = DataUtilities.reader(features);
0457:
0458: while (r1.hasNext() && r2.hasNext()) {
0459: Feature f1 = r1.next();
0460: Feature f2 = r2.next();
0461: assertEquals(f1, f2);
0462: }
0463:
0464: assertEquals(r1.hasNext(), r2.hasNext());
0465: r1.close();
0466: r2.close();
0467: }
0468:
0469: void assertCovered(Feature[] features, FeatureReader reader)
0470: throws NoSuchElementException, IOException,
0471: IllegalAttributeException {
0472: int count = 0;
0473:
0474: try {
0475: while (reader.hasNext()) {
0476: assertTrue(contains(features, reader.next()));
0477: count++;
0478: }
0479: } finally {
0480: reader.close();
0481: }
0482:
0483: assertEquals(features.length, count);
0484: }
0485:
0486: /**
0487: * Ensure that FeatureReader reader contains extactly the contents of array.
0488: *
0489: * @param reader DOCUMENT ME!
0490: * @param array DOCUMENT ME!
0491: * @return DOCUMENT ME!
0492: * @throws NoSuchElementException DOCUMENT ME!
0493: * @throws IOException DOCUMENT ME!
0494: * @throws IllegalAttributeException DOCUMENT ME!
0495: */
0496: boolean covers(FeatureReader reader, Feature[] array)
0497: throws NoSuchElementException, IOException,
0498: IllegalAttributeException {
0499: Feature feature;
0500: int count = 0;
0501:
0502: try {
0503: while (reader.hasNext()) {
0504: feature = reader.next();
0505:
0506: if (!contains(array, feature)) {
0507: return false;
0508: }
0509:
0510: count++;
0511: }
0512: } finally {
0513: reader.close();
0514: }
0515:
0516: return count == array.length;
0517: }
0518:
0519: boolean covers(FeatureIterator reader, Feature[] array)
0520: throws NoSuchElementException, IOException,
0521: IllegalAttributeException {
0522: Feature feature;
0523: int count = 0;
0524:
0525: try {
0526: while (reader.hasNext()) {
0527: feature = reader.next();
0528:
0529: if (!contains(array, feature)) {
0530: return false;
0531: }
0532:
0533: count++;
0534: }
0535: } finally {
0536: reader.close();
0537: }
0538:
0539: return count == array.length;
0540: }
0541:
0542: boolean coversLax(FeatureReader reader, Feature[] array)
0543: throws NoSuchElementException, IOException,
0544: IllegalAttributeException {
0545: Feature feature;
0546: int count = 0;
0547:
0548: try {
0549: while (reader.hasNext()) {
0550: feature = reader.next();
0551:
0552: if (!containsLax(array, feature)) {
0553: return false;
0554: }
0555:
0556: count++;
0557: }
0558: } finally {
0559: reader.close();
0560: }
0561:
0562: return count == array.length;
0563: }
0564:
0565: boolean coversLax(FeatureIterator reader, Feature[] array)
0566: throws NoSuchElementException, IOException,
0567: IllegalAttributeException {
0568: Feature feature;
0569: int count = 0;
0570:
0571: try {
0572: while (reader.hasNext()) {
0573: feature = reader.next();
0574:
0575: if (!containsLax(array, feature)) {
0576: return false;
0577: }
0578:
0579: count++;
0580: }
0581: } finally {
0582: reader.close();
0583: }
0584:
0585: return count == array.length;
0586: }
0587:
0588: void dump(FeatureReader reader) throws NoSuchElementException,
0589: IOException, IllegalAttributeException {
0590: Feature feature;
0591: int count = 0;
0592:
0593: try {
0594: while (reader.hasNext()) {
0595: feature = reader.next();
0596: System.out.println(count + " feature:" + feature);
0597: count++;
0598: }
0599: } finally {
0600: reader.close();
0601: }
0602: }
0603:
0604: void dump(Object[] array) {
0605: for (int i = 0; i < array.length; i++) {
0606: System.out.println(i + " feature:" + array[i]);
0607: }
0608: }
0609:
0610: /*
0611: * Test for FeatureWriter getFeatureWriter(String, Filter, Transaction)
0612: */
0613: public void testGetFeatureWriter() throws NoSuchElementException,
0614: IOException, IllegalAttributeException {
0615: FeatureWriter writer = data.getFeatureWriter("road",
0616: Transaction.AUTO_COMMIT);
0617: assertEquals(roadFeatures.length, count(writer));
0618:
0619: try {
0620: writer.hasNext();
0621: fail("Should not be able to use a closed writer");
0622: } catch (IOException expected) {
0623: }
0624:
0625: try {
0626: writer.next();
0627: fail("Should not be able to use a closed writer");
0628: } catch (IOException expected) {
0629: }
0630: }
0631:
0632: public void testGetFeatureWriterRemove() throws IOException,
0633: IllegalAttributeException {
0634: FeatureWriter writer = data.getFeatureWriter("road",
0635: Transaction.AUTO_COMMIT);
0636: Feature feature;
0637:
0638: while (writer.hasNext()) {
0639: feature = writer.next();
0640:
0641: if (feature.getID().equals("road.rd1")) {
0642: writer.remove();
0643: }
0644: }
0645:
0646: assertEquals(roadFeatures.length - 1, data.getFeatureSource(
0647: "road").getFeatures().size());
0648: }
0649:
0650: public void testGetFeaturesWriterAdd() throws IOException,
0651: IllegalAttributeException {
0652: FeatureWriter writer = data.getFeatureWriter("road",
0653: Transaction.AUTO_COMMIT);
0654: SimpleFeature feature;
0655:
0656: while (writer.hasNext()) {
0657: feature = (SimpleFeature) writer.next();
0658: }
0659:
0660: assertFalse(writer.hasNext());
0661: feature = (SimpleFeature) writer.next();
0662: feature.setAttributes(newRoad.getAttributes(null));
0663: writer.write();
0664: assertFalse(writer.hasNext());
0665: assertEquals(roadFeatures.length + 1, data.getFeatureSource(
0666: "road").getFeatures().size());
0667: }
0668:
0669: public void testGetFeaturesWriterModify() throws IOException,
0670: IllegalAttributeException {
0671: FeatureWriter writer = data.getFeatureWriter("road",
0672: Transaction.AUTO_COMMIT);
0673: Feature feature;
0674:
0675: while (writer.hasNext()) {
0676: feature = writer.next();
0677:
0678: if (feature.getID().equals("road.rd1")) {
0679: feature.setAttribute("name", "changed");
0680: writer.write();
0681: }
0682: }
0683:
0684: Iterator it = data.getFeatureSource("road").getFeatures(
0685: ff.createFidFilter("road.rd1")).iterator();
0686: assertTrue(it.hasNext());
0687: feature = (Feature) it.next();
0688: assertEquals("changed", feature.getAttribute("name"));
0689: }
0690:
0691: public void testGetFeatureWriterTypeNameTransaction()
0692: throws NoSuchElementException, IOException,
0693: IllegalAttributeException {
0694: FeatureWriter writer;
0695:
0696: writer = data.getFeatureWriter("road", Transaction.AUTO_COMMIT);
0697: assertEquals(roadFeatures.length, count(writer));
0698: writer.close();
0699: }
0700:
0701: public void testGetFeatureWriterAppendTypeNameTransaction()
0702: throws Exception {
0703: FeatureWriter writer;
0704:
0705: writer = data.getFeatureWriterAppend("road",
0706: Transaction.AUTO_COMMIT);
0707: assertEquals(0, count(writer));
0708: writer.close();
0709: }
0710:
0711: /*
0712: * Test for FeatureWriter getFeatureWriter(String, boolean, Transaction)
0713: */
0714: public void testGetFeatureWriterFilter()
0715: throws NoSuchElementException, IOException,
0716: IllegalAttributeException {
0717: FeatureWriter writer;
0718:
0719: writer = data.getFeatureWriter("road", Filter.EXCLUDE,
0720: Transaction.AUTO_COMMIT);
0721: assertTrue(writer instanceof EmptyFeatureWriter);
0722: assertEquals(0, count(writer));
0723:
0724: writer = data.getFeatureWriter("road", Filter.INCLUDE,
0725: Transaction.AUTO_COMMIT);
0726: assertFalse(writer instanceof FilteringFeatureWriter);
0727: assertEquals(roadFeatures.length, count(writer));
0728:
0729: writer = data.getFeatureWriter("road", rd1Filter,
0730: Transaction.AUTO_COMMIT);
0731: assertTrue(writer instanceof FilteringFeatureWriter);
0732: assertEquals(1, count(writer));
0733: }
0734:
0735: /**
0736: * Test two transactions one removing feature, and one adding a feature.
0737: *
0738: * @throws Exception DOCUMENT ME!
0739: */
0740: public void testGetFeatureWriterTransaction() throws Exception {
0741: Transaction t1 = new DefaultTransaction();
0742: Transaction t2 = new DefaultTransaction();
0743: FeatureWriter writer1 = data.getFeatureWriter("road",
0744: rd1Filter, t1);
0745: FeatureWriter writer2 = data.getFeatureWriterAppend("road", t2);
0746:
0747: FeatureType road = data.getSchema("road");
0748: FeatureReader reader;
0749: SimpleFeature feature;
0750: SimpleFeature[] ORIGIONAL = roadFeatures;
0751: Feature[] REMOVE = new Feature[ORIGIONAL.length - 1];
0752: Feature[] ADD = new Feature[ORIGIONAL.length + 1];
0753: Feature[] FINAL = new Feature[ORIGIONAL.length];
0754: int i;
0755: int index;
0756: index = 0;
0757:
0758: for (i = 0; i < ORIGIONAL.length; i++) {
0759: feature = ORIGIONAL[i];
0760:
0761: if (!feature.getID().equals("road.rd1")) {
0762: REMOVE[index++] = feature;
0763: }
0764: }
0765:
0766: for (i = 0; i < ORIGIONAL.length; i++) {
0767: ADD[i] = ORIGIONAL[i];
0768: }
0769:
0770: ADD[i] = newRoad;
0771:
0772: for (i = 0; i < REMOVE.length; i++) {
0773: FINAL[i] = REMOVE[i];
0774: }
0775:
0776: FINAL[i] = newRoad;
0777:
0778: // start of with ORIGINAL
0779: reader = data.getFeatureReader(new DefaultQuery("road"),
0780: Transaction.AUTO_COMMIT);
0781: assertTrue(covers(reader, ORIGIONAL));
0782:
0783: // writer 1 removes road.rd1 on t1
0784: // -------------------------------
0785: // - tests transaction independence from DataStore
0786: while (writer1.hasNext()) {
0787: feature = (SimpleFeature) writer1.next();
0788: assertEquals("road.rd1", feature.getID());
0789: writer1.remove();
0790: }
0791:
0792: // still have ORIGIONAL and t1 has REMOVE
0793: reader = data.getFeatureReader(new DefaultQuery("road"),
0794: Transaction.AUTO_COMMIT);
0795: assertTrue(covers(reader, ORIGIONAL));
0796: reader = data.getFeatureReader(new DefaultQuery("road"), t1);
0797: assertTrue(covers(reader, REMOVE));
0798:
0799: // close writer1
0800: // --------------
0801: // ensure that modification is left up to transaction commmit
0802: writer1.close();
0803:
0804: // We still have ORIGIONAL and t1 has REMOVE
0805: reader = data.getFeatureReader(new DefaultQuery("road"),
0806: Transaction.AUTO_COMMIT);
0807: assertTrue(covers(reader, ORIGIONAL));
0808: reader = data.getFeatureReader(new DefaultQuery("road"), t1);
0809: assertTrue(covers(reader, REMOVE));
0810:
0811: // writer 2 adds road.rd4 on t2
0812: // ----------------------------
0813: // - tests transaction independence from each other
0814: feature = (SimpleFeature) writer2.next();
0815: feature.setAttributes(newRoad.getAttributes(null));
0816: writer2.write();
0817:
0818: // We still have ORIGIONAL and t2 has ADD
0819: reader = data.getFeatureReader(new DefaultQuery("road"),
0820: Transaction.AUTO_COMMIT);
0821: assertTrue(covers(reader, ORIGIONAL));
0822: reader = data.getFeatureReader(new DefaultQuery("road"), t2);
0823: assertTrue(coversLax(reader, ADD));
0824:
0825: // close writer2
0826: // -------------
0827: // ensure that modification is left up to transaction commmit
0828: writer2.close();
0829:
0830: // Still have ORIGIONAL and t2 has ADD
0831: reader = data.getFeatureReader(new DefaultQuery("road"),
0832: Transaction.AUTO_COMMIT);
0833: assertTrue(covers(reader, ORIGIONAL));
0834: reader = data.getFeatureReader(new DefaultQuery("road"), t2);
0835: assertTrue(coversLax(reader, ADD));
0836:
0837: // commit t1
0838: // ---------
0839: // -ensure that delayed writing of transactions takes place
0840: //
0841: t1.commit();
0842:
0843: // We now have REMOVE, as does t1 (which has not additional diffs)
0844: // t2 will have FINAL
0845: reader = data.getFeatureReader(new DefaultQuery("road"),
0846: Transaction.AUTO_COMMIT);
0847: assertTrue(covers(reader, REMOVE));
0848: reader = data.getFeatureReader(new DefaultQuery("road"), t1);
0849: assertTrue(covers(reader, REMOVE));
0850: reader = data.getFeatureReader(new DefaultQuery("road"), t2);
0851: assertTrue(coversLax(reader, FINAL));
0852:
0853: // commit t2
0854: // ---------
0855: // -ensure that everyone is FINAL at the end of the day
0856: t2.commit();
0857:
0858: // We now have Number( remove one and add one)
0859: reader = data.getFeatureReader(new DefaultQuery("road"),
0860: Transaction.AUTO_COMMIT);
0861: reader = data.getFeatureReader(new DefaultQuery("road"),
0862: Transaction.AUTO_COMMIT);
0863: assertTrue(coversLax(reader, FINAL));
0864: reader = data.getFeatureReader(new DefaultQuery("road"), t1);
0865: assertTrue(coversLax(reader, FINAL));
0866: reader = data.getFeatureReader(new DefaultQuery("road"), t2);
0867: assertTrue(coversLax(reader, FINAL));
0868: }
0869:
0870: /**
0871: * Test the transaction when multiple edits occur using a transaction and a fid filter.
0872: */
0873: public void testModifyInTransactionFidFilter() throws Exception {
0874: Transaction t1 = new DefaultTransaction();
0875:
0876: GeometryFactory fac = new GeometryFactory();
0877:
0878: FeatureWriter writer1 = data.getFeatureWriter("road",
0879: rd1Filter, t1);
0880: writer1.next().setDefaultGeometry(
0881: fac.createLineString(new Coordinate[] {
0882: new Coordinate(0, 0), new Coordinate(0, 1) }));
0883: writer1.write();
0884:
0885: writer1.close();
0886:
0887: FeatureReader reader = data.getFeatureReader(new DefaultQuery(
0888: "road", rd1Filter), t1);
0889: Geometry geom1 = reader.next().getDefaultGeometry();
0890: reader.close();
0891: assertEquals(new Coordinate(0, 0), geom1.getCoordinates()[0]);
0892: assertEquals(new Coordinate(0, 1), geom1.getCoordinates()[1]);
0893:
0894: writer1 = data.getFeatureWriter("road", rd1Filter, t1);
0895: writer1.next()
0896: .setDefaultGeometry(
0897: fac.createLineString(new Coordinate[] {
0898: new Coordinate(10, 0),
0899: new Coordinate(10, 1) }));
0900: writer1.write();
0901: writer1.close();
0902:
0903: reader = data.getFeatureReader(new DefaultQuery("road",
0904: rd1Filter), t1);
0905: geom1 = reader.next().getDefaultGeometry();
0906: reader.close();
0907: assertEquals(new Coordinate(10, 0), geom1.getCoordinates()[0]);
0908: assertEquals(new Coordinate(10, 1), geom1.getCoordinates()[1]);
0909:
0910: FeatureWriter writer = data.getFeatureWriterAppend("road", t1);
0911: Feature feature = writer.next();
0912: feature
0913: .setDefaultGeometry(fac
0914: .createLineString(new Coordinate[] {
0915: new Coordinate(20, 0),
0916: new Coordinate(20, 1) }));
0917: writer.write();
0918: writer.close();
0919:
0920: FidFilter filter = FilterFactoryFinder.createFilterFactory()
0921: .createFidFilter(feature.getID());
0922:
0923: reader = data.getFeatureReader(
0924: new DefaultQuery("road", filter), t1);
0925: geom1 = reader.next().getDefaultGeometry();
0926: reader.close();
0927: assertEquals(new Coordinate(20, 0), geom1.getCoordinates()[0]);
0928: assertEquals(new Coordinate(20, 1), geom1.getCoordinates()[1]);
0929:
0930: writer1 = data.getFeatureWriter("road", filter, t1);
0931: writer1.next()
0932: .setDefaultGeometry(
0933: fac.createLineString(new Coordinate[] {
0934: new Coordinate(30, 0),
0935: new Coordinate(30, 1) }));
0936: writer1.write();
0937: writer1.close();
0938:
0939: reader = data.getFeatureReader(
0940: new DefaultQuery("road", filter), t1);
0941: geom1 = reader.next().getDefaultGeometry();
0942: reader.close();
0943: assertEquals(new Coordinate(30, 0), geom1.getCoordinates()[0]);
0944: assertEquals(new Coordinate(30, 1), geom1.getCoordinates()[1]);
0945: }
0946:
0947: // Feature Source Testing
0948: public void testGetFeatureSourceRoad() throws IOException {
0949: FeatureSource road = data.getFeatureSource("road");
0950:
0951: assertSame(roadType, road.getSchema());
0952: assertSame(data, road.getDataStore());
0953: assertEquals(3, road.getCount(Query.ALL));
0954: assertEquals(new Envelope(1, 5, 0, 4), road
0955: .getBounds(Query.ALL));
0956:
0957: FeatureCollection all = road.getFeatures();
0958: assertEquals(3, all.size());
0959: assertEquals(roadBounds, all.getBounds());
0960:
0961: FeatureCollection expected = DataUtilities
0962: .collection(roadFeatures);
0963:
0964: assertCovers("all", expected, all);
0965: assertEquals(roadBounds, all.getBounds());
0966:
0967: FeatureCollection some = road.getFeatures(rd12Filter);
0968: assertEquals(2, some.size());
0969: assertEquals(rd12Bounds, some.getBounds());
0970: assertEquals(some.getSchema(), road.getSchema());
0971:
0972: DefaultQuery query = new DefaultQuery("road", rd12Filter,
0973: new String[] { "name", });
0974:
0975: FeatureCollection half = road.getFeatures(query);
0976: assertEquals(2, half.size());
0977: assertEquals(1, half.getSchema().getAttributeCount());
0978:
0979: FeatureIterator reader = half.features();
0980: FeatureType type = half.getSchema();
0981: reader.close();
0982:
0983: FeatureType actual = half.getSchema();
0984:
0985: assertEquals(type.getTypeName(), actual.getTypeName());
0986: assertEquals(type.getNamespace(), actual.getNamespace());
0987: assertEquals(type.getAttributeCount(), actual
0988: .getAttributeCount());
0989:
0990: for (int i = 0; i < type.getAttributeCount(); i++) {
0991: assertEquals(type.getAttributeType(i), actual
0992: .getAttributeType(i));
0993: }
0994:
0995: assertNull(type.getDefaultGeometry());
0996: assertEquals(type.getDefaultGeometry(), actual
0997: .getDefaultGeometry());
0998: assertEquals(type, actual);
0999:
1000: Envelope b = half.getBounds();
1001: assertEquals(new Envelope(1, 5, 0, 4), b);
1002: }
1003:
1004: public void testGetFeatureSourceRiver()
1005: throws NoSuchElementException, IOException,
1006: IllegalAttributeException {
1007: FeatureSource river = data.getFeatureSource("river");
1008:
1009: assertSame(riverType, river.getSchema());
1010: assertSame(data, river.getDataStore());
1011:
1012: FeatureCollection all = river.getFeatures();
1013: assertEquals(2, all.size());
1014: assertEquals(riverBounds, all.getBounds());
1015: assertTrue("rivers", covers(all.features(), riverFeatures));
1016:
1017: FeatureCollection expected = DataUtilities
1018: .collection(riverFeatures);
1019: assertCovers("all", expected, all);
1020: assertEquals(riverBounds, all.getBounds());
1021: }
1022:
1023: //
1024: // Feature Store Testing
1025: //
1026: public void testGetFeatureStoreModifyFeatures1() throws IOException {
1027: FeatureStore road = (FeatureStore) data
1028: .getFeatureSource("road");
1029: AttributeType name = roadType.getAttributeType("name");
1030: road.modifyFeatures(name, "changed", rd1Filter);
1031:
1032: FeatureCollection results = road.getFeatures(rd1Filter);
1033: assertEquals("changed", results.features().next().getAttribute(
1034: "name"));
1035: }
1036:
1037: public void testGetFeatureStoreModifyFeatures2() throws IOException {
1038: FeatureStore road = (FeatureStore) data
1039: .getFeatureSource("road");
1040: AttributeType name = roadType.getAttributeType("name");
1041: road.modifyFeatures(new AttributeType[] { name, },
1042: new Object[] { "changed", }, rd1Filter);
1043:
1044: FeatureCollection results = road.getFeatures(rd1Filter);
1045: assertEquals("changed", results.features().next().getAttribute(
1046: "name"));
1047: }
1048:
1049: public void testGetFeatureStoreRemoveFeatures() throws IOException {
1050: FeatureStore road = (FeatureStore) data
1051: .getFeatureSource("road");
1052:
1053: road.removeFeatures(rd1Filter);
1054: assertEquals(0, road.getFeatures(rd1Filter).size());
1055: assertEquals(roadFeatures.length - 1, road.getFeatures().size());
1056: }
1057:
1058: public void testGetFeatureStoreAddFeatures() throws IOException {
1059: FeatureReader reader = DataUtilities
1060: .reader(new Feature[] { newRoad, });
1061: FeatureStore road = (FeatureStore) data
1062: .getFeatureSource("road");
1063:
1064: road.addFeatures(DataUtilities.collection(reader));
1065: assertEquals(roadFeatures.length + 1, road.getFeatures().size());
1066: }
1067:
1068: public void testGetFeatureStoreSetFeatures() throws IOException {
1069: FeatureReader reader = DataUtilities
1070: .reader(new Feature[] { newRoad, });
1071: FeatureStore road = (FeatureStore) data
1072: .getFeatureSource("road");
1073:
1074: road.setFeatures(reader);
1075: assertEquals(1, road.getFeatures().size());
1076: }
1077:
1078: public void testGetFeatureStoreTransactionSupport()
1079: throws Exception {
1080: Transaction t1 = new DefaultTransaction();
1081: Transaction t2 = new DefaultTransaction();
1082:
1083: FeatureStore road = (FeatureStore) data
1084: .getFeatureSource("road");
1085: FeatureStore road1 = (FeatureStore) data
1086: .getFeatureSource("road");
1087: FeatureStore road2 = (FeatureStore) data
1088: .getFeatureSource("road");
1089:
1090: road1.setTransaction(t1);
1091: road2.setTransaction(t2);
1092:
1093: Feature feature;
1094: Feature[] ORIGIONAL = roadFeatures;
1095: Feature[] REMOVE = new Feature[ORIGIONAL.length - 1];
1096: Feature[] ADD = new Feature[ORIGIONAL.length + 1];
1097: Feature[] FINAL = new Feature[ORIGIONAL.length];
1098: int i;
1099: int index;
1100: index = 0;
1101:
1102: for (i = 0; i < ORIGIONAL.length; i++) {
1103: feature = ORIGIONAL[i];
1104:
1105: if (!feature.getID().equals("road.rd1")) {
1106: REMOVE[index++] = feature;
1107: }
1108: }
1109:
1110: for (i = 0; i < ORIGIONAL.length; i++) {
1111: ADD[i] = ORIGIONAL[i];
1112: }
1113:
1114: ADD[i] = newRoad;
1115:
1116: for (i = 0; i < REMOVE.length; i++) {
1117: FINAL[i] = REMOVE[i];
1118: }
1119:
1120: FINAL[i] = newRoad;
1121:
1122: // start of with ORIGINAL
1123: assertTrue(covers(road.getFeatures().features(), ORIGIONAL));
1124:
1125: // road1 removes road.rd1 on t1
1126: // -------------------------------
1127: // - tests transaction independence from DataStore
1128: road1.removeFeatures(rd1Filter);
1129:
1130: // still have ORIGIONAL and t1 has REMOVE
1131: assertTrue(covers(road.getFeatures().features(), ORIGIONAL));
1132: assertTrue(covers(road1.getFeatures().features(), REMOVE));
1133:
1134: // road2 adds road.rd4 on t2
1135: // ----------------------------
1136: // - tests transaction independence from each other
1137: FeatureReader reader = DataUtilities
1138: .reader(new Feature[] { newRoad, });
1139: road2.addFeatures(DataUtilities.collection(reader));
1140:
1141: // We still have ORIGIONAL, t1 has REMOVE, and t2 has ADD
1142: assertTrue(covers(road.getFeatures().features(), ORIGIONAL));
1143: assertTrue(covers(road1.getFeatures().features(), REMOVE));
1144: assertTrue(coversLax(road2.getFeatures().features(), ADD));
1145:
1146: // commit t1
1147: // ---------
1148: // -ensure that delayed writing of transactions takes place
1149: //
1150: t1.commit();
1151:
1152: // We now have REMOVE, as does t1 (which has not additional diffs)
1153: // t2 will have FINAL
1154: assertTrue(covers(road.getFeatures().features(), REMOVE));
1155: assertTrue(covers(road1.getFeatures().features(), REMOVE));
1156: assertTrue(coversLax(road2.getFeatures().features(), FINAL));
1157:
1158: // commit t2
1159: // ---------
1160: // -ensure that everyone is FINAL at the end of the day
1161: t2.commit();
1162:
1163: // We now have Number( remove one and add one)
1164: assertTrue(coversLax(road.getFeatures().features(), FINAL));
1165: assertTrue(coversLax(road1.getFeatures().features(), FINAL));
1166: assertTrue(coversLax(road2.getFeatures().features(), FINAL));
1167: }
1168:
1169: boolean isLocked(String typeName, String fid) {
1170: InProcessLockingManager lockingManager = (InProcessLockingManager) data
1171: .getLockingManager();
1172:
1173: return lockingManager.isLocked(typeName, fid);
1174: }
1175:
1176: public void ztestFeatureEvents() throws Exception {
1177: FeatureStore store1 = (FeatureStore) data
1178: .getFeatureSource(roadFeatures[0].getFeatureType()
1179: .getTypeName());
1180: FeatureStore store2 = (FeatureStore) data
1181: .getFeatureSource(roadFeatures[0].getFeatureType()
1182: .getTypeName());
1183: store1.setTransaction(new DefaultTransaction());
1184: class Listener implements FeatureListener {
1185: String name;
1186: List events = new ArrayList();
1187:
1188: public Listener(String name) {
1189: this .name = name;
1190: }
1191:
1192: public void changed(FeatureEvent featureEvent) {
1193: this .events.add(featureEvent);
1194: }
1195:
1196: FeatureEvent getEvent(int i) {
1197: return (FeatureEvent) events.get(i);
1198: }
1199: }
1200:
1201: Listener listener1 = new Listener("one");
1202: Listener listener2 = new Listener("two");
1203:
1204: store1.addFeatureListener(listener1);
1205: store2.addFeatureListener(listener2);
1206:
1207: FilterFactory factory = FilterFactoryFinder
1208: .createFilterFactory();
1209:
1210: // test that only the listener listening with the current transaction gets the event.
1211: final Feature feature = roadFeatures[0];
1212: store1.removeFeatures(factory.createFidFilter(feature.getID()));
1213: assertEquals(1, listener1.events.size());
1214: assertEquals(0, listener2.events.size());
1215:
1216: FeatureEvent event = listener1.getEvent(0);
1217: assertEquals(feature.getBounds(), event.getBounds());
1218: assertEquals(FeatureEvent.FEATURES_REMOVED, event
1219: .getEventType());
1220:
1221: // test that commit only sends events to listener2.
1222: listener1.events.clear();
1223: listener2.events.clear();
1224:
1225: store1.getTransaction().commit();
1226:
1227: assertEquals(0, listener1.events.size());
1228:
1229: // changed 3 to 2
1230: assertEquals(2, listener2.events.size());
1231: event = listener2.getEvent(0);
1232: assertEquals(feature.getBounds(), event.getBounds());
1233: assertEquals(FeatureEvent.FEATURES_REMOVED, event
1234: .getEventType());
1235:
1236: // test add same as modify
1237: listener1.events.clear();
1238: listener2.events.clear();
1239:
1240: store1.addFeatures(DataUtilities.collection(feature));
1241:
1242: assertEquals(1, listener1.events.size());
1243: event = listener1.getEvent(0);
1244: assertEquals(feature.getBounds(), event.getBounds());
1245: assertEquals(FeatureEvent.FEATURES_ADDED, event.getEventType());
1246: assertEquals(0, listener2.events.size());
1247:
1248: // test that rollback only sends events to listener1.
1249: listener1.events.clear();
1250: listener2.events.clear();
1251:
1252: store1.getTransaction().rollback();
1253:
1254: assertEquals(1, listener1.events.size());
1255: event = listener1.getEvent(0);
1256: assertNull(event.getBounds());
1257: assertEquals(FeatureEvent.FEATURES_CHANGED, event
1258: .getEventType());
1259:
1260: assertEquals(0, listener2.events.size());
1261:
1262: // this is how Auto_commit is supposed to work
1263: listener1.events.clear();
1264: listener2.events.clear();
1265: store2.addFeatures(DataUtilities.collection(feature));
1266:
1267: // ???
1268: assertEquals(1, listener1.events.size());
1269: event = listener1.getEvent(0);
1270: assertEquals(feature.getBounds(), event.getBounds());
1271: assertEquals(FeatureEvent.FEATURES_ADDED, event.getEventType());
1272: assertEquals(0, listener2.events.size());
1273: }
1274:
1275: //
1276: // FeatureLocking Testing
1277: //
1278: /*
1279: * Test for void lockFeatures()
1280: */
1281: public void testLockFeatures() throws IOException {
1282: FeatureLock lock = FeatureLockFactory.generate("test", 3600);
1283: FeatureLocking road = (FeatureLocking) data
1284: .getFeatureSource("road");
1285: road.setFeatureLock(lock);
1286:
1287: assertFalse(isLocked("road", "road.rd1"));
1288: road.lockFeatures();
1289: assertTrue(isLocked("road", "road.rd1"));
1290: }
1291:
1292: public void testUnLockFeatures() throws IOException {
1293: FeatureLock lock = FeatureLockFactory.generate("test", 3600);
1294: FeatureLocking road = (FeatureLocking) data
1295: .getFeatureSource("road");
1296: road.setFeatureLock(lock);
1297: road.lockFeatures();
1298:
1299: try {
1300: road.unLockFeatures();
1301: fail("unlock should fail due on AUTO_COMMIT");
1302: } catch (IOException expected) {
1303: }
1304:
1305: Transaction t = new DefaultTransaction();
1306: road.setTransaction(t);
1307:
1308: try {
1309: road.unLockFeatures();
1310: fail("unlock should fail due lack of authorization");
1311: } catch (IOException expected) {
1312: }
1313:
1314: t.addAuthorization(lock.getAuthorization());
1315: road.unLockFeatures();
1316: }
1317:
1318: public void testLockFeatureInteraction() throws IOException {
1319: FeatureLock lockA = FeatureLockFactory.generate("LockA", 3600);
1320: FeatureLock lockB = FeatureLockFactory.generate("LockB", 3600);
1321: Transaction t1 = new DefaultTransaction();
1322: Transaction t2 = new DefaultTransaction();
1323: FeatureLocking road1 = (FeatureLocking) data
1324: .getFeatureSource("road");
1325: FeatureLocking road2 = (FeatureLocking) data
1326: .getFeatureSource("road");
1327: road1.setTransaction(t1);
1328: road2.setTransaction(t2);
1329: road1.setFeatureLock(lockA);
1330: road2.setFeatureLock(lockB);
1331:
1332: assertFalse(isLocked("road", "road.rd1"));
1333: assertFalse(isLocked("road", "road.rd2"));
1334: assertFalse(isLocked("road", "road.rd3"));
1335:
1336: road1.lockFeatures(rd1Filter);
1337: assertTrue(isLocked("road", "road.rd1"));
1338: assertFalse(isLocked("road", "road.rd2"));
1339: assertFalse(isLocked("road", "road.rd3"));
1340:
1341: road2.lockFeatures(rd2Filter);
1342: assertTrue(isLocked("road", "road.rd1"));
1343: assertTrue(isLocked("road", "road.rd2"));
1344: assertFalse(isLocked("road", "road.rd3"));
1345:
1346: try {
1347: road1.unLockFeatures(rd1Filter);
1348: fail("need authorization");
1349: } catch (IOException expected) {
1350: }
1351:
1352: t1.addAuthorization(lockA.getAuthorization());
1353:
1354: try {
1355: road1.unLockFeatures(rd2Filter);
1356: fail("need correct authorization");
1357: } catch (IOException expected) {
1358: }
1359:
1360: road1.unLockFeatures(rd1Filter);
1361: assertFalse(isLocked("road", "road.rd1"));
1362: assertTrue(isLocked("road", "road.rd2"));
1363: assertFalse(isLocked("road", "road.rd3"));
1364:
1365: t2.addAuthorization(lockB.getAuthorization());
1366: road2.unLockFeatures(rd2Filter);
1367: assertFalse(isLocked("road", "road.rd1"));
1368: assertFalse(isLocked("road", "road.rd2"));
1369: assertFalse(isLocked("road", "road.rd3"));
1370: }
1371:
1372: public void testGetFeatureLockingExpire() throws Exception {
1373: FeatureLock lock = FeatureLockFactory.generate("Timed", 1);
1374: FeatureLocking road = (FeatureLocking) data
1375: .getFeatureSource("road");
1376: road.setFeatureLock(lock);
1377: assertFalse(isLocked("road", "road.rd1"));
1378: road.lockFeatures(rd1Filter);
1379: assertTrue(isLocked("road", "road.rd1"));
1380: Thread.sleep(100);
1381: assertFalse(isLocked("road", "road.rd1"));
1382: }
1383: }
|