KickJava   Java API By Example, From Geeks To Geeks.

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


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: A_EtypeOutildate.java,v 1.7 2003/12/05 14:03:19 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import java.util.Collection JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import javax.ejb.FinderException JavaDoc;
32
33 import org.objectweb.jonas.jtests.beans.etype.outildate.Outildate;
34 import org.objectweb.jonas.jtests.beans.etype.outildate.OutildateHome;
35 import org.objectweb.jonas.jtests.util.JTestCase;
36
37 /**
38  * This set of test are common to CMP version 1 and CMP version 2
39  * These are tests about java.lang.Utildate field of entity bean .
40  * Beans used: etype/outildate
41  * @author Helene Joanin
42  */

43 public abstract class A_EtypeOutildate extends JTestCase {
44
45     private static final long ONE_HOUR = 60L * 60L * 1000L;
46     private static final long ONE_DAY = 24L * ONE_HOUR;
47
48     public A_EtypeOutildate(String JavaDoc name) {
49         super(name);
50     }
51
52     protected void setUp() {
53         super.setUp();
54         useBeans("outildate", false);
55     }
56
57     /**
58      * Return OutildateHome, that can be either CMP version 1 or CMP version 2 bean.
59      */

60     abstract public OutildateHome getHome();
61
62     /**
63      * findByPrimaryKey() test
64      */

65     public void testUtilDateFindByPk() throws Exception JavaDoc {
66         String JavaDoc pk = "pk1";
67         Outildate bean = getHome().findByPrimaryKey(pk);
68         assertEquals("Pk", pk, bean.getPk());
69     }
70
71     /**
72      * findByF1() test
73      */

74     public void testUtilDateFindByF1() throws Exception JavaDoc {
75         Date JavaDoc d5 = new Date JavaDoc(5 * ONE_DAY);
76         Collection JavaDoc cBeans = getHome().findByF1(d5);
77         int nb = 0;
78         Iterator JavaDoc iBeans = cBeans.iterator();
79         while (iBeans.hasNext()) {
80             Outildate bean = (Outildate) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
81                                                                                Outildate.class);
82             Date JavaDoc f1 = bean.getF1();
83             // This assert may fail because of TIME ZONE problem ??
84
// F1 expected:<Tue Jan 06 01:00:00 CET 1970> but was:<1970-01-06>
85
// assertEquals("F1", d5, f1);
86
if ((f1.getTime() - d5.getTime() > 2 * ONE_HOUR)
87                 || (f1.getTime() - d5.getTime() < -2 * ONE_HOUR)) {
88                 // The two dates differs than more 2 HOURS -> error
89
assertEquals("F1", d5, f1);
90             }
91             nb++;
92         }
93         assertEquals("Beans number: ", 2, nb);
94     }
95
96     /**
97      * getF1() test
98      */

99     public void testUtilDateGetF1() throws Exception JavaDoc {
100         String JavaDoc pk = "pk2";
101         Outildate bean = getHome().findByPrimaryKey(pk);
102         Date JavaDoc d2 = new Date JavaDoc(2 * ONE_DAY);
103         Date JavaDoc f1 = bean.getF1();
104         assertEquals("Pk", pk, bean.getPk());
105         // The following assert may fail because of TIME ZONE problem ??
106
// F1 expected:<Sat Jan 03 01:00:00 CET 1970> but was:<1970-01-03>
107
// assertEquals("F1", new Date(2*ONE_DAY), f1);
108
if ((f1.getTime() - d2.getTime() > 2 * ONE_HOUR)
109             || (f1.getTime() - d2.getTime() < -2 * ONE_HOUR)) {
110             // The two dates differs than more 2 HOURS -> error
111
assertEquals("F1", d2, f1);
112         }
113     }
114
115     /**
116      * getF1Null() test: f1 with a 'null' value
117      */

