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


001:        /*
002:         * $Id: AbstractTableTest.java,v 1.21 2005/12/20 18:32:42 ahimanikya Exp $
003:         * =======================================================================
004:         * Copyright (c) 2002 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.engine.tables;
042:
043:        import java.io.File;
044:        import java.io.FileOutputStream;
045:        import java.util.NoSuchElementException;
046:        import java.util.Random;
047:
048:        import org.axiondb.AbstractDbdirTest;
049:        import org.axiondb.AxionException;
050:        import org.axiondb.Column;
051:        import org.axiondb.ColumnIdentifier;
052:        import org.axiondb.Database;
053:        import org.axiondb.Index;
054:        import org.axiondb.Literal;
055:        import org.axiondb.Person;
056:        import org.axiondb.Row;
057:        import org.axiondb.RowIterator;
058:        import org.axiondb.Table;
059:        import org.axiondb.TableIdentifier;
060:        import org.axiondb.constraints.NotNullConstraint;
061:        import org.axiondb.constraints.NullConstraint;
062:        import org.axiondb.constraints.PrimaryKeyConstraint;
063:        import org.axiondb.engine.DiskDatabase;
064:        import org.axiondb.engine.commands.AddConstraintCommand;
065:        import org.axiondb.engine.commands.AlterTableCommand;
066:        import org.axiondb.engine.rows.SimpleRow;
067:        import org.axiondb.io.FileUtil;
068:        import org.axiondb.types.BooleanType;
069:        import org.axiondb.types.CharacterVaryingType;
070:        import org.axiondb.types.IntegerType;
071:        import org.axiondb.types.ObjectType;
072:
073:        /**
074:         * @version $Revision: 1.21 $ $Date: 2005/12/20 18:32:42 $
075:         * @author Rodney Waldhoff
076:         * @author Chuck Burdick
077:         */
078:        public abstract class AbstractTableTest extends AbstractDbdirTest {
079:
080:            //------------------------------------------------------------ Conventional
081:
082:            public AbstractTableTest(String testName) {
083:                super (testName);
084:            }
085:
086:            //--------------------------------------------------------------- Lifecycle
087:
088:            protected Table table = null;
089:
090:            protected abstract Table createTable(String name) throws Exception;
091:
092:            protected abstract File getDataFile() throws Exception;
093:
094:            protected abstract Database getDatabase() throws Exception;
095:
096:            protected Table getTable() {
097:                return table;
098:            }
099:
100:            protected String getTableName() {
101:                return "FOO";
102:            }
103:
104:            public void setUp() throws Exception {
105:                super .setUp();
106:                table = createTable(getTableName());
107:            }
108:
109:            public void tearDown() throws Exception {
110:                if (table != null) {
111:                    table.shutdown();
112:                    table = null;
113:                }
114:                super .tearDown();
115:            }
116:
117:            //------------------------------------------------------------------- Tests
118:
119:            public void testGetName() throws Exception {
120:                assertEquals(getTableName().toUpperCase(), table.getName());
121:            }
122:
123:            public void testToString() throws Exception {
124:                assertNotNull(table.getName());
125:                RowIterator iter = table.getRowIterator(false);
126:                assertNotNull(iter.toString());
127:            }
128:
129:            public void testAddThenDropConstraint() throws Exception {
130:                addColumns();
131:                addRows();
132:                Database db = getDatabase();
133:                db.addTable(table);
134:                PrimaryKeyConstraint pk = new PrimaryKeyConstraint("PK_FOO");
135:                pk.addSelectable(new ColumnIdentifier(new TableIdentifier(table
136:                        .getName()), "ID"));
137:                table.addConstraint(pk);
138:                Column column = table.getColumn("ID");
139:                if (!table.isColumnIndexed(column)) {
140:                    assertFalse(table.hasIndex("BOGUS"));
141:                    Index index1 = db.getIndexFactory("btree")
142:                            .makeNewSystemInstance(table, column,
143:                                    db.getDBDirectory() == null);
144:                    Index index2 = db.getIndexFactory("array").makeNewInstance(
145:                            "INDEX_FOO", column, true,
146:                            db.getDBDirectory() == null);
147:                    db.addIndex(index1, table, true);
148:                    assertTrue(table.hasIndex(index1.getName()));
149:                    db.addIndex(index2, table, true);
150:                    assertTrue(table.hasIndex(index2.getName()));
151:                }
152:
153:                try {
154:                    table.addConstraint(new PrimaryKeyConstraint("PK_BAR"));
155:                    fail("Expected AxionException");
156:                } catch (AxionException e) {
157:                    // expected
158:                }
159:                try {
160:                    table.addConstraint(new NotNullConstraint("PK_FOO"));
161:                    fail("Expected AxionException");
162:                } catch (AxionException e) {
163:                    // expected
164:                }
165:
166:                table.addConstraint(new NotNullConstraint("NN_FOO"));
167:                table.removeConstraint("this constraint does not exist");
168:                table.removeConstraint("PRIMARYKEY");
169:                table.addConstraint(pk);
170:                table.removeConstraint("PK_FOO");
171:                table.removeConstraint("primarykey"); // shd be silent
172:
173:                table.addConstraint(pk);
174:                table.removeConstraint("NN_FOO");
175:                table.addConstraint(new NullConstraint("N_FOO"));
176:                table.removeConstraint("N_FOO");
177:                table.removeConstraint(null);
178:                db.dropTable(table.getName());
179:                table = null;
180:            }
181:
182:            public void testAddThenDropColumn() throws Exception {
183:                addColumns();
184:                addRows();
185:                Database db = getDatabase();
186:                db.addTable(getTable());
187:
188:                PrimaryKeyConstraint pk = new PrimaryKeyConstraint("PK_FOO");
189:                pk.addSelectable(new ColumnIdentifier(new TableIdentifier(
190:                        getTableName()), "ID", null, new IntegerType()));
191:
192:                AddConstraintCommand addCCmd = new AddConstraintCommand(
193:                        getTableName(), pk);
194:                addCCmd.execute(db);
195:
196:                AlterTableCommand alterCmd = new AlterTableCommand(table
197:                        .getName(), false);
198:                alterCmd.addColumn("NEWCOL", "varchar", "10", "0", new Literal(
199:                        "Test"), null);
200:                alterCmd.execute(db);
201:                table = db.getTable(getTableName());
202:                assertTrue(table.hasColumn(new ColumnIdentifier("NEWCOL")));
203:                assertNotNull(table.getConstraints().next());
204:                RowIterator iter = table.getRowIterator(true);
205:                assertNotNull(iter);
206:                assertTrue(iter.hasNext());
207:                Row row = iter.next();
208:                assertEquals("Test", row.get(2));
209:                assertTrue(iter.hasNext());
210:                row = iter.next();
211:                assertEquals("Test", row.get(2));
212:                assertTrue(!iter.hasNext());
213:
214:                alterCmd = new AlterTableCommand(table.getName(), false);
215:                alterCmd.dropColumn("NEWCOL");
216:                alterCmd.execute(db);
217:                table = db.getTable(getTableName());
218:                assertFalse(table.hasColumn(new ColumnIdentifier("NEWCOL")));
219:                iter = table.getRowIterator(true);
220:                assertNotNull(iter);
221:                assertTrue(iter.hasNext());
222:                row = iter.next();
223:                assertEquals("one", row.get(1));
224:                assertTrue(iter.hasNext());
225:                row = iter.next();
226:                assertEquals("two", row.get(1));
227:                assertTrue(!iter.hasNext());
228:
229:                try {
230:                    alterCmd = new AlterTableCommand("BOGUS", false);
231:                    alterCmd.dropColumn("NEWCOL");
232:                    alterCmd.execute(db);
233:                    fail("Expected Exception - table does not exist");
234:                } catch (AxionException e) {
235:                    // expected table does not exist
236:                }
237:
238:                try {
239:                    alterCmd = new AlterTableCommand(table.getName(), false);
240:                    alterCmd.dropColumn("NEWCOL"); // does not exist
241:                    alterCmd.execute(db);
242:                    fail("Expected Exception - Bad column to drop");
243:                } catch (AxionException e) {
244:                    // expected table does not exist
245:                }
246:
247:                try {
248:                    AlterTableCommand cmd = new AlterTableCommand("FOO", false);
249:                    cmd.executeQuery(db);
250:                    fail("Expected UnsupportedOperationException");
251:                } catch (UnsupportedOperationException e) {
252:                    // expected
253:                }
254:            }
255:
256:            public void testGetMatchingRowsForNull() throws Exception {
257:                RowIterator iter = table.getMatchingRows(null, null, true);
258:                assertNotNull(iter);
259:            }
260:
261:            public void testHasColumn() throws Exception {
262:                ColumnIdentifier id = new ColumnIdentifier("FOO");
263:                assertTrue("Should not have column", !table.hasColumn(id));
264:                try {
265:                    table.getColumnIndex("FOO");
266:                    fail("Expected AxionException");
267:                } catch (AxionException e) {
268:                    // expected
269:                }
270:                table
271:                        .addColumn(new Column("FOO", new CharacterVaryingType(
272:                                10)));
273:                assertTrue("Should have column", table.hasColumn(id));
274:                id.setTableIdentifier(new TableIdentifier(getTableName()));
275:                assertTrue("Should have column", table.hasColumn(id));
276:
277:                id.setTableIdentifier(new TableIdentifier("BOGUS"));
278:                assertTrue("Should not have column", !table.hasColumn(id));
279:            }
280:
281:            protected void addColumns() throws Exception {
282:                table.addColumn(new Column("ID", new IntegerType()));
283:                table
284:                        .addColumn(new Column("NAME", new CharacterVaryingType(
285:                                10)));
286:            }
287:
288:            public void addRows() throws Exception {
289:                {
290:                    Row row = new SimpleRow(2);
291:                    row.set(0, new Integer(1));
292:                    row.set(1, "one");
293:                    table.addRow(row);
294:                }
295:                {
296:                    Row row = new SimpleRow(2);
297:                    row.set(0, new Integer(2));
298:                    row.set(1, "two");
299:                    table.addRow(row);
300:                }
301:                assertEquals("Should have 2 rows", 2, table.getRowCount());
302:            }
303:
304:            public void testAddRow() throws Exception {
305:                addColumns();
306:                addRows();
307:            }
308:
309:            public void testTruncate() throws Exception {
310:                table.truncate();
311:                RowIterator iter = table.getRowIterator(true);
312:                assertNotNull(iter);
313:                assertFalse(iter.hasNext());
314:
315:                addColumns();
316:                addRows();
317:                iter = table.getRowIterator(true);
318:                assertNotNull(iter);
319:                assertNotNull(iter.next());
320:                assertNotNull(iter.next());
321:                assertFalse(iter.hasNext());
322:
323:                // create backup file before to test truncate deletes it
324:                File df = getDataFile();
325:                if (df != null) {
326:                    File bkupFile = new File(df.getParentFile(), df.getName()
327:                            + ".backup");
328:                    FileOutputStream out = new FileOutputStream(bkupFile);
329:                    out.write("test".getBytes());
330:                    out.close();
331:                }
332:
333:                table.truncate();
334:                iter = table.getRowIterator(true);
335:                assertNotNull(iter);
336:                assertFalse(iter.hasNext());
337:
338:                addRows();
339:                iter = table.getRowIterator(true);
340:                assertNotNull(iter);
341:                assertNotNull(iter.next());
342:                assertNotNull(iter.next());
343:                assertFalse(iter.hasNext());
344:
345:                // TODO: Somehow Unix is not locking bkupFile, so find out a way to lock the backup file and try this test
346:                // create backup file before to test truncate deletes it
347:                //        df = getDataFile();
348:                //        if (df != null) {
349:                //            File bkupFile = new File(df.getParentFile(), df.getName() + ".backup");
350:                //            FileOutputStream fos = new FileOutputStream(bkupFile);
351:                //            FileLock lock = fos.getChannel().lock(0L, Long.MAX_VALUE, false);
352:                //            
353:                //            try {
354:                //                table.truncate();
355:                //                fail("Expected Exception");
356:                //            } catch (AxionException e) {
357:                //                // expected
358:                //            } finally {
359:                //                try {
360:                //                    lock.release();
361:                //                    fos.close();
362:                //                    FileUtil.delete(bkupFile);
363:                //                } catch (IOException ioe) {
364:                //                    // ignore
365:                //                }
366:                //            }
367:                //        }
368:
369:            }
370:
371:            public void testDefrag() throws Exception {
372:                Database db = getDatabase();
373:
374:                if (db instanceof  DiskDatabase) {
375:
376:                    DiskDatabase diskDB = (DiskDatabase) db;
377:                    addColumns();
378:                    addRows();
379:                    db.addTable(table);
380:                    table.shutdown();
381:
382:                    long oldLength = FileUtil.getLength(getDataFile());
383:                    diskDB.defragTable(getTableName());
384:                    long newLength = FileUtil.getLength(getDataFile());
385:                    assertTrue("Expected " + oldLength + " = " + newLength,
386:                            oldLength == newLength);
387:
388:                    table = db.getTable(getTableName());
389:                    RowIterator iter = table.getRowIterator(false);
390:                    assertNotNull(iter);
391:
392:                    assertTrue(iter.hasNext());
393:                    assertNotNull(iter.next());
394:                    Row row = iter.current();
395:                    iter.remove();
396:                    table.addRow(new SimpleRow(row));
397:                    iter.reset();
398:
399:                    assertTrue(iter.hasNext());
400:                    assertNotNull(iter.next());
401:                    row = iter.current();
402:                    iter.set(new SimpleRow(row));
403:
404:                    assertTrue(iter.hasNext());
405:                    assertNotNull(iter.next());
406:
407:                    assertFalse(iter.hasNext());
408:
409:                    oldLength = FileUtil.getLength(getDataFile());
410:                    ;
411:                    diskDB.defragTable(getTableName());
412:
413:                    newLength = FileUtil.getLength(getDataFile());
414:
415:                    assertTrue("Expected " + oldLength + " > " + newLength,
416:                            oldLength > newLength);
417:
418:                    table = diskDB.getTable(getTableName());
419:                    iter = table.getRowIterator(false);
420:                    assertNotNull(iter);
421:
422:                    assertTrue(iter.hasNext());
423:                    assertNotNull(iter.next());
424:
425:                    assertTrue(iter.hasNext());
426:                    assertNotNull(iter.next());
427:
428:                    assertFalse(iter.hasNext());
429:                }
430:            }
431:
432:            public void testGetRowIterator() throws Exception {
433:                table.addColumn(new Column("ID", new IntegerType()));
434:                table
435:                        .addColumn(new Column("NAME", new CharacterVaryingType(
436:                                10)));
437:                {
438:                    Row row = new SimpleRow(2);
439:                    row.set(0, new Integer(1));
440:                    row.set(1, "one");
441:                    table.addRow(row);
442:                }
443:                {
444:                    Row row = new SimpleRow(2);
445:                    row.set(0, new Integer(2));
446:                    row.set(1, "two");
447:                    table.addRow(row);
448:                }
449:                {
450:                    Row row = new SimpleRow(2);
451:                    row.set(0, new Integer(3));
452:                    row.set(1, "three");
453:                    table.addRow(row);
454:                }
455:                RowIterator iter = table.getRowIterator(false);
456:                assertNotNull(iter);
457:
458:                try {
459:                    iter.current();
460:                    fail("Expected NoSuchElementException");
461:                } catch (NoSuchElementException ex) {
462:                    // Expected
463:                }
464:
465:                try {
466:                    iter.set(null);
467:                    fail("Expected IllegalStateException");
468:                } catch (IllegalStateException ex) {
469:                    // Expected
470:                }
471:
472:                try {
473:                    iter.remove();
474:                    fail("Expected IllegalStateException");
475:                } catch (IllegalStateException ex) {
476:                    // Expected
477:                }
478:
479:                // Iteration Pass 1
480:                assertFalse(iter.hasPrevious());
481:
482:                try {
483:                    iter.previous();
484:                    fail("Expected NoSuchElementException");
485:                } catch (NoSuchElementException ex) {
486:                    // Expected
487:                }
488:
489:                assertTrue(iter.hasNext());
490:                assertNotNull(iter.next());
491:                assertNotNull(iter.current());
492:                assertTrue(iter.hasNext());
493:                assertNotNull(iter.next());
494:                assertNotNull(iter.current());
495:                assertTrue(iter.hasNext());
496:                assertNotNull(iter.next());
497:                assertNotNull(iter.current());
498:
499:                assertFalse(iter.hasNext());
500:                assertEquals(iter.previousIndex(), iter.nextIndex() - 1);
501:                try {
502:                    iter.next();
503:                    fail("Expected NoSuchElementException");
504:                } catch (NoSuchElementException ex) {
505:                    // Expected
506:                }
507:
508:                // Iteration Pass 2 : update row
509:                Row row = new SimpleRow(2);
510:                row.set(0, new Integer(4));
511:                row.set(1, "newRow");
512:
513:                iter.reset();
514:                assertFalse(iter.hasPrevious());
515:
516:                assertTrue(iter.hasNext());
517:                assertNotNull(iter.next());
518:                iter.set(row);
519:                assertNotNull(iter.current());
520:
521:                assertTrue(iter.hasNext());
522:                assertNotNull(iter.next());
523:                iter.set(row);
524:                assertNotNull(iter.current());
525:
526:                assertTrue(iter.hasNext());
527:                assertNotNull(iter.next());
528:                iter.set(row);
529:                assertNotNull(iter.current());
530:
531:                assertFalse(iter.hasNext());
532:
533:                // Iteration Pass 3
534:                assertTrue(iter.hasPrevious());
535:                assertNotNull(iter.previous());
536:                assertTrue(iter.hasPrevious());
537:                assertNotNull(iter.previous());
538:                assertTrue(iter.hasPrevious());
539:                assertNotNull(iter.previous());
540:
541:                assertFalse(iter.hasPrevious());
542:
543:                assertTrue(iter.hasNext());
544:            }
545:
546:            public void testNoNewColumnsAfterRowsAdded() throws Exception {
547:                table.addColumn(new Column("ID", new IntegerType()));
548:                table
549:                        .addColumn(new Column("NAME", new CharacterVaryingType(
550:                                10)));
551:                Row row = new SimpleRow(2);
552:                row.set(0, new Integer(1));
553:                row.set(1, "one");
554:                table.addRow(row);
555:                try {
556:                    table.addColumn(new Column("NAMETWO",
557:                            new CharacterVaryingType(10)));
558:                    fail("Expected AxionException");
559:                } catch (AxionException e) {
560:                    // expected
561:                }
562:            }
563:
564:            public void testGetColumnByIndex() throws Exception {
565:                table.addColumn(new Column("ID", new IntegerType()));
566:                assertEquals("ID", table.getColumn(0).getName());
567:                table
568:                        .addColumn(new Column("NAME", new CharacterVaryingType(
569:                                10)));
570:                assertEquals("ID", table.getColumn(0).getName());
571:                assertEquals("NAME", table.getColumn(1).getName());
572:            }
573:
574:            public void testGetColumnByIndexBadIndex() throws Exception {
575:                try {
576:                    table.getColumn(-1);
577:                    fail("Expected IndexOutOfBoundsException");
578:                } catch (IndexOutOfBoundsException e) {
579:                    // expected
580:                }
581:
582:                try {
583:                    table.getColumn(0);
584:                    fail("Expected IndexOutOfBoundsException");
585:                } catch (IndexOutOfBoundsException e) {
586:                    // expected
587:                }
588:
589:                table.addColumn(new Column("ID", new IntegerType()));
590:
591:                try {
592:                    table.getColumn(-1);
593:                    fail("Expected IndexOutOfBoundsException");
594:                } catch (IndexOutOfBoundsException e) {
595:                    // expected
596:                }
597:
598:                try {
599:                    table.getColumn(1);
600:                    fail("Expected IndexOutOfBoundsException");
601:                } catch (IndexOutOfBoundsException e) {
602:                    // expected
603:                }
604:
605:                table
606:                        .addColumn(new Column("NAME", new CharacterVaryingType(
607:                                10)));
608:
609:                try {
610:                    table.getColumn(-1);
611:                    fail("Expected IndexOutOfBoundsException");
612:                } catch (IndexOutOfBoundsException e) {
613:                    // expected
614:                }
615:
616:                try {
617:                    table.getColumn(2);
618:                    fail("Expected IndexOutOfBoundsException");
619:                } catch (IndexOutOfBoundsException e) {
620:                    // expected
621:                }
622:            }
623:
624:            public void testGetAndFreeRowId() throws Exception {
625:                int id = table.getNextRowId();
626:                table.freeRowId(id);
627:                assertEquals(id, table.getNextRowId());
628:                int id2 = table.getNextRowId();
629:                assertTrue(id != id2);
630:            }
631:
632:            public void testGetColumnByName() throws Exception {
633:                table.addColumn(new Column("ID", new IntegerType()));
634:                assertTrue(table.getColumn("ID").getDataType() instanceof  IntegerType);
635:                table
636:                        .addColumn(new Column("NAME", new CharacterVaryingType(
637:                                10)));
638:                assertTrue(table.getColumn("ID").getDataType() instanceof  IntegerType);
639:                assertTrue(table.getColumn("NAME").getDataType() instanceof  CharacterVaryingType);
640:            }
641:
642:            public void testGetColumnByNameBadName() throws Exception {
643:                assertNull(table.getColumn("FOO"));
644:                assertNull(table.getColumn("ID"));
645:                table.addColumn(new Column("ID", new IntegerType()));
646:                assertNull(table.getColumn("FOO"));
647:                assertNull(table.getColumn("NAME"));
648:                table
649:                        .addColumn(new Column("NAME", new CharacterVaryingType(
650:                                10)));
651:                assertNull(table.getColumn("FOO"));
652:            }
653:
654:            public void testDataTypes() throws Exception {
655:                Table typeTable = createTable("TYPETABLE");
656:
657:                typeTable.addColumn(new Column("STRCOL",
658:                        new CharacterVaryingType(30)));
659:                typeTable.addColumn(new Column("INTCOL", new IntegerType()));
660:                typeTable.addColumn(new Column("BOOLCOL", new BooleanType()));
661:
662:                Object[][] values = new Object[][] {
663:                        new Object[] { "", "A String", "Another String", null },
664:                        new Object[] { new Integer(17), new Integer(0),
665:                                new Integer(5575), null },
666:                        new Object[] { Boolean.TRUE, Boolean.TRUE,
667:                                Boolean.FALSE, null } };
668:
669:                Random random = new Random();
670:                int numRows = 7;
671:
672:                for (int i = 0; i < numRows; i++) {
673:                    Row row = new SimpleRow(typeTable.getColumnCount());
674:                    for (int j = 0; j < typeTable.getColumnCount(); j++) {
675:                        row.set(j, values[j][random.nextInt(values[j].length)]);
676:                    }
677:                    typeTable.addRow(row);
678:                }
679:
680:                RowIterator iter = typeTable.getRowIterator(true);
681:                assertNotNull(iter);
682:                for (int i = 0; i < numRows; i++) {
683:                    assertTrue(iter.hasNext());
684:                    assertNotNull(iter.next());
685:                }
686:                assertTrue(!iter.hasNext());
687:                typeTable.shutdown();
688:            }
689:
690:            public void testObjectTable() throws Exception {
691:                Table personTable = createTable("PERSON");
692:
693:                personTable.addColumn(new Column("KEY", new ObjectType()));
694:                personTable.addColumn(new Column("VALUE", new ObjectType()));
695:
696:                Row row = new SimpleRow(personTable.getColumnCount());
697:                Person person = new Person("James", 1969);
698:
699:                row.set(0, person.getName());
700:                row.set(1, person);
701:                personTable.addRow(row);
702:
703:                row = new SimpleRow(personTable.getColumnCount());
704:                person = new Person("Simon", 1971);
705:
706:                row.set(0, person.getName());
707:                row.set(1, person);
708:                personTable.addRow(row);
709:
710:                RowIterator iter = personTable.getRowIterator(true);
711:                assertNotNull(iter);
712:
713:                assertTrue(iter.hasNext());
714:                row = iter.next();
715:                assertNotNull(row);
716:
717:                Object value = row.get(0);
718:                assertEquals("James", value);
719:                value = row.get(1);
720:                assertTrue("Found person object", value instanceof  Person);
721:                person = (Person) value;
722:                assertEquals("James", person.getName());
723:                assertEquals(1969, person.getDateOfBirth());
724:
725:                assertTrue(iter.hasNext());
726:                row = iter.next();
727:                assertNotNull(row);
728:
729:                value = row.get(0);
730:                assertEquals("Simon", value);
731:                value = row.get(1);
732:                assertTrue("Found person object", value instanceof  Person);
733:                person = (Person) value;
734:                assertEquals("Simon", person.getName());
735:                assertEquals(1971, person.getDateOfBirth());
736:
737:                assertTrue(!iter.hasNext());
738:                personTable.shutdown();
739:            }
740:
741:            public void testAddPrimaryKeyConstraintOnPopulatedTable()
742:                    throws Exception {
743:                table.addColumn(new Column("ID", new IntegerType()));
744:                table
745:                        .addColumn(new Column("NAME", new CharacterVaryingType(
746:                                10)));
747:                {
748:                    Row row = new SimpleRow(2);
749:                    row.set(0, new Integer(1));
750:                    row.set(1, "one");
751:                    table.addRow(row);
752:                }
753:                {
754:                    Row row = new SimpleRow(2);
755:                    row.set(0, new Integer(2));
756:                    row.set(1, "two");
757:                    table.addRow(row);
758:                }
759:
760:                PrimaryKeyConstraint pk = new PrimaryKeyConstraint("PK_FOO");
761:                ColumnIdentifier colId = new ColumnIdentifier(
762:                        new TableIdentifier(table.getName()), "ID");
763:                pk.addSelectable(colId);
764:
765:                table.addConstraint(pk);
766:
767:                // Adding duplicate ID should fail.
768:                {
769:                    Row row = new SimpleRow(2);
770:                    row.set(0, new Integer(2));
771:                    row.set(1, "two");
772:                    try {
773:                        table.addRow(row);
774:                        fail("Expected AxionException on adding row with duplicate ID to table with PK");
775:                    } catch (AxionException expected) {
776:                        // Expected.
777:                    }
778:                }
779:
780:                // Now drop constraint, then add a duplicate row - primary key constraint should
781:                // fail on add
782:                table.removeConstraint("PK_FOO");
783:                {
784:                    Row row = new SimpleRow(2);
785:                    row.set(0, new Integer(2));
786:                    row.set(1, "two");
787:                    table.addRow(row);
788:                }
789:                try {
790:                    table.addConstraint(pk);
791:                    fail("Expected AxionException on applying PK constraint to existing table with dup rows");
792:                } catch (AxionException expected) {
793:                    // Expected.
794:                }
795:            }
796:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.