1 22 package org.jboss.test.cmp2.idxandusersql.test; 23 24 import java.sql.Connection ; 25 import java.sql.DatabaseMetaData ; 26 import java.sql.ResultSet ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingException ; 29 import javax.sql.DataSource ; 30 31 import junit.framework.Test; 32 import net.sourceforge.junitejb.EJBTestCase; 33 import org.jboss.logging.Logger; 34 import org.jboss.test.JBossTestCase; 35 36 41 public class IdxAndUsersqlUnitTestCase extends EJBTestCase 42 { 43 44 static final Logger log = Logger.getLogger(IdxAndUsersqlUnitTestCase.class); 45 InitialContext ic = null; 46 47 public static Test suite() throws Exception 48 { 49 return JBossTestCase.getDeploySetup( 50 IdxAndUsersqlUnitTestCase.class, 51 "cmp2-idxandusersql.jar"); 52 } 53 54 public IdxAndUsersqlUnitTestCase(String name) 55 { 56 super(name); 57 try 58 { 59 ic = new InitialContext (); 60 } 61 catch (NamingException e) 62 { 63 ic = null; 64 } 65 } 66 67 public void setUpEJB() throws Exception 68 { 69 super.setUpEJB(); 70 } 71 72 79 80 public void testHsqlDriver() throws Exception 81 { 82 Connection cConn = getConnection(); 83 DatabaseMetaData dMeta = cConn.getMetaData(); 84 85 String driver = dMeta.getDriverName(); 86 String version = dMeta.getDriverVersion(); 87 88 cConn.close(); 89 assertTrue("Error: We are not using HSQL", driver.startsWith("HSQL Database")); 90 91 } 92 93 public void testDBIndexFoo() throws Exception 94 { 95 boolean found = false; 96 97 found = lookForIndexOnTable("DBINDEXTEST", "FOO"); 99 assertTrue("Error: column foo is not indexed", found); 100 101 } 102 103 public void testDBIndexBaz() throws Exception 104 { 105 boolean found = true; 106 107 found = lookForIndexOnTable("DBINDEXTEST", "BAZ"); 109 assertTrue("Error: column baz is indexed but shouldn't", found == false); 110 111 } 112 113 public void testUserSQL1() throws Exception 114 { 115 boolean found = false; 116 117 found = lookForIndexOnTable("DBINDEXTEST", "BAR"); 120 assertTrue("Error column bar is not indexed via post-table-create", found); 121 } 122 123 127 public void testCMR1IndexFoo() throws Exception 128 { 129 boolean found = false; 130 131 found = lookForIndexOnTable("CMR1", "FOO1"); 132 assertTrue("Error: column foo1 is not indexed", found); 133 } 134 135 public void testCMR1IndexBar() throws Exception 136 { 137 boolean found = true; 138 139 found = lookForIndexOnTable("CMR1", "BAR1"); 140 assertTrue("Error: column bar1 is indexed and shouldn't", found == false); 141 } 142 143 public void testCMR2IndexFoo() throws Exception 144 { 145 boolean found = false; 146 147 found = lookForIndexOnTable("CMR2", "FOO2"); 148 assertTrue("Error: column foo1 is not indexed", found); 149 } 150 151 public void testCMR2IndexBar() throws Exception 152 { 153 boolean found = true; 154 155 found = lookForIndexOnTable("CMR2", "BAR2"); 156 assertTrue("Eror: column bar2 is indexed and shouldn't", found == false); 157 } 158 159 public void testFkIdxCmr1() throws Exception 163 { 164 boolean found = false; 165 166 found = lookForIndexOnTable("CMR1", "IDXFK"); 167 assertTrue("Error: Foreign key idxFk is not indexed", found); 168 } 169 170 176 177 public void testCMRmn1() throws Exception 178 { 179 boolean found = false; 180 181 found = lookForIndexOnTable("IDX_CMR2_REL", "IDX_ID"); 182 assertTrue("Error: FKey idx_id is not indexed via <dbindex>", found); 183 } 184 185 public void testCMRmn2() throws Exception 186 { 187 boolean found = true; 188 189 found = lookForIndexOnTable("IDX_CMR2_REL", "CMR2_ID"); 190 assertTrue("Error: FKey cmr2_id is indexed", found == false); 191 } 192 193 194 197 198 public void testDefault1() throws Exception 199 { 200 boolean found = true; 201 202 found = lookForIndexOnTable("DBINDXTEST","BLABLA"); 206 assertTrue("Error: Field blabla in table dbindex has an index",found==false); 207 } 208 public void testDefault2() throws Exception 209 { 210 boolean found = false; 211 212 found = lookForIndexOnTable("CMR1","BLABLA"); 213 assertTrue("Error: Field blabla in table cmr1 has no index",found); 214 } 215 public void testDefault3() throws Exception 216 { 217 boolean found = false; 218 219 found = lookForIndexOnTable("CMR2","BLABLA"); 220 assertTrue("Error: Field blabla in table cmr2 has no index",found); 221 } 222 223 224 225 226 227 231 private boolean lookForIndexOnTable(String table, String column) 232 throws Exception 233 { 234 boolean found = false; 235 Connection cConn = getConnection(); 236 DatabaseMetaData dMeta = cConn.getMetaData(); 237 238 ResultSet rs = dMeta.getIndexInfo(null, null, table, false, false); 239 240 while (rs.next()) 241 { 242 String cName = rs.getString(9); String iName = rs.getString(6); 245 if (cName.equals(column) && !iName.startsWith("PK_IDX")) 247 { 248 found = true; 249 } 250 } 251 rs.close(); 252 cConn.close(); 253 254 return found; 255 } 256 257 261 private Connection getConnection() throws Exception 262 { 263 Connection cConn; 264 265 InitialContext ic = new InitialContext (); 266 DataSource ds; 267 try 268 { 269 Object o = ic.lookup("java:/DefaultDS"); 270 if (o == null) 271 log.error("java:/DefaultDS not found"); 272 ds = (DataSource ) o; 273 } 274 catch (NamingException e) 275 { 276 log.error("Problem looking up Datasource " + e.toString()); 277 throw e; 278 } 279 280 cConn = ds.getConnection(); 281 return cConn; 282 } 283 284 } 285 | Popular Tags |