KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > idxandusersql > test > IdxAndUsersqlUnitTestCase


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.idxandusersql.test;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DatabaseMetaData JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.sql.DataSource JavaDoc;
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 /**
37  * Test the <dbindex> and <post-table-create> features
38  * @author heiko.rupp@cellent.de
39  * @version $Revision: 58115 $
40  */

41 public class IdxAndUsersqlUnitTestCase extends EJBTestCase
42 {
43
44    static final Logger log = Logger.getLogger(IdxAndUsersqlUnitTestCase.class);
45    InitialContext JavaDoc ic = null;
46
47    public static Test suite() throws Exception JavaDoc
48    {
49       return JBossTestCase.getDeploySetup(
50          IdxAndUsersqlUnitTestCase.class,
51          "cmp2-idxandusersql.jar");
52    }
53
54    public IdxAndUsersqlUnitTestCase(String JavaDoc name)
55    {
56       super(name);
57       try
58       {
59          ic = new InitialContext JavaDoc();
60       }
61       catch (NamingException JavaDoc e)
62       {
63          ic = null;
64       }
65    }
66
67    public void setUpEJB() throws Exception JavaDoc
68    {
69       super.setUpEJB();
70    }
71
72    /**
73     * Check if we use the HSQL Database, as the way how the column
74     * names are stored in the database (upper vs lower case) is probably
75     * database specific.
76     * For HSQL this is currently in uppercase, as it is for the tablenames
77     * @throws Exception
78     */

79
80    public void testHsqlDriver() throws Exception JavaDoc
81    {
82       Connection JavaDoc cConn = getConnection();
83       DatabaseMetaData JavaDoc dMeta = cConn.getMetaData();
84
85       String JavaDoc driver = dMeta.getDriverName();
86       String JavaDoc 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 JavaDoc
94    {
95       boolean found = false;
96
97       // Column FOO should be indexed via <dbindex/>
98
found = lookForIndexOnTable("DBINDEXTEST", "FOO");
99       assertTrue("Error: column foo is not indexed", found);
100
101    }
102
103    public void testDBIndexBaz() throws Exception JavaDoc
104    {
105       boolean found = true;
106
107       // Column BAZ should not be indexed
108
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 JavaDoc
114    {
115       boolean found = false;
116
117       // Column BAR should be indexed by the <post-table-create/>
118
// setting
119
found = lookForIndexOnTable("DBINDEXTEST", "BAR");
120       assertTrue("Error column bar is not indexed via post-table-create", found);
121    }
122
123    //
124
// Check the "default" case for CMR1- and CMR2-Bean, which is
125
// foo* has an index and bar* has no index
126

127    public void testCMR1IndexFoo() throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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    //
160
// Look for index on fk-mapping in 1:n case
161
//
162
public void testFkIdxCmr1() throws Exception JavaDoc
163    {
164       boolean found = false;
165
166       found = lookForIndexOnTable("CMR1", "IDXFK");
167       assertTrue("Error: Foreign key idxFk is not indexed", found);
168    }
169
170    /*
171     * Look for indices on the m:n mapping table
172     * This is for hsql a strange case, at indices are put there
173     * anyway, but it has been told that other databases don't do
174     * it by themselves, so we check if the creation succeeds.
175     */

176
177    public void testCMRmn1() throws Exception JavaDoc
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 JavaDoc
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    /*
195     * Test if the replacement of %%t and %%n in <post-table-create> worked
196     */

197     
198     public void testDefault1() throws Exception JavaDoc
199     {
200         boolean found = true;
201        
202         // This one should not have this index, as the <default>
203
// <post-table-create> is overwritten by a 'bean local'
204
// <post-table-create>
205
found = lookForIndexOnTable("DBINDXTEST","BLABLA");
206         assertTrue("Error: Field blabla in table dbindex has an index",found==false);
207     }
208     public void testDefault2() throws Exception JavaDoc
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 JavaDoc
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    /**
228     * Find out if the named column in the given table has an index
229     * other than the one (possibly) created by a (composite) primary key
230     */

231    private boolean lookForIndexOnTable(String JavaDoc table, String JavaDoc column)
232       throws Exception JavaDoc
233    {
234       boolean found = false;
235       Connection JavaDoc cConn = getConnection();
236       DatabaseMetaData JavaDoc dMeta = cConn.getMetaData();
237
238       ResultSet JavaDoc rs = dMeta.getIndexInfo(null, null, table, false, false);
239
240       while (rs.next())
241       {
242          String JavaDoc cName = rs.getString(9); // name of column
243
String JavaDoc iName = rs.getString(6); // name of index
244

245          // Ignore indices that are autogenerated for PKs
246
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    /**
258     * Get a JDBC connection from the default datasource, which is
259     * expected to be hsqldb
260     */

261    private Connection JavaDoc getConnection() throws Exception JavaDoc
262    {
263       Connection JavaDoc cConn;
264
265       InitialContext JavaDoc ic = new InitialContext JavaDoc();
266       DataSource JavaDoc ds;
267       try
268       {
269          Object JavaDoc o = ic.lookup("java:/DefaultDS");
270          if (o == null)
271             log.error("java:/DefaultDS not found");
272          ds = (DataSource JavaDoc) o;
273       }
274       catch (NamingException JavaDoc 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