KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > dbtest > test > DbTypesUnitTestCase


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.dbtest.test;
23
24 import java.rmi.*;
25 import java.util.Collection JavaDoc;
26
27 import java.util.Date JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Properties JavaDoc;
31 import javax.ejb.DuplicateKeyException JavaDoc;
32 import javax.ejb.EJBMetaData JavaDoc;
33 import javax.ejb.FinderException JavaDoc;
34 import javax.ejb.Handle JavaDoc;
35
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38
39 import junit.framework.*;
40
41 import org.jboss.test.dbtest.interfaces.AllTypes;
42 import org.jboss.test.dbtest.interfaces.AllTypesHome;
43 import org.jboss.test.dbtest.interfaces.MyObject;
44 import org.jboss.test.dbtest.interfaces.Record;
45 import org.jboss.test.dbtest.interfaces.RecordHome;
46
47 import org.jboss.test.JBossTestCase;
48
49 /**
50  * Test case to try out all db types using cmp against DefaultDS
51  */

52 public class DbTypesUnitTestCase
53        extends JBossTestCase
54 {
55
56    static boolean deployed = false;
57
58    /**
59     * Constructor for the DbTypesUnitTestCase object
60     *
61     * @param name Description of Parameter
62     */

63    public DbTypesUnitTestCase(String JavaDoc name)
64    {
65       super(name);
66    }
67
68
69
70
71    /**
72     * A unit test for JUnit
73     *
74     * @exception Exception Description of Exception
75     */

76    public void testAllTypesBean() throws Exception JavaDoc
77    {
78       int test = 0;
79
80       Context JavaDoc ctx = new InitialContext JavaDoc();
81
82       getLog().debug(++test + "- " + "Looking up the home AllTypes...");
83
84       AllTypesHome allTypesHome;
85
86       try
87       {
88          allTypesHome = (AllTypesHome)ctx.lookup("AllTypes");
89
90          if (allTypesHome == null)
91          {
92             throw new Exception JavaDoc("abort");
93          }
94          getLog().debug("OK");
95
96       }
97       catch (Exception JavaDoc e)
98       {
99          getLog().debug("Could not lookup the context: the beans are probably not deployed");
100          getLog().debug("Check the server trace for details");
101          log.debug("failed", e);
102          throw new Exception JavaDoc();
103       }
104
105       getLog().debug(++test + "- " + "Calling findByPrimaryKey on AllTypesHome with name seb...");
106
107       AllTypes allTypes = null;
108
109       try
110       {
111          allTypes = allTypesHome.findByPrimaryKey("seb");
112       }
113       catch (Exception JavaDoc e)
114       {
115          getLog().debug(e.getMessage());
116       }
117
118       if (allTypes == null)
119       {
120
121          getLog().debug("not found OK");
122          getLog().debug(++test + "- " + "Calling create on AllTypesHome with name seb...");
123          allTypes = allTypesHome.create("seb");
124       }
125
126       if (allTypes != null)
127       {
128          getLog().debug("OK");
129       }
130       else
131       {
132          getLog().debug("Could not find or create the alltypes bean");
133          getLog().debug("Check the server trace for details");
134
135          throw new Exception JavaDoc();
136       }
137
138       getLog().debug("Getting all the fields");
139       getLog().debug(++test + "- " + "boolean " + allTypes.getBoolean() + " OK");
140       getLog().debug(++test + "- " + "byte " + allTypes.getByte() + " OK");
141       getLog().debug(++test + "- " + "short " + allTypes.getShort() + " OK");
142       getLog().debug(++test + "- " + "int " + allTypes.getInt() + " OK");
143       getLog().debug(++test + "- " + "long " + allTypes.getLong() + " OK");
144       getLog().debug(++test + "- " + "float " + allTypes.getFloat() + " OK");
145       getLog().debug(++test + "- " + "double " + allTypes.getDouble() + " OK");
146       getLog().debug("No char test yet, bug in jdk");
147       getLog().debug(++test + "- " + "String " + allTypes.getString() + " OK");
148       getLog().debug(++test + "- " + "Date " + allTypes.getDate() + " OK");
149       getLog().debug(++test + "- " + "Time " + allTypes.getTime() + " OK");
150       getLog().debug(++test + "- " + "Timestamp " + allTypes.getTimestamp() + " OK");
151
152       getLog().debug(++test + "- " + "MyObject ");
153       MyObject obj = allTypes.getObject();
154       getLog().debug("OK");
155
156       getLog().debug(++test + "- " + "Creating Record beans and adding them to the Collection in Alltypes..");
157       RecordHome recordHome = (RecordHome)ctx.lookup("Record");
158
159       Record[] record = new Record[3];
160       for (int i = 0; i < 3; i++)
161       {
162          try
163          {
164             record[i] = recordHome.findByPrimaryKey("bill " + i);
165          }
166          catch (FinderException JavaDoc e)
167          {
168             record[i] = recordHome.create("bill " + i);
169          }
170
171          record[i].setAddress("SanFrancisco, CA 9411" + i);
172          allTypes.addObjectToList(record[i]);
173       }
174       getLog().debug("OK");
175
176       getLog().debug(++test + "- " + "Getting them back..");
177
178       Collection JavaDoc collection = allTypes.getObjectList();
179       boolean ok = true;
180
181       for (int i = 0; i < 3; i++)
182       {
183          ok = ok && collection.contains(record[i]);
184       }
185
186       if (ok)
187       {
188          getLog().debug("OK");
189       }
190       else
191       {
192          getLog().debug("failed");
193          throw new Exception JavaDoc("abort");
194       }
195
196       getLog().debug("All basic tests passed; Now testing min/max values.");
197       getLog().debug("This is just for information, it's okay if some fail.");
198       getLog().debug("Not all DBs have a column type that supports 8-byte numbers.");
199       // NOTE: In order from most likely to fail to least likely to fail
200
// Oracle in particular demonstrates cascading failures and
201
// we don't want to miss that
202

203       // Double
204
try
205       {
206          allTypes.setDouble(Double.MIN_VALUE);
207          double d;
208          if ((d = allTypes.getDouble()) == Double.MIN_VALUE)
209          {
210             getLog().debug(++test + "- Double Min Value OK");
211          }
212          else
213          {
214             getLog().debug(++test + "- Double Min Value Different (" + d + " <> " + Double.MIN_VALUE + ")");
215          }
216       }
217       catch (Exception JavaDoc e)
218       {
219          getLog().debug(++test + "- Double Min Value Failed");
220       }
221       try
222       {
223          allTypes.setDouble(Double.MAX_VALUE);
224          double d;
225          if ((d = allTypes.getDouble()) == Double.MAX_VALUE)
226          {
227             getLog().debug(++test + "- Double Max Value OK");
228          }
229          else
230          {
231             getLog().debug(++test + "- Double Max Value Different (" + d + " <> " + Double.MAX_VALUE + ")");
232          }
233       }
234       catch (Exception JavaDoc e)
235       {
236          getLog().debug(++test + "- Double Max Value Failed");
237       }
238       // Float
239
try
240       {
241          allTypes.setFloat(Float.MIN_VALUE);
242          float f;
243          if ((f = allTypes.getFloat()) == Float.MIN_VALUE)
244          {
245             getLog().debug(++test + "- Float Min Value OK");
246          }
247          else
248          {
249             getLog().debug(++test + "- Float Min Value Different (" + f + " <> " + Float.MIN_VALUE + ")");
250          }
251       }
252       catch (Exception JavaDoc e)
253       {
254          getLog().debug(++test + "- Float Min Value Failed");
255       }
256       try
257       {
258          allTypes.setFloat(Float.MAX_VALUE);
259          float f;
260          if ((f = allTypes.getFloat()) == Float.MAX_VALUE)
261          {
262             getLog().debug(++test + "- Float Max Value OK");
263          }
264          else
265          {
266             getLog().debug(++test + "- Float Max Value Different (" + f + " <> " + Float.MAX_VALUE + ")");
267          }
268       }
269       catch (Exception JavaDoc e)
270       {
271          getLog().debug(++test + "- Float Max Value Failed");
272       }
273
274       // Long
275
try
276       {
277          allTypes.setLong(Long.MIN_VALUE);
278          long l;
279          if ((l = allTypes.getLong()) == Long.MIN_VALUE)
280          {
281             getLog().debug(++test + "- Long Min Value OK");
282          }
283          else
284          {
285             getLog().debug(++test + "- Long Min Value Different (" + l + " <> " + Long.MIN_VALUE + ")");
286          }
287       }
288       catch (Exception JavaDoc e)
289       {
290          getLog().debug(++test + "- Long Min Value Failed");
291       }
292       try
293       {
294          allTypes.setLong(Long.MAX_VALUE);
295          long l;
296          if ((l = allTypes.getLong()) == Long.MAX_VALUE)
297          {
298             getLog().debug(++test + "- Long Max Value OK");
299          }
300          else
301          {
302             getLog().debug(++test + "- Long Max Value Different (" + l + " <> " + Long.MAX_VALUE + ")");
303          }
304       }
305       catch (Exception JavaDoc e)
306       {
307          getLog().debug(++test + "- Long Max Value Failed");
308       }
309       // Short
310
try
311       {
312          allTypes.setShort(Short.MIN_VALUE);
313          short s;
314          if ((s = allTypes.getShort()) == Short.MIN_VALUE)
315          {
316             getLog().debug(++test + "- Short Min Value OK");
317          }
318          else
319          {
320             getLog().debug(++test + "- Short Min Value Different (" + s + " <> " + Short.MIN_VALUE + ")");
321          }
322       }
323       catch (Exception JavaDoc e)
324       {
325          getLog().debug(++test + "- Short Min Value Failed");
326       }
327       try
328       {
329          allTypes.setShort(Short.MAX_VALUE);
330          short s;
331          if ((s = allTypes.getShort()) == Short.MAX_VALUE)
332          {
333             getLog().debug(++test + "- Short Max Value OK");
334          }
335          else
336          {
337             getLog().debug(++test + "- Short Max Value Different (" + s + " <> " + Short.MAX_VALUE + ")");
338          }
339       }
340       catch (Exception JavaDoc e)
341       {
342          getLog().debug(++test + "- Short Max Value Failed");
343       }
344       // Byte
345
try
346       {
347          allTypes.setByte(Byte.MIN_VALUE);
348          byte b;
349          if ((b = allTypes.getByte()) == Byte.MIN_VALUE)
350          {
351             getLog().debug(++test + "- Byte Min Value OK");
352          }
353          else
354          {
355             getLog().debug(++test + "- Byte Min Value Different (" + b + " <> " + Byte.MIN_VALUE + ")");
356          }
357       }
358       catch (Exception JavaDoc e)
359       {
360          getLog().debug(++test + "- Byte Min Value Failed");
361       }
362       try
363       {
364          allTypes.setByte(Byte.MAX_VALUE);
365          byte b;
366          if ((b = allTypes.getByte()) == Byte.MAX_VALUE)
367          {
368             getLog().debug(++test + "- Byte Max Value OK");
369          }
370          else
371          {
372             getLog().debug(++test + "- Byte Max Value Different (" + b + " <> " + Byte.MAX_VALUE + ")");
373          }
374       }
375       catch (Exception JavaDoc e)
376       {
377          getLog().debug(++test + "- Byte Max Value Failed");
378       }
379       // Int
380
try
381       {
382          allTypes.setInt(Integer.MIN_VALUE);
383          int i;
384          if ((i = allTypes.getInt()) == Integer.MIN_VALUE)
385          {
386             getLog().debug(++test + "- Int Min Value OK");
387          }
388          else
389          {
390             getLog().debug(++test + "- Int Min Value Different (" + i + " <> " + Integer.MIN_VALUE + ")");
391          }
392       }
393       catch (Exception JavaDoc e)
394       {
395          getLog().debug(++test + "- Int Min Value Failed");
396       }
397       try
398       {
399          allTypes.setInt(Integer.MAX_VALUE);
400          int i;
401          if ((i = allTypes.getInt()) == Integer.MAX_VALUE)
402          {
403             getLog().debug(++test + "- Int Max Value OK");
404          }
405          else
406          {
407             getLog().debug(++test + "- Int Max Value Different (" + i + " <> " + Integer.MAX_VALUE + ")");
408          }
409       }
410       catch (Exception JavaDoc e)
411       {
412          getLog().debug(++test + "- Int Max Value Failed");
413       }
414    }
415
416
417    public static Test suite() throws Exception JavaDoc
418    {
419       return getDeploySetup(DbTypesUnitTestCase.class, "dbtest.jar");
420    }
421
422 }
423
Popular Tags