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