KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > entity > F_BasicEjbqlEC2


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: F_BasicEjbqlEC2.java,v 1.12 2004/09/23 10:02:17 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import javax.naming.NamingException JavaDoc;
32 import javax.rmi.PortableRemoteObject JavaDoc;
33
34 import junit.framework.Test;
35 import junit.framework.TestSuite;
36
37 import org.objectweb.jonas.jtests.beans.ebasic.E4Query;
38 import org.objectweb.jonas.jtests.beans.ebasic.E4QueryHome;
39 import org.objectweb.jonas.jtests.util.JTestCase;
40
41
42 /**
43  * For testing basic EJB QL queries.
44  * @author Helene Joanin
45  */

46
47 public class F_BasicEjbqlEC2 extends JTestCase {
48
49     private static String JavaDoc BEAN_HOME_E4QUERY = "ebasicE4QueryEC2Home";
50     protected static E4QueryHome home = null;
51
52
53     public F_BasicEjbqlEC2(String JavaDoc name) {
54         super(name);
55     }
56
57     protected void setUp() {
58         if (home == null) {
59             // load bean if not loaded yet
60
useBeans("ebasic", true);
61             try {
62                 home = (E4QueryHome) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_E4QUERY),
63                                                                  E4QueryHome.class);
64             } catch (NamingException JavaDoc e) {
65                 fail("Cannot get bean home: " + e.getMessage());
66             }
67             // check if tables have been initialized
68
try {
69                 home.findByPrimaryKey("id1");
70             } catch (Exception JavaDoc e) {
71                 try {
72                     home.create("idnull", null, 0, 0.0);
73                     home.create("id1","helene", 1959, 1959.0);
74                     home.create("id2","ahelene", -1959, 1959.0);
75                     home.create("id3","helene-bis", 1959*1959, 1959.0*1959.0);
76                     home.create("id4","eric", 1957, 1957.0);
77                     home.create("id4e","ric", 1957, 1957.0);
78                 } catch (Exception JavaDoc i) {
79                     fail("InitialState creation problem:: "+i);
80                 }
81             }
82         }
83     }
84
85     /**
86      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE LENGTH(o.fstring) = ?1
87      */

88     public void testLength() throws Exception JavaDoc {
89         int l = "helene".length();
90         Collection JavaDoc cBeans = home.findByLengthString(l);
91         Iterator JavaDoc iBeans = cBeans.iterator();
92         int nb=0;
93         while(iBeans.hasNext()) {
94             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
95                                                                            E4Query.class);
96             assertEquals("Id="+bean.getId(), l, bean.getFstring().length());
97             nb++;
98         }
99         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 JavaDoc {
107         String JavaDoc l = "helene";
108         Collection JavaDoc cBeans = home.findByLocateString(l);
109         Iterator JavaDoc iBeans = cBeans.iterator();
110         int nb=0;
111         while(iBeans.hasNext()) {
112             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
113                                                                            E4Query.class);
114             String JavaDoc 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 JavaDoc {
127         String JavaDoc l = "helene";
128         Collection JavaDoc cBeans = home.findByLocateStringAt(l, 2);
129         Iterator JavaDoc iBeans = cBeans.iterator();
130         int nb=0;
131         while(iBeans.hasNext()) {
132             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
133                                                                            E4Query.class);
134             String JavaDoc f = bean.getFstring();
135             assertTrue("Id=" + bean.getId() + ",f=" + f, f.indexOf(l, 1)>-1);
136             nb++;
137         }
138         assertEquals("Beans number: ", 1, nb);
139     
140     }
141
142     /**
143      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE SUBSTRING(o.fstring, ?2, ?3) = ?1
144      */

145     public void testSubstring() throws Exception JavaDoc {
146         String JavaDoc s = "el";
147         int is = 2;
148         int il = "el".length();
149         Collection JavaDoc cBeans = home.findBySubstring(s, is, il);
150         Iterator JavaDoc iBeans = cBeans.iterator();
151         int nb=0;
152         int ib = is - 1;
153         int ie = is - 1 + il;
154         while(iBeans.hasNext()) {
155             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
156                                                                            E4Query.class);
157             String JavaDoc f = bean.getFstring();
158             assertTrue("id="+bean.getId(), s.equals(f.substring(ib, ie)));
159             nb++;
160         }
161         assertEquals("Beans number: ", 2, nb);
162     
163     }
164
165     /**
166      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE CONCAT(o.id, o.fstring) = ?1
167      */

