1   package org.apache.torque.engine.database.model;
2   
3   /*
4    * Copyright 2001-2005 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;
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 239626 2005-08-24 12:19:51Z henning $
30   */
31  public class TableTest extends TestCase
32  {
33      private XmlToAppData xmlToAppData = null;
34      private Database db = null;
35  
36      public TableTest(String name)
37      {
38          super(name);
39      }
40  
41      protected void setUp() throws Exception
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
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
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
68      {
69          Table table = db.getTable("nopk");
70          assertFalse(table.hasPrimaryKey());
71          List pks = table.getPrimaryKey();
72          assertTrue(pks.size() == 0);
73      }
74  
75      public void testSinglePk() throws Exception
76      {
77          Table table = db.getTable("singlepk");
78          assertTrue(table.hasPrimaryKey());
79          List 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
86      {
87          Table table = db.getTable("multipk");
88          assertTrue(table.hasPrimaryKey());
89          List 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
99      {
100         Table table = db.getTable("singlefk");
101         List 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
111     {
112         Table table = db.getTable("singlefk1");
113         List 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
123     {
124         Table table = db.getTable("multifk");
125         List 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
133     {
134         Table table = db.getTable("singlepk");
135         List 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
142     {
143         Table table = db.getTable("unique_test");
144         List 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 }