KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > engine > database > model > TableTest


1 package org.apache.torque.engine.database.model;
2
3 /*
4  * Copyright 2003-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.List JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.apache.torque.engine.database.transform.XmlToAppData;
24
25 /**
26  * Tests for package handling.
27  *
28  * @author <a HREF="mailto:mpoeschl@marmot.at>Martin Poeschl</a>
29  * @version $Id: TableTest.java,v 1.9 2004/02/22 06:29:38 jmcnally Exp $
30  */

31 public class TableTest extends TestCase
32 {
33     private XmlToAppData xmlToAppData = null;
34     private Database db = null;
35
36     public TableTest(String JavaDoc name)
37     {
38         super(name);
39     }
40
41     protected void setUp() throws Exception JavaDoc
42     {
43         super.setUp();
44         xmlToAppData = new XmlToAppData("mysql", "defaultpackage");
45         db = xmlToAppData.parseFile(
46             "src/test/org/apache/torque/engine/database/model/tabletest-schema.xml");
47     }
48
49     protected void tearDown() throws Exception JavaDoc
50     {
51         xmlToAppData = null;
52         super.tearDown();
53     }
54
55     /**
56      * test if the tables get the package name from the properties file
57      */

58     public void testIdMethodHandling() throws Exception JavaDoc
59     {
60         assertEquals(IDMethod.ID_BROKER, db.getDefaultIdMethod());
61         Table table = db.getTable("table_idbroker");
62         assertEquals(IDMethod.ID_BROKER, table.getIdMethod());
63         Table table2 = db.getTable("table_native");
64         assertEquals(IDMethod.NATIVE, table2.getIdMethod());
65     }
66     
67     public void testNoPk() throws Exception JavaDoc
68     {
69         Table table = db.getTable("nopk");
70         assertFalse(table.hasPrimaryKey());
71         List JavaDoc pks = table.getPrimaryKey();
72         assertTrue(pks.size() == 0);
73     }
74     
75     public void testSinglePk() throws Exception JavaDoc
76     {
77         Table table = db.getTable("singlepk");
78         assertTrue(table.hasPrimaryKey());
79         List JavaDoc pks = table.getPrimaryKey();
80         assertTrue(pks.size() == 1);
81         Column col = (Column) pks.get(0);
82         assertEquals(col.getName(), "singlepk_id");
83     }
84     
85     public void testMultiPk() throws Exception JavaDoc
86     {
87         Table table = db.getTable("multipk");
88         assertTrue(table.hasPrimaryKey());
89         List JavaDoc pks = table.getPrimaryKey();
90         assertTrue(pks.size() == 2);
91         Column cola = (Column) pks.get(0);
92         assertEquals(cola.getName(), "multipk_a");
93         Column colb = (Column) pks.get(1);
94         assertEquals(colb.getName(), "multipk_b");
95         assertEquals(table.printPrimaryKey(), "multipk_a,multipk_b");
96     }
97  
98     public void testSingleFk() throws Exception JavaDoc
99     {
100         Table table = db.getTable("singlefk");
101         List JavaDoc fks = table.getForeignKeys();
102         assertTrue(fks.size() == 1);
103         ForeignKey fk = (ForeignKey) fks.get(0);
104         assertEquals(fk.getForeignTableName(), "singlepk");
105         assertTrue(fk.getForeignColumns().size() == 1);
106         assertFalse(fk.hasOnDelete());
107         assertFalse(fk.hasOnUpdate());
108     }
109
110     public void testOnUpdateOnDelete() throws Exception JavaDoc
111     {
112         Table table = db.getTable("singlefk1");
113         List JavaDoc fks = table.getForeignKeys();
114         assertTrue(fks.size() == 1);
115         ForeignKey fk = (ForeignKey) fks.get(0);
116         assertTrue(fk.hasOnUpdate());
117         assertEquals("CASCADE", fk.getOnUpdate());
118         assertTrue(fk.hasOnDelete());
119         assertEquals("SET NULL", fk.getOnDelete());
120     }
121
122     public void testMultiFk() throws Exception JavaDoc
123     {
124         Table table = db.getTable("multifk");
125         List JavaDoc fks = table.getForeignKeys();
126         assertTrue(fks.size() == 1);
127         ForeignKey fk = (ForeignKey) fks.get(0);
128         assertEquals(fk.getForeignTableName(), "multipk");
129         assertTrue(fk.getForeignColumns().size() == 2);
130     }
131     
132     public void testReferrers() throws Exception JavaDoc
133     {
134         Table table = db.getTable("singlepk");
135         List JavaDoc refs = table.getReferrers();
136         assertTrue(refs.size() == 1);
137         ForeignKey fk = (ForeignKey) refs.get(0);
138         assertEquals(fk.getTableName(), "singlefk");
139     }
140     
141     public void testUnique() throws Exception JavaDoc
142     {
143         Table table = db.getTable("unique_test");
144         List JavaDoc unices = table.getUnices();
145         assertTrue(unices.size() == 1);
146         Unique unique = (Unique) unices.get(0);
147         assertEquals(unique.getName(), "unique_name");
148         assertTrue(unique.getColumns().size() == 2);
149     }
150     
151 }
152
Popular Tags