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 junit.framework.TestCase;
23  
24  import org.apache.torque.engine.database.transform.XmlToAppData;
25  
26  /***
27   * Tests for domain handling (for MySql).
28   *
29   * @version $Id: DomainTest.java 473814 2006-11-11 22:30:30Z tv $
30   */
31  public class DomainTest extends TestCase
32  {
33      private XmlToAppData xmlToAppData = null;
34      private Database db = null;
35  
36      public DomainTest(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/domaintest-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 testAllAttributes() throws Exception
59      {
60          Domain amount = db.getDomain("amount");
61          assertEquals(SchemaType.NUMERIC, amount.getType());
62          assertEquals("DECIMAL", amount.getSqlType());
63          assertEquals("10", amount.getSize());
64          assertEquals("2", amount.getScale());
65          assertEquals("0", amount.getDefaultValue());
66          assertEquals("amount domain", amount.getDescription());
67      }
68  
69      /***
70       * test if the tables get the package name from the properties file
71       */
72      public void testDomainColumn() throws Exception
73      {
74          Table table = db.getTable("product");
75          Column name = table.getColumn("name");
76          assertEquals("VARCHAR", name.getDomain().getSqlType());
77          assertEquals("40", name.getSize());
78          assertEquals("name VARCHAR(40)", name.getSqlString());
79          Column price = table.getColumn("price");
80          assertEquals("NUMERIC", price.getTorqueType());
81          assertEquals("DECIMAL", price.getDomain().getSqlType());
82          assertEquals("10", price.getSize());
83          assertEquals("2", price.getScale());
84          assertEquals("0", price.getDefaultValue());
85          assertEquals("(10,2)", price.printSize());
86          assertEquals("price DECIMAL(10,2) default 0", price.getSqlString());
87      }
88  
89      /***
90       * test if the tables get the package name from the properties file
91       */
92      public void testExtendedDomainColumn() throws Exception
93      {
94          Table table = db.getTable("article");
95          Column price = table.getColumn("price");
96          assertEquals("NUMERIC", price.getTorqueType());
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.getDomain().getSqlType());
111         assertEquals("10", col.getSize());
112         assertEquals("3", col.getScale());
113         assertEquals("(10,3)", col.printSize());
114         assertEquals("decimal_col DECIMAL(10,3)", col.getSqlString());
115     }
116 
117     public void testDateColumn() throws Exception
118     {
119         Table table = db.getTable("article");
120         Column col = table.getColumn("date_col");
121         assertEquals("DATE", col.getTorqueType());
122         assertEquals("DATETIME", col.getDomain().getSqlType());
123         assertEquals("", col.printSize());
124         assertEquals("date_col DATETIME", col.getSqlString());
125     }
126 
127     public void testNativeAutoincrement() throws Exception
128     {
129         Table table = db.getTable("native");
130         Column col = table.getColumn("native_id");
131         assertEquals("AUTO_INCREMENT", col.getAutoIncrementString());
132         assertEquals("native_id INTEGER NOT NULL AUTO_INCREMENT", col.getSqlString());
133         col = table.getColumn("name");
134         assertEquals("", col.getAutoIncrementString());
135     }
136 
137     public void testIdBrokerAutoincrement() throws Exception
138     {
139         Table table = db.getTable("article");
140         Column col = table.getColumn("article_id");
141         assertEquals("", col.getAutoIncrementString());
142         assertEquals("article_id INTEGER NOT NULL", col.getSqlString());
143         col = table.getColumn("name");
144         assertEquals("", col.getAutoIncrementString());
145     }
146 
147     public void testBooleanint() throws Exception
148     {
149         Table table = db.getTable("types");
150         Column col = table.getColumn("cbooleanint");
151         assertEquals("", col.getAutoIncrementString());
152         assertEquals("BOOLEANINT", col.getTorqueType());
153         assertEquals("INTEGER", col.getDomain().getSqlType());
154         assertEquals("cbooleanint INTEGER", col.getSqlString());
155     }
156 
157     public void testBlob() throws Exception
158     {
159         Table table = db.getTable("types");
160         Column col = table.getColumn("cblob");
161         assertEquals("", col.getAutoIncrementString());
162         assertEquals("BLOB", col.getTorqueType());
163         assertEquals("LONGBLOB", col.getDomain().getSqlType());
164         assertEquals("cblob LONGBLOB", col.getSqlString());
165     }
166 
167 }