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 junit.framework.TestCase;
20  
21  import org.apache.torque.engine.database.transform.XmlToAppData;
22  
23  /***
24   * Tests for domain handling (for MySql).
25   *
26   * @version $Id: DomainTest.java 239626 2005-08-24 12:19:51Z henning $
27   */
28  public class DomainTest extends TestCase
29  {
30      private XmlToAppData xmlToAppData = null;
31      private Database db = null;
32  
33      public DomainTest(String name)
34      {
35          super(name);
36      }
37  
38      protected void setUp() throws Exception
39      {
40          super.setUp();
41          xmlToAppData = new XmlToAppData("mysql", "defaultpackage");
42          db = xmlToAppData.parseFile(
43              "src/test/org/apache/torque/engine/database/model/domaintest-schema.xml");
44      }
45  
46      protected void tearDown() throws Exception
47      {
48          xmlToAppData = null;
49          super.tearDown();
50      }
51  
52      /***
53       * test if the tables get the package name from the properties file
54       */
55      public void testAllAttributes() throws Exception
56      {
57          Domain amount = db.getDomain("amount");
58          assertEquals(SchemaType.NUMERIC, amount.getType());
59          assertEquals("DECIMAL", amount.getSqlType());
60          assertEquals("10", amount.getSize());
61          assertEquals("2", amount.getScale());
62          assertEquals("0", amount.getDefaultValue());
63          assertEquals("amount domain", amount.getDescription());
64      }
65  
66      /***
67       * test if the tables get the package name from the properties file
68       */
69      public void testDomainColumn() throws Exception
70      {
71          Table table = db.getTable("product");
72          Column name = table.getColumn("name");
73          assertEquals("VARCHAR", name.getType());
74          assertEquals("VARCHAR", name.getDomain().getSqlType());
75          assertEquals("40", name.getSize());
76          assertEquals("name VARCHAR(40)", name.getSqlString());
77          Column price = table.getColumn("price");
78          assertEquals("NUMERIC", price.getTorqueType());
79          assertEquals("NUMERIC", price.getType());
80          assertEquals("DECIMAL", price.getDomain().getSqlType());
81          assertEquals("10", price.getSize());
82          assertEquals("2", price.getScale());
83          assertEquals("0", price.getDefaultValue());
84          assertEquals("(10,2)", price.printSize());
85          assertEquals("price DECIMAL(10,2) default 0", price.getSqlString());
86      }
87  
88      /***
89       * test if the tables get the package name from the properties file
90       */
91      public void testExtendedDomainColumn() throws Exception
92      {
93          Table table = db.getTable("article");
94          Column price = table.getColumn("price");
95          assertEquals("NUMERIC", price.getTorqueType());
96          assertEquals("NUMERIC", price.getType());
97          assertEquals("DECIMAL", price.getDomain().getSqlType());
98          assertEquals("12", price.getSize());
99          assertEquals("2", price.getScale());
100         assertEquals("1000", price.getDefaultValue());
101         assertEquals("(12,2)", price.printSize());
102         assertEquals("price DECIMAL(12,2) default 1000", price.getSqlString());
103     }
104 
105     public void testDecimalColumn() throws Exception
106     {
107         Table table = db.getTable("article");
108         Column col = table.getColumn("decimal_col");
109         assertEquals("DECIMAL", col.getTorqueType());
110         assertEquals("DECIMAL", col.getType());
111         assertEquals("DECIMAL", col.getDomain().getSqlType());
112         assertEquals("10", col.getSize());
113         assertEquals("3", col.getScale());
114         assertEquals("(10,3)", col.printSize());
115         assertEquals("decimal_col DECIMAL(10,3)", col.getSqlString());
116     }
117 
118     public void testDateColumn() throws Exception
119     {
120         Table table = db.getTable("article");
121         Column col = table.getColumn("date_col");
122         assertEquals("DATE", col.getTorqueType());
123         assertEquals("DATE", col.getType());
124         assertEquals("DATETIME", col.getDomain().getSqlType());
125         assertEquals("", col.printSize());
126         assertEquals("date_col DATETIME", col.getSqlString());
127     }
128 
129     public void testNativeAutoincrement() throws Exception
130     {
131         Table table = db.getTable("native");
132         Column col = table.getColumn("native_id");
133         assertEquals("AUTO_INCREMENT", col.getAutoIncrementString());
134         assertEquals("native_id MEDIUMINT NOT NULL AUTO_INCREMENT", col.getSqlString());
135         col = table.getColumn("name");
136         assertEquals("", col.getAutoIncrementString());
137     }
138 
139     public void testIdBrokerAutoincrement() throws Exception
140     {
141         Table table = db.getTable("article");
142         Column col = table.getColumn("article_id");
143         assertEquals("", col.getAutoIncrementString());
144         assertEquals("article_id MEDIUMINT NOT NULL", col.getSqlString());
145         col = table.getColumn("name");
146         assertEquals("", col.getAutoIncrementString());
147     }
148 
149     public void testBooleanint() throws Exception
150     {
151         Table table = db.getTable("types");
152         Column col = table.getColumn("cbooleanint");
153         assertEquals("", col.getAutoIncrementString());
154         assertEquals("BOOLEANINT", col.getTorqueType());
155         assertEquals("INTEGER", col.getType());
156         assertEquals("INTEGER", col.getDomain().getSqlType());
157         assertEquals("cbooleanint INTEGER", col.getSqlString());
158     }
159 
160     public void testBlob() throws Exception
161     {
162         Table table = db.getTable("types");
163         Column col = table.getColumn("cblob");
164         assertEquals("", col.getAutoIncrementString());
165         assertEquals("BLOB", col.getTorqueType());
166         assertEquals("LONGBLOB", col.getDomain().getSqlType());
167         assertEquals("cblob LONGBLOB", col.getSqlString());
168     }
169 
170 }