118     public void testUtilDateGetF1Null() throws Exception JavaDoc {
119         String JavaDoc pk = "pknull";
120         Outildate bean = getHome().findByPrimaryKey(pk);
121         Date JavaDoc f1 = bean.getF1();
122         assertEquals("Pk", pk, bean.getPk());
123         assertNull("F1 not null", f1);
124     }
125
126     /**
127      * setF1() test
128      */

129     public void testUtilDateSetF1() throws Exception JavaDoc {
130         String JavaDoc pk = "pk3";
131         Outildate bean = getHome().findByPrimaryKey(pk);
132         Date JavaDoc f1 = new Date JavaDoc(30 * ONE_DAY);
133         bean.setF1(f1);
134         assertEquals("Pk", pk, bean.getPk());
135         assertEquals("F1", f1, bean.getF1());
136         //cleaning
137
bean.setF1(new Date JavaDoc(3 * ONE_DAY));
138     }
139
140     /**
141      * setF1Null() test: f1 with a 'null' value
142      */

143     public void testUtilDateSetF1Null() throws Exception JavaDoc {
144         String JavaDoc pk = "pkchangenull";
145         Outildate bean = getHome().findByPrimaryKey(pk);
146         bean.setF1(null);
147         assertEquals("Pk", pk, bean.getPk());
148         assertNull("F1", bean.getF1());
149     }
150
151     /**
152      * create() test
153      */

154     public void testUtilDateCreate() throws Exception JavaDoc {
155         String JavaDoc pk = "pkcreated";
156         Date JavaDoc f1 = new Date JavaDoc(1959 * ONE_DAY);
157         Outildate bean = getHome().create(pk, f1);
158         assertEquals("Pk", pk, bean.getPk());
159         assertEquals("F1", f1, bean.getF1());
160         bean = getHome().findByPrimaryKey(pk);
161         // cleaning
162
bean.remove();
163     }
164
165     /**
166      * remove() test
167      */

168     public void testUtilDateRemove() throws Exception JavaDoc {
169         String JavaDoc pk = "pktoremove";
170         Outildate bean = getHome().findByPrimaryKey(pk);
171         Date JavaDoc f1 = bean.getF1();
172         bean.remove();
173         try {
174             getHome().findByPrimaryKey(pk);
175             fail("not removed");
176         } catch (FinderException JavaDoc e) {
177             // ok
178
}
179         // cleaning
180
getHome().create(pk, f1);
181     }
182     /**
183      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_outildate o WHERE o.f1 < ?1
184      */

185     public void testUtilDateLessThen() throws Exception JavaDoc {
186         Date JavaDoc d3 = new Date JavaDoc(3*ONE_DAY);
187         Collection JavaDoc cBeans = getHome().findByF1LessThen(d3);
188         Iterator JavaDoc iBeans = cBeans.iterator();
189         int nb=0;
190         while(iBeans.hasNext()) {
191             Outildate bean = (Outildate) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
192                                                                                Outildate.class);
193             Date JavaDoc f = bean.getF1();
194             assertTrue("Pk="+bean.getPk()+",f1="+f, f.compareTo(d3) < 0);
195             nb++;
196         }
197         assertEquals("Beans number: ", 2, nb);
198     }
199
200     /**
201      * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_outildate o WHERE o.f1 <= ?1
202      */

203     public void testUtilDateLessOrEqualThen() throws Exception JavaDoc {
204         Date JavaDoc d3 = new Date JavaDoc(3*ONE_DAY);
205         Collection JavaDoc cBeans = getHome().findByF1LessOrEqualThen(d3);
206         Iterator JavaDoc iBeans = cBeans.iterator();
207         int nb=0;
208         while(iBeans.hasNext()) {
209             Outildate bean = (Outildate) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
210                                                                                Outildate.class);
211             Date JavaDoc f = bean.getF1();
212             assertTrue("Pk="+bean.getPk()+",f1="+f, f.compareTo(d3) <= 0);
213             nb++;
214         }
215         assertEquals("Beans number: ", 3, nb);
216     }
217
218
219 }
220
Popular Tags