1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbc4; 23 24 import junit.framework.*; 25 26 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 27 28 import java.sql.*; 29 import java.io.*; 30 import java.lang.reflect.*; 31 import java.util.*; 32 33 45 class ExemptBlobMD { 46 private String methodName_; 48 49 private Class [] params_; 51 52 private boolean isClientFramework_; 55 private boolean isEmbeddedFramework_; 56 57 72 public ExemptBlobMD(String methodName,Class [] params, 73 boolean isClientFramework, 74 boolean isEmbeddedFramework) { 75 methodName_ = methodName; 76 params_ = params; 77 isClientFramework_ = isClientFramework; 78 isEmbeddedFramework_ = isEmbeddedFramework; 79 } 80 81 88 public String getMethodName() { return methodName_; } 89 90 97 public Class [] getParams() { return params_; } 98 99 104 public boolean getIfClientFramework() { return isClientFramework_; } 105 106 111 public boolean getIfEmbeddedFramework() { return isEmbeddedFramework_; } 112 } 113 114 117 public class BlobTest 118 extends BaseJDBCTestCase { 119 120 121 private Blob blob = null; 122 123 127 private ExemptBlobMD [] emd = new ExemptBlobMD [] { 128 new ExemptBlobMD( "getBinaryStream", new Class [] { long.class,long.class } 129 ,true,true ), 130 new ExemptBlobMD( "setBinaryStream", new Class [] { long.class },false,true ), 131 new ExemptBlobMD( "setBytes", new Class [] { long.class, byte[].class } 132 ,false,true ), 133 new ExemptBlobMD( "setBytes", new Class [] { long.class, byte[].class 134 , int.class, int.class },false,true ), 135 new ExemptBlobMD( "truncate", new Class [] { long.class },false,true), 136 new ExemptBlobMD( "free",null,true,true) 137 }; 138 139 143 private HashMap<Method,ExemptBlobMD> excludedMethodSet = 144 new HashMap<Method,ExemptBlobMD>(); 145 146 151 public BlobTest(String name) { 152 super(name); 153 } 154 155 public void setUp() 156 throws SQLException { 157 158 blob = BlobClobTestSetup.getSampleBlob(getConnection()); 159 160 buildHashSet(); 165 } 166 167 171 void buildHashSet() { 172 Class iface = Blob.class; 173 for(int i=0;i<emd.length;i++) { 174 try { 175 Method m = iface.getMethod(emd[i].getMethodName() 176 ,emd[i].getParams()); 177 excludedMethodSet.put(m,emd[i]); 178 } 179 catch(NoSuchMethodException nsme) { 180 fail("The method could not be found in the interface"); 181 } 182 } 183 } 184 185 193 public void testFreeandMethodsAfterCallingFree() 194 throws SQLException { 195 blob.free(); 196 blob.free(); 201 202 buildMethodList(blob); 207 } 208 209 215 void buildMethodList(Object LOB) { 216 boolean valid = true; 219 220 Vector<Method> methodList = new Vector<Method>(); 222 223 Class clazz = Blob.class; 225 226 Method [] methods = clazz.getMethods(); 229 230 for(int i=0;i<methods.length;i++) { 233 if(!checkIfExempted(methods[i])) { 234 valid = checkIfMethodThrowsSQLException(LOB,methods[i]); 235 236 if(valid == false) methodList.add(methods[i]); 239 240 valid = true; 242 } 243 } 244 245 if(!methodList.isEmpty()) { 246 int c=0; 247 String failureMessage = "The Following methods don't throw " + 248 "required exception - "; 249 for (Method m : methodList) { 250 c = c + 1; 251 if(c == methodList.size() && c != 1) 252 failureMessage += " & "; 253 else if(c != 1) 254 failureMessage += " , "; 255 failureMessage += m.getName(); 256 } 257 fail(failureMessage); 258 } 259 } 260 261 268 boolean checkIfExempted(Method m) { 269 ExemptBlobMD md = excludedMethodSet.get(m); 270 271 if(md != null && usingDerbyNetClient()) { 272 if(md.getIfClientFramework()) 273 return true; 274 else 275 return false; 276 } 277 if(md != null && usingEmbedded()) { 278 if(md.getIfEmbeddedFramework()) 279 return true; 280 else 281 return false; 282 } 283 return false; 284 } 285 286 297 boolean checkIfMethodThrowsSQLException(Object LOB,Method method) { 298 try { 299 method.invoke(LOB,getNullValues(method.getParameterTypes())); 300 } catch(Throwable e) { 301 if(e instanceof InvocationTargetException) { 302 Throwable cause = e.getCause(); 303 if (cause instanceof SQLException ) { 304 SQLException sqle = (SQLException)cause; 305 if(sqle.getSQLState().equals("XJ215")) 306 return true; 307 else 308 return false; 309 } else { 310 return false; 311 } 312 313 } 314 } 315 return false; 316 } 317 318 327 328 Object [] getNullValues(Class <?> [] params) { 329 Object [] args = new Object [params.length]; 330 for (int i = 0; i < params.length; i++) { 331 args[i] = getNullValueForType(params[i]); 332 } 333 return args; 334 } 335 336 344 Object getNullValueForType(Class type) 345 { 346 if (!type.isPrimitive()) { 347 return null; 348 } 349 if (type == Boolean.TYPE) { 350 return Boolean.FALSE; 351 } 352 if (type == Character.TYPE) { 353 return new Character ((char) 0); 354 } 355 if (type == Byte.TYPE) { 356 return new Byte ((byte) 0); 357 } 358 if (type == Short.TYPE) { 359 return new Short ((short) 0); 360 } 361 if (type == Integer.TYPE) { 362 return new Integer (0); 363 } 364 if (type == Long.TYPE) { 365 return new Long (0L); 366 } 367 if (type == Float.TYPE) { 368 return new Float (0f); 369 } 370 if (type == Double.TYPE) { 371 return new Double (0d); 372 } 373 fail("Don't know how to handle type " + type); 374 return null; } 376 377 378 public void testGetBinaryStringLongNotImplemented() 379 throws SQLException { 380 try { 381 blob.getBinaryStream(5l, 10l); 382 fail("Blob.getBinaryStream(long,long) should not be implemented"); 383 } catch (SQLFeatureNotSupportedException sfnse) { 384 } 386 } 387 388 391 public static Test suite() { 392 return new BlobClobTestSetup(new TestSuite(BlobTest.class, 393 "BlobTest suite")); 394 } 395 396 } | Popular Tags |