Added: db/torque/test/trunk/test-project/src/java/org/apache/torque/map/DatabaseMapTest.java URL: http://svn.apache.org/viewvc/db/torque/test/trunk/test-project/src/java/org/apache/torque/map/DatabaseMapTest.java?rev=414531&view=auto ============================================================================== --- db/torque/test/trunk/test-project/src/java/org/apache/torque/map/DatabaseMapTest.java (added) +++ db/torque/test/trunk/test-project/src/java/org/apache/torque/map/DatabaseMapTest.java Thu Jun 15 03:07:45 2006 @@ -0,0 +1,297 @@ +package org.apache.torque.map; + +import org.apache.torque.BaseRuntimeTestCase; +import org.apache.torque.Torque; +import org.apache.torque.TorqueException; + +/* + * Copyright 2001-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License") + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Test code for Database Map functions + * + * @author <a href="mailto:[EMAIL PROTECTED]">Greg Monroe</a> + * @version $Id + */ +public class DatabaseMapTest extends BaseRuntimeTestCase +{ + + public static final String TABLE_NAME1 = "Not_Used_Before_Map_Init"; + + public static final String DATABASE_NAME = "bookstore"; + + public static final String INVALID_DATABASE_NAME = "erotskoob"; + + public static final String TABLE_NAME2 = "After_NotUsedBeforeMapInit"; + + public static final String[] COLUMN_NAMES = + { + "ID", "ONE", "TWO", "THREE" + }; + + public DatabaseMapTest(String name) + { + super(name); + } + + /** + * Test initializing the default database. <p> Assumptions: <ul> A table + * called NotUsedBeforeMapInit table exists in the default schema + * (bookstore)<br> This table has not been used by any other test prior to + * this test.<br> </ul> + */ + public void testDatabaseMapInitialize() throws TorqueException + { + // Get default schema DB + DatabaseMap map = Torque.getDatabaseMap(); + TableMap tMap = map.getTable(TABLE_NAME1); + assertTrue( + "Did not expect to find NotUsedBeforeMapInit table before " + + "initialize!", + tMap == null); + try + { + map.initialize(); + } + catch (Exception e) + { + fail("The following error occured while initializing the " + + "database map, msg='" + + e.getMessage() + "'"); + } + tMap = map.getTable(TABLE_NAME1); + assertTrue( + "Did not find table named NotUsedBeforeMapInit after " + + "initialization!", + tMap != null); + } + + /** + * Test that XML column order is preserved in TableMap objects. + * <p> + * Assumptions: + * <ul> + * The default database is bookstore<br> + * The book table has columns ordered as in COLUMN_NAMES property<br> + * </ul> + * + * @throws TorqueException + */ + public void testColumnOrder() throws TorqueException + { + DatabaseMap map = Torque.getDatabaseMap(DATABASE_NAME); + map.initialize(); + TableMap tMap = map.getTable(TABLE_NAME1); + ColumnMap[] columns = tMap.getColumns(); + assertTrue("TestColumnOrder: Did not get enough columns from table!", + columns.length >= COLUMN_NAMES.length); + for (int i = 0; i < COLUMN_NAMES.length; i++) + { + String cName = columns[i].getColumnName(); + String expected = COLUMN_NAMES[i]; + assertTrue("TableMap for " + TABLE_NAME1 + + " did not preserve XML column order! Expected " + + expected + " but found " + + cName, cName.equals(expected)); + } + } + + /** + * Test that XML table order is preserved in DatabaseMap objects. + * <p> + * Assumptions: + * <ul> + * The default database is bookstore<br> + * TABLE_NAME1 is followed by TABLE_NAME2 in the array<br> + * </ul> + * + * @throws TorqueException + */ + public void testTableOrder() throws TorqueException + { + DatabaseMap map = Torque.getDatabaseMap(DATABASE_NAME); + map.initialize(); + TableMap[] tables = map.getTables(); + boolean found1 = false; + int i; + for (i = 0; i < tables.length; i++) + { + String tName = tables[i].getName(); + if (tName.equals(TABLE_NAME1)) + { + found1 = true; + break; + } + } + assertTrue("XML Table order not preserved!\nDid not find table '" + + TABLE_NAME1 + "' in DatabaseMap.getTables() array!", found1); + assertTrue("XML Table order not preserved!\nDid not find table '" + + TABLE_NAME2 + "' after '" + TABLE_NAME1 + + " in DatabaseMap.getTables() array!", TABLE_NAME2 + .equals(tables[i + 1].getName())); + } + + /** + * Test that XML table order is preserved in DatabaseMap objects. + * <p> + * Assumptions: + * <ul> + * The default database is bookstore<br> + * TABLE_NAME1 is followed by TABLE_NAME2 in the array<br> + * </ul> + * + * @throws TorqueException + */ + public void testExternalSchemaTables() throws TorqueException + { + DatabaseMap map = Torque.getDatabaseMap(DATABASE_NAME); + map.initialize(); + + TableMap table = map.getTable("ext"); + assertTrue("Did not find external schema table, 'ext'!", + table != null); + + table = map.getTable("extext"); + assertTrue("Did not find external schema table, 'extext'!", + table != null); + } + + /** + * Test that various table properties get set correctly from the XML + */ + public void testTableAttributes() throws TorqueException + { + DatabaseMap map = Torque.getDatabaseMap(DATABASE_NAME); + map.initialize(); + TableMap table = map.getTable(TABLE_NAME1); + + validateAttribute("TableMap Javaname", "UninitializedTable", table + .getJavaName()); + validateAttribute("TableMap description", "A table description", table + .getDescription()); + assertTrue("TableMap OM Class was null!", table.getOMClass() != null); + assertTrue("TableMap Peer Class was null!", + table.getPeerClass() != null); + } + + /** + * Test that various column properties get set correctly from the XML + */ + public void testColumnAttributes() throws TorqueException + { + DatabaseMap map = Torque.getDatabaseMap(DATABASE_NAME); + map.initialize(); + TableMap table = map.getTable(TABLE_NAME1); + + ColumnMap column = table.getColumn("ID"); + + validateAttribute("Column JavaName", "Id", column.getJavaName()); + validateAttribute("Column description", "id column", column + .getDescription()); + + assertTrue( + "Column isPrimaryKey attribute returned false instead of true!", + column.isPrimaryKey()); + assertTrue( + "Column isAutoIncrement attribute returned false instead of true!", + column.isAutoIncrement()); + assertTrue( + "Column isNotNull attribute returned false instead of true!", + column.isNotNull()); + assertTrue( + "Column isUsePrimitive attribute returned false instead of true!", + column.isUsePrimitive()); + assertTrue("Column type attribute was not Integer!", + column.getType() instanceof Integer); + + column = table.getColumn("ONE"); + assertTrue( + "Column isProtected attribute returned false instead of true!", + column.isProtected()); + assertTrue("Column size != 50", column.getSize() == 50); + validateAttribute("Column default", "unknown", column + .getDefault()); + + column = table.getColumn("THREE"); + assertTrue("Column position attribute != 4", + column.getPosition() == 4); + assertTrue("Column isForeignKey attribute was false instead of true!", + column.isForeignKey()); + validateAttribute("Column relatedTableName", "CIRCULAR_REFERENCE_A", + column.getRelatedTableName()); + validateAttribute("Column relatedColumnName", + "CIRCULAR_REFERENCE_A_ID", column.getRelatedColumnName()); + } + + /** + * Test that Inheritance info is stored correctly + */ + public void testInheritanceMapping() throws TorqueException + { + DatabaseMap map = Torque.getDatabaseMap(DATABASE_NAME); + map.initialize(); + TableMap table = map.getTable(TABLE_NAME1); + assertTrue("Table isUseInheritance returned false!", table + .isUseInheritance()); + + ColumnMap column = table.getColumn("CLASS_NAME"); + + assertTrue("Column isUseInheritance returned false!", column + .isUseInheritance()); + + InheritanceMap[] inhArray = column.getInheritanceMaps(); + + assertTrue("Did not get 3 mappings back!", inhArray.length == 3); + InheritanceMap inh = column.getInheritanceMap("C"); + assertTrue("Inheritance map did not preserve XML order!", inh.getKey() + .equals(inhArray[1].getKey())); + validateAttribute("Inheritance key", "C", inh.getKey()); + validateAttribute("Inheritance className", "MapInheritanceChildC", inh + .getClassName()); + validateAttribute("Inheritance extends", + "org.apache.torque.test.UninitializedTable", inh.getExtends()); + } + + /** + * Test for controlled error on getting invalid database + */ + public void testInvalidDatabaseName() throws TorqueException + { + DatabaseMap map = Torque.getDatabaseMap(INVALID_DATABASE_NAME); + try + { + map.initialize(); + } + catch (TorqueException e) + { + return; + } + fail("DatabaseMap.initialize() should fail if invalid DB name used!"); + } + + /** + * Validate that the attribute value matches + * @param name + */ + protected void validateAttribute(String name, + String expected, + String result) + { + assertTrue(name + " attribute not set correctly!\n Expected '" + + expected + "' and got '" + result + "'", expected + .equals(result)); + } +}
Modified: db/torque/test/trunk/test-project/src/schema/test-schema.xml URL: http://svn.apache.org/viewvc/db/torque/test/trunk/test-project/src/schema/test-schema.xml?rev=414531&r1=414530&r2=414531&view=diff ============================================================================== --- db/torque/test/trunk/test-project/src/schema/test-schema.xml (original) +++ db/torque/test/trunk/test-project/src/schema/test-schema.xml Thu Jun 15 03:07:45 2006 @@ -366,8 +366,8 @@ foreign="CIRCULAR_REFERENCE_A_ID"/> </foreign-key> </table> - - <!-- Torque OM generated code does not compile if bean generation is activated and a table references an other table several times --> + + <!-- Torque OM generated code did not compile if bean generation is activated and a table references an other table several times --> <table name="referenced" javaName="Referenced" description="Table referenced by several columns of an other table"> <column name="referenced_pk" javaName="referencedPK" required="true" primaryKey="true" type="VARCHAR" size="12"/> <column name="other_information" javaName="otherInformation" type="VARCHAR" size="255"/> @@ -388,5 +388,29 @@ <foreign-key foreignTable="referenced"> <reference local="reference_3" foreign="referenced_pk"/> </foreign-key> + </table> + +<!-- Table for use by Database Map tests --> + <table name="Not_Used_Before_Map_Init" javaName="UninitializedTable" + description="A table description"> + <column name="id" required="true" primaryKey="true" type="INTEGER" + description="id column" autoIncrement="true" /> + <column name="one" required="true" type="VARCHAR" size="50" default="unknown" protected="true"/> + <column name="two" required="true" type="VARCHAR" size="50" /> + <column name="three" required="true" type="INTEGER"/> + <column name="CLASS_NAME" inheritance="single" type="CHAR" size="1"> + <inheritance key="B" class="MapInheritanceChildB" extends="org.apache.torque.test.UninitializedTable"/> + <inheritance key="C" class="MapInheritanceChildC" extends="org.apache.torque.test.UninitializedTable"/> + <inheritance key="D" class="MapInheritanceChildD" extends="org.apache.torque.test.MapInheritanceChildC"/> + </column> + <foreign-key foreignTable="CIRCULAR_REFERENCE_A"> + <reference + local="three" + foreign="CIRCULAR_REFERENCE_A_ID"/> + </foreign-key> + </table> + <table name="After_NotUsedBeforeMapInit" javaName="AfterNotUsedBeforeMapInit"> + <column name="id" required="true" primaryKey="true" type="INTEGER" + description="id column" autoIncrement="true"/> </table> </database> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