168     public void testConcat() throws Exception JavaDoc {
169         String JavaDoc s = "id4"+"eric";
170         Collection JavaDoc cBeans = home.findByConcatString(s);
171         Iterator JavaDoc iBeans = cBeans.iterator();
172         int nb=0;
173         while(iBeans.hasNext()) {
174             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
175                                                                            E4Query.class);
176             String JavaDoc f = bean.getFstring();
177             assertTrue("Id="+bean.getId(), s.equals(bean.getId().concat(bean.getFstring())));
178             nb++;
179         }
180         assertEquals("Beans number: ", 2, nb);
181     
182     }
183
184     /**
185      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE ABS(o.fint) = ?1
186      */

187     public void testAbsInt() throws Exception JavaDoc {
188         int i = 1959;
189         Collection JavaDoc cBeans = home.findByAbsInt(i);
190         Iterator JavaDoc iBeans = cBeans.iterator();
191         int nb=0;
192         while(iBeans.hasNext()) {
193             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
194                                                                            E4Query.class);
195             int f = bean.getFint();
196             assertTrue("Id="+bean.getId(), (f==-i) || (f==i));
197             nb++;
198         }
199         assertEquals("Beans number: ", 2, nb);
200     
201     }
202
203     /**
204      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE SQRT(o.fdouble) BETWEEN ?1 - 0.1 AND ?1 + 0.1
205      */

206     public void testSqrt() throws Exception JavaDoc {
207         int i = 1959;
208         Collection JavaDoc cBeans = home.findBySqrtDouble(1959.0);
209         Iterator JavaDoc iBeans = cBeans.iterator();
210         int nb=0;
211         while(iBeans.hasNext()) {
212             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
213                                                                            E4Query.class);
214             int f = bean.getFint();
215             assertTrue("Id="+bean.getId(), f==i*i);
216             nb++;
217         }
218         assertEquals("Beans number: ", 1, nb);
219     
220     }
221
222     /**
223      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE o.fstring IS NULL
224      */

225     public void testIsNull() throws Exception JavaDoc {
226         Collection JavaDoc cBeans = home.findByIsNull();
227         Iterator JavaDoc iBeans = cBeans.iterator();
228         int nb=0;
229         while(iBeans.hasNext()) {
230             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
231                                                                            E4Query.class);
232             assertNull("Id="+bean.getId(), bean.getFstring());
233             nb++;
234         }
235         assertEquals("Beans number: ", 1, nb);
236     
237     }
238
239     /**
240      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE ?1 IS NULL
241      */

242     public void testIsNull1Param() throws Exception JavaDoc {
243         Collection JavaDoc cBeans = home.findByIsNullParam(null);
244         Iterator JavaDoc iBeans = cBeans.iterator();
245         int nb=0;
246         while(iBeans.hasNext()) {
247             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
248                                                                            E4Query.class);
249             nb++;
250         }
251         assertEquals("Beans number: ", 6, nb);
252     }
253
254     /**
255      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE ?1 IS NULL
256      */

257     public void testIsNull2Param() throws Exception JavaDoc {
258         Collection JavaDoc cBeans = home.findByIsNullParam("hello");
259         Iterator JavaDoc iBeans = cBeans.iterator();
260         int nb=0;
261         while(iBeans.hasNext()) {
262             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
263                                                                            E4Query.class);
264             nb++;
265         }
266         assertEquals("Beans number: ", 0, nb);
267     }
268
269     /**
270      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE o.fstring IN ('helene', 'eric')
271      */

272     public void testInStrings() throws Exception JavaDoc {
273         Collection JavaDoc cBeans = home.findByInStrings();
274         Iterator JavaDoc iBeans = cBeans.iterator();
275         int nb=0;
276         while(iBeans.hasNext()) {
277             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
278                                                                            E4Query.class);
279             boolean ok = "helene".equals(bean.getFstring()) || "eric".equals(bean.getFstring());
280             assertTrue("Id="+bean.getId(), ok);
281             nb++;
282         }
283         assertEquals("Beans number: ", 2, nb);
284     
285     }
286
287     /**
288      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE ABS(o.fint) = -100
289      */

