KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbc4 > BlobTest


1 /*
2  
3    Derby - Class BlobTest
4  
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11  
12       http://www.apache.org/licenses/LICENSE-2.0
13  
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19  
20  */

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 /* This class is used to store the details of the methods that
34  * throw a SQLFeatureNotSupportedException in the implementation
35  * of java.sql.Blob.
36  *
37  * It store the following information about the methods
38  *
39  * a) Name
40  * b) Method Parameters
41  * c) Whether the method is exempted in the Embedded Sever
42  * d) Whether the method is exempted in the NetworkClient
43  *
44  */

45 class ExemptBlobMD {
46     // The Name of the method
47
private String JavaDoc methodName_;
48     
49     // The parameters of the method
50
private Class JavaDoc [] params_;
51     
52     //Whether it is exempted in the
53
//Client or the Embedded framework
54
private boolean isClientFramework_;
55     private boolean isEmbeddedFramework_;
56     
57     /**
58      * The Constructor for the ExemptBlobMD class that
59      * initialized the object with the details of the
60      * methods that have been exempted
61      *
62      * @param methodName A String that contains the name of the method
63      * that has been exempted.
64      * @param params A array of Class that contains the parameters
65      * of the methods.
66      * @param isClientFramework true if the method is exempted in the
67      * Client framework.
68      * @param isEmbeddedFramework true if the method is exempted in the
69      * Embedded framework.
70      *
71      */

72     public ExemptBlobMD(String JavaDoc methodName,Class JavaDoc [] params,
73                             boolean isClientFramework,
74                             boolean isEmbeddedFramework) {
75         methodName_ = methodName;
76         params_ = params;
77         isClientFramework_ = isClientFramework;
78         isEmbeddedFramework_ = isEmbeddedFramework;
79     }
80     
81     /**
82      *
83      * Returns the name of the method.
84      *
85      * @return A String containing the name of the method.
86      *
87      */

88     public String JavaDoc getMethodName() { return methodName_; }
89     
90     /**
91      * Returns a array of Class containing the type of the parameters
92      * of this method.
93      *
94      * @return A array of Class containing the type of the parameters
95      * of the method.
96      */

97     public Class JavaDoc [] getParams() { return params_; }
98     
99     /**
100      * Returns if the method is exempted from the Client Framework.
101      *
102      * @return true if the method is exempted from the Client Framework.
103      */

104     public boolean getIfClientFramework() { return isClientFramework_; }
105     
106      /**
107      * Returns if the method is exempted from the Embedded Framework.
108      *
109      * @return true if the method is exempted from the Embedded Framework.
110      */

111     public boolean getIfEmbeddedFramework() { return isEmbeddedFramework_; }
112 }
113
114 /*
115  * Tests of the JDBC 4.0 specific <code>Blob</code> methods.
116  */

117 public class BlobTest
118     extends BaseJDBCTestCase {
119
120     /** Default Blob object used by the tests. */
121     private Blob blob = null;
122     
123     // Initialize with the details of the method that are exempted from
124
//throwing a SQLException when they are called after calling free()
125
//on a LOB.
126

127     private ExemptBlobMD [] emd = new ExemptBlobMD [] {
128         new ExemptBlobMD( "getBinaryStream", new Class JavaDoc[] { long.class,long.class }
129                                                                    ,true,true ),
130         new ExemptBlobMD( "setBinaryStream", new Class JavaDoc[] { long.class },false,true ),
131         new ExemptBlobMD( "setBytes", new Class JavaDoc[] { long.class, byte[].class }
132                                                                    ,false,true ),
133         new ExemptBlobMD( "setBytes", new Class JavaDoc[] { long.class, byte[].class
134                                            , int.class, int.class },false,true ),
135         new ExemptBlobMD( "truncate", new Class JavaDoc[] { long.class },false,true),
136         new ExemptBlobMD( "free",null,true,true)
137     };
138     
139     // An HashMap that is indexed by the Method which facilitated easy
140
//search for whether the given method has been exempted from the
141
//LOB interface.
142

143     private HashMap<Method,ExemptBlobMD> excludedMethodSet =
144                             new HashMap<Method,ExemptBlobMD>();
145        
146     /**
147      * Create the test with the given name.
148      *
149      * @param name name of the test.
150      */

151     public BlobTest(String JavaDoc name) {
152         super(name);
153     }
154     
155     public void setUp()
156         throws SQLException {
157
158         blob = BlobClobTestSetup.getSampleBlob(getConnection());
159         
160         //call the buildHashSetMethod to initialize the
161
//HashSet with the method signatures that are exempted
162
//from throwing a SQLException after free has been called
163
//on the Clob object.
164
buildHashSet();
165     }
166     
167     /**
168      * Builds the HashSet which will be used to test whether the given methods
169      * can be exempted or not
170      */

171     void buildHashSet() {
172         Class JavaDoc 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 JavaDoc nsme) {
180                 fail("The method could not be found in the interface");
181             }
182         }
183     }
184     
185     /**
186      * Tests the implementation for the free() method in the
187      * Blob interface.
188      *
189      * @throws SQLException if an error occurs during releasing
190      * the Blob resources
191      *
192      */

193     public void testFreeandMethodsAfterCallingFree()
194         throws SQLException {
195         blob.free();
196         //testing the idempotence of the free() method
197
//the method can be called multiple times on
198
//the same instance. subsequent calls after
199
//the first are treated as no-ops
200
blob.free();
201
202         //blob becomes invalid after the first call
203
//to the free method so testing calling
204
//a method on this invalid object should throw
205
//an SQLException
206
buildMethodList(blob);
207     }
208     
209     /*
210      *
211      * Enumerate the methods of the Blob interface and
212      * get the list of methods present in the interface
213      * @param LOB an instance of the Blob interface implementation
214      */

215     void buildMethodList(Object JavaDoc LOB) {
216         //If the given method throws the correct exception
217
//set this to true and add it to the
218
boolean valid = true;
219         
220         //create a list of the methods that fail the test
221
Vector<Method> methodList = new Vector<Method>();
222         
223         //The class whose methods are to be verified
224
Class JavaDoc clazz = Blob.class;
225         
226         //The list of the methods in the class that need to be invoked
227
//and verified
228
Method [] methods = clazz.getMethods();
229         
230         //Check each of the methods to ensure that
231
//they throw the required exception
232
for(int i=0;i<methods.length;i++) {
233             if(!checkIfExempted(methods[i])) {
234                 valid = checkIfMethodThrowsSQLException(LOB,methods[i]);
235                 
236                 //add the method to the list if the method does
237
//not throw the required exception
238
if(valid == false) methodList.add(methods[i]);
239                 
240                 //reset valid
241
valid = true;
242             }
243         }
244         
245         if(!methodList.isEmpty()) {
246             int c=0;
247             String JavaDoc 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     /**
262      *Checks if the method throws a SQLFeatureNotSupportedException
263      *@param m The method object that needs to be verified to see if it
264      * is exempted
265      *@return true if the given method does not throw the required SQLException
266      *
267      */

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     /**
287      * Checks if the invocation of the method throws a SQLExceptio
288      * as expected.
289      * @param LOB the Object that implements the Blob interface
290      * @param method the method that needs to be tested to ensure
291      * that it throws the correct exception
292      * @return true If the method throws the SQLException required
293      * after the free method has been called on the
294      * LOB object
295      *
296      */

297     boolean checkIfMethodThrowsSQLException(Object JavaDoc LOB,Method method) {
298         try {
299             method.invoke(LOB,getNullValues(method.getParameterTypes()));
300         } catch(Throwable JavaDoc e) {
301             if(e instanceof InvocationTargetException) {
302                 Throwable JavaDoc 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     /*
319      * Return a array of objects containing the default values for
320      * the objects passed in as parameters
321      *
322      * @param parameterTypes an array containing the types of the parameter
323      * to the method
324      * @return an array of Objects containing the null values for the
325      * parameter inputs
326      */

327     
328     Object JavaDoc[] getNullValues(Class JavaDoc<?> [] params) {
329         Object JavaDoc[] args = new Object JavaDoc[params.length];
330         for (int i = 0; i < params.length; i++) {
331             args[i] = getNullValueForType(params[i]);
332         }
333         return args;
334     }
335     
336     /*
337      * Returns the null value for the specific type
338      *
339      * @param type the type of the parameter for which the null
340      * value is required
341      * @return the null value for the specific type
342      *
343      */

344      Object JavaDoc getNullValueForType(Class JavaDoc 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 JavaDoc((char) 0);
354         }
355         if (type == Byte.TYPE) {
356             return new Byte JavaDoc((byte) 0);
357         }
358         if (type == Short.TYPE) {
359             return new Short JavaDoc((short) 0);
360         }
361         if (type == Integer.TYPE) {
362             return new Integer JavaDoc(0);
363         }
364         if (type == Long.TYPE) {
365             return new Long JavaDoc(0L);
366         }
367         if (type == Float.TYPE) {
368             return new Float JavaDoc(0f);
369         }
370         if (type == Double.TYPE) {
371             return new Double JavaDoc(0d);
372         }
373         fail("Don't know how to handle type " + type);
374         return null; // unreachable statement
375
}
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             // Do nothing, we are fine
385
}
386     }
387
388     /**
389      * Create test suite for this test.
390      */

391     public static Test suite() {
392         return new BlobClobTestSetup(new TestSuite(BlobTest.class,
393                                                    "BlobTest suite"));
394     }
395
396 } // End class BlobTest
397
Popular Tags