Source Code Cross Referenced for MemoryDataStoreTest.java in  » GIS » GeoTools-2.4.1 » org » geotools » caching » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » GeoTools 2.4.1 » org.geotools.caching 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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