Source Code Cross Referenced for ResultSetJdbcGpsDeviceTests.java in  » Search-Engine » compass-2.0 » org » compass » gps » device » jdbc » 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 » Search Engine » compass 2.0 » org.compass.gps.device.jdbc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2006 the original author or authors.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.compass.gps.device.jdbc;
018:
019:        import java.sql.Connection;
020:        import java.sql.PreparedStatement;
021:
022:        import org.compass.core.Compass;
023:        import org.compass.core.CompassDetachedHits;
024:        import org.compass.core.CompassTemplate;
025:        import org.compass.core.Resource;
026:        import org.compass.core.config.CompassConfiguration;
027:        import org.compass.core.config.CompassEnvironment;
028:        import org.compass.gps.device.jdbc.mapping.DataColumnToPropertyMapping;
029:        import org.compass.gps.device.jdbc.mapping.IdColumnToPropertyMapping;
030:        import org.compass.gps.device.jdbc.mapping.ResultSetToResourceMapping;
031:        import org.compass.gps.device.jdbc.mapping.VersionColumnMapping;
032:        import org.compass.gps.impl.SingleCompassGps;
033:
034:        /**
035:         * @author kimchy
036:         */
037:        public class ResultSetJdbcGpsDeviceTests extends
038:                AbstractJdbcGpsDeviceTests {
039:
040:            protected Compass compass;
041:
042:            protected CompassTemplate compassTemplate;
043:
044:            private ResultSetJdbcGpsDevice gpsDevice;
045:
046:            private SingleCompassGps gps;
047:
048:            protected void tearDown() throws Exception {
049:                if (gps != null) {
050:                    gps.stop();
051:                }
052:                if (compass != null) {
053:                    compass.close();
054:                }
055:                super .tearDown();
056:            }
057:
058:            protected void setUpExactMappingNoMirror() throws Exception {
059:                // set up the database mappings, since they are used both to generate
060:                // the resource mappings and configure the jdbc gps device
061:                ResultSetToResourceMapping mapping = new ResultSetToResourceMapping();
062:                mapping.setAlias("result-set");
063:                mapping
064:                        .setSelectQuery("select "
065:                                + "p.id as parent_id, p.first_name as parent_first_name, p.last_name as parent_last_name, "
066:                                + "c.id as child_id, c.first_name as child_first_name, c.last_name child_last_name "
067:                                + "from parent p left join child c on p.id = c.parent_id");
068:                mapping.addIdMapping(new IdColumnToPropertyMapping("parent_id",
069:                        "parent_id"));
070:                mapping.addIdMapping(new IdColumnToPropertyMapping("child_id",
071:                        "child_id"));
072:                mapping.addDataMapping(new DataColumnToPropertyMapping(
073:                        "parent_first_name", "parent_first_name"));
074:                mapping.addDataMapping(new DataColumnToPropertyMapping(
075:                        "parent_first_name", "first_name"));
076:                mapping.addDataMapping(new DataColumnToPropertyMapping(
077:                        "child_first_name", "child_first_name"));
078:                mapping.addDataMapping(new DataColumnToPropertyMapping(
079:                        "child_first_name", "first_name"));
080:
081:                CompassConfiguration conf = new CompassConfiguration()
082:                        .setSetting(CompassEnvironment.CONNECTION,
083:                                "target/test-index");
084:                conf.addMappingResover(new ResultSetResourceMappingResolver(
085:                        mapping, dataSource));
086:                compass = conf.buildCompass();
087:                compass.getSearchEngineIndexManager().deleteIndex();
088:                compass.getSearchEngineIndexManager().verifyIndex();
089:
090:                compassTemplate = new CompassTemplate(compass);
091:
092:                gpsDevice = new ResultSetJdbcGpsDevice();
093:                gpsDevice.setDataSource(dataSource);
094:                gpsDevice.setName("resultSetJdbcDevice");
095:                // setting up no mirroring, even though it should not mirror since we
096:                // mapped no version columns
097:                gpsDevice.setMirrorDataChanges(false);
098:                gpsDevice.addMapping(mapping);
099:
100:                gps = new SingleCompassGps(compass);
101:                gps.addGpsDevice(gpsDevice);
102:                gps.start();
103:            }
104:
105:            public void testExactMappingNoMirror() throws Exception {
106:                setUpExactMappingNoMirror();
107:                gps.index();
108:                Resource r = compassTemplate
109:                        .getResource("result-set", "1", "1");
110:                assertNotNull(r.getProperty("parent_id"));
111:                assertNotNull(r.getProperty("parent_first_name"));
112:                assertNotNull(r.getProperty("child_id"));
113:                assertNotNull(r.getProperty("child_first_name"));
114:                assertNotNull(r.getProperty("first_name"));
115:                assertNull(r.getProperty("ID"));
116:                assertNull(r.getProperty("FIRST_NAME"));
117:                assertNull(r.getProperty("LAST_NAME"));
118:                assertNotNull(r);
119:                r = compassTemplate.getResource("result-set", "4", "6");
120:                assertNotNull(r);
121:                CompassDetachedHits hits = compassTemplate
122:                        .findWithDetach("parent");
123:                assertEquals(6, hits.getLength());
124:            }
125:
126:            protected void setUpUnmappedMappingNoMirror() throws Exception {
127:                // set up the database mappings, since they are used both to generate
128:                // the resource mappings and configure the jdbc gps device
129:                ResultSetToResourceMapping mapping = new ResultSetToResourceMapping(
130:                        "result-set",
131:                        "select * from parent p left join child c on p.id = c.parent_id");
132:                mapping.setIndexUnMappedColumns(true);
133:                mapping.addIdMapping(new IdColumnToPropertyMapping(1,
134:                        "parent_id"));
135:                mapping.addIdMapping(new IdColumnToPropertyMapping(5,
136:                        "child_id"));
137:                mapping.addDataMapping(new DataColumnToPropertyMapping(2,
138:                        "parent_first_name"));
139:                mapping.addDataMapping(new DataColumnToPropertyMapping(7,
140:                        "child_first_name"));
141:
142:                CompassConfiguration conf = new CompassConfiguration()
143:                        .setSetting(CompassEnvironment.CONNECTION,
144:                                "target/test-index");
145:                conf.addMappingResover(new ResultSetResourceMappingResolver(
146:                        mapping, this .dataSource));
147:                compass = conf.buildCompass();
148:                compass.getSearchEngineIndexManager().deleteIndex();
149:                compass.getSearchEngineIndexManager().verifyIndex();
150:
151:                compassTemplate = new CompassTemplate(compass);
152:
153:                gpsDevice = new ResultSetJdbcGpsDevice();
154:                gpsDevice.setDataSource(dataSource);
155:                gpsDevice.setName("resultSetJdbcDevice");
156:                // it should not mirror the data since we did not mapped any version
157:                // columns
158:                gpsDevice.setMirrorDataChanges(true);
159:                gpsDevice.addMapping(mapping);
160:
161:                gps = new SingleCompassGps(compass);
162:                gps.addGpsDevice(gpsDevice);
163:                gps.start();
164:            }
165:
166:            public void testUnmappedMappingNoMirror() throws Exception {
167:                setUpUnmappedMappingNoMirror();
168:                gps.index();
169:                Resource r = compassTemplate
170:                        .getResource("result-set", "1", "1");
171:                assertNotNull(r);
172:                assertNotNull(r.getProperty("parent_id"));
173:                assertNotNull(r.getProperty("parent_first_name"));
174:                assertNotNull(r.getProperty("child_id"));
175:                assertNotNull(r.getProperty("child_first_name"));
176:                assertNotNull(r.getProperty("LAST_NAME"));
177:                assertNull(r.getProperty("FIRST_NAME"));
178:                r = compassTemplate.getResource("result-set", "4", "6");
179:                assertNotNull(r);
180:                CompassDetachedHits hits = compassTemplate
181:                        .findWithDetach("parent");
182:                assertEquals(6, hits.getLength());
183:            }
184:
185:            protected void setUpExactMappingWithMirror() throws Exception {
186:                // set up the database mappings, since they are used both to generate
187:                // the resource mappings and configure the jdbc gps device
188:                ResultSetToResourceMapping mapping = new ResultSetToResourceMapping();
189:                mapping.setAlias("result-set");
190:                mapping
191:                        .setSelectQuery("select "
192:                                + "p.id as parent_id, p.first_name as parent_first_name, p.last_name as parent_last_name, p.version as parent_version, "
193:                                + "COALESCE(c.id, 0) as child_id, c.first_name as child_first_name, c.last_name child_last_name, COALESCE(c.version, 0) as child_version "
194:                                + "from parent p left join child c on p.id = c.parent_id");
195:                mapping
196:                        .setVersionQuery("select p.id as parent_id, COALESCE(c.id, 0) as child_id, p.version as parent_version, COALESCE(c.version, 0) as child_version from parent p left join child c on p.id = c.parent_id");
197:                mapping.addIdMapping(new IdColumnToPropertyMapping("parent_id",
198:                        "parent_id", "p.id"));
199:                mapping.addIdMapping(new IdColumnToPropertyMapping("child_id",
200:                        "child_id", "COALESCE(c.id, 0)"));
201:                mapping.addDataMapping(new DataColumnToPropertyMapping(
202:                        "parent_first_name", "parent_first_name"));
203:                mapping.addDataMapping(new DataColumnToPropertyMapping(
204:                        "child_first_name", "child_first_name"));
205:                mapping.addVersionMapping(new VersionColumnMapping(
206:                        "parent_version"));
207:                mapping.addVersionMapping(new VersionColumnMapping(
208:                        "child_version"));
209:
210:                CompassConfiguration conf = new CompassConfiguration()
211:                        .setSetting(CompassEnvironment.CONNECTION,
212:                                "target/testindex");
213:                conf.addMappingResover(new ResultSetResourceMappingResolver(
214:                        mapping, this .dataSource));
215:                compass = conf.buildCompass();
216:                compass.getSearchEngineIndexManager().deleteIndex();
217:                compass.getSearchEngineIndexManager().verifyIndex();
218:
219:                compassTemplate = new CompassTemplate(compass);
220:
221:                gpsDevice = new ResultSetJdbcGpsDevice();
222:                gpsDevice.setDataSource(dataSource);
223:                gpsDevice.setName("resultSetJdbcDevice");
224:                gpsDevice.setMirrorDataChanges(true);
225:                gpsDevice.addMapping(mapping);
226:
227:                gps = new SingleCompassGps(compass);
228:                gps.addGpsDevice(gpsDevice);
229:                gps.start();
230:            }
231:
232:            public void testExactMappingWithMirrorMockEvent() throws Exception {
233:                setUpExactMappingWithMirror();
234:                MockSnapshotEventListener eventListener = new MockSnapshotEventListener();
235:                gpsDevice.setSnapshotEventListener(eventListener);
236:                gps.index();
237:                compassTemplate.loadResource("result-set", "1", "1");
238:                Resource r = compassTemplate
239:                        .getResource("result-set", "4", "6");
240:                assertNotNull(r);
241:                CompassDetachedHits hits = compassTemplate
242:                        .findWithDetach("parent");
243:                assertEquals(6, hits.getLength());
244:
245:                eventListener.clear();
246:                gpsDevice.performMirroring();
247:                assertFalse(eventListener.isCreateAndUpdateCalled());
248:                assertFalse(eventListener.isDeleteCalled());
249:
250:                // test that create works
251:                Connection con = JdbcUtils.getConnection(dataSource);
252:                PreparedStatement ps = con
253:                        .prepareStatement("INSERT INTO parent VALUES (999, 'parent first 999', 'last 999', 1);");
254:                ps.execute();
255:                ps.close();
256:                con.commit();
257:                con.close();
258:
259:                eventListener.clear();
260:                gpsDevice.performMirroring();
261:                assertTrue(eventListener.isCreateAndUpdateCalled());
262:                assertTrue(eventListener.isCreateHappen());
263:                assertFalse(eventListener.isUpdateHappen());
264:                assertFalse(eventListener.isDeleteCalled());
265:
266:                // test that update parent works
267:                con = JdbcUtils.getConnection(dataSource);
268:                ps = con
269:                        .prepareStatement("update parent set first_name = 'new first name', version = 2 where id = 1");
270:                ps.execute();
271:                ps.close();
272:                con.commit();
273:                con.close();
274:
275:                eventListener.clear();
276:                gpsDevice.performMirroring();
277:                assertTrue(eventListener.isCreateAndUpdateCalled());
278:                assertTrue(eventListener.isUpdateHappen());
279:                assertFalse(eventListener.isCreateHappen());
280:                assertFalse(eventListener.isDeleteCalled());
281:
282:                // test that update child works
283:                con = JdbcUtils.getConnection(dataSource);
284:                ps = con
285:                        .prepareStatement("update child set first_name = 'new first name', version = 2 where id = 1");
286:                ps.execute();
287:                ps.close();
288:                con.commit();
289:                con.close();
290:
291:                eventListener.clear();
292:                gpsDevice.performMirroring();
293:                assertTrue(eventListener.isCreateAndUpdateCalled());
294:                assertTrue(eventListener.isUpdateHappen());
295:                assertFalse(eventListener.isCreateHappen());
296:                assertFalse(eventListener.isDeleteCalled());
297:
298:                // test that delete works
299:                con = JdbcUtils.getConnection(dataSource);
300:                ps = con.prepareStatement("delete from parent where id = 999");
301:                ps.execute();
302:                ps.close();
303:                con.commit();
304:                con.close();
305:
306:                eventListener.clear();
307:                gpsDevice.performMirroring();
308:                assertTrue(eventListener.isDeleteCalled());
309:                assertTrue(eventListener.isDeleteHappen());
310:                assertFalse(eventListener.isCreateAndUpdateCalled());
311:
312:                eventListener.clear();
313:                gpsDevice.performMirroring();
314:                assertFalse(eventListener.isCreateAndUpdateCalled());
315:                assertFalse(eventListener.isDeleteCalled());
316:            }
317:
318:            public void testExactMappingWithMirror() throws Exception {
319:                setUpExactMappingWithMirror();
320:                gps.index();
321:                compassTemplate.loadResource("result-set", "1", "1");
322:                Resource r = compassTemplate
323:                        .getResource("result-set", "4", "6");
324:                assertNotNull(r);
325:                CompassDetachedHits hits = compassTemplate
326:                        .findWithDetach("parent");
327:                assertEquals(6, hits.getLength());
328:
329:                // test that create works
330:                Connection con = JdbcUtils.getConnection(dataSource);
331:                PreparedStatement ps = con
332:                        .prepareStatement("INSERT INTO parent VALUES (999, 'parent first 999', 'last 999', 1);");
333:                ps.execute();
334:                ps.close();
335:                con.commit();
336:                con.close();
337:
338:                r = compassTemplate.getResource("result-set", "999", "0");
339:                assertNull(r);
340:                gpsDevice.performMirroring();
341:                compassTemplate.loadResource("result-set", "999", "0");
342:
343:                // test that update works
344:                con = JdbcUtils.getConnection(dataSource);
345:                ps = con
346:                        .prepareStatement("update parent set first_name = 'new first name', version = 2 where id = 1");
347:                ps.execute();
348:                ps.close();
349:                con.commit();
350:                con.close();
351:
352:                gpsDevice.performMirroring();
353:                r = compassTemplate.loadResource("result-set", "1", "1");
354:                assertEquals("new first name", r.getValue("parent_first_name"));
355:
356:                // test that delete works
357:                con = JdbcUtils.getConnection(dataSource);
358:                ps = con.prepareStatement("delete from parent where id = 999");
359:                ps.execute();
360:                ps.close();
361:                con.commit();
362:                con.close();
363:
364:                gpsDevice.performMirroring();
365:                r = compassTemplate.getResource("result-set", "999", "0");
366:                assertNull(r);
367:            }
368:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.