Source Code Cross Referenced for DataStoreTest.java in  » Database-Client » SQL-Workbench » workbench » storage » 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 » Database Client » SQL Workbench » workbench.storage 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DataStoreTest.java
003:         *
004:         * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005:         *
006:         * Copyright 2002-2008, Thomas Kellerer
007:         * No part of this code maybe reused without the permission of the author
008:         *
009:         * To contact the author please send an email to: support@sql-workbench.net
010:         *
011:         */
012:        package workbench.storage;
013:
014:        import java.io.File;
015:        import java.io.FileWriter;
016:        import java.sql.Connection;
017:        import java.sql.PreparedStatement;
018:        import java.sql.ResultSet;
019:        import java.sql.Statement;
020:        import java.sql.Types;
021:        import java.util.List;
022:        import junit.framework.TestCase;
023:        import workbench.TestUtil;
024:        import workbench.db.ColumnIdentifier;
025:        import workbench.db.ConnectionMgr;
026:        import workbench.db.TableIdentifier;
027:        import workbench.db.WbConnection;
028:        import workbench.resource.Settings;
029:        import workbench.storage.filter.AndExpression;
030:        import workbench.storage.filter.ComplexExpression;
031:        import workbench.storage.filter.LessThanComparator;
032:        import workbench.storage.filter.NumberEqualsComparator;
033:        import workbench.storage.filter.OrExpression;
034:        import workbench.storage.filter.StartsWithComparator;
035:        import workbench.storage.filter.StringEqualsComparator;
036:        import workbench.util.ExceptionUtil;
037:        import workbench.util.SqlUtil;
038:
039:        /**
040:         *
041:         * @author support@sql-workbench.net
042:         */
043:        public class DataStoreTest extends TestCase {
044:            private final int rowcount = 10;
045:            private TestUtil util;
046:
047:            public DataStoreTest(String testName) throws Exception {
048:                super (testName);
049:                this .util = new TestUtil(testName);
050:                util.prepareEnvironment();
051:            }
052:
053:            public void testMissingPkColumns() {
054:                try {
055:                    util.emptyBaseDirectory();
056:                    WbConnection con = util.getConnection("pkTestDb");
057:                    Statement stmt = con.createStatement();
058:                    stmt
059:                            .executeUpdate("CREATE TABLE junit_test (id1 integer, id2 integer, id3 integer, some_data varchar(100), primary key (id1, id2, id3))");
060:                    stmt
061:                            .executeUpdate("insert into junit_test (id1,id2,id3, some_data) values (1,2,3,'bla')");
062:
063:                    String sql = "select id1, id2, some_data from JUnit_Test";
064:
065:                    ResultSet rs = stmt.executeQuery(sql);
066:                    DataStore ds = new DataStore(rs, con);
067:                    rs.close();
068:                    stmt.close();
069:                    ds.setGeneratingSql(sql);
070:                    ds.checkUpdateTable(con);
071:                    assertEquals("Missing PK columns not detected", false, ds
072:                            .pkColumnsComplete());
073:                    List<ColumnIdentifier> cols = ds.getMissingPkColumns();
074:                    assertEquals("Not all missing columns detected", 1, cols
075:                            .size());
076:                    String col1 = cols.get(0).getColumnName();
077:                    assertEquals("Wrong column detected", true, col1
078:                            .equals("ID3"));
079:                } catch (Exception e) {
080:                    e.printStackTrace();
081:                    fail(e.getMessage());
082:                }
083:            }
084:
085:            public void testDefinePK() {
086:                try {
087:                    util.emptyBaseDirectory();
088:                    WbConnection con = util.getConnection("pkTestDb");
089:                    Statement stmt = con.createStatement();
090:                    stmt
091:                            .executeUpdate("CREATE TABLE junit_test (id1 integer, some_data varchar(100))");
092:                    stmt
093:                            .executeUpdate("insert into junit_test (id1, some_data) values (1,'General Failure')");
094:                    stmt
095:                            .executeUpdate("insert into junit_test (id1, some_data) values (2,'Major Bug')");
096:
097:                    String sql = "select id1, some_data from JUnit_Test";
098:
099:                    ResultSet rs = stmt.executeQuery(sql);
100:                    DataStore ds = new DataStore(rs, con);
101:                    SqlUtil.closeAll(rs, stmt);
102:
103:                    ds.setGeneratingSql(sql);
104:
105:                    PkMapping.getInstance().addMapping("JUNIT_TEST", "id1");
106:                    ds.updatePkInformation();
107:                    assertTrue(ds.hasPkColumns());
108:                    ds.setValue(0, 1, "Corporal Clegg");
109:                    ds.updateDb(con, null);
110:                    stmt = con.createStatement();
111:                    rs = stmt
112:                            .executeQuery("select some_data from junit_test where id1 = 1");
113:                    if (rs.next()) {
114:                        String data = rs.getString(1);
115:                        assertEquals("Corporal Clegg", data);
116:                    } else {
117:                        fail("No rows selected");
118:                    }
119:                    SqlUtil.closeAll(rs, stmt);
120:                } catch (Exception e) {
121:                    e.printStackTrace();
122:                    fail(e.getMessage());
123:                }
124:            }
125:
126:            public void testQuotedKeyColumns() {
127:
128:                try {
129:                    WbConnection con = prepareDatabase();
130:
131:                    String[] cols = new String[] { "\"KEY\"", "LASTNAME",
132:                            "FIRSTNAME", "LASTNAME" };
133:                    int[] types = new int[] { java.sql.Types.VARCHAR,
134:                            java.sql.Types.VARCHAR, java.sql.Types.VARCHAR,
135:                            java.sql.Types.VARCHAR };
136:
137:                    int[] sizes = new int[] { 20, 20, 20, 20 };
138:                    ResultInfo info = new ResultInfo(cols, types, sizes);
139:                    DataStore ds = new DataStore(info);
140:                    ds
141:                            .setGeneratingSql("SELECT \"KEY\", LASTNAME, FIRSTNAME FROM JUNIT_TEST");
142:                    TableIdentifier tbl = new TableIdentifier("JUNIT_TEST");
143:                    ds.setUpdateTableToBeUsed(tbl);
144:                    ds.checkUpdateTable(con);
145:
146:                    ResultInfo newInfo = ds.getResultInfo();
147:                    assertEquals(newInfo.getColumnCount(), 4);
148:                    assertEquals(newInfo.isPkColumn(0), true);
149:                    assertEquals(ds.pkColumnsComplete(), true);
150:                } catch (Exception e) {
151:                    e.printStackTrace();
152:                    fail(e.getMessage());
153:                } finally {
154:                    ConnectionMgr.getInstance().disconnectAll();
155:                }
156:            }
157:
158:            public void testCasePreserving() {
159:                WbConnection con = null;
160:                Statement stmt = null;
161:                try {
162:                    con = prepareDatabase();
163:
164:                    stmt = con.createStatement();
165:                    stmt.executeUpdate("delete from junit_test");
166:                    con.commit();
167:
168:                    String sql = "select key, lastname, firstname from JUnit_Test";
169:                    ResultSet rs = stmt.executeQuery(sql);
170:                    DataStore ds = new DataStore(rs, con);
171:                    ds.setGeneratingSql(sql);
172:
173:                    int row = ds.addRow();
174:                    ds.setValue(row, 0, new Integer(1));
175:                    ds.setValue(row, 1, "Dent");
176:                    ds.setValue(row, 2, "Arthur");
177:
178:                    ds.checkUpdateTable(con);
179:
180:                    assertEquals("Not all PK columns detected", ds
181:                            .pkColumnsComplete(), true);
182:
183:                    // Set to case preserving
184:                    Settings.getInstance().setGeneratedSqlTableCase("original");
185:                    List<DmlStatement> l = ds.getUpdateStatements(con);
186:                    assertEquals("Wrong number of update statements", 1, l
187:                            .size());
188:
189:                    DmlStatement dml = l.get(0);
190:                    SqlLiteralFormatter f = new SqlLiteralFormatter(con);
191:                    CharSequence insert = dml.getExecutableStatement(f);
192:                    String verb = SqlUtil.getSqlVerb(insert);
193:                    assertEquals("Wrong statement generated", "INSERT", verb
194:                            .toUpperCase());
195:                    String table = SqlUtil.getInsertTable(insert);
196:                    assertEquals("JUnit_Test", table);
197:
198:                    // Set to upper case
199:                    Settings.getInstance().setGeneratedSqlTableCase("upper");
200:                    l = ds.getUpdateStatements(con);
201:                    assertEquals("Wrong number of update statements", 1, l
202:                            .size());
203:
204:                    dml = l.get(0);
205:                    insert = dml.getExecutableStatement(f);
206:                    table = SqlUtil.getInsertTable(insert);
207:                    assertEquals("JUNIT_TEST", table);
208:
209:                    // Test lower case
210:                    Settings.getInstance().setGeneratedSqlTableCase("lower");
211:                    l = ds.getUpdateStatements(con);
212:                    assertEquals("Wrong number of update statements", 1, l
213:                            .size());
214:
215:                    dml = l.get(0);
216:                    insert = dml.getExecutableStatement(f);
217:                    table = SqlUtil.getInsertTable(insert);
218:                    assertEquals("junit_test", table);
219:
220:                    ds.resetStatus();
221:                    ds.setValue(0, 1, "Dent2");
222:
223:                    // Test uppercase 
224:                    Settings.getInstance().setGeneratedSqlTableCase("upper");
225:                    l = ds.getUpdateStatements(con);
226:                    assertEquals("Wrong number of update statements", 1, l
227:                            .size());
228:
229:                    dml = l.get(0);
230:                    CharSequence update = dml.getExecutableStatement(f);
231:                    verb = SqlUtil.getSqlVerb(update);
232:                    table = SqlUtil.getUpdateTable(update);
233:                    assertEquals("UPDATE", verb);
234:                    assertEquals("JUNIT_TEST", table);
235:
236:                    // Test lower case
237:                    Settings.getInstance().setGeneratedSqlTableCase("lower");
238:                    l = ds.getUpdateStatements(con);
239:                    assertEquals("Wrong number of update statements", 1, l
240:                            .size());
241:
242:                    dml = l.get(0);
243:                    update = dml.getExecutableStatement(f);
244:                    table = SqlUtil.getUpdateTable(update);
245:                    assertEquals("junit_test", table);
246:
247:                    // Test lower case
248:                    Settings.getInstance().setGeneratedSqlTableCase("original");
249:                    l = ds.getUpdateStatements(con);
250:                    assertEquals("Wrong number of update statements", 1, l
251:                            .size());
252:
253:                    dml = l.get(0);
254:                    update = dml.getExecutableStatement(f);
255:                    table = SqlUtil.getUpdateTable(update);
256:                    assertEquals("JUnit_Test", table);
257:
258:                } catch (Exception e) {
259:                    e.printStackTrace();
260:                    fail(ExceptionUtil.getDisplay(e));
261:                } finally {
262:                    ConnectionMgr.getInstance().disconnectAll();
263:                }
264:            }
265:
266:            public void testPkDetection() {
267:                try {
268:                    WbConnection con = prepareDatabase();
269:                    Statement stmt = con.createStatement();
270:                    final String sql = "select key, firstname, lastname from junit_test";
271:                    ResultSet rs = stmt.executeQuery(sql);
272:                    DataStore ds = new DataStore(rs, con);
273:                    rs.close();
274:                    ds.setGeneratingSql(sql);
275:
276:                    assertEquals("Non-existing primary key found", false, ds
277:                            .hasPkColumns());
278:                    ds.checkUpdateTable();
279:                    assertEquals("Primary key not found", true, ds
280:                            .hasPkColumns());
281:                    assertEquals("Not all PK columns detected", ds
282:                            .pkColumnsComplete(), true);
283:
284:                    stmt.executeUpdate("DROP TABLE junit_test");
285:                    stmt
286:                            .executeUpdate("CREATE TABLE junit_test (key integer, firstname varchar(100), lastname varchar(100))");
287:                    stmt
288:                            .executeUpdate("insert into junit_test (key, firstname, lastname) values (42, 'Zaphod', 'Beeblebrox')");
289:                    stmt
290:                            .executeUpdate("insert into junit_test (key, firstname, lastname) values (1, 'Mary', 'Moviestar')");
291:                    con.commit();
292:
293:                    rs = stmt.executeQuery(sql);
294:                    ds = new DataStore(rs, con);
295:                    ds.setGeneratingSql(sql);
296:                    rs.close();
297:
298:                    ds.updatePkInformation();
299:                    assertEquals("Non-existing primary key found", false, ds
300:                            .hasPkColumns());
301:
302:                    ResultInfo info = ds.getResultInfo();
303:                    info.setIsPkColumn(0, true);
304:                    assertEquals("Primary key not recognized", true, ds
305:                            .hasPkColumns());
306:
307:                    ds.setValue(0, 1, "Arthur");
308:                    ds.setValue(0, 2, "Dent");
309:                    int rows = ds.updateDb(con, null);
310:                    assertEquals("Incorrect number of rows updated", 1, rows);
311:
312:                    rs = stmt
313:                            .executeQuery("select firstname, lastname from junit_test where key = 42");
314:                    boolean hasRows = rs.next();
315:                    assertEquals("No rows fetched", true, hasRows);
316:
317:                    String fname = rs.getString(1);
318:                    String lname = rs.getString(2);
319:                    assertEquals("Firstname incorrectly updated", "Arthur",
320:                            fname);
321:                    assertEquals("Lastname incorrectly updated", "Dent", lname);
322:                    rs.close();
323:
324:                    rs = stmt
325:                            .executeQuery("select firstname, lastname from junit_test where key = 1");
326:                    hasRows = rs.next();
327:                    assertEquals("No rows fetched", true, hasRows);
328:
329:                    fname = rs.getString(1);
330:                    lname = rs.getString(2);
331:                    assertEquals("Incorrect firstname affected", "Mary", fname);
332:                    assertEquals("Incorrect lastname affected", "Moviestar",
333:                            lname);
334:                    rs.close();
335:
336:                    String mapping = "junit_test=key";
337:                    File f = new File(util.getBaseDir(),
338:                            "pk_mapping.properties");
339:                    FileWriter w = new FileWriter(f);
340:                    w.write(mapping);
341:                    w.close();
342:                    PkMapping.getInstance().loadMapping(f.getAbsolutePath());
343:
344:                    rs = stmt.executeQuery(sql);
345:                    ds = new DataStore(rs, con);
346:                    ds.setGeneratingSql(sql);
347:                    rs.close();
348:
349:                    ds.updatePkInformation();
350:                    assertEquals("Primary key from mapping not found", true, ds
351:                            .hasPkColumns());
352:
353:                } catch (Exception e) {
354:                    e.printStackTrace();
355:                    fail(e.getMessage());
356:                } finally {
357:                    ConnectionMgr.getInstance().disconnectAll();
358:                }
359:            }
360:
361:            private WbConnection prepareDatabase() throws Exception {
362:                util.emptyBaseDirectory();
363:                WbConnection wb = util.getConnection();
364:                Connection con = wb.getSqlConnection();
365:                Statement stmt = con.createStatement();
366:                try {
367:                    stmt.executeUpdate("DROP TABLE junit_test");
368:                } catch (Throwable th) {
369:                }
370:                stmt
371:                        .executeUpdate("CREATE TABLE junit_test (key integer primary key, firstname varchar(100), lastname varchar(100))");
372:                stmt.close();
373:                PreparedStatement pstmt = con
374:                        .prepareStatement("insert into junit_test (key, firstname, lastname) values (?,?,?)");
375:                for (int i = 0; i < rowcount; i++) {
376:                    pstmt.setInt(1, i);
377:                    pstmt.setString(2, "FirstName" + i);
378:                    pstmt.setString(3, "LastName" + i);
379:                    pstmt.executeUpdate();
380:                }
381:                con.commit();
382:                return wb;
383:            }
384:
385:            public void testUpdate() {
386:                WbConnection con = null;
387:                Statement stmt = null;
388:                try {
389:                    con = prepareDatabase();
390:
391:                    stmt = con.createStatement();
392:                    String sql = "select key, lastname, firstname from junit_test";
393:                    ResultSet rs = stmt.executeQuery(sql);
394:                    DataStore ds = new DataStore(rs, con);
395:                    SqlUtil.closeResult(rs);
396:
397:                    List<String> tbl = SqlUtil.getTables(sql);
398:                    assertEquals("Wrong number of tables retrieved from SQL",
399:                            1, tbl.size());
400:
401:                    String table = tbl.get(0);
402:                    assertEquals("Wrong update table returned", "junit_test",
403:                            table);
404:
405:                    TableIdentifier id = new TableIdentifier(table);
406:                    ds.setUpdateTable(id);
407:                    assertEquals(rowcount, ds.getRowCount());
408:
409:                    ds.setValue(0, 1, "Dent");
410:                    ds.setValue(0, 2, "Arthur");
411:                    ds.updateDb(con, null);
412:
413:                    rs = stmt
414:                            .executeQuery("select lastname, firstname from junit_test where key = 0");
415:                    boolean hasNext = rs.next();
416:                    assertEquals("Updated row not found", true, hasNext);
417:                    String lastname = rs.getString(1);
418:                    String firstname = rs.getString(2);
419:                    assertEquals("Firstname not updated", "Arthur", firstname);
420:                    assertEquals("Lastname not updated", "Dent", lastname);
421:                    SqlUtil.closeResult(rs);
422:
423:                    rs = stmt
424:                            .executeQuery("select lastname, firstname from junit_test where key = 1");
425:                    hasNext = rs.next();
426:                    assertEquals("Updated row not found", true, hasNext);
427:                    lastname = rs.getString(1);
428:                    firstname = rs.getString(2);
429:                    assertEquals("Firstname updated", "FirstName1", firstname);
430:                    assertEquals("Lastname updated", "LastName1", lastname);
431:                    SqlUtil.closeResult(rs);
432:
433:                    int row = ds.addRow();
434:                    ds.setValue(row, 0, new Integer(42));
435:                    ds.setValue(row, 1, "Beeblebrox");
436:                    assertEquals("Row not inserted", rowcount + 1, ds
437:                            .getRowCount());
438:                    ds.setValue(row, 2, "Zaphod");
439:                    ds.updateDb(con, null);
440:
441:                    rs = stmt
442:                            .executeQuery("select lastname, firstname from junit_test where key = 42");
443:                    hasNext = rs.next();
444:                    assertEquals("Updated row not found", true, hasNext);
445:                    lastname = rs.getString(1);
446:                    firstname = rs.getString(2);
447:                    assertEquals("Firstname not updated", "Zaphod", firstname);
448:                    assertEquals("Lastname not updated", "Beeblebrox", lastname);
449:                    SqlUtil.closeResult(rs);
450:
451:                    stmt
452:                            .executeUpdate("update junit_test set firstname = null where key = 42");
453:                    con.commit();
454:                    rs = stmt
455:                            .executeQuery("select key, lastname, firstname from junit_test where key = 42");
456:                    ds = new DataStore(rs, con);
457:                    SqlUtil.closeResult(rs);
458:                    ds.setUpdateTable(id);
459:                    ds.setValue(0, 2, "Arthur");
460:                    ds.updateDb(con, null);
461:                    rs = stmt
462:                            .executeQuery("select firstname from junit_test where key = 42");
463:                    hasNext = rs.next();
464:                    assertEquals("Updated row not found", true, hasNext);
465:                    firstname = rs.getString(1);
466:                    assertEquals("Arthur", firstname);
467:
468:                    rs = stmt
469:                            .executeQuery("select key, lastname, firstname from junit_test where key = 42");
470:                    ds = new DataStore(rs, con);
471:                    SqlUtil.closeResult(rs);
472:                    ds.setUpdateTable(id);
473:                    ds.deleteRow(0);
474:
475:                    List<DmlStatement> statements = ds.getUpdateStatements(con);
476:                    assertEquals(1, statements.size());
477:
478:                    ds.updateDb(con, null);
479:
480:                    rs = stmt
481:                            .executeQuery("select count(*) from junit_test where key = 42");
482:                    rs.next();
483:                    int count = rs.getInt(1);
484:                    assertEquals(0, count);
485:
486:                    SqlUtil.closeAll(rs, stmt);
487:                } catch (Exception e) {
488:                    e.printStackTrace();
489:                    fail(ExceptionUtil.getDisplay(e));
490:                } finally {
491:                    ConnectionMgr.getInstance().disconnectAll();
492:                }
493:            }
494:
495:            public void testCascadingDelete() {
496:                WbConnection con = null;
497:                try {
498:                    con = util.getConnection();
499:                    Statement stmt = con.createStatement();
500:                    stmt
501:                            .executeUpdate("CREATE TABLE person (id integer primary key, firstname varchar(20), lastname varchar(20))");
502:                    stmt
503:                            .executeUpdate("insert into person (id, firstname, lastname) values (42, 'Zaphod', 'Beeblebrox')");
504:                    stmt
505:                            .executeUpdate("insert into person (id, firstname, lastname) values (1, 'Mary', 'Moviestar')");
506:
507:                    stmt
508:                            .executeUpdate("create table detail (did integer primary key, person_id integer, detail_info varchar(100))");
509:                    stmt
510:                            .executeUpdate("alter table detail ADD CONSTRAINT fk_pers FOREIGN KEY (person_id) REFERENCES person (id)");
511:                    stmt
512:                            .executeUpdate("insert into detail (did, person_id, detail_info) values (1, 42, 'some stuff')");
513:                    stmt
514:                            .executeUpdate("insert into detail (did, person_id, detail_info) values (2, 42, 'more stuff')");
515:                    stmt
516:                            .executeUpdate("insert into detail (did, person_id, detail_info) values (3, 1, 'mary1')");
517:                    stmt
518:                            .executeUpdate("insert into detail (did, person_id, detail_info) values (4, 1, 'mary2')");
519:                    con.commit();
520:
521:                    ResultSet rs = stmt
522:                            .executeQuery("select id, firstname, lastname from person order by id");
523:                    DataStore ds = new DataStore(rs, true);
524:                    rs.close();
525:                    assertEquals(2, ds.getRowCount());
526:
527:                    ds.setOriginalConnection(con);
528:                    ds
529:                            .setGeneratingSql("select id, firstname, lastname from person order by id");
530:                    ds.deleteRowWithDependencies(0);
531:                    ds.updateDb(con, null);
532:                    rs = stmt.executeQuery("select count(*) from person");
533:                    rs.next();
534:                    int count = rs.getInt(1);
535:                    assertEquals(1, count);
536:                    rs.close();
537:
538:                    rs = stmt.executeQuery("select count(*) from detail");
539:                    rs.next();
540:                    count = rs.getInt(1);
541:                    assertEquals(2, count);
542:                    rs.close();
543:                } catch (Exception e) {
544:                    e.printStackTrace();
545:                    fail(e.getMessage());
546:                } finally {
547:                    con.close();
548:                }
549:            }
550:
551:            public void testFilter() {
552:                WbConnection con = null;
553:                Statement stmt = null;
554:                try {
555:                    con = prepareDatabase();
556:
557:                    stmt = con.createStatement();
558:                    stmt
559:                            .executeUpdate("insert into junit_test (key, firstname, lastname) values (42, 'Zaphod', 'Beeblebrox')");
560:                    con.commit();
561:
562:                    String sql = "select key, lastname, firstname from junit_test";
563:                    ResultSet rs = stmt.executeQuery(sql);
564:                    DataStore ds = new DataStore(rs, con);
565:
566:                    ComplexExpression expr = new AndExpression();
567:                    expr.addColumnExpression("FIRSTNAME",
568:                            new StringEqualsComparator(), "Zaphod");
569:                    expr.addColumnExpression("LASTNAME",
570:                            new StartsWithComparator(), "Bee");
571:
572:                    ds.applyFilter(expr);
573:                    assertEquals("AND Filter not correct", 1, ds.getRowCount());
574:
575:                    expr = new AndExpression();
576:                    expr.addColumnExpression("KEY",
577:                            new NumberEqualsComparator(), new Integer(100));
578:                    ds.applyFilter(expr);
579:                    assertEquals("Number Filter not correct", 0, ds
580:                            .getRowCount());
581:
582:                    expr = new OrExpression();
583:                    expr.addColumnExpression("KEY", new LessThanComparator(),
584:                            new Integer(1));
585:                    expr.addColumnExpression("FIRSTNAME",
586:                            new StringEqualsComparator(), "Zaphod");
587:
588:                    ds.applyFilter(expr);
589:                    assertEquals("OR Filter not correct", 2, ds.getRowCount());
590:                } catch (Exception e) {
591:                    e.printStackTrace();
592:                    fail(ExceptionUtil.getDisplay(e));
593:                } finally {
594:                    ConnectionMgr.getInstance().disconnectAll();
595:                }
596:
597:            }
598:
599:            public void testUpdateCaseSensitive() {
600:                Statement stmt = null;
601:                ResultSet rs = null;
602:                try {
603:                    util.emptyBaseDirectory();
604:                    WbConnection con = util.getConnection();
605:                    stmt = con.createStatement();
606:                    stmt
607:                            .executeUpdate("create table \"case_test\" (nr integer primary key, text_data varchar(100))");
608:                    String sql = "select * from \"case_test\"";
609:                    rs = stmt.executeQuery(sql);
610:                    DataStore ds = new DataStore(rs, con, true);
611:                    ds.setGeneratingSql(sql);
612:                    rs.close();
613:
614:                    ds.checkUpdateTable(con);
615:                    int row = ds.addRow();
616:                    ds.setValue(row, 0, new Integer(42));
617:                    ds.setValue(row, 1, "TestData");
618:                    ds.updateDb(con, null);
619:
620:                    rs = stmt.executeQuery(sql);
621:                    if (rs.next()) {
622:                        int nr = rs.getInt(1);
623:                        assertEquals(42, nr);
624:
625:                        String s = rs.getString(2);
626:                        assertEquals("TestData", s);
627:                    } else {
628:                        fail("New rows not updated");
629:                    }
630:                    rs.close();
631:                } catch (Exception e) {
632:                    e.printStackTrace();
633:                    fail(e.getMessage());
634:                } finally {
635:                    ConnectionMgr.getInstance().disconnectAll();
636:                }
637:            }
638:
639:            public void testList() {
640:                try {
641:                    String[] cols = new String[] { "CHAR", "INT", "DOUBLE" };
642:                    int[] types = new int[] { Types.VARCHAR, Types.INTEGER,
643:                            Types.DOUBLE };
644:                    DataStore ds = new DataStore(cols, types);
645:                    ds.addRow();
646:                    assertEquals(1, ds.getRowCount());
647:                    String s = "Testvalue";
648:                    Integer i = new Integer(42);
649:                    Double d = new Double(4.2);
650:                    ds.setValue(0, 0, s);
651:                    ds.setValue(0, 1, i);
652:                    ds.setValue(0, 2, d);
653:                    assertEquals(s, ds.getValue(0, 0));
654:                    assertEquals(i, ds.getValue(0, 1));
655:                    assertEquals(i.intValue(), ds.getValueAsInt(0, 1, -1));
656:                    assertEquals(d, ds.getValue(0, 2));
657:                    ds.resetStatus();
658:
659:                    String newValue = "Newvalue";
660:                    ds.setValue(0, 0, newValue);
661:                    assertEquals(newValue, ds.getValue(0, 0));
662:                    assertEquals(s, ds.getOriginalValue(0, 0));
663:                    assertEquals(true, ds.isModified());
664:                    ds.resetStatus();
665:                    assertEquals(false, ds.isModified());
666:                    assertEquals(newValue, ds.getOriginalValue(0, 0));
667:                } catch (Exception e) {
668:                    fail(e.getMessage());
669:                } finally {
670:                    ConnectionMgr.getInstance().disconnectAll();
671:                }
672:            }
673:
674:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.