Source Code Cross Referenced for TestConstraints.java in  » Database-DBMS » axion » org » axiondb » functional » 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 DBMS » axion » org.axiondb.functional 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: TestConstraints.java,v 1.15 2005/12/20 18:32:44 ahimanikya Exp $
003:         * =======================================================================
004:         * Copyright (c) 2002-2003 Axion Development Team.  All rights reserved.
005:         *
006:         * Redistribution and use in source and binary forms, with or without
007:         * modification, are permitted provided that the following conditions
008:         * are met:
009:         *
010:         * 1. Redistributions of source code must retain the above
011:         *    copyright notice, this list of conditions and the following
012:         *    disclaimer.
013:         *
014:         * 2. Redistributions in binary form must reproduce the above copyright
015:         *    notice, this list of conditions and the following disclaimer in
016:         *    the documentation and/or other materials provided with the
017:         *    distribution.
018:         *
019:         * 3. The names "Tigris", "Axion", nor the names of its contributors may
020:         *    not be used to endorse or promote products derived from this
021:         *    software without specific prior written permission.
022:         *
023:         * 4. Products derived from this software may not be called "Axion", nor
024:         *    may "Tigris" or "Axion" appear in their names without specific prior
025:         *    written permission.
026:         *
027:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028:         * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029:         * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030:         * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031:         * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034:         * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035:         * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037:         * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038:         * =======================================================================
039:         */
040:
041:        package org.axiondb.functional;
042:
043:        import java.sql.PreparedStatement;
044:        import java.sql.SQLException;
045:
046:        import junit.framework.Test;
047:        import junit.framework.TestSuite;
048:
049:        /**
050:         * @version $Revision: 1.15 $ $Date: 2005/12/20 18:32:44 $
051:         * @author Rodney Waldhoff
052:         */
053:        public class TestConstraints extends AbstractFunctionalTest {
054:
055:            //------------------------------------------------------------ Conventional
056:
057:            public TestConstraints(String testName) {
058:                super (testName);
059:            }
060:
061:            public static Test suite() {
062:                return new TestSuite(TestConstraints.class);
063:            }
064:
065:            //--------------------------------------------------------------- Lifecycle
066:
067:            //-------------------------------------------------------------------- Util
068:
069:            private void createTableFoobar() throws Exception {
070:                _stmt
071:                        .execute("create table FOOBAR ( NUM1 integer, NUM2 integer, STR varchar2, PRIMARY KEY (NUM1, NUM2) )");
072:            }
073:
074:            private void populateTableFoobar() throws Exception {
075:                PreparedStatement pstmt = _conn
076:                        .prepareStatement("insert into FOOBAR ( NUM1, NUM2, STR ) values ( ?, ?, ?)");
077:                for (int i = 0; i < NUM_ROWS_IN_FOO; i++) {
078:                    pstmt.setInt(1, i);
079:                    pstmt.setInt(2, i);
080:                    pstmt.setString(3, String.valueOf(i));
081:                    pstmt.executeUpdate();
082:                }
083:                pstmt.close();
084:            }
085:
086:            private void addNotNullConstraintToFooNum() throws Exception {
087:                _stmt
088:                        .execute("alter table FOO add constraint FOO_NUM_NN not null ( NUM )");
089:            }
090:
091:            private void addDeferredNotNullConstraintToFooNum()
092:                    throws Exception {
093:                _stmt
094:                        .execute("alter table FOO add constraint FOO_NUM_NN not null ( NUM ) deferrable initially deferred");
095:            }
096:
097:            private void addUniqueConstraintToFooNum() throws Exception {
098:                _stmt
099:                        .execute("alter table FOO add constraint FOO_NUM_UNIQUE unique ( NUM )");
100:            }
101:
102:            private void addUniqueConstraintToFooStr() throws Exception {
103:                _stmt
104:                        .execute("alter table FOO add constraint FOO_STR_UNIQUE unique ( STR )");
105:            }
106:
107:            private void addUniqueConstraintToUpperFooStr() throws Exception {
108:                _stmt
109:                        .execute("alter table FOO add constraint FOO_USTR_UNIQUE unique ( upper(STR) )");
110:            }
111:
112:            private void addNotEmptyOrNullConstraintToFooStr() throws Exception {
113:                _stmt
114:                        .execute("alter table FOO add constraint STR_NOT_EMPTY check ( STR is not null and str <> '' )");
115:            }
116:
117:            //------------------------------------------------------------------- Tests
118:
119:            public void testAddPrimaryKey() throws Exception {
120:                createTableFoo();
121:                _stmt
122:                        .execute("alter table FOO add constraint primary key (NUMTWO)");
123:            }
124:
125:            public void testAddForeignKey() throws Exception {
126:                createTableFoo();
127:                createTableBar();
128:                try {
129:                    _stmt
130:                            .execute("alter table FOO add constraint foreign key (NUM) REFERENCES BAR(ID)");
131:                    fail("Expected SQLException: Key not found");
132:                } catch (SQLException e) {
133:                    // expected
134:                }
135:
136:                _stmt
137:                        .execute("alter table BAR add constraint primary key (ID) ");
138:                _stmt
139:                        .execute("alter table FOO add constraint foreign key (NUM) REFERENCES BAR(ID)");
140:
141:                _stmt
142:                        .execute("insert into BAR ( ID, DESCR, DESCR2 ) values ( 1, '1', '11')");
143:                _stmt
144:                        .execute("insert into FOO ( NUM, STR, NUMTWO ) values ( 1, '1', 1)");
145:
146:                try {
147:                    _stmt
148:                            .execute("insert into FOO ( NUM, STR, NUMTWO ) values ( 2, '1', 1)");
149:                    fail("Expected SQLException");
150:                } catch (SQLException e) {
151:                    // expected
152:                }
153:            }
154:
155:            public void testCreateInvalidForeignKey() throws Exception {
156:                try {
157:                    _stmt
158:                            .execute("create table FOO ( NUM integer, STR varchar2(255), NUMTWO integer foreign key (NUM) REFERENCES BAR(ID))");
159:                    fail("Expected SQLException");
160:                } catch (SQLException e) {
161:                    // expected
162:                }
163:
164:                // make sure if child command fails then table should not have been created.
165:                assertFalse(_conn.getDatabase().hasTable("FOO"));
166:            }
167:
168:            public void testAddAtMostOnePrimaryKey() throws Exception {
169:                createTableFoo();
170:                _stmt
171:                        .execute("alter table FOO add constraint FOO_PK primary key (NUM)");
172:                try {
173:                    _stmt
174:                            .execute("alter table FOO add constraint BAR_PK primary key (NUMTWO)");
175:                    fail("Expected SQLException");
176:                } catch (SQLException e) {
177:                    // expected
178:                }
179:                _stmt.execute("alter table FOO drop constraint FOO_PK");
180:                _stmt
181:                        .execute("alter table FOO add constraint FOO_PK primary key (NUM)");
182:            }
183:
184:            public void testInsertNotNullInt() throws Exception {
185:                createTableFoo();
186:                addNotNullConstraintToFooNum();
187:                populateTableFoo();
188:                assertEquals(1, _stmt.executeUpdate("insert into FOO values ( "
189:                        + (NUM_ROWS_IN_FOO + 1) + ", '" + (NUM_ROWS_IN_FOO + 1)
190:                        + "', " + (NUM_ROWS_IN_FOO + 1) + " )"));
191:            }
192:
193:            public void testPrimaryKeyProhibitsNull() throws Exception {
194:                createTableFoobar();
195:                try {
196:                    _stmt
197:                            .executeUpdate("insert into FOOBAR values ( null, 0, '0' )");
198:                    fail("Expected SQLException");
199:                } catch (SQLException e) {
200:                    // expected
201:                }
202:            }
203:
204:            public void testInsertNullInt() throws Exception {
205:                createTableFoo();
206:                addNotNullConstraintToFooNum();
207:                populateTableFoo();
208:                try {
209:                    _stmt
210:                            .executeUpdate("insert into FOO values ( null, '0', 0 )");
211:                    fail("Expected SQLException");
212:                } catch (SQLException e) {
213:                    // expected
214:                }
215:            }
216:
217:            public void testUpdateToNonNullInt() throws Exception {
218:                createTableFoo();
219:                addNotNullConstraintToFooNum();
220:                populateTableFoo();
221:                assertEquals(1, _stmt
222:                        .executeUpdate("update FOO set num = 2 where num = 2"));
223:            }
224:
225:            public void testUpdateToNullInt() throws Exception {
226:                createTableFoo();
227:                addNotNullConstraintToFooNum();
228:                populateTableFoo();
229:                try {
230:                    _stmt
231:                            .executeUpdate("update FOO set num = null where num = 2");
232:                    fail("Expected SQLException");
233:                } catch (SQLException e) {
234:                    // expected
235:                }
236:            }
237:
238:            public void testInsertUniqueInt() throws Exception {
239:                createTableFoo();
240:                addUniqueConstraintToFooNum();
241:                populateTableFoo();
242:                assertEquals(1, _stmt.executeUpdate("insert into FOO values ( "
243:                        + (NUM_ROWS_IN_FOO + 1) + ", '" + (NUM_ROWS_IN_FOO + 1)
244:                        + "', " + (NUM_ROWS_IN_FOO + 1) + " )"));
245:            }
246:
247:            public void testInsertNonUniqueInt() throws Exception {
248:                createTableFoo();
249:                addUniqueConstraintToFooNum();
250:                populateTableFoo();
251:                try {
252:                    _stmt.executeUpdate("insert into FOO values ( 0, '0', 0 )");
253:                    fail("Expected SQLException");
254:                } catch (SQLException e) {
255:                    // expected
256:                }
257:            }
258:
259:            public void testInsertUniqueStr() throws Exception {
260:                createTableFoo();
261:                addUniqueConstraintToFooStr();
262:                populateTableFoo();
263:                assertEquals(1, _stmt.executeUpdate("insert into FOO values ( "
264:                        + (NUM_ROWS_IN_FOO + 1) + ", '" + (NUM_ROWS_IN_FOO + 1)
265:                        + "', " + (NUM_ROWS_IN_FOO + 1) + " )"));
266:            }
267:
268:            public void testInsertNonUniqueStr() throws Exception {
269:                createTableFoo();
270:                addUniqueConstraintToFooStr();
271:                populateTableFoo();
272:                try {
273:                    _stmt.executeUpdate("insert into FOO values ( 0, '0', 0 )");
274:                    fail("Expected SQLException");
275:                } catch (SQLException e) {
276:                    // expected
277:                }
278:            }
279:
280:            public void testUpdateUniqueInt() throws Exception {
281:                createTableFoo();
282:                addUniqueConstraintToFooNum();
283:                populateTableFoo();
284:                assertEquals(
285:                        1,
286:                        _stmt
287:                                .executeUpdate("update FOO set num = 2 where str = '2'"));
288:            }
289:
290:            public void testUpdateNonUniqueInt() throws Exception {
291:                createTableFoo();
292:                addUniqueConstraintToFooNum();
293:                populateTableFoo();
294:                try {
295:                    _stmt.executeUpdate("update FOO set num = 2 where num = 3");
296:                    fail("Expected SQLException");
297:                } catch (SQLException e) {
298:                    // expected
299:                }
300:            }
301:
302:            public void testInsertUniquePrimaryKey() throws Exception {
303:                createTableFoobar();
304:                populateTableFoobar();
305:                assertEquals(1, _stmt
306:                        .executeUpdate("insert into FOOBAR values ( "
307:                                + (NUM_ROWS_IN_FOO + 1) + ", "
308:                                + (NUM_ROWS_IN_FOO + 1) + ", '"
309:                                + (NUM_ROWS_IN_FOO + 1) + "' )"));
310:                assertEquals(
311:                        1,
312:                        _stmt
313:                                .executeUpdate("insert into FOOBAR values ( 0, 1, null )"));
314:                assertEquals(
315:                        1,
316:                        _stmt
317:                                .executeUpdate("insert into FOOBAR values ( 1, 0, null )"));
318:            }
319:
320:            public void testInsertNonUniquePrimaryKey() throws Exception {
321:                createTableFoo();
322:                addUniqueConstraintToFooNum();
323:                populateTableFoo();
324:                try {
325:                    _stmt
326:                            .executeUpdate("insert into FOOBAR values ( 0, 0, '0')");
327:                    fail("Expected SQLException");
328:                } catch (SQLException e) {
329:                    // expected
330:                }
331:            }
332:
333:            public void testDeferredSuccess() throws Exception {
334:                _conn.setAutoCommit(false);
335:                createTableFoo();
336:                addDeferredNotNullConstraintToFooNum();
337:                populateTableFoo();
338:                _conn.commit();
339:                assertEquals(1, _stmt.executeUpdate("insert into FOO values ( "
340:                        + (NUM_ROWS_IN_FOO + 1) + ", '" + (NUM_ROWS_IN_FOO + 1)
341:                        + "', " + (NUM_ROWS_IN_FOO + 1) + " )"));
342:                _conn.commit();
343:            }
344:
345:            public void testDeferredSuccess2() throws Exception {
346:                _conn.setAutoCommit(false);
347:                createTableFoo();
348:                addDeferredNotNullConstraintToFooNum();
349:                populateTableFoo();
350:                _conn.commit();
351:                _stmt.executeUpdate("insert into FOO values ( null, '0', 0 )");
352:                _stmt.executeUpdate("delete from FOO where NUM is null");
353:                _conn.commit();
354:            }
355:
356:            public void testDeferredFailure() throws Exception {
357:                _conn.setAutoCommit(false);
358:                createTableFoo();
359:                addDeferredNotNullConstraintToFooNum();
360:                populateTableFoo();
361:                _conn.commit();
362:                _stmt.executeUpdate("insert into FOO values ( null, '0', 0 )");
363:                try {
364:                    _conn.commit();
365:                    fail("Expected SQLException");
366:                } catch (SQLException e) {
367:                    // expected
368:                }
369:            }
370:
371:            public void testInsertUniqueUpperStr() throws Exception {
372:                createTableFoo();
373:                addUniqueConstraintToUpperFooStr();
374:                populateTableFoo();
375:                assertEquals(1, _stmt.executeUpdate("insert into FOO values ( "
376:                        + (NUM_ROWS_IN_FOO + 1) + ", 'test', "
377:                        + (NUM_ROWS_IN_FOO + 1) + " )"));
378:                assertEquals(1, _stmt.executeUpdate("insert into FOO values ( "
379:                        + (NUM_ROWS_IN_FOO + 2) + ", 'test2', "
380:                        + (NUM_ROWS_IN_FOO + 2) + " )"));
381:            }
382:
383:            public void testInsertNonUniqueUpperStr() throws Exception {
384:                createTableFoo();
385:                addUniqueConstraintToUpperFooStr();
386:                populateTableFoo();
387:                assertEquals(1, _stmt.executeUpdate("insert into FOO values ( "
388:                        + (NUM_ROWS_IN_FOO + 1) + ", 'test', "
389:                        + (NUM_ROWS_IN_FOO + 1) + " )"));
390:                try {
391:                    _stmt
392:                            .executeUpdate("insert into FOO values ( 0, 'Test', 0 )");
393:                    fail("Expected SQLException");
394:                } catch (SQLException e) {
395:                    // expected
396:                }
397:            }
398:
399:            public void testInsertCheckSuccess() throws Exception {
400:                createTableFoo();
401:                addNotEmptyOrNullConstraintToFooStr();
402:                populateTableFoo();
403:                assertEquals(1, _stmt.executeUpdate("insert into FOO values ( "
404:                        + (NUM_ROWS_IN_FOO + 1) + ", 'test', "
405:                        + (NUM_ROWS_IN_FOO + 1) + " )"));
406:                assertEquals(1, _stmt.executeUpdate("insert into FOO values ( "
407:                        + (NUM_ROWS_IN_FOO + 2) + ", 'test2', "
408:                        + (NUM_ROWS_IN_FOO + 2) + " )"));
409:            }
410:
411:            public void testInsertCheckFailure1() throws Exception {
412:                createTableFoo();
413:                addNotEmptyOrNullConstraintToFooStr();
414:                populateTableFoo();
415:                assertEquals(1, _stmt.executeUpdate("insert into FOO values ( "
416:                        + (NUM_ROWS_IN_FOO + 1) + ", 'test', "
417:                        + (NUM_ROWS_IN_FOO + 1) + " )"));
418:                try {
419:                    _stmt.executeUpdate("insert into FOO values ( 0, '', 0 )");
420:                    fail("Expected SQLException");
421:                } catch (SQLException e) {
422:                    // expected
423:                }
424:            }
425:
426:            public void testInsertCheckFailure2() throws Exception {
427:                createTableFoo();
428:                addNotEmptyOrNullConstraintToFooStr();
429:                populateTableFoo();
430:                assertEquals(1, _stmt.executeUpdate("insert into FOO values ( "
431:                        + (NUM_ROWS_IN_FOO + 1) + ", 'test', "
432:                        + (NUM_ROWS_IN_FOO + 1) + " )"));
433:                try {
434:                    _stmt
435:                            .executeUpdate("insert into FOO values ( 0, null, 0 )");
436:                    fail("Expected SQLException");
437:                } catch (SQLException e) {
438:                    // expected
439:                }
440:            }
441:
442:            // Self-Referring Table
443:            public void testSelfReferringTable() throws Exception {
444:                _stmt.executeUpdate("create table a(x number(5), y number(5))");
445:                _stmt
446:                        .executeUpdate("alter table a add constraint primary key (x)");
447:                _stmt
448:                        .executeUpdate("alter table a add constraint foreign key (y) references a(x)");
449:            }
450:
451:            // Dropping child table should drop related FK-constraints
452:            public void testDropChildTable() throws Exception {
453:                _stmt
454:                        .executeUpdate("create table x(y number(5), constraint x_pk primary key(y))");
455:                _stmt
456:                        .executeUpdate("create table y(y number(5), constraint y_fk foreign key (y) references x)");
457:                _stmt.executeUpdate("drop table y");
458:                _stmt
459:                        .executeUpdate("create table y(y number(5), constraint y_fk foreign key (y) references x)");
460:            }
461:
462:            public void testNullValueForRefColumns() throws Exception {
463:                _stmt
464:                        .executeUpdate("create table a(x number(9) not null, y number(9))");
465:                _stmt.executeUpdate("create table b(x number(9), y number(9))");
466:                _stmt
467:                        .executeUpdate("alter table a add constraint a_pk primary key (x)");
468:                _stmt
469:                        .executeUpdate("alter table b add constraint b_fk foreign key (x) references a(x)");
470:                _stmt.executeUpdate("insert into b values (null, 1)");
471:                _stmt.executeUpdate("insert into a values (1, 1)");
472:                _stmt.executeUpdate("insert into b values (1, 1)");
473:            }
474:
475:            public void testNullValueForRefColumns2() throws Exception {
476:                _stmt
477:                        .executeUpdate("create table a(x number(9) not null, y number(9))");
478:                _stmt.executeUpdate("create table c(x number(9), y number(9))");
479:
480:                _stmt
481:                        .executeUpdate("alter table a add constraint a_pk primary key (x,y)");
482:                _stmt
483:                        .executeUpdate("alter table c add constraint c_fk foreign key (x,y) references a(x,y)");
484:
485:                _stmt.executeUpdate("insert into c values (null, null)");
486:                _stmt.executeUpdate("insert into c values (1, null)");
487:                _stmt.executeUpdate("insert into a values (1, 1)");
488:                _stmt.executeUpdate("insert into c values (1, 1)");
489:            }
490:
491:            //multi-column-PK/FK-feature issues
492:            public void testMultiColumnPKFK() throws Exception {
493:                _stmt
494:                        .executeUpdate("create table A(X number(9,0), Y number(9,0))");
495:                _stmt
496:                        .executeUpdate("create table B (X number(9,0),  Y number(9,0))");
497:                _stmt
498:                        .executeUpdate("ALTER TABLE A ADD CONSTRAINT A_PK PRIMARY KEY (X,Y)");
499:                _stmt
500:                        .executeUpdate("ALTER TABLE B ADD CONSTRAINT B2A FOREIGN KEY (X,Y) REFERENCES A");
501:
502:                _stmt.executeUpdate("insert into a values (1,1)");
503:                _stmt.executeUpdate("insert into a values (1,2)");
504:                _stmt.executeUpdate("insert into b values (1,1)");
505:                _stmt.executeUpdate("insert into b values (1,2)");
506:            }
507:
508:            public void testMultiColumnPKFK2() throws Exception {
509:                _stmt
510:                        .executeUpdate("create table A  (X number(9,0),  Y number(9,0))");
511:                _stmt
512:                        .executeUpdate("create table B (X number(9,0),  Y number(9,0))");
513:                _stmt
514:                        .executeUpdate("ALTER TABLE A ADD CONSTRAINT A_PK PRIMARY KEY (X,Y)");
515:
516:                _stmt.executeUpdate("insert into a values (1,1)");
517:                _stmt.executeUpdate("insert into a values (1,2)");
518:                _stmt.executeUpdate("insert into a values (2,1)");
519:                _stmt.executeUpdate("insert into a values (2,2)");
520:
521:                _stmt
522:                        .executeUpdate("ALTER TABLE B ADD CONSTRAINT B2A FOREIGN KEY (X,Y) REFERENCES A(X,Y)");
523:            }
524:
525:            public void testMultiTableFKForOnePK() throws Exception {
526:                _stmt.executeUpdate("create table A (X number(9,0))");
527:                _stmt.executeUpdate("create table B (X number(9,0))");
528:                _stmt.executeUpdate("create table C (Y number(9,0))");
529:
530:                _stmt
531:                        .executeUpdate("ALTER TABLE A ADD CONSTRAINT A_PK PRIMARY KEY (X)");
532:                _stmt
533:                        .executeUpdate("ALTER TABLE B ADD CONSTRAINT B2A FOREIGN KEY (X)  REFERENCES A");
534:                _stmt
535:                        .executeUpdate("ALTER TABLE C ADD CONSTRAINT C2A FOREIGN KEY (Y)  REFERENCES A");
536:                // TODO: make this test richer        
537:            }
538:
539:            public void testAutoReferenceFK() throws Exception {
540:                _stmt
541:                        .executeUpdate("create table x(y number(5), constraint x_pk primary key(y))");
542:                _stmt.executeUpdate("create table y(y number(5) references x)");
543:                // TODO: make this test richer
544:            }
545:
546:            //<referential action> ::= CASCADE | SET NULL | SET DEFAULT | RESTRICT | NO ACTION
547:            public void testReferentialActions() throws Exception {
548:                _stmt
549:                        .executeUpdate("create table x(y number(5), constraint x_pk primary key(y))");
550:                _stmt
551:                        .executeUpdate("create table y(y number(5), constraint y_fk foreign key (y) references x ON DELETE CASCADE)");
552:                // TODO: make this test richer
553:                //_stmt.executeUpdate("create table y(y number(5), constraint y_fk foreign key (y) references x ON DELETE SET NULL)");
554:                //_stmt.executeUpdate("create table y(y number(5), constraint y_fk foreign key (y) references x ON DELETE SET DEFAULT)");
555:                //_stmt.executeUpdate("create table y(y number(5), constraint y_fk foreign key (y) references x ON DELETE RESTRICT)");
556:            }
557:
558:            //<referential action> ::= CASCADE | SET NULL | SET DEFAULT | RESTRICT | NO ACTION
559:            public void testReferentialActions2() throws Exception {
560:                _stmt
561:                        .executeUpdate("create table x(y number(5), constraint x_pk primary key(y))");
562:                _stmt
563:                        .executeUpdate("create table y(y number(5), constraint y_fk foreign key (y) references x ON UPDATE CASCADE)");
564:                // TODO: make this test richer
565:                //_stmt.executeUpdate("create table y(y number(5), constraint y_fk foreign key (y) references x ON UPDATE SET NULL)");
566:                //_stmt.executeUpdate("create table y(y number(5), constraint y_fk foreign key (y) references x ON UPDATE SET DEFAULT)");
567:                //_stmt.executeUpdate("create table y(y number(5), constraint y_fk foreign key (y) references x ON UPDATE RESTRICT)");
568:            }
569:
570:            // Deferred evaluation of constraints
571:            public void testDeferredEvaluationOfConstraints() throws Exception {
572:                _stmt.executeUpdate("create table x(y number(5))");
573:                _stmt
574:                        .executeUpdate("alter table x add  constraint x_pk primary key(y) deferrable initially deferred");
575:
576:                _stmt.executeUpdate("create table y(y number(5))");
577:                _stmt
578:                        .executeUpdate("alter table y add  constraint y_fk foreign key(y) references x deferrable initially deferred");
579:
580:                _conn.setAutoCommit(false);
581:                _stmt.executeUpdate("insert into x values(1)");
582:                _stmt.executeUpdate("insert into x values(2)");
583:
584:                _stmt.executeUpdate("insert into y values(2)");
585:                _conn.commit();
586:            }
587:
588:            // Dropping of PK while FK exists
589:            // When PK is deleted the FK depend on it should be deleted too, 
590:            // may be we can force CASCADE to be used.
591:            public void testDroppingPKWhileFKExists() throws Exception {
592:                _stmt
593:                        .executeUpdate("create table x(y number(5), constraint x_pk primary key(y))");
594:                _stmt
595:                        .executeUpdate("create table y(y number(5), constraint y_fk foreign key (y) references x)");
596:                _stmt
597:                        .executeUpdate("alter table x drop constraint x_pk cascade");
598:                _stmt.executeUpdate("insert into y values(1)");
599:            }
600:
601:            // TODO : test "drop table table_name cascade constraints"
602:            // should delete all foreign keys that reference the table to be dropped, 
603:            // then drops the table. dropping table should check if there is any view 
604:            // refering the table.
605:
606:            // TODO : test "truncate table table_name cascade constraints"
607:            // Allow table to be truncated if all the child tables has zero rows; 
608:            // fail when child table exist with non-zero row count.
609:
610:            // TODO : test "drop view table_name" 
611:            // view could be refered by another view; in such case throw exception
612:
613:            // TODO: Test check for reference while:    
614:            // a) renaming table b) dropping/renaming column. (may be used by index/constraint)
615:
616:            // TODO : Test, should not be able to delete dblink if one or more external table is 
617:            // still referring it, unless user choose to use cascade.
618:
619:            // Note: CASCADE can be used to force the operation by removing the refence or object.
620:
621:            // TODO : test "drop table table_name cascade constraints"    
622:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.