1 2 22 23 package org.apache.derbyTesting.functionTests.tests.jdbc4; 24 25 import junit.framework.*; 26 27 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 28 29 import java.sql.*; 30 import java.io.*; 31 import java.lang.reflect.*; 32 import java.util.*; 33 34 46 class ExemptClobMD { 47 private String methodName_; 49 50 private Class [] params_; 52 53 private boolean isClientFramework_; 56 private boolean isEmbeddedFramework_; 57 58 73 public ExemptClobMD(String methodName,Class [] params, 74 boolean isClientFramework, 75 boolean isEmbeddedFramework) { 76 methodName_ = methodName; 77 params_ = params; 78 isClientFramework_ = isClientFramework; 79 isEmbeddedFramework_ = isEmbeddedFramework; 80 } 81 82 89 public String getMethodName() { return methodName_; } 90 91 98 public Class [] getParams() { return params_; } 99 100 105 public boolean getIfClientFramework() { return isClientFramework_; } 106 107 112 public boolean getIfEmbeddedFramework() { return isEmbeddedFramework_; } 113 } 114 115 118 public class ClobTest 119 extends BaseJDBCTestCase { 120 121 122 private Clob clob = null; 123 124 128 private ExemptClobMD [] emd = new ExemptClobMD [] { 129 new ExemptClobMD( "getCharacterStream", new Class [] { long.class, long.class } ,true,true), 130 new ExemptClobMD( "setAsciiStream", new Class [] { long.class } ,false,true), 131 new ExemptClobMD( "setCharacterStream", new Class [] { long.class } ,true,true), 132 new ExemptClobMD( "setString", new Class [] { long.class, String .class } ,false,true), 133 new ExemptClobMD( "setString", new Class [] { long.class, String .class, int.class, int.class},false,true), 134 new ExemptClobMD( "truncate", new Class [] { long.class },false,true), 135 new ExemptClobMD( "free", null,true,true) 136 }; 137 138 142 private HashMap<Method,ExemptClobMD> excludedMethodSet = 143 new HashMap<Method,ExemptClobMD>(); 144 145 150 public ClobTest(String name) { 151 super(name); 152 } 153 154 public void setUp() 155 throws SQLException { 156 clob = BlobClobTestSetup.getSampleClob(getConnection()); 157 158 buildHashSet(); 163 } 164 165 169 void buildHashSet() { 170 Class iface = Clob.class; 171 for(int i=0;i<emd.length;i++) { 172 try { 173 Method m = iface.getMethod(emd[i].getMethodName() 174 ,emd[i].getParams()); 175 excludedMethodSet.put(m,emd[i]); 176 } 177 catch(NoSuchMethodException nsme) { 178 fail("The method could not be found in the interface"); 179 } 180 } 181 } 182 183 191 public void testFreeandMethodsAfterCallingFree() 192 throws SQLException { 193 InputStream asciiStream = clob.getAsciiStream(); 194 Reader charStream = clob.getCharacterStream(); 195 clob.free(); 196 clob.free(); 201 202 buildMethodList(clob); 207 } 208 209 215 void buildMethodList(Object LOB) { 216 boolean valid = true; 219 220 Vector<Method> methodList = new Vector<Method>(); 222 223 Class clazz = Clob.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 ExemptClobMD 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 public void testGetCharacterStreamLongNotImplemented() 378 throws SQLException { 379 try { 380 clob.getCharacterStream(5l, 10l); 381 fail("Clob.getCharacterStream(long,long)" + 382 "should not be implemented"); 383 } catch (SQLFeatureNotSupportedException sfnse) { 384 } 386 } 387 388 391 public static Test suite() { 392 return new BlobClobTestSetup(new TestSuite(ClobTest.class, 393 "ClobTest suite")); 394 } 395 396 } | Popular Tags |