File: AbstractCompatibilityTest.java

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (444 lines) | stat: -rw-r--r-- 13,429 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*

   Derby - Class org.apache.derbyTesting.functionTests.tests.compatibility.AbstractCompatibilityTest

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to you under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */
package org.apache.derbyTesting.functionTests.tests.compatibility;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.DerbyVersion;
import org.apache.derbyTesting.junit.Version;

/**
 * Abstract test case with common functionality often required when writing
 * JDBC client driver compatibility tests.
 */
abstract class AbstractCompatibilityTest
        extends BaseJDBCTestCase
{
    /////////////////////////////////////////////////////////////
    //
    //    CONSTANTS
    //
    /////////////////////////////////////////////////////////////

    public static final String SERVER_VERSION_FUNCTION = "getVMVersion";

    private static final String VERSION_PROPERTY = "java.version";

    /////////////////////////////////////////////////////////////
    //
    //    STATE
    //
    /////////////////////////////////////////////////////////////

    private static Version _clientVMLevel;        // level of client-side vm
    private static Version _serverVMLevel;        // level of server vm
    private static DerbyVersion _driverLevel;     // client rev level

    public AbstractCompatibilityTest(String name) {
        super(name);
    }

    /////////////////////////////////////////////////////////////
    //
    //    PUBLIC BEHAVIOR
    //
    /////////////////////////////////////////////////////////////

    public DerbyVersion getServerVersion() throws SQLException {
        return getServerVersion(getConnection());
    }

    /**
     * <p>
     * Get the version of the server.
     * </p>
     */
    protected static DerbyVersion getServerVersion(Connection con)
            throws SQLException {
        return DerbyVersion.parseVersionString(
                con.getMetaData().getDatabaseProductVersion());
    }

    /**
     * <p>
     * Get the version of the client.
     * </p>
     */
    public DerbyVersion getDriverVersion()
            throws SQLException {
        if (_driverLevel == null) {
            _driverLevel = DerbyVersion.parseVersionString(
                    getConnection().getMetaData().getDriverVersion());
        }
        return _driverLevel;
    }

    /**
     * <p>
     * Get the vm level of the server.
     * </p>
     */
    public    static    Version    getServerVMVersion()    { return _serverVMLevel; }

    /**
     * <p>
     * Get the vm level of the client.
     * </p>
     */
    public    Version    getClientVMVersion() { return _clientVMLevel; }

    /**
     * <p>
     *  Report whether the server supports ANSI UDTs.
     * </p>
     */
    public boolean serverSupportsUDTs()
            throws SQLException {
        return serverSupportsUDTs(getConnection());
    }

    public static boolean serverSupportsUDTs(Connection con)
            throws SQLException {
        return getServerVersion(con).atLeast( DerbyVersion._10_6 );
    }

    /////////////////////////////////////////////////////////////
    //
    //    DATABASE-SIDE FUNCTIONS
    //
    /////////////////////////////////////////////////////////////

    /**
     * <p>
     * Get the vm level of the server.
     * </p>
     */
    public    static    String    getVMVersion()
    {
        return System.getProperty( VERSION_PROPERTY );
    }

    // POTENTIALLY TEMPORARY METHODS - CLEANUP LATER

    /**
     * <p>
     * Assert two objects are equal, allowing nulls to be equal.
     * </p>
     */
    public    void    compareObjects( String message, Object left, Object right )
            throws SQLException
    {
        message = message + "\n\t expected = " + left + "\n\t actual = " + right;

        if ( left == null )
        {
            assertNull( message, right );
        }
        else
        {
            assertNotNull( message, right );

            if ( left instanceof byte[] ) { compareBytes( message, left, right ); }
            else if ( left instanceof java.util.Date ) { compareDates( message, left, right ); }
            else { assertTrue( message, left.equals( right ) ); }
        }
    }

    /**
     * <p>
     * Assert two byte arrays are equal, allowing nulls to be equal.
     * </p>
     */
    public    void    compareBytes( String message, Object left, Object right )
    {
        if ( left == null )    { assertNull( message, right ); }
        else { assertNotNull( right ); }

        if ( !(left instanceof byte[] ) ) { fail( message ); }
        if ( !(right instanceof byte[] ) ) { fail( message ); }

        byte[]    leftBytes = (byte[]) left;
        byte[]    rightBytes = (byte[]) right;
        int        count = leftBytes.length;

        assertEquals( message, count, rightBytes.length );

        for ( int i = 0; i < count; i++ )
        {
            assertEquals( message + "[ " + i + " ]", leftBytes[ i ], rightBytes[ i ] );
        }
    }

    /**
     * <p>
     * Assert two Dates are equal, allowing nulls to be equal.
     * </p>
     */
    public    void    compareDates( String message, Object left, Object right )
    {
        if ( left == null )    { assertNull( message, right ); }
        else { assertNotNull( right ); }

        if ( !(left instanceof java.util.Date ) ) { fail( message ); }
        if ( !(right instanceof java.util.Date ) ) { fail( message ); }

        assertEquals( message, left.toString(), right.toString() );
    }

    /**
     * <p>
     * Read a column from a ResultSet given its column name and expected jdbc
     * type. This method is useful if you are want to verify the getXXX() logic
     * most naturally fitting the declared SQL type.
     * </p>
     */
    private static final int JDBC_BOOLEAN = 16;
    protected    Object    getColumn( ResultSet rs, String columnName, int jdbcType )
        throws SQLException
    {
        Object        retval = null;

        switch( jdbcType )
        {
            case JDBC_BOOLEAN:
                retval = Boolean.valueOf(rs.getBoolean(columnName));
                break;

            case Types.BIGINT:
                retval = rs.getLong( columnName );
                break;

            case Types.BLOB:
                retval = rs.getBlob( columnName );
                break;

            case Types.CHAR:
            case Types.LONGVARCHAR:
            case Types.VARCHAR:
                retval = rs.getString( columnName );
                break;

            case Types.BINARY:
            case Types.LONGVARBINARY:
            case Types.VARBINARY:
                retval = rs.getBytes( columnName );
                break;

            case Types.CLOB:
                retval = rs.getClob( columnName );
                break;

            case Types.DATE:
                retval = rs.getDate( columnName );
                break;

            case Types.DECIMAL:
            case Types.NUMERIC:
                retval = rs.getBigDecimal( columnName );
                break;

            case Types.DOUBLE:
                retval = rs.getDouble( columnName );
                break;

            case Types.REAL:
                retval = rs.getFloat( columnName );
                break;

            case Types.INTEGER:
                retval = rs.getInt( columnName );
                break;

            case Types.SMALLINT:
                retval = rs.getShort( columnName );
                break;

            case Types.TIME:
                retval = rs.getTime( columnName );
                break;

            case Types.TIMESTAMP:
                retval = rs.getTimestamp( columnName );
                break;

            default:
                fail( "Unknown jdbc type " + jdbcType + " used to retrieve column: " + columnName );
                break;
        }

        if ( rs.wasNull() ) { retval = null; }

        return retval;
    }

    /**
     * <p>
     * Stuff a PreparedStatement parameter given its 1-based parameter position
     * and expected jdbc type. This method is useful for testing the setXXX()
     * methods most natural for a declared SQL type.
     * </p>
     */
    protected    void    setParameter( PreparedStatement ps, int param, int jdbcType, Object value )
        throws SQLException
    {
        if ( value == null )
        {
            ps.setNull( param, jdbcType );

            return;
        }

        switch( jdbcType )
        {
            case JDBC_BOOLEAN:
                ps.setBoolean( param, ((Boolean) value ).booleanValue() );
                break;

            case Types.BIGINT:
                ps.setLong( param, ((Long) value ).longValue() );
                break;

            case Types.BLOB:
                ps.setBlob( param, ((java.sql.Blob) value ) );
                break;

            case Types.CHAR:
            case Types.LONGVARCHAR:
            case Types.VARCHAR:
                ps.setString( param, ((String) value ) );
                break;

            case Types.BINARY:
            case Types.LONGVARBINARY:
            case Types.VARBINARY:
                ps.setBytes( param, (byte[]) value );
                break;

            case Types.CLOB:
                ps.setClob( param, ((java.sql.Clob) value ) );
                break;

            case Types.DATE:
                ps.setDate( param, ((java.sql.Date) value ) );
                break;

            case Types.DECIMAL:
            case Types.NUMERIC:
                ps.setBigDecimal( param, ((java.math.BigDecimal) value ) );
                break;

            case Types.DOUBLE:
                ps.setDouble( param, ((Double) value ).doubleValue() );
                break;

            case Types.REAL:
                ps.setFloat( param, ((Float) value ).floatValue() );
                break;

            case Types.INTEGER:
                ps.setInt( param, ((Integer) value ).intValue() );
                break;

            case Types.SMALLINT:
                ps.setShort( param, ((Short) value ).shortValue() );
                break;

            case Types.TIME:
                ps.setTime( param, (java.sql.Time) value );
                break;

            case Types.TIMESTAMP:
                ps.setTimestamp( param, (java.sql.Timestamp) value );
                break;

            default:
                fail( "Unknown jdbc type: " + jdbcType );
                break;
        }

    }
    /**
     * <p>
     * Drop a function regardless of whether it exists. If the function does not
     * exist, don't log an error unless
     * running in debug mode. This method is to be used for reinitializing
     * a schema in case a previous test run failed to clean up after itself.
     * Do not use this method if you need to verify that the function really exists.
     * </p>
     */
    protected    void    dropFunction(String name )
    {
        dropSchemaObject(FUNCTION, name, false );
    }

    /**
     * <p>
     * Drop a procedure regardless of whether it exists. If the procedure does
     * not exist, don't log an error unless
     * running in debug mode. This method is to be used for reinitializing
     * a schema in case a previous test run failed to clean up after itself.
     * Do not use this method if you need to verify that the procedure really exists.
     * </p>
     */
    protected    void    dropProcedure(String name )
    {
        dropSchemaObject(PROCEDURE, name, false );
    }

    /**
     * <p>
     * Drop a UDT regardless of whether it exists. If the UDT does
     * not exist, don't log an error unless
     * running in debug mode. This method is to be used for reinitializing
     * a schema in case a previous test run failed to clean up after itself.
     * Do not use this method if you need to verify that the UDT really exists.
     * </p>
     */
    protected    void    dropUDT(String name )
    {
        dropSchemaObject(TYPE, name, true );
    }

    protected    void    dropSchemaObject(String genus, String objectName, boolean restrict )
    {
        try {
            String text = "drop " + genus + " " + objectName;
            if ( restrict ) { text = text + " restrict"; }
            PreparedStatement ps = prepareStatement(text );

            ps.execute();
            ps.close();
        }
        catch (SQLException e)
        {
        }

    }

    private    static    final    String    FUNCTION = "function";
    private    static    final    String    PROCEDURE = "procedure";
    private    static    final    String    TYPE = "type";
}