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.idxandusersql.test;
023:
024: import java.sql.Connection;
025: import java.sql.DatabaseMetaData;
026: import java.sql.ResultSet;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029: import javax.sql.DataSource;
030:
031: import junit.framework.Test;
032: import org.jboss.test.util.ejb.EJBTestCase;
033: import org.apache.log4j.Logger;
034: import org.jboss.test.JBossTestCase;
035:
036: /**
037: * Test the <dbindex> and <post-table-create> features
038: * @author heiko.rupp@cellent.de
039: * @version $Revision: 61780 $
040: */
041: public class IdxAndUsersqlUnitTestCase extends EJBTestCase {
042:
043: static final Logger log = Logger
044: .getLogger(IdxAndUsersqlUnitTestCase.class);
045: InitialContext ic = null;
046:
047: public static Test suite() throws Exception {
048: return JBossTestCase.getDeploySetup(
049: IdxAndUsersqlUnitTestCase.class,
050: "cmp2-idxandusersql.jar");
051: }
052:
053: public IdxAndUsersqlUnitTestCase(String name) {
054: super (name);
055: try {
056: ic = new InitialContext();
057: } catch (NamingException e) {
058: ic = null;
059: }
060: }
061:
062: public void setUpEJB() throws Exception {
063: super .setUpEJB();
064: }
065:
066: /**
067: * Check if we use the HSQL Database, as the way how the column
068: * names are stored in the database (upper vs lower case) is probably
069: * database specific.
070: * For HSQL this is currently in uppercase, as it is for the tablenames
071: * @throws Exception
072: */
073:
074: public void testHsqlDriver() throws Exception {
075: Connection cConn = getConnection();
076: DatabaseMetaData dMeta = cConn.getMetaData();
077:
078: String driver = dMeta.getDriverName();
079: String version = dMeta.getDriverVersion();
080:
081: cConn.close();
082: assertTrue("Error: We are not using HSQL", driver
083: .startsWith("HSQL Database"));
084:
085: }
086:
087: public void testDBIndexFoo() throws Exception {
088: boolean found = false;
089:
090: // Column FOO should be indexed via <dbindex/>
091: found = lookForIndexOnTable("DBINDEXTEST", "FOO");
092: assertTrue("Error: column foo is not indexed", found);
093:
094: }
095:
096: public void testDBIndexBaz() throws Exception {
097: boolean found = true;
098:
099: // Column BAZ should not be indexed
100: found = lookForIndexOnTable("DBINDEXTEST", "BAZ");
101: assertTrue("Error: column baz is indexed but shouldn't",
102: found == false);
103:
104: }
105:
106: public void testUserSQL1() throws Exception {
107: boolean found = false;
108:
109: // Column BAR should be indexed by the <post-table-create/>
110: // setting
111: found = lookForIndexOnTable("DBINDEXTEST", "BAR");
112: assertTrue(
113: "Error column bar is not indexed via post-table-create",
114: found);
115: }
116:
117: //
118: // Check the "default" case for CMR1- and CMR2-Bean, which is
119: // foo* has an index and bar* has no index
120:
121: public void testCMR1IndexFoo() throws Exception {
122: boolean found = false;
123:
124: found = lookForIndexOnTable("CMR1", "FOO1");
125: assertTrue("Error: column foo1 is not indexed", found);
126: }
127:
128: public void testCMR1IndexBar() throws Exception {
129: boolean found = true;
130:
131: found = lookForIndexOnTable("CMR1", "BAR1");
132: assertTrue("Error: column bar1 is indexed and shouldn't",
133: found == false);
134: }
135:
136: public void testCMR2IndexFoo() throws Exception {
137: boolean found = false;
138:
139: found = lookForIndexOnTable("CMR2", "FOO2");
140: assertTrue("Error: column foo1 is not indexed", found);
141: }
142:
143: public void testCMR2IndexBar() throws Exception {
144: boolean found = true;
145:
146: found = lookForIndexOnTable("CMR2", "BAR2");
147: assertTrue("Eror: column bar2 is indexed and shouldn't",
148: found == false);
149: }
150:
151: //
152: // Look for index on fk-mapping in 1:n case
153: //
154: public void testFkIdxCmr1() throws Exception {
155: boolean found = false;
156:
157: found = lookForIndexOnTable("CMR1", "IDXFK");
158: assertTrue("Error: Foreign key idxFk is not indexed", found);
159: }
160:
161: /*
162: * Look for indices on the m:n mapping table
163: * This is for hsql a strange case, at indices are put there
164: * anyway, but it has been told that other databases don't do
165: * it by themselves, so we check if the creation succeeds.
166: */
167:
168: public void testCMRmn1() throws Exception {
169: boolean found = false;
170:
171: found = lookForIndexOnTable("IDX_CMR2_REL", "IDX_ID");
172: assertTrue("Error: FKey idx_id is not indexed via <dbindex>",
173: found);
174: }
175:
176: public void testCMRmn2() throws Exception {
177: boolean found = true;
178:
179: found = lookForIndexOnTable("IDX_CMR2_REL", "CMR2_ID");
180: assertTrue("Error: FKey cmr2_id is indexed", found == false);
181: }
182:
183: /*
184: * Test if the replacement of %%t and %%n in <post-table-create> worked
185: */
186:
187: public void testDefault1() throws Exception {
188: boolean found = true;
189:
190: // This one should not have this index, as the <default>
191: // <post-table-create> is overwritten by a 'bean local'
192: // <post-table-create>
193: found = lookForIndexOnTable("DBINDXTEST", "BLABLA");
194: assertTrue("Error: Field blabla in table dbindex has an index",
195: found == false);
196: }
197:
198: public void testDefault2() throws Exception {
199: boolean found = false;
200:
201: found = lookForIndexOnTable("CMR1", "BLABLA");
202: assertTrue("Error: Field blabla in table cmr1 has no index",
203: found);
204: }
205:
206: public void testDefault3() throws Exception {
207: boolean found = false;
208:
209: found = lookForIndexOnTable("CMR2", "BLABLA");
210: assertTrue("Error: Field blabla in table cmr2 has no index",
211: found);
212: }
213:
214: /**
215: * Find out if the named column in the given table has an index
216: * other than the one (possibly) created by a (composite) primary key
217: */
218: private boolean lookForIndexOnTable(String table, String column)
219: throws Exception {
220: boolean found = false;
221: Connection cConn = getConnection();
222: DatabaseMetaData dMeta = cConn.getMetaData();
223:
224: ResultSet rs = dMeta.getIndexInfo(null, null, table, false,
225: false);
226:
227: while (rs.next()) {
228: String cName = rs.getString(9); // name of column
229: String iName = rs.getString(6); // name of index
230:
231: // Ignore indices that are autogenerated for PKs
232: if (cName.equals(column) && !iName.startsWith("PK_IDX")) {
233: found = true;
234: }
235: }
236: rs.close();
237: cConn.close();
238:
239: return found;
240: }
241:
242: /**
243: * Get a JDBC connection from the default datasource, which is
244: * expected to be hsqldb
245: */
246: private Connection getConnection() throws Exception {
247: Connection cConn;
248:
249: InitialContext ic = new InitialContext();
250: DataSource ds;
251: try {
252: Object o = ic.lookup("java:/DefaultDS");
253: if (o == null)
254: log.error("java:/DefaultDS not found");
255: ds = (DataSource) o;
256: } catch (NamingException e) {
257: log.error("Problem looking up Datasource " + e.toString());
258: throw e;
259: }
260:
261: cConn = ds.getConnection();
262: return cConn;
263: }
264:
265: }
|