KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > ejbselect > EJBSelectUnitTestCase


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.ejbselect;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.ejb.FinderException JavaDoc;
28
29 import junit.framework.Test;
30 import net.sourceforge.junitejb.EJBTestCase;
31 import org.jboss.test.JBossTestCase;
32
33 /**
34  * @author others + <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
35  */

36 public class EJBSelectUnitTestCase extends EJBTestCase
37 {
38    static org.jboss.logging.Logger log =
39       org.jboss.logging.Logger.getLogger(EJBSelectUnitTestCase.class);
40
41    public static Test suite() throws Exception JavaDoc
42    {
43       return JBossTestCase.getDeploySetup(EJBSelectUnitTestCase.class, "cmp2-ejbselect.jar");
44    }
45
46    public EJBSelectUnitTestCase(String JavaDoc name)
47    {
48       super(name);
49    }
50
51    private ALocal a1;
52    private ALocal a2;
53    private BLocal b1;
54    private BLocal b2;
55    private BLocal b3;
56    private BLocal b4;
57
58    public void setUpEJB() throws Exception JavaDoc
59    {
60       ALocalHome ahome = AUtil.getLocalHome();
61       BLocalHome bhome = BUtil.getLocalHome();
62
63       a1 = ahome.create("A1");
64       a1.setIntField(3);
65       Collection JavaDoc bs = a1.getBs();
66       b1 = bhome.create("B1", "Alice", true);
67       bs.add(b1);
68       b2 = bhome.create("B2", "Bob", true);
69       bs.add(b2);
70       b3 = bhome.create("B3", "Charlie", false);
71       bs.add(b3);
72       b4 = bhome.create("B4", "Dan", false);
73       bs.add(b4);
74
75       a2 = ahome.create("A2");
76       a2.setIntField(9);
77    }
78
79    public void tearDownEJB() throws Exception JavaDoc
80    {
81       a1.remove();
82       a2.remove();
83    }
84
85    public void testReturnedInterface() throws Exception JavaDoc
86    {
87       Iterator JavaDoc i = a1.getSomeBs().iterator();
88       while(i.hasNext())
89       {
90          Object JavaDoc obj = i.next();
91          assertTrue(obj instanceof BLocal);
92          BLocal b = (BLocal)obj;
93          b.getName();
94       }
95
96       i = a1.getSomeBs().iterator();
97       while(i.hasNext())
98       {
99          Object JavaDoc obj = i.next();
100          assertTrue(obj instanceof BLocal);
101          BLocal b = (BLocal)obj;
102          b.getName();
103       }
104    }
105
106    public void testEJBSelectFromEJBHomeMethod() throws Exception JavaDoc
107    {
108       ALocalHome ahome = AUtil.getLocalHome();
109       Collection JavaDoc results = ahome.getSomeBs(a1);
110       for(Iterator JavaDoc iterator = results.iterator(); iterator.hasNext();)
111       {
112          Object JavaDoc obj = iterator.next();
113          assertTrue(obj instanceof BLocal);
114          BLocal b = (BLocal)obj;
115          b.getName();
116       }
117
118       assertTrue(results.contains(b1));
119       assertTrue(results.contains(b2));
120       assertTrue(results.contains(b3));
121       assertTrue(results.contains(b4));
122       assertEquals(4, results.size());
123    }
124
125    public void testCheckFinderForNullInEJBSELECT() throws Exception JavaDoc
126    {
127       ALocalHome ahome = AUtil.getLocalHome();
128       try
129       {
130           ahome.checkFinderForNull();
131           fail("Should not be here");
132       }
133       catch (FinderException JavaDoc expected)
134       {
135          log.debug("Got expected error", expected);
136       }
137    }
138
139    public void testGetSomeBxDeclaredSQL() throws Exception JavaDoc
140    {
141       ALocalHome ahome = AUtil.getLocalHome();
142       Collection JavaDoc results = ahome.getSomeBsDeclaredSQL(a1);
143       for(Iterator JavaDoc iterator = results.iterator(); iterator.hasNext();)
144       {
145          Object JavaDoc obj = iterator.next();
146          assertTrue(obj instanceof BLocal);
147          BLocal b = (BLocal)obj;
148          b.getName();
149       }
150
151       assertTrue(results.contains(b1));
152       assertTrue(results.contains(b2));
153       assertTrue(results.contains(b3));
154       assertTrue(results.contains(b4));
155       assertEquals(4, results.size());
156    }
157
158    public void testGetTrue() throws Exception JavaDoc
159    {
160       Collection JavaDoc bs = b1.getTrue();
161       assertEquals(2, bs.size());
162       assertTrue(bs.contains(b1));
163       assertTrue(bs.contains(b2));
164       assertTrue(!bs.contains(b3));
165       assertTrue(!bs.contains(b4));
166
167       Iterator JavaDoc i = bs.iterator();
168       while(i.hasNext())
169       {
170          BLocal b = (BLocal)i.next();
171          assertTrue(b.getBool());
172       }
173    }
174
175    public void testGetFalse() throws Exception JavaDoc
176    {
177       Collection JavaDoc bs = b1.getFalse();
178       assertEquals(2, bs.size());
179       assertTrue(!bs.contains(b1));
180       assertTrue(!bs.contains(b2));
181       assertTrue(bs.contains(b3));
182       assertTrue(bs.contains(b4));
183
184       Iterator JavaDoc i = bs.iterator();
185       while(i.hasNext())
186       {
187          BLocal b = (BLocal)i.next();
188          assertTrue(!b.getBool());
189       }
190    }
191
192    public void testGetAWithBs() throws Exception JavaDoc
193    {
194       Collection JavaDoc as = a1.getAWithBs();
195       assertEquals(1, as.size());
196       assertTrue(as.contains(a1));
197       assertTrue(!as.contains(a2));
198
199       Iterator JavaDoc i = as.iterator();
200       while(i.hasNext())
201       {
202          ALocal a = (ALocal)i.next();
203          assertTrue(!a.getBs().isEmpty());
204       }
205    }
206
207    // SQL funsctions in SELECT clause
208

209    public void testCountInSelectClause() throws Exception JavaDoc
210    {
211       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
212          "SELECT COUNT(b.id) FROM B AS b", new Object JavaDoc[]{}
213       );
214       assertTrue("COUNT(b.id) = 4", ((Long JavaDoc)result.iterator().next()).longValue() == 4);
215    }
216
217    public void testMaxInSelectClause() throws Exception JavaDoc
218    {
219       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
220          "SELECT MAX(a.intField) FROM A AS a", new Object JavaDoc[]{}
221       );
222       assertTrue("MAX(a.id) = 9", ((Double JavaDoc)result.iterator().next()).doubleValue() == 9.0);
223    }
224
225    public void testMinInSelectClause() throws Exception JavaDoc
226    {
227       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
228          "SELECT MIN(a.intField) FROM A AS a", new Object JavaDoc[]{}
229       );
230       assertTrue("MIN(a.id) = 3", ((Double JavaDoc)result.iterator().next()).doubleValue() == 3.0);
231    }
232
233    public void testSumInSelectClause() throws Exception JavaDoc
234    {
235       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
236          "SELECT SUM(a.intField) FROM A AS a", new Object JavaDoc[]{}
237       );
238       assertTrue("SUM(a.id) = 12", ((Double JavaDoc)result.iterator().next()).doubleValue() == 12.0);
239    }
240
241    public void testAvgInSelectClause() throws Exception JavaDoc
242    {
243       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
244          "SELECT AVG(a.intField) FROM A AS a", new Object JavaDoc[]{}
245       );
246       assertTrue("AVG(a.id) = 6", ((Double JavaDoc)result.iterator().next()).doubleValue() == 6.0);
247    }
248
249    public void testSqrtInSelectClause() throws Exception JavaDoc
250    {
251       String JavaDoc pk = "B1";
252       BLocal b = BUtil.getLocalHome().findByPrimaryKey(pk);
253       b.setLongField(64);
254
255       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
256          "SELECT SQRT(b.longField) FROM B AS b WHERE b.id = ?1", new Object JavaDoc[]{pk}
257       );
258       assertTrue("SQRT(b.longField) = 8", ((Double JavaDoc)result.iterator().next()).doubleValue() == 8.0);
259    }
260
261    /* HSQL has problems with returning ABS? (returns null)
262    public void testAbsInSelectClause() throws Exception
263    {
264       String pk = "B1";
265       BLocal b = BUtil.getLocalHome().findByPrimaryKey(pk);
266       b.setLongField(-5);
267
268       Collection result = BUtil.getLocalHome().selectDynamic(
269          "SELECT ABS(b.longField) FROM B AS b WHERE b.id = ?1", new Object[]{pk}
270       );
271       assertTrue("ABS(b.longField) = 5", ((Long)result.iterator().next()).longValue() == 5);
272    }
273    */

274
275    public void testLcaseInSelectClause() throws Exception JavaDoc
276    {
277       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
278          "SELECT LCASE(b.name) FROM B AS b WHERE b.id = ?1", new Object JavaDoc[]{"B1"}
279       );
280       assertTrue("LCASE(b.name) = alice", "alice".equals(result.iterator().next()));
281    }
282
283    public void testUcaseInSelectClause() throws Exception JavaDoc
284    {
285       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
286          "SELECT UCASE(b.name) FROM B AS b", new Object JavaDoc[]{}
287       );
288       assertTrue("result.size() == 4", result.size() == 4);
289       assertTrue("result.contains('ALICE')", result.contains("ALICE"));
290       assertTrue("result.contains('BOB')", result.contains("BOB"));
291       assertTrue("result.contains('CHARLIE')", result.contains("CHARLIE"));
292       assertTrue("result.contains('DAN')", result.contains("DAN"));
293    }
294
295    public void testLengthInSelectClause() throws Exception JavaDoc
296    {
297       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
298          "SELECT LENGTH(b.name) FROM B AS b WHERE b.id = ?1", new Object JavaDoc[]{"B1"}
299       );
300       assertTrue("LENGTH(b.name) = 5", ((Long JavaDoc)result.iterator().next()).longValue() == 5);
301    }
302
303    public void testConcatInSelectClause() throws Exception JavaDoc
304    {
305       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
306          "SELECT CONCAT('Dear ', b.name) FROM B AS b WHERE b.id = ?1", new Object JavaDoc[]{"B1"}
307       );
308       assertTrue("CONCAT('Dear ', b.name) = Dear Alice", "Dear Alice".equals(result.iterator().next()));
309    }
310
311    public void testLocateInSelectClause() throws Exception JavaDoc
312    {
313       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
314          "SELECT LOCATE('ice', b.name, 1) FROM B AS b WHERE b.id = ?1", new Object JavaDoc[]{"B1"}
315       );
316       assertTrue("LOCATE('ice', b.name, 1) = 3", ((Long JavaDoc)result.iterator().next()).longValue() == 3);
317    }
318
319    public void testSubstringInSelectClause() throws Exception JavaDoc
320    {
321       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
322          "SELECT SUBSTRING(b.name, 3, 5) FROM B AS b WHERE b.id = ?1", new Object JavaDoc[]{"B1"}
323       );
324       assertTrue("SUBSTRING(b.name, 3, 5) = ice", "ice".equals(result.iterator().next()));
325    }
326
327    public void testNestedFunctionsInSelectClause() throws Exception JavaDoc
328    {
329       Collection JavaDoc result = BUtil.getLocalHome().selectDynamic(
330          "SELECT UCASE(SUBSTRING(CONCAT(b.id, b.name), 5, 7)) FROM B AS b WHERE b.id = ?1", new Object JavaDoc[]{"B1"}
331       );
332       assertTrue("UCASE(SUBSTRING(CONCAT(b.id, b.name), 5, 7)) = ICE", "ICE".equals(result.iterator().next()));
333    }
334 }
335
Popular Tags