KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > dbschema > relationship > RelationshipSchemaUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.dbschema.relationship;
23
24 import junit.framework.Test;
25 import org.jboss.test.JBossTestCase;
26 import org.jboss.test.cmp2.dbschema.util.DBSchemaHelper;
27 import org.jboss.test.cmp2.dbschema.util.AbstractDBSchemaTest;
28 import org.jboss.test.cmp2.dbschema.util.Column;
29 import org.jboss.test.cmp2.dbschema.util.Table;
30
31 import java.sql.Connection JavaDoc;
32 import java.sql.DatabaseMetaData JavaDoc;
33 import java.sql.Types JavaDoc;
34
35
36 /**
37  * The tests for generated database schema for entity beans from cmp2/relationship.
38  * Each test method is named by the pattern: test${ejb-relationship-name}
39  *
40  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
41  */

42 public class RelationshipSchemaUnitTestCase
43    extends AbstractDBSchemaTest
44 {
45    public static Test suite() throws Exception JavaDoc
46    {
47       return JBossTestCase.getDeploySetup(RelationshipSchemaUnitTestCase.class, "cmp2-dbschema.jar");
48    }
49
50    public RelationshipSchemaUnitTestCase(String JavaDoc s)
51    {
52       super(s);
53    }
54
55    public void testAB_OneToOne_Bi_Table() throws Exception JavaDoc
56    {
57       assertTableMapping(
58          "A_OneToOne_Bi_Table_EJB".toUpperCase(),
59          "B_OneToOne_Bi_Table_EJB".toUpperCase(),
60          "AB_OneToOneBi".toUpperCase()
61       );
62    }
63
64    public void testAB_OneToOne_Bi_FK() throws Exception JavaDoc
65    {
66       final String JavaDoc aTableName = "A_OneToOne_Bi_FK_EJB".toUpperCase();
67       final String JavaDoc bTableName = "B_OneToOne_Bi_FK_EJB".toUpperCase();
68       final String JavaDoc aFKName = "A";
69       final String JavaDoc bFKName = "B";
70
71       Connection JavaDoc con = null;
72       try
73       {
74          con = getConnection();
75          DatabaseMetaData JavaDoc dbMD = con.getMetaData();
76
77          Table A = DBSchemaHelper.getTable(dbMD, aTableName);
78          assertEquals(2, A.getColumnsNumber());
79          Column aId = A.getColumn("ID");
80          aId.assertTypeNotNull(Types.INTEGER, true);
81          Column aB = A.getColumn(bFKName);
82          aB.assertTypeNotNull(Types.INTEGER, false);
83
84          Table B = DBSchemaHelper.getTable(dbMD, bTableName);
85          assertEquals(2, B.getColumnsNumber());
86          Column bId = B.getColumn("ID");
87          bId.assertTypeNotNull(Types.INTEGER, true);
88          Column bA = B.getColumn(aFKName);
89          bA.assertTypeNotNull(Types.INTEGER, false);
90       }
91       finally
92       {
93          DBSchemaHelper.safeClose(con);
94       }
95    }
96
97    public void testAB_OneToOne_Uni_Table() throws Exception JavaDoc
98    {
99       assertTableMapping(
100          "A_OneToOne_Uni_Table_EJB".toUpperCase(),
101          "B_OneToOne_Uni_Table_EJB".toUpperCase(),
102          "AB_OneToOneUni".toUpperCase()
103       );
104    }
105
106    public void testAB_OneToOne_Uni_FK() throws Exception JavaDoc
107    {
108       assertFKMapping(
109          "B_OneToOne_Uni_FK_EJB".toUpperCase(),
110          "A_OneToOne_Uni_FK_EJB".toUpperCase(),
111          "B".toUpperCase()
112       );
113    }
114
115    public void testAB_OneToMany_Bi_Table() throws Exception JavaDoc
116    {
117       assertTableMapping(
118          "A_OneToMany_Bi_Table_EJB".toUpperCase(),
119          "B_OneToMany_Bi_Table_EJB".toUpperCase(),
120          "AB_OneToManyBi".toUpperCase()
121       );
122    }
123
124    public void testAB_OneToMany_Bi_FK() throws Exception JavaDoc
125    {
126       assertFKMapping(
127          "A_OneToMany_Bi_FK_EJB".toUpperCase(),
128          "B_OneToMany_Bi_FK_EJB".toUpperCase(),
129          "A"
130       );
131    }
132
133    public void testAB_OneToMany_Uni_Table() throws Exception JavaDoc
134    {
135       assertTableMapping(
136          "A_OneToMany_Uni_Table_EJB".toUpperCase(),
137          "B_OneToMany_Uni_Table_EJB".toUpperCase(),
138          "AB_OneToManyUni".toUpperCase()
139       );
140    }
141
142    public void testAB_OneToMany_Uni_FK() throws Exception JavaDoc
143    {
144       assertFKMapping(
145          "A_OneToMany_Uni_FK_EJB".toUpperCase(),
146          "B_OneToMany_Uni_FK_EJB".toUpperCase(),
147          "A_OneToMany_Uni_FK_EJB_b".toUpperCase()
148       );
149    }
150
151    public void testAB_ManyToOne_Uni_Table() throws Exception JavaDoc
152    {
153       assertTableMapping(
154          "A_ManyToOne_Uni_Table_EJB".toUpperCase(),
155          "B_ManyToOne_Uni_Table_EJB".toUpperCase(),
156          "AB_ManyToOneUni".toUpperCase()
157       );
158    }
159
160    public void testAB_ManyToOne_Uni_FK() throws Exception JavaDoc
161    {
162       assertFKMapping(
163          "A_ManyToOne_Uni_FK_EJB".toUpperCase(),
164          "B_ManyToOne_Uni_FK_EJB".toUpperCase(),
165          "A"
166       );
167    }
168
169    public void testAB_ManyToMany_Bi() throws Exception JavaDoc
170    {
171       assertTableMapping(
172          "A_ManyToMany_Bi_EJB".toUpperCase(),
173          "B_ManyToMany_Bi_EJB".toUpperCase(),
174          "AB_ManyToManyBi".toUpperCase()
175       );
176    }
177
178    public void testAB_ManyToMany_Uni() throws Exception JavaDoc
179    {
180       assertTableMapping(
181          "A_ManyToMany_Uni_EJB".toUpperCase(),
182          "B_ManyToMany_Uni_EJB".toUpperCase(),
183          "AB_ManyToManyUni".toUpperCase()
184       );
185    }
186
187    // Private
188

189    /**
190     * Tests default schema generation for relationships with relation table
191     */

192    private void assertTableMapping(String JavaDoc aTableName, String JavaDoc bTableName, String JavaDoc abTableName)
193       throws Exception JavaDoc
194    {
195       Connection JavaDoc con = null;
196       try
197       {
198          con = getConnection();
199          DatabaseMetaData JavaDoc dbMD = con.getMetaData();
200
201          Table A = DBSchemaHelper.getTable(dbMD, aTableName);
202          assertEquals(1, A.getColumnsNumber());
203          Column aId = A.getColumn("ID");
204          aId.assertTypeNotNull(Types.INTEGER, true);
205
206          Table B = DBSchemaHelper.getTable(dbMD, bTableName);
207          assertEquals(1, B.getColumnsNumber());
208          Column bId = B.getColumn("ID");
209          bId.assertTypeNotNull(Types.INTEGER, true);
210
211          Table AB = DBSchemaHelper.getTable(dbMD, abTableName);
212          assertEquals(AB.getColumnsNumber(), 2);
213          Column aFk = AB.getColumn(aTableName);
214          aFk.assertTypeNotNull(Types.INTEGER, true);
215          Column bFk = AB.getColumn(bTableName);
216          bFk.assertTypeNotNull(Types.INTEGER, true);
217       }
218       finally
219       {
220          DBSchemaHelper.safeClose(con);
221       }
222    }
223
224    /**
225     * Tests default schema generation for relationships with foreign key mapping
226     */

227    private void assertFKMapping(final String JavaDoc aTableName, final String JavaDoc bTableName, final String JavaDoc aFKName)
228       throws Exception JavaDoc
229    {
230       Connection JavaDoc con = null;
231       try
232       {
233          con = getConnection();
234          DatabaseMetaData JavaDoc dbMD = con.getMetaData();
235
236          Table A = DBSchemaHelper.getTable(dbMD, aTableName);
237          assertEquals(1, A.getColumnsNumber());
238          Column aId = A.getColumn("ID");
239          aId.assertTypeNotNull(Types.INTEGER, true);
240
241          Table B = DBSchemaHelper.getTable(dbMD, bTableName);
242          assertEquals(2, B.getColumnsNumber());
243          Column bId = B.getColumn("ID");
244          bId.assertTypeNotNull(Types.INTEGER, true);
245          Column bA = B.getColumn(aFKName);
246          bA.assertTypeNotNull(Types.INTEGER, false);
247       }
248       finally
249       {
250          DBSchemaHelper.safeClose(con);
251       }
252    }
253 }
254
Popular Tags