001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.dbschema.relationship;
023:
024: import junit.framework.Test;
025: import org.jboss.test.JBossTestCase;
026: import org.jboss.test.cmp2.dbschema.util.DBSchemaHelper;
027: import org.jboss.test.cmp2.dbschema.util.AbstractDBSchemaTest;
028: import org.jboss.test.cmp2.dbschema.util.Column;
029: import org.jboss.test.cmp2.dbschema.util.Table;
030:
031: import java.sql.Connection;
032: import java.sql.DatabaseMetaData;
033: import java.sql.Types;
034:
035: /**
036: * The tests for generated database schema for entity beans from cmp2/relationship.
037: * Each test method is named by the pattern: test${ejb-relationship-name}
038: *
039: * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
040: */
041: public class RelationshipSchemaUnitTestCase extends
042: AbstractDBSchemaTest {
043: public static Test suite() throws Exception {
044: return JBossTestCase.getDeploySetup(
045: RelationshipSchemaUnitTestCase.class,
046: "cmp2-dbschema.jar");
047: }
048:
049: public RelationshipSchemaUnitTestCase(String s) {
050: super (s);
051: }
052:
053: public void testAB_OneToOne_Bi_Table() throws Exception {
054: assertTableMapping("A_OneToOne_Bi_Table_EJB".toUpperCase(),
055: "B_OneToOne_Bi_Table_EJB".toUpperCase(),
056: "AB_OneToOneBi".toUpperCase());
057: }
058:
059: public void testAB_OneToOne_Bi_FK() throws Exception {
060: final String aTableName = "A_OneToOne_Bi_FK_EJB".toUpperCase();
061: final String bTableName = "B_OneToOne_Bi_FK_EJB".toUpperCase();
062: final String aFKName = "A";
063: final String bFKName = "B";
064:
065: Connection con = null;
066: try {
067: con = getConnection();
068: DatabaseMetaData dbMD = con.getMetaData();
069:
070: Table A = DBSchemaHelper.getTable(dbMD, aTableName);
071: assertEquals(2, A.getColumnsNumber());
072: Column aId = A.getColumn("ID");
073: aId.assertTypeNotNull(Types.INTEGER, true);
074: Column aB = A.getColumn(bFKName);
075: aB.assertTypeNotNull(Types.INTEGER, false);
076:
077: Table B = DBSchemaHelper.getTable(dbMD, bTableName);
078: assertEquals(2, B.getColumnsNumber());
079: Column bId = B.getColumn("ID");
080: bId.assertTypeNotNull(Types.INTEGER, true);
081: Column bA = B.getColumn(aFKName);
082: bA.assertTypeNotNull(Types.INTEGER, false);
083: } finally {
084: DBSchemaHelper.safeClose(con);
085: }
086: }
087:
088: public void testAB_OneToOne_Uni_Table() throws Exception {
089: assertTableMapping("A_OneToOne_Uni_Table_EJB".toUpperCase(),
090: "B_OneToOne_Uni_Table_EJB".toUpperCase(),
091: "AB_OneToOneUni".toUpperCase());
092: }
093:
094: public void testAB_OneToOne_Uni_FK() throws Exception {
095: assertFKMapping("B_OneToOne_Uni_FK_EJB".toUpperCase(),
096: "A_OneToOne_Uni_FK_EJB".toUpperCase(), "B"
097: .toUpperCase());
098: }
099:
100: public void testAB_OneToMany_Bi_Table() throws Exception {
101: assertTableMapping("A_OneToMany_Bi_Table_EJB".toUpperCase(),
102: "B_OneToMany_Bi_Table_EJB".toUpperCase(),
103: "AB_OneToManyBi".toUpperCase());
104: }
105:
106: public void testAB_OneToMany_Bi_FK() throws Exception {
107: assertFKMapping("A_OneToMany_Bi_FK_EJB".toUpperCase(),
108: "B_OneToMany_Bi_FK_EJB".toUpperCase(), "A");
109: }
110:
111: public void testAB_OneToMany_Uni_Table() throws Exception {
112: assertTableMapping("A_OneToMany_Uni_Table_EJB".toUpperCase(),
113: "B_OneToMany_Uni_Table_EJB".toUpperCase(),
114: "AB_OneToManyUni".toUpperCase());
115: }
116:
117: public void testAB_OneToMany_Uni_FK() throws Exception {
118: assertFKMapping("A_OneToMany_Uni_FK_EJB".toUpperCase(),
119: "B_OneToMany_Uni_FK_EJB".toUpperCase(),
120: "A_OneToMany_Uni_FK_EJB_b".toUpperCase());
121: }
122:
123: public void testAB_ManyToOne_Uni_Table() throws Exception {
124: assertTableMapping("A_ManyToOne_Uni_Table_EJB".toUpperCase(),
125: "B_ManyToOne_Uni_Table_EJB".toUpperCase(),
126: "AB_ManyToOneUni".toUpperCase());
127: }
128:
129: public void testAB_ManyToOne_Uni_FK() throws Exception {
130: assertFKMapping("A_ManyToOne_Uni_FK_EJB".toUpperCase(),
131: "B_ManyToOne_Uni_FK_EJB".toUpperCase(), "A");
132: }
133:
134: public void testAB_ManyToMany_Bi() throws Exception {
135: assertTableMapping("A_ManyToMany_Bi_EJB".toUpperCase(),
136: "B_ManyToMany_Bi_EJB".toUpperCase(), "AB_ManyToManyBi"
137: .toUpperCase());
138: }
139:
140: public void testAB_ManyToMany_Uni() throws Exception {
141: assertTableMapping("A_ManyToMany_Uni_EJB".toUpperCase(),
142: "B_ManyToMany_Uni_EJB".toUpperCase(),
143: "AB_ManyToManyUni".toUpperCase());
144: }
145:
146: // Private
147:
148: /**
149: * Tests default schema generation for relationships with relation table
150: */
151: private void assertTableMapping(String aTableName,
152: String bTableName, String abTableName) throws Exception {
153: Connection con = null;
154: try {
155: con = getConnection();
156: DatabaseMetaData dbMD = con.getMetaData();
157:
158: Table A = DBSchemaHelper.getTable(dbMD, aTableName);
159: assertEquals(1, A.getColumnsNumber());
160: Column aId = A.getColumn("ID");
161: aId.assertTypeNotNull(Types.INTEGER, true);
162:
163: Table B = DBSchemaHelper.getTable(dbMD, bTableName);
164: assertEquals(1, B.getColumnsNumber());
165: Column bId = B.getColumn("ID");
166: bId.assertTypeNotNull(Types.INTEGER, true);
167:
168: Table AB = DBSchemaHelper.getTable(dbMD, abTableName);
169: assertEquals(AB.getColumnsNumber(), 2);
170: Column aFk = AB.getColumn(aTableName);
171: aFk.assertTypeNotNull(Types.INTEGER, true);
172: Column bFk = AB.getColumn(bTableName);
173: bFk.assertTypeNotNull(Types.INTEGER, true);
174: } finally {
175: DBSchemaHelper.safeClose(con);
176: }
177: }
178:
179: /**
180: * Tests default schema generation for relationships with foreign key mapping
181: */
182: private void assertFKMapping(final String aTableName,
183: final String bTableName, final String aFKName)
184: throws Exception {
185: Connection con = null;
186: try {
187: con = getConnection();
188: DatabaseMetaData dbMD = con.getMetaData();
189:
190: Table A = DBSchemaHelper.getTable(dbMD, aTableName);
191: assertEquals(1, A.getColumnsNumber());
192: Column aId = A.getColumn("ID");
193: aId.assertTypeNotNull(Types.INTEGER, true);
194:
195: Table B = DBSchemaHelper.getTable(dbMD, bTableName);
196: assertEquals(2, B.getColumnsNumber());
197: Column bId = B.getColumn("ID");
198: bId.assertTypeNotNull(Types.INTEGER, true);
199: Column bA = B.getColumn(aFKName);
200: bA.assertTypeNotNull(Types.INTEGER, false);
201: } finally {
202: DBSchemaHelper.safeClose(con);
203: }
204: }
205: }
|