1   package org.apache.torque.engine.database.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.torque.engine.database.transform.XmlToAppData;
27  
28  /***
29   * Tests for package handling.
30   *
31   * @author <a href="mailto:mpoeschl@marmot.at>Martin Poeschl</a>
32   * @version $Id: TableTest.java 473814 2006-11-11 22:30:30Z tv $
33   */
34  public class TableTest extends TestCase
35  {
36      private XmlToAppData xmlToAppData = null;
37      private Database db = null;
38  
39      public TableTest(String name)
40      {
41          super(name);
42      }
43  
44      protected void setUp() throws Exception
45      {
46          super.setUp();
47          xmlToAppData = new XmlToAppData("mysql", "defaultpackage");
48          db = xmlToAppData.parseFile(
49              "src/test/org/apache/torque/engine/database/model/tabletest-schema.xml");
50      }
51  
52      protected void tearDown() throws Exception
53      {
54          xmlToAppData = null;
55          super.tearDown();
56      }
57  
58      /***
59       * test if the tables get the package name from the properties file
60       */
61      public void testIdMethodHandling() throws Exception
62      {
63          assertEquals(IDMethod.ID_BROKER, db.getDefaultIdMethod());
64          Table table = db.getTable("table_idbroker");
65          assertEquals(IDMethod.ID_BROKER, table.getIdMethod());
66  		Table table2 = db.getTable("table_native");
67  		assertEquals(IDMethod.NATIVE, table2.getIdMethod());
68      }
69  
70      public void testNoPk() throws Exception
71      {
72          Table table = db.getTable("nopk");
73          assertFalse(table.hasPrimaryKey());
74          List pks = table.getPrimaryKey();
75          assertTrue(pks.size() == 0);
76      }
77  
78      public void testSinglePk() throws Exception
79      {
80          Table table = db.getTable("singlepk");
81          assertTrue(table.hasPrimaryKey());
82          List pks = table.getPrimaryKey();
83          assertTrue(pks.size() == 1);
84          Column col = (Column) pks.get(0);
85          assertEquals(col.getName(), "singlepk_id");
86      }
87  
88      public void testMultiPk() throws Exception
89      {
90          Table table = db.getTable("multipk");
91          assertTrue(table.hasPrimaryKey());
92          List pks = table.getPrimaryKey();
93          assertTrue(pks.size() == 2);
94          Column cola = (Column) pks.get(0);
95          assertEquals(cola.getName(), "multipk_a");
96          Column colb = (Column) pks.get(1);
97          assertEquals(colb.getName(), "multipk_b");
98          assertEquals(table.printPrimaryKey(), "multipk_a,multipk_b");
99      }
100 
101     public void testSingleFk() throws Exception
102     {
103         Table table = db.getTable("singlefk");
104         List fks = table.getForeignKeys();
105         assertTrue(fks.size() == 1);
106         ForeignKey fk = (ForeignKey) fks.get(0);
107         assertEquals(fk.getForeignTableName(), "singlepk");
108         assertTrue(fk.getForeignColumns().size() == 1);
109         assertFalse(fk.hasOnDelete());
110         assertFalse(fk.hasOnUpdate());
111     }
112 
113     public void testOnUpdateOnDelete() throws Exception
114     {
115         Table table = db.getTable("singlefk1");
116         List fks = table.getForeignKeys();
117         assertTrue(fks.size() == 1);
118         ForeignKey fk = (ForeignKey) fks.get(0);
119         assertTrue(fk.hasOnUpdate());
120         assertEquals("CASCADE", fk.getOnUpdate());
121         assertTrue(fk.hasOnDelete());
122         assertEquals("SET NULL", fk.getOnDelete());
123     }
124 
125     public void testMultiFk() throws Exception
126     {
127         Table table = db.getTable("multifk");
128         List fks = table.getForeignKeys();
129         assertTrue(fks.size() == 1);
130         ForeignKey fk = (ForeignKey) fks.get(0);
131         assertEquals(fk.getForeignTableName(), "multipk");
132         assertTrue(fk.getForeignColumns().size() == 2);
133     }
134 
135     public void testReferrers() throws Exception
136     {
137         Table table = db.getTable("singlepk");
138         List refs = table.getReferrers();
139         assertTrue(refs.size() == 1);
140         ForeignKey fk = (ForeignKey) refs.get(0);
141         assertEquals(fk.getTableName(), "singlefk");
142     }
143 
144     public void testUnique() throws Exception
145     {
146         Table table = db.getTable("unique_test");
147         List unices = table.getUnices();
148         assertTrue(unices.size() == 1);
149         Unique unique = (Unique) unices.get(0);
150         assertEquals(unique.getName(), "unique_name");
151         assertTrue(unique.getColumns().size() == 2);
152     }
153 
154 }