290     public void testLessThanMinus100() throws Exception JavaDoc {
291         Collection JavaDoc cBeans = home.findByLessThanMinus100();
292         Iterator JavaDoc iBeans = cBeans.iterator();
293         int nb=0;
294         while(iBeans.hasNext()) {
295             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
296                                                                            E4Query.class);
297             int f = bean.getFint();
298             assertTrue("Id="+bean.getId(), f < -100);
299             nb++;
300         }
301         assertEquals("Beans number: ", 1, nb);
302     
303     }
304
305     /**
306      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE o.fint = ?1 + ?2 - ?3
307      */

308     public void testIntEqualExpr() throws Exception JavaDoc {
309         Collection JavaDoc cBeans = home.findByIntEqualExpr(1959, 255, 255);
310         Iterator JavaDoc iBeans = cBeans.iterator();
311         int nb=0;
312         while(iBeans.hasNext()) {
313             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
314                                                                            E4Query.class);
315             int f = bean.getFint();
316             assertEquals("Id="+bean.getId(), 1959, f);
317             nb++;
318         }
319         assertEquals("Beans number: ", 1, nb);
320     
321     }
322
323     /**
324      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE o.fstring > ?1
325      */

326     public void testStringGreaterThenExpr() throws Exception JavaDoc {
327         Collection JavaDoc cBeans = home.findByStringGreaterThenExpr("helene");
328         Iterator JavaDoc iBeans = cBeans.iterator();
329         int nb=0;
330         while(iBeans.hasNext()) {
331             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
332                                                                            E4Query.class);
333             String JavaDoc f = bean.getFstring();
334             assertTrue("Id="+bean.getId()+",fString="+f, f.compareTo("helene") > 0);
335             nb++;
336         }
337         assertEquals("Beans number: ", 2, nb);
338     }
339
340     /**
341      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE o.fstring >= ?1
342      */

343     public void testStringGreaterOrEqualThenExpr() throws Exception JavaDoc {
344         Collection JavaDoc cBeans = home.findByStringGreaterOrEqualThenExpr("helene");
345         Iterator JavaDoc iBeans = cBeans.iterator();
346         int nb=0;
347         while(iBeans.hasNext()) {
348             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
349                                                                            E4Query.class);
350             String JavaDoc f = bean.getFstring();
351             assertTrue("Id="+bean.getId()+",fString="+f, f.compareTo("helene") >= 0);
352             nb++;
353         }
354         assertEquals("Beans number: ", 3, nb);
355     }
356
357     /**
358      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_e4query o WHERE MOD(o.fint, ?1) = 0
359      */

360     public void testMod() throws Exception JavaDoc {
361         int op2 = 1959;
362         Collection JavaDoc cBeans = home.findByIntModXIsZero(op2);
363         Iterator JavaDoc iBeans = cBeans.iterator();
364         int nb=0;
365         while(iBeans.hasNext()) {
366             E4Query bean = (E4Query) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
367                                                                            E4Query.class);
368             int f = bean.getFint();
369             //System.out.println("Id="+bean.getId()+",fInt="+f);
370
assertTrue("Id="+bean.getId()+",fInt="+f, (f % op2) == 0);
371             nb++;
372         }
373         assertEquals("Beans number: ", 4, nb);
374     }
375
376
377     public static Test suite() {
378         return new TestSuite(F_BasicEjbqlEC2.class);
379     }
380
381     public static void main(String JavaDoc args[]) {
382         String JavaDoc testtorun = null;
383         // Get args
384
for (int argn = 0; argn < args.length; argn++) {
385             String JavaDoc s_arg = args[argn];
386             Integer JavaDoc i_arg;
387             if (s_arg.equals("-n")) {
388                 testtorun = args[++argn];
389             }
390         }
391         if (testtorun == null) {
392             junit.textui.TestRunner.run(suite());
393         } else {
394             junit.textui.TestRunner.run(new F_BasicEjbqlEC2(testtorun));
395         }
396     }
397
398 }
399
Popular Tags