1 22 package org.jboss.test.dbtest.test; 23 24 import java.rmi.*; 25 import java.util.Collection ; 26 27 import java.util.Date ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import java.util.Properties ; 31 import javax.ejb.DuplicateKeyException ; 32 import javax.ejb.EJBMetaData ; 33 import javax.ejb.FinderException ; 34 import javax.ejb.Handle ; 35 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 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 52 public class DbTypesUnitTestCase 53 extends JBossTestCase 54 { 55 56 static boolean deployed = false; 57 58 63 public DbTypesUnitTestCase(String name) 64 { 65 super(name); 66 } 67 68 69 70 71 76 public void testAllTypesBean() throws Exception 77 { 78 int test = 0; 79 80 Context ctx = new InitialContext (); 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 ("abort"); 93 } 94 getLog().debug("OK"); 95 96 } 97 catch (Exception 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 (); 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 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 (); 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 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 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 ("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 203 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 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 e) 235 { 236 getLog().debug(++test + "- Double Max Value Failed"); 237 } 238 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 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 e) 270 { 271 getLog().debug(++test + "- Float Max Value Failed"); 272 } 273 274 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 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 e) 306 { 307 getLog().debug(++test + "- Long Max Value Failed"); 308 } 309 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 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 e) 341 { 342 getLog().debug(++test + "- Short Max Value Failed"); 343 } 344 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 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 e) 376 { 377 getLog().debug(++test + "- Byte Max Value Failed"); 378 } 379 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 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 e) 411 { 412 getLog().debug(++test + "- Int Max Value Failed"); 413 } 414 } 415 416 417 public static Test suite() throws Exception 418 { 419 return getDeploySetup(DbTypesUnitTestCase.class, "dbtest.jar"); 420 } 421 422 } 423 | Popular Tags |