KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

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

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

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

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

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

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

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

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

191     public void testFreeandMethodsAfterCallingFree()
192         throws SQLException {
193             InputStream asciiStream = clob.getAsciiStream();
194             Reader charStream = clob.getCharacterStream();
195             clob.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
clob.free();
201             
202             //clob 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(clob);
207     }
208     
209     /*
210      *
211      * Enumerate the methods of the Clob interface and
212      * get the list of methods present in the interface
213      * @param LOB an instance of the Clob 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 = Clob.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         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     /*
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     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             // 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(ClobTest.class,
393                                                    "ClobTest suite"));
394     }
395
396 } // End class ClobTest
397
Popular Tags