001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.derbyTesting.functionTests.tests.jdbc4;
021:
022: import java.sql.Connection;
023: import java.sql.PreparedStatement;
024: import java.sql.SQLException;
025: import java.sql.SQLFeatureNotSupportedException;
026: import java.sql.Types;
027: import junit.framework.Test;
028: import junit.framework.TestSuite;
029:
030: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
031:
032: /**
033: * Tests that calling <code>setObject()</code> and <code>setNull()</code> with
034: * <code>sqlTargetType</code> set to an unsupported type fails with
035: * <code>SQLFeatureNotSupportedException</code>.
036: *
037: * <p> The test is run as part of <code>PreparedStatementTest</code>
038: * and <code>CallableStatementTest</code>.
039: */
040: public class SetObjectUnsupportedTest extends BaseJDBCTestCase {
041: /** Name and id of the target type used in the test. */
042: private final TypeInfo typeInfo;
043: /** Flag indicating whether the test should use a
044: * CallableStatement instead of a PreparedStatement. */
045: private final boolean callable;
046:
047: /**
048: * Creates a new <code>SetObjectUnsupportedTest</code> instance.
049: *
050: * @param name name of the test
051: * @param typeInfo description of the target type to use in the test
052: * @param callable if <code>true</code>, use a
053: * <code>CallableStatement</code> instead of a
054: * <code>PreparedStatement</code>.
055: */
056: private SetObjectUnsupportedTest(String name, TypeInfo typeInfo,
057: boolean callable) {
058: super (name);
059: this .typeInfo = typeInfo;
060: this .callable = callable;
061: }
062:
063: /**
064: * Returns the name of the test.
065: */
066: public String getName() {
067: return super .getName() + "_" + typeInfo.name;
068: }
069:
070: /**
071: * Prepares a <code>PreparedStatement</code> or a
072: * <code>CallableStatement</code> to use in the test.
073: *
074: * @return a statement (prepared or callable)
075: * @exception SQLException if a database error occurs
076: */
077: private PreparedStatement prepare() throws SQLException {
078: String sql = "values (CAST (? AS VARCHAR(128)))";
079: return callable ? prepareCall(sql) : prepareStatement(sql);
080: }
081:
082: /**
083: * Test that <code>setObject()</code> with the specified
084: * <code>sqlTargetType</code> throws
085: * <code>SQLFeatureNotSupportedException</code>.
086: *
087: * @exception SQLException if a database error occurs
088: */
089: public void testUnsupportedSetObject() throws SQLException {
090: PreparedStatement ps = prepare();
091: try {
092: ps.setObject(1, null, typeInfo.type);
093: fail("No exception thrown.");
094: } catch (SQLFeatureNotSupportedException e) {
095: // expected exception
096: }
097: ps.close();
098: }
099:
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 {
108: PreparedStatement ps = prepare();
109: try {
110: ps.setObject(1, null, typeInfo.type, 0);
111: fail("No exception thrown.");
112: } catch (SQLFeatureNotSupportedException 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 {
126: PreparedStatement ps = prepare();
127: try {
128: ps.setNull(1, typeInfo.type);
129: fail("No exception thrown.");
130: } catch (SQLFeatureNotSupportedException 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()
144: throws SQLException {
145: PreparedStatement ps = prepare();
146: try {
147: ps.setNull(1, typeInfo.type, typeInfo.name);
148: fail("No exception thrown.");
149: } catch (SQLFeatureNotSupportedException e) {
150: // expected exception
151: }
152: ps.close();
153: }
154:
155: /**
156: * The target types to test.
157: */
158: private static final TypeInfo[] TYPES = {
159: new TypeInfo("ARRAY", Types.ARRAY),
160: new TypeInfo("DATALINK", Types.DATALINK),
161: new TypeInfo("NCHAR", Types.NCHAR),
162: new TypeInfo("NCLOB", Types.NCLOB),
163: new TypeInfo("NVARCHAR", Types.NVARCHAR),
164: new TypeInfo("LONGNVARCHAR", Types.LONGNVARCHAR),
165: new TypeInfo("REF", Types.REF),
166: new TypeInfo("ROWID", Types.ROWID),
167: new TypeInfo("SQLXML", Types.SQLXML),
168: new TypeInfo("STRUCT", Types.STRUCT), };
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", typeInfo,
186: callable));
187: suite.addTest(new SetObjectUnsupportedTest(
188: "testUnsupportedSetNull", typeInfo, callable));
189: suite.addTest(new SetObjectUnsupportedTest(
190: "testUnsupportedSetNullWithTypeName", typeInfo,
191: 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 name;
199: final int type;
200:
201: TypeInfo(String name, int type) {
202: this.name = name;
203: this.type = type;
204: }
205: }
206: }
|