001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: F_BasicEjbqlEC2.java 10103 2007-03-28 08:07:47Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.entity;
027:
028: import java.util.Collection;
029: import java.util.Iterator;
030:
031: import javax.naming.NamingException;
032: import javax.rmi.PortableRemoteObject;
033:
034: import junit.framework.Test;
035: import junit.framework.TestSuite;
036:
037: import org.objectweb.jonas.jtests.beans.ebasic.E4Query;
038: import org.objectweb.jonas.jtests.beans.ebasic.E4QueryHome;
039: import org.objectweb.jonas.jtests.util.JTestCase;
040:
041: /**
042: * For testing basic EJB QL queries.
043: * @author Helene Joanin
044: */
045:
046: public class F_BasicEjbqlEC2 extends JTestCase {
047:
048: private static String BEAN_HOME_E4QUERY = "ebasicE4QueryEC2Home";
049: protected static E4QueryHome home = null;
050:
051: public F_BasicEjbqlEC2(String name) {
052: super (name);
053: }
054:
055: protected void setUp() {
056: if (home == null) {
057: // load bean if not loaded yet
058: useBeans("ebasic", true);
059: try {
060: home = (E4QueryHome) PortableRemoteObject.narrow(ictx
061: .lookup(BEAN_HOME_E4QUERY), E4QueryHome.class);
062: } catch (NamingException e) {
063: fail("Cannot get bean home: " + e.getMessage());
064: }
065: // check if tables have been initialized
066: try {
067: home.findByPrimaryKey("id1");
068: } catch (Exception e) {
069: try {
070: home.create("idnull", null, 0, 0.0);
071: home.create("id1", "helene", 1959, 1959.0);
072: home.create("id2", "ahelene", -1959, 1959.0);
073: home.create("id3", "helene-bis", 1959 * 1959,
074: 1959.0 * 1959.0);
075: home.create("id4", "eric", 1957, 1957.0);
076: home.create("id4e", "ric", 1957, 1957.0);
077: } catch (Exception i) {
078: fail("InitialState creation problem:: " + i);
079: }
080: }
081: }
082: }
083:
084: /**
085: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE LENGTH(o.fstring) = ?1
086: */
087: public void testLength() throws Exception {
088: int l = "helene".length();
089: Collection cBeans = home.findByLengthString(l);
090: Iterator iBeans = cBeans.iterator();
091: int nb = 0;
092: while (iBeans.hasNext()) {
093: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
094: .narrow(iBeans.next(), E4Query.class);
095: assertEquals("Id=" + bean.getId(), l, bean.getFstring()
096: .length());
097: nb++;
098: }
099: assertEquals("Beans number: ", 1, nb);
100:
101: }
102:
103: /**
104: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE LOCATE(?1, o.fstring) > 0
105: */
106: public void testLocate() throws Exception {
107: String l = "helene";
108: Collection cBeans = home.findByLocateString(l);
109: Iterator iBeans = cBeans.iterator();
110: int nb = 0;
111: while (iBeans.hasNext()) {
112: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
113: .narrow(iBeans.next(), E4Query.class);
114: String f = bean.getFstring();
115: assertTrue("Id=" + bean.getId(), f.indexOf(l) > -1);
116: nb++;
117: }
118: assertEquals("Beans number: ", 3, nb);
119:
120: }
121:
122: /**
123: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE LOCATE(?1, o.fstring, ?2) > 0
124: * FAIL in postgresql. Spec 2.1 says that this implementation is optional.
125: */
126: public void testLocateAt() throws Exception {
127: String l = "helene";
128: Collection cBeans = home.findByLocateStringAt(l, 2);
129: Iterator iBeans = cBeans.iterator();
130: int nb = 0;
131: while (iBeans.hasNext()) {
132: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
133: .narrow(iBeans.next(), E4Query.class);
134: String f = bean.getFstring();
135: assertTrue("Id=" + bean.getId() + ",f=" + f, f
136: .indexOf(l, 1) > -1);
137: nb++;
138: }
139: assertEquals("Beans number: ", 1, nb);
140:
141: }
142:
143: /**
144: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE SUBSTRING(o.fstring, ?2, ?3) = ?1
145: */
146: public void testSubstring() throws Exception {
147: String s = "el";
148: int is = 2;
149: int il = "el".length();
150: Collection cBeans = home.findBySubstring(s, is, il);
151: Iterator iBeans = cBeans.iterator();
152: int nb = 0;
153: int ib = is - 1;
154: int ie = is - 1 + il;
155: while (iBeans.hasNext()) {
156: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
157: .narrow(iBeans.next(), E4Query.class);
158: String f = bean.getFstring();
159: assertTrue("id=" + bean.getId(), s.equals(f.substring(ib,
160: ie)));
161: nb++;
162: }
163: assertEquals("Beans number: ", 2, nb);
164:
165: }
166:
167: /**
168: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE CONCAT(o.id, o.fstring) = ?1
169: */
170: public void testConcat() throws Exception {
171: String s = "id4" + "eric";
172: Collection cBeans = home.findByConcatString(s);
173: Iterator iBeans = cBeans.iterator();
174: int nb = 0;
175: while (iBeans.hasNext()) {
176: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
177: .narrow(iBeans.next(), E4Query.class);
178: String f = bean.getFstring();
179: assertTrue("Id=" + bean.getId(), s.equals(bean.getId()
180: .concat(bean.getFstring())));
181: nb++;
182: }
183: assertEquals("Beans number: ", 2, nb);
184:
185: }
186:
187: /**
188: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE ABS(o.fint) = ?1
189: */
190: public void testAbsInt() throws Exception {
191: int i = 1959;
192: Collection cBeans = home.findByAbsInt(i);
193: Iterator iBeans = cBeans.iterator();
194: int nb = 0;
195: while (iBeans.hasNext()) {
196: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
197: .narrow(iBeans.next(), E4Query.class);
198: int f = bean.getFint();
199: assertTrue("Id=" + bean.getId(), (f == -i) || (f == i));
200: nb++;
201: }
202: assertEquals("Beans number: ", 2, nb);
203:
204: }
205:
206: /**
207: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE SQRT(o.fdouble) BETWEEN ?1 - 0.1 AND ?1 + 0.1
208: */
209: public void testSqrt() throws Exception {
210: int i = 1959;
211: Collection cBeans = home.findBySqrtDouble(1959.0);
212: Iterator iBeans = cBeans.iterator();
213: int nb = 0;
214: while (iBeans.hasNext()) {
215: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
216: .narrow(iBeans.next(), E4Query.class);
217: int f = bean.getFint();
218: assertTrue("Id=" + bean.getId(), f == i * i);
219: nb++;
220: }
221: assertEquals("Beans number: ", 1, nb);
222:
223: }
224:
225: /**
226: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE o.fstring IS NULL
227: */
228: public void testIsNull() throws Exception {
229: Collection cBeans = home.findByIsNull();
230: Iterator iBeans = cBeans.iterator();
231: int nb = 0;
232: while (iBeans.hasNext()) {
233: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
234: .narrow(iBeans.next(), E4Query.class);
235: assertNull("Id=" + bean.getId(), bean.getFstring());
236: nb++;
237: }
238: assertEquals("Beans number: ", 1, nb);
239:
240: }
241:
242: /**
243: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE ?1 IS NULL
244: */
245: public void testIsNull1Param() throws Exception {
246: Collection cBeans = home.findByIsNullParam(null);
247: Iterator iBeans = cBeans.iterator();
248: int nb = 0;
249: while (iBeans.hasNext()) {
250: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
251: .narrow(iBeans.next(), E4Query.class);
252: nb++;
253: }
254: assertEquals("Beans number: ", 6, nb);
255: }
256:
257: /**
258: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE ?1 IS NULL
259: */
260: public void testIsNull2Param() throws Exception {
261: Collection cBeans = home.findByIsNullParam("hello");
262: Iterator iBeans = cBeans.iterator();
263: int nb = 0;
264: while (iBeans.hasNext()) {
265: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
266: .narrow(iBeans.next(), E4Query.class);
267: nb++;
268: }
269: assertEquals("Beans number: ", 0, nb);
270: }
271:
272: /**
273: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE o.fstring IN ('helene', 'eric')
274: */
275: public void testInStrings() throws Exception {
276: Collection cBeans = home.findByInStrings();
277: Iterator iBeans = cBeans.iterator();
278: int nb = 0;
279: while (iBeans.hasNext()) {
280: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
281: .narrow(iBeans.next(), E4Query.class);
282: boolean ok = "helene".equals(bean.getFstring())
283: || "eric".equals(bean.getFstring());
284: assertTrue("Id=" + bean.getId(), ok);
285: nb++;
286: }
287: assertEquals("Beans number: ", 2, nb);
288:
289: }
290:
291: /**
292: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE ABS(o.fint) = -100
293: */
294: public void testLessThanMinus100() throws Exception {
295: Collection cBeans = home.findByLessThanMinus100();
296: Iterator iBeans = cBeans.iterator();
297: int nb = 0;
298: while (iBeans.hasNext()) {
299: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
300: .narrow(iBeans.next(), E4Query.class);
301: int f = bean.getFint();
302: assertTrue("Id=" + bean.getId(), f < -100);
303: nb++;
304: }
305: assertEquals("Beans number: ", 1, nb);
306:
307: }
308:
309: /**
310: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE o.fint = ?1 + ?2 - ?3
311: */
312: public void testIntEqualExpr() throws Exception {
313: Collection cBeans = home.findByIntEqualExpr(1959, 255, 255);
314: Iterator iBeans = cBeans.iterator();
315: int nb = 0;
316: while (iBeans.hasNext()) {
317: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
318: .narrow(iBeans.next(), E4Query.class);
319: int f = bean.getFint();
320: assertEquals("Id=" + bean.getId(), 1959, f);
321: nb++;
322: }
323: assertEquals("Beans number: ", 1, nb);
324:
325: }
326:
327: /**
328: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE o.fint = ?1 - ?2 - ?3
329: * to reproduce bug 303587
330: * This bug is pending: Remove this test from the list for now.
331: */
332: public void _testIntEqualExpr2() throws Exception {
333: Collection cBeans = home.findByIntEqualExpr2(1962, 2, 1);
334: Iterator iBeans = cBeans.iterator();
335: int nb = 0;
336: while (iBeans.hasNext()) {
337: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
338: .narrow(iBeans.next(), E4Query.class);
339: int f = bean.getFint();
340: assertEquals("Id=" + bean.getId(), 1959, f);
341: nb++;
342: }
343: assertEquals("Beans number: ", 1, nb);
344:
345: }
346:
347: /**
348: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE o.fstring > ?1
349: */
350: public void testStringGreaterThenExpr() throws Exception {
351: Collection cBeans = home.findByStringGreaterThenExpr("helene");
352: Iterator iBeans = cBeans.iterator();
353: int nb = 0;
354: while (iBeans.hasNext()) {
355: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
356: .narrow(iBeans.next(), E4Query.class);
357: String f = bean.getFstring();
358: assertTrue("Id=" + bean.getId() + ",fString=" + f, f
359: .compareTo("helene") > 0);
360: nb++;
361: }
362: assertEquals("Beans number: ", 2, nb);
363: }
364:
365: /**
366: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE o.fstring >= ?1
367: */
368: public void testStringGreaterOrEqualThenExpr() throws Exception {
369: Collection cBeans = home
370: .findByStringGreaterOrEqualThenExpr("helene");
371: Iterator iBeans = cBeans.iterator();
372: int nb = 0;
373: while (iBeans.hasNext()) {
374: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
375: .narrow(iBeans.next(), E4Query.class);
376: String f = bean.getFstring();
377: assertTrue("Id=" + bean.getId() + ",fString=" + f, f
378: .compareTo("helene") >= 0);
379: nb++;
380: }
381: assertEquals("Beans number: ", 3, nb);
382: }
383:
384: /**
385: * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE MOD(o.fint, ?1) = 0
386: */
387: public void testMod() throws Exception {
388: int op2 = 1959;
389: Collection cBeans = home.findByIntModXIsZero(op2);
390: Iterator iBeans = cBeans.iterator();
391: int nb = 0;
392: while (iBeans.hasNext()) {
393: E4Query bean = (E4Query) javax.rmi.PortableRemoteObject
394: .narrow(iBeans.next(), E4Query.class);
395: int f = bean.getFint();
396: //System.out.println("Id="+bean.getId()+",fInt="+f);
397: assertTrue("Id=" + bean.getId() + ",fInt=" + f,
398: (f % op2) == 0);
399: nb++;
400: }
401: assertEquals("Beans number: ", 4, nb);
402: }
403:
404: public static Test suite() {
405: return new TestSuite(F_BasicEjbqlEC2.class);
406: }
407:
408: public static void main(String args[]) {
409: String testtorun = null;
410: // Get args
411: for (int argn = 0; argn < args.length; argn++) {
412: String s_arg = args[argn];
413: Integer i_arg;
414: if (s_arg.equals("-n")) {
415: testtorun = args[++argn];
416: }
417: }
418: if (testtorun == null) {
419: junit.textui.TestRunner.run(suite());
420: } else {
421: junit.textui.TestRunner.run(new F_BasicEjbqlEC2(testtorun));
422: }
423: }
424:
425: }
|