1 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 ; 32 import java.sql.DatabaseMetaData ; 33 import java.sql.Types ; 34 35 36 42 public class RelationshipSchemaUnitTestCase 43 extends AbstractDBSchemaTest 44 { 45 public static Test suite() throws Exception 46 { 47 return JBossTestCase.getDeploySetup(RelationshipSchemaUnitTestCase.class, "cmp2-dbschema.jar"); 48 } 49 50 public RelationshipSchemaUnitTestCase(String s) 51 { 52 super(s); 53 } 54 55 public void testAB_OneToOne_Bi_Table() throws Exception 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 65 { 66 final String aTableName = "A_OneToOne_Bi_FK_EJB".toUpperCase(); 67 final String bTableName = "B_OneToOne_Bi_FK_EJB".toUpperCase(); 68 final String aFKName = "A"; 69 final String bFKName = "B"; 70 71 Connection con = null; 72 try 73 { 74 con = getConnection(); 75 DatabaseMetaData 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 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 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 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 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 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 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 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 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 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 179 { 180 assertTableMapping( 181 "A_ManyToMany_Uni_EJB".toUpperCase(), 182 "B_ManyToMany_Uni_EJB".toUpperCase(), 183 "AB_ManyToManyUni".toUpperCase() 184 ); 185 } 186 187 189 192 private void assertTableMapping(String aTableName, String bTableName, String abTableName) 193 throws Exception 194 { 195 Connection con = null; 196 try 197 { 198 con = getConnection(); 199 DatabaseMetaData 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 227 private void assertFKMapping(final String aTableName, final String bTableName, final String aFKName) 228 throws Exception 229 { 230 Connection con = null; 231 try 232 { 233 con = getConnection(); 234 DatabaseMetaData 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 |