001: package org.apache.torque.engine.database.model;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.List;
023:
024: import junit.framework.TestCase;
025:
026: import org.apache.torque.engine.database.transform.XmlToAppData;
027:
028: /**
029: * Tests for package handling.
030: *
031: * @author <a href="mailto:mpoeschl@marmot.at>Martin Poeschl</a>
032: * @version $Id: TableTest.java 473814 2006-11-11 22:30:30Z tv $
033: */
034: public class TableTest extends TestCase {
035: private XmlToAppData xmlToAppData = null;
036: private Database db = null;
037:
038: public TableTest(String name) {
039: super (name);
040: }
041:
042: protected void setUp() throws Exception {
043: super .setUp();
044: xmlToAppData = new XmlToAppData("mysql", "defaultpackage");
045: db = xmlToAppData
046: .parseFile("src/test/org/apache/torque/engine/database/model/tabletest-schema.xml");
047: }
048:
049: protected void tearDown() throws Exception {
050: xmlToAppData = null;
051: super .tearDown();
052: }
053:
054: /**
055: * test if the tables get the package name from the properties file
056: */
057: public void testIdMethodHandling() throws Exception {
058: assertEquals(IDMethod.ID_BROKER, db.getDefaultIdMethod());
059: Table table = db.getTable("table_idbroker");
060: assertEquals(IDMethod.ID_BROKER, table.getIdMethod());
061: Table table2 = db.getTable("table_native");
062: assertEquals(IDMethod.NATIVE, table2.getIdMethod());
063: }
064:
065: public void testNoPk() throws Exception {
066: Table table = db.getTable("nopk");
067: assertFalse(table.hasPrimaryKey());
068: List pks = table.getPrimaryKey();
069: assertTrue(pks.size() == 0);
070: }
071:
072: public void testSinglePk() throws Exception {
073: Table table = db.getTable("singlepk");
074: assertTrue(table.hasPrimaryKey());
075: List pks = table.getPrimaryKey();
076: assertTrue(pks.size() == 1);
077: Column col = (Column) pks.get(0);
078: assertEquals(col.getName(), "singlepk_id");
079: }
080:
081: public void testMultiPk() throws Exception {
082: Table table = db.getTable("multipk");
083: assertTrue(table.hasPrimaryKey());
084: List pks = table.getPrimaryKey();
085: assertTrue(pks.size() == 2);
086: Column cola = (Column) pks.get(0);
087: assertEquals(cola.getName(), "multipk_a");
088: Column colb = (Column) pks.get(1);
089: assertEquals(colb.getName(), "multipk_b");
090: assertEquals(table.printPrimaryKey(), "multipk_a,multipk_b");
091: }
092:
093: public void testSingleFk() throws Exception {
094: Table table = db.getTable("singlefk");
095: List fks = table.getForeignKeys();
096: assertTrue(fks.size() == 1);
097: ForeignKey fk = (ForeignKey) fks.get(0);
098: assertEquals(fk.getForeignTableName(), "singlepk");
099: assertTrue(fk.getForeignColumns().size() == 1);
100: assertFalse(fk.hasOnDelete());
101: assertFalse(fk.hasOnUpdate());
102: }
103:
104: public void testOnUpdateOnDelete() throws Exception {
105: Table table = db.getTable("singlefk1");
106: List fks = table.getForeignKeys();
107: assertTrue(fks.size() == 1);
108: ForeignKey fk = (ForeignKey) fks.get(0);
109: assertTrue(fk.hasOnUpdate());
110: assertEquals("CASCADE", fk.getOnUpdate());
111: assertTrue(fk.hasOnDelete());
112: assertEquals("SET NULL", fk.getOnDelete());
113: }
114:
115: public void testMultiFk() throws Exception {
116: Table table = db.getTable("multifk");
117: List fks = table.getForeignKeys();
118: assertTrue(fks.size() == 1);
119: ForeignKey fk = (ForeignKey) fks.get(0);
120: assertEquals(fk.getForeignTableName(), "multipk");
121: assertTrue(fk.getForeignColumns().size() == 2);
122: }
123:
124: public void testReferrers() throws Exception {
125: Table table = db.getTable("singlepk");
126: List refs = table.getReferrers();
127: assertTrue(refs.size() == 1);
128: ForeignKey fk = (ForeignKey) refs.get(0);
129: assertEquals(fk.getTableName(), "singlefk");
130: }
131:
132: public void testUnique() throws Exception {
133: Table table = db.getTable("unique_test");
134: List unices = table.getUnices();
135: assertTrue(unices.size() == 1);
136: Unique unique = (Unique) unices.get(0);
137: assertEquals(unique.getName(), "unique_name");
138: assertTrue(unique.getColumns().size() == 2);
139: }
140:
141: }
|