KickJava   Java API By Example, From Geeks To Geeks.

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


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

19
20 package org.apache.derbyTesting.functionTests.tests.jdbc4;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.SQLFeatureNotSupportedException JavaDoc;
26 import java.sql.Types JavaDoc;
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
31
32 /**
33  * Tests that calling <code>setObject()</code> and <code>setNull()</code> with
34  * <code>sqlTargetType</code> set to an unsupported type fails with
35  * <code>SQLFeatureNotSupportedException</code>.
36  *
37  * <p> The test is run as part of <code>PreparedStatementTest</code>
38  * and <code>CallableStatementTest</code>.
39  */

40 public class SetObjectUnsupportedTest extends BaseJDBCTestCase {
41     /** Name and id of the target type used in the test. */
42     private final TypeInfo typeInfo;
43     /** Flag indicating whether the test should use a
44      * CallableStatement instead of a PreparedStatement. */

45     private final boolean callable;
46
47     /**
48      * Creates a new <code>SetObjectUnsupportedTest</code> instance.
49      *
50      * @param name name of the test
51      * @param typeInfo description of the target type to use in the test
52      * @param callable if <code>true</code>, use a
53      * <code>CallableStatement</code> instead of a
54      * <code>PreparedStatement</code>.
55      */

56     private SetObjectUnsupportedTest(String JavaDoc name, TypeInfo typeInfo,
57                                      boolean callable) {
58         super(name);
59         this.typeInfo = typeInfo;
60         this.callable = callable;
61     }
62
63     /**
64      * Returns the name of the test.
65      */

66     public String JavaDoc getName() {
67         return super.getName() + "_" + typeInfo.name;
68     }
69
70     /**
71      * Prepares a <code>PreparedStatement</code> or a
72      * <code>CallableStatement</code> to use in the test.
73      *
74      * @return a statement (prepared or callable)
75      * @exception SQLException if a database error occurs
76      */

77     private PreparedStatement JavaDoc prepare() throws SQLException JavaDoc {
78         String JavaDoc sql = "values (CAST (? AS VARCHAR(128)))";
79         return callable ? prepareCall(sql) : prepareStatement(sql);
80     }
81
82     /**
83      * Test that <code>setObject()</code> with the specified
84      * <code>sqlTargetType</code> throws
85      * <code>SQLFeatureNotSupportedException</code>.
86      *
87      * @exception SQLException if a database error occurs
88      */

89     public void testUnsupportedSetObject() throws SQLException JavaDoc {
90         PreparedStatement JavaDoc ps = prepare();
91         try {
92             ps.setObject(1, null, typeInfo.type);
93             fail("No exception thrown.");
94         } catch (SQLFeatureNotSupportedException JavaDoc e) {
95             // expected exception
96
}
97         ps.close();
98     }
99
100     /**
101      * Test that <code>setObject()</code> with the specified
102      * <code>sqlTargetType</code> throws
103      * <code>SQLFeatureNotSupportedException</code>.
104      *
105      * @exception SQLException if a database error occurs
106      */

107     public void testUnsupportedSetObjectWithScale() throws SQLException JavaDoc {
108         PreparedStatement JavaDoc ps = prepare();
109         try {
110             ps.setObject(1, null, typeInfo.type, 0);
111             fail("No exception thrown.");
112         } catch (SQLFeatureNotSupportedException JavaDoc e) {
113             // expected exception
114
}
115         ps.close();
116     }
117
118     /**
119      * Test that <code>setNull()</code> with the specified
120      * <code>sqlTargetType</code> throws
121      * <code>SQLFeatureNotSupportedException</code>.
122      *
123      * @exception SQLException if a database error occurs
124      */

125     public void testUnsupportedSetNull() throws SQLException JavaDoc {
126         PreparedStatement JavaDoc ps = prepare();
127         try {
128             ps.setNull(1, typeInfo.type);
129             fail("No exception thrown.");
130         } catch (SQLFeatureNotSupportedException JavaDoc e) {
131             // expected exception
132
}
133         ps.close();
134     }
135
136     /**
137      * Test that <code>setNull()</code> with the specified
138      * <code>sqlTargetType</code> throws
139      * <code>SQLFeatureNotSupportedException</code>.
140      *
141      * @exception SQLException if a database error occurs
142      */

143     public void testUnsupportedSetNullWithTypeName() throws SQLException JavaDoc {
144         PreparedStatement JavaDoc ps = prepare();
145         try {
146             ps.setNull(1, typeInfo.type, typeInfo.name);
147             fail("No exception thrown.");
148         } catch (SQLFeatureNotSupportedException JavaDoc e) {
149             // expected exception
150
}
151         ps.close();
152     }
153
154     /**
155      * The target types to test.
156      */

157     private static final TypeInfo[] TYPES = {
158         new TypeInfo("ARRAY", Types.ARRAY),
159         new TypeInfo("DATALINK", Types.DATALINK),
160         new TypeInfo("NCHAR", Types.NCHAR),
161         new TypeInfo("NCLOB", Types.NCLOB),
162         new TypeInfo("NVARCHAR", Types.NVARCHAR),
163         new TypeInfo("LONGNVARCHAR", Types.LONGNVARCHAR),
164         new TypeInfo("REF", Types.REF),
165         new TypeInfo("ROWID", Types.ROWID),
166         new TypeInfo("SQLXML", Types.SQLXML),
167         new TypeInfo("STRUCT", Types.STRUCT),
168     };
169
170     /**
171      * Build a test suite which tests <code>setObject()</code> with
172      * each of the types in <code>TYPES</code>.
173      *
174      * @param callable if <code>true</code>, test with a
175      * <code>CallableStatement</code>; otherwise, test with a
176      * <code>PreparedStatement</code>
177      * @return a test suite
178      */

179     static Test suite(boolean callable) {
180         TestSuite suite = new TestSuite();
181         for (TypeInfo typeInfo : TYPES) {
182             suite.addTest(new SetObjectUnsupportedTest
183                           ("testUnsupportedSetObject", typeInfo, callable));
184             suite.addTest(new SetObjectUnsupportedTest
185                           ("testUnsupportedSetObjectWithScale",
186                            typeInfo, callable));
187             suite.addTest(new SetObjectUnsupportedTest
188                           ("testUnsupportedSetNull", typeInfo, callable));
189             suite.addTest(new SetObjectUnsupportedTest
190                           ("testUnsupportedSetNullWithTypeName",
191                            typeInfo, callable));
192         }
193         return suite;
194     }
195
196     /** Class with name and id for the target type used in a test. */
197     private static class TypeInfo {
198         final String JavaDoc name;
199         final int type;
200         TypeInfo(String JavaDoc name, int type) {
201             this.name = name;
202             this.type = type;
203         }
204     }
205 }
206
Popular Tags