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.ejbselect;
023:
024: import java.util.Collection;
025: import java.util.Iterator;
026:
027: import javax.ejb.FinderException;
028:
029: import junit.framework.Test;
030: import org.jboss.test.util.ejb.EJBTestCase;
031: import org.jboss.test.JBossTestCase;
032:
033: /**
034: * @author others + <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
035: */
036: public class EJBSelectUnitTestCase extends EJBTestCase {
037: static org.apache.log4j.Category log = org.apache.log4j.Category
038: .getInstance(EJBSelectUnitTestCase.class);
039:
040: public static Test suite() throws Exception {
041: return JBossTestCase.getDeploySetup(
042: EJBSelectUnitTestCase.class, "cmp2-ejbselect.jar");
043: }
044:
045: public EJBSelectUnitTestCase(String name) {
046: super (name);
047: }
048:
049: private ALocal a1;
050: private ALocal a2;
051: private BLocal b1;
052: private BLocal b2;
053: private BLocal b3;
054: private BLocal b4;
055:
056: public void setUpEJB() throws Exception {
057: ALocalHome ahome = AUtil.getLocalHome();
058: BLocalHome bhome = BUtil.getLocalHome();
059:
060: a1 = ahome.create("A1");
061: a1.setIntField(3);
062: Collection bs = a1.getBs();
063: b1 = bhome.create("B1", "Alice", true);
064: bs.add(b1);
065: b2 = bhome.create("B2", "Bob", true);
066: bs.add(b2);
067: b3 = bhome.create("B3", "Charlie", false);
068: bs.add(b3);
069: b4 = bhome.create("B4", "Dan", false);
070: bs.add(b4);
071:
072: a2 = ahome.create("A2");
073: a2.setIntField(9);
074: }
075:
076: public void tearDownEJB() throws Exception {
077: a1.remove();
078: a2.remove();
079: }
080:
081: public void testReturnedInterface() throws Exception {
082: Iterator i = a1.getSomeBs().iterator();
083: while (i.hasNext()) {
084: Object obj = i.next();
085: assertTrue(obj instanceof BLocal);
086: BLocal b = (BLocal) obj;
087: b.getName();
088: }
089:
090: i = a1.getSomeBs().iterator();
091: while (i.hasNext()) {
092: Object obj = i.next();
093: assertTrue(obj instanceof BLocal);
094: BLocal b = (BLocal) obj;
095: b.getName();
096: }
097: }
098:
099: public void testEJBSelectFromEJBHomeMethod() throws Exception {
100: ALocalHome ahome = AUtil.getLocalHome();
101: Collection results = ahome.getSomeBs(a1);
102: for (Iterator iterator = results.iterator(); iterator.hasNext();) {
103: Object obj = iterator.next();
104: assertTrue(obj instanceof BLocal);
105: BLocal b = (BLocal) obj;
106: b.getName();
107: }
108:
109: assertTrue(results.contains(b1));
110: assertTrue(results.contains(b2));
111: assertTrue(results.contains(b3));
112: assertTrue(results.contains(b4));
113: assertEquals(4, results.size());
114: }
115:
116: public void testCheckFinderForNullInEJBSELECT() throws Exception {
117: ALocalHome ahome = AUtil.getLocalHome();
118: try {
119: ahome.checkFinderForNull();
120: fail("Should not be here");
121: } catch (FinderException expected) {
122: log.debug("Got expected error", expected);
123: }
124: }
125:
126: public void testGetSomeBxDeclaredSQL() throws Exception {
127: ALocalHome ahome = AUtil.getLocalHome();
128: Collection results = ahome.getSomeBsDeclaredSQL(a1);
129: for (Iterator iterator = results.iterator(); iterator.hasNext();) {
130: Object obj = iterator.next();
131: assertTrue(obj instanceof BLocal);
132: BLocal b = (BLocal) obj;
133: b.getName();
134: }
135:
136: assertTrue(results.contains(b1));
137: assertTrue(results.contains(b2));
138: assertTrue(results.contains(b3));
139: assertTrue(results.contains(b4));
140: assertEquals(4, results.size());
141: }
142:
143: public void testGetTrue() throws Exception {
144: Collection bs = b1.getTrue();
145: assertEquals(2, bs.size());
146: assertTrue(bs.contains(b1));
147: assertTrue(bs.contains(b2));
148: assertTrue(!bs.contains(b3));
149: assertTrue(!bs.contains(b4));
150:
151: Iterator i = bs.iterator();
152: while (i.hasNext()) {
153: BLocal b = (BLocal) i.next();
154: assertTrue(b.getBool());
155: }
156: }
157:
158: public void testGetFalse() throws Exception {
159: Collection bs = b1.getFalse();
160: assertEquals(2, bs.size());
161: assertTrue(!bs.contains(b1));
162: assertTrue(!bs.contains(b2));
163: assertTrue(bs.contains(b3));
164: assertTrue(bs.contains(b4));
165:
166: Iterator i = bs.iterator();
167: while (i.hasNext()) {
168: BLocal b = (BLocal) i.next();
169: assertTrue(!b.getBool());
170: }
171: }
172:
173: public void testGetAWithBs() throws Exception {
174: Collection as = a1.getAWithBs();
175: assertEquals(1, as.size());
176: assertTrue(as.contains(a1));
177: assertTrue(!as.contains(a2));
178:
179: Iterator i = as.iterator();
180: while (i.hasNext()) {
181: ALocal a = (ALocal) i.next();
182: assertTrue(!a.getBs().isEmpty());
183: }
184: }
185:
186: // SQL funsctions in SELECT clause
187:
188: public void testCountInSelectClause() throws Exception {
189: Collection result = BUtil.getLocalHome().selectDynamic(
190: "SELECT COUNT(b.id) FROM B AS b", new Object[] {});
191: assertTrue("COUNT(b.id) = 4", ((Long) result.iterator().next())
192: .longValue() == 4);
193: }
194:
195: public void testMaxInSelectClause() throws Exception {
196: Collection result = BUtil.getLocalHome().selectDynamic(
197: "SELECT MAX(a.intField) FROM A AS a", new Object[] {});
198: assertTrue("MAX(a.id) = 9", ((Double) result.iterator().next())
199: .doubleValue() == 9.0);
200: }
201:
202: public void testMinInSelectClause() throws Exception {
203: Collection result = BUtil.getLocalHome().selectDynamic(
204: "SELECT MIN(a.intField) FROM A AS a", new Object[] {});
205: assertTrue("MIN(a.id) = 3", ((Double) result.iterator().next())
206: .doubleValue() == 3.0);
207: }
208:
209: public void testSumInSelectClause() throws Exception {
210: Collection result = BUtil.getLocalHome().selectDynamic(
211: "SELECT SUM(a.intField) FROM A AS a", new Object[] {});
212: assertTrue(
213: "SUM(a.id) = 12",
214: ((Double) result.iterator().next()).doubleValue() == 12.0);
215: }
216:
217: public void testAvgInSelectClause() throws Exception {
218: Collection result = BUtil.getLocalHome().selectDynamic(
219: "SELECT AVG(a.intField) FROM A AS a", new Object[] {});
220: assertTrue("AVG(a.id) = 6", ((Double) result.iterator().next())
221: .doubleValue() == 6.0);
222: }
223:
224: public void testSqrtInSelectClause() throws Exception {
225: String pk = "B1";
226: BLocal b = BUtil.getLocalHome().findByPrimaryKey(pk);
227: b.setLongField(64);
228:
229: Collection result = BUtil.getLocalHome().selectDynamic(
230: "SELECT SQRT(b.longField) FROM B AS b WHERE b.id = ?1",
231: new Object[] { pk });
232: assertTrue("SQRT(b.longField) = 8", ((Double) result.iterator()
233: .next()).doubleValue() == 8.0);
234: }
235:
236: /* HSQL has problems with returning ABS? (returns null)
237: public void testAbsInSelectClause() throws Exception
238: {
239: String pk = "B1";
240: BLocal b = BUtil.getLocalHome().findByPrimaryKey(pk);
241: b.setLongField(-5);
242:
243: Collection result = BUtil.getLocalHome().selectDynamic(
244: "SELECT ABS(b.longField) FROM B AS b WHERE b.id = ?1", new Object[]{pk}
245: );
246: assertTrue("ABS(b.longField) = 5", ((Long)result.iterator().next()).longValue() == 5);
247: }
248: */
249:
250: public void testLcaseInSelectClause() throws Exception {
251: Collection result = BUtil.getLocalHome().selectDynamic(
252: "SELECT LCASE(b.name) FROM B AS b WHERE b.id = ?1",
253: new Object[] { "B1" });
254: assertTrue("LCASE(b.name) = alice", "alice".equals(result
255: .iterator().next()));
256: }
257:
258: public void testUcaseInSelectClause() throws Exception {
259: Collection result = BUtil.getLocalHome().selectDynamic(
260: "SELECT UCASE(b.name) FROM B AS b", new Object[] {});
261: assertTrue("result.size() == 4", result.size() == 4);
262: assertTrue("result.contains('ALICE')", result.contains("ALICE"));
263: assertTrue("result.contains('BOB')", result.contains("BOB"));
264: assertTrue("result.contains('CHARLIE')", result
265: .contains("CHARLIE"));
266: assertTrue("result.contains('DAN')", result.contains("DAN"));
267: }
268:
269: public void testLengthInSelectClause() throws Exception {
270: Collection result = BUtil.getLocalHome().selectDynamic(
271: "SELECT LENGTH(b.name) FROM B AS b WHERE b.id = ?1",
272: new Object[] { "B1" });
273: assertTrue("LENGTH(b.name) = 5", ((Long) result.iterator()
274: .next()).longValue() == 5);
275: }
276:
277: public void testConcatInSelectClause() throws Exception {
278: Collection result = BUtil
279: .getLocalHome()
280: .selectDynamic(
281: "SELECT CONCAT('Dear ', b.name) FROM B AS b WHERE b.id = ?1",
282: new Object[] { "B1" });
283: assertTrue("CONCAT('Dear ', b.name) = Dear Alice", "Dear Alice"
284: .equals(result.iterator().next()));
285: }
286:
287: public void testLocateInSelectClause() throws Exception {
288: Collection result = BUtil
289: .getLocalHome()
290: .selectDynamic(
291: "SELECT LOCATE('ice', b.name, 1) FROM B AS b WHERE b.id = ?1",
292: new Object[] { "B1" });
293: assertTrue("LOCATE('ice', b.name, 1) = 3", ((Long) result
294: .iterator().next()).longValue() == 3);
295: }
296:
297: public void testSubstringInSelectClause() throws Exception {
298: Collection result = BUtil
299: .getLocalHome()
300: .selectDynamic(
301: "SELECT SUBSTRING(b.name, 3, 5) FROM B AS b WHERE b.id = ?1",
302: new Object[] { "B1" });
303: assertTrue("SUBSTRING(b.name, 3, 5) = ice", "ice".equals(result
304: .iterator().next()));
305: }
306:
307: public void testNestedFunctionsInSelectClause() throws Exception {
308: Collection result = BUtil
309: .getLocalHome()
310: .selectDynamic(
311: "SELECT UCASE(SUBSTRING(CONCAT(b.id, b.name), 5, 7)) FROM B AS b WHERE b.id = ?1",
312: new Object[] { "B1" });
313: assertTrue(
314: "UCASE(SUBSTRING(CONCAT(b.id, b.name), 5, 7)) = ICE",
315: "ICE".equals(result.iterator().next()));
316: }
317:
318: public void testLimit() throws Exception {
319: Collection col = BUtil
320: .getLocalHome()
321: .selectDynamic(
322: "select object(b) from B b where b.id is not null order by b.id desc limit 1",
323: null);
324: assertEquals(1, col.size());
325: BLocal b = (BLocal) col.iterator().next();
326: assertEquals("B4", b.getId());
327: }
328:
329: public void testOffset() throws Exception {
330: Collection col = BUtil
331: .getLocalHome()
332: .selectDynamic(
333: "select object(b) from B b where b.id is not null order by b.id offset 1",
334: null);
335: assertEquals(3, col.size());
336: Iterator iter = col.iterator();
337: int i = 2;
338: while (iter.hasNext()) {
339: BLocal b = (BLocal) iter.next();
340: assertEquals("B" + i++, b.getId());
341: }
342: }
343:
344: public void testOffsetLimit() throws Exception {
345: Collection col = BUtil
346: .getLocalHome()
347: .selectDynamic(
348: "select object(b) from B b where b.id is not null order by b.id offset 1 limit 2",
349: null);
350: assertEquals(2, col.size());
351: Iterator iter = col.iterator();
352: int i = 2;
353: while (iter.hasNext()) {
354: BLocal b = (BLocal) iter.next();
355: assertEquals("B" + i++, b.getId());
356: }
357: }
358: }
|