File: Changes10_10.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (514 lines) | stat: -rw-r--r-- 18,110 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*

Derby - Class org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_10

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.upgradeTests;

import java.io.File;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.PrivilegedFileOpsForTests;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.SupportFilesSetup;
import org.apache.derbyTesting.junit.TestConfiguration;


/**
 * Upgrade test cases for 10.10.
 */
public class Changes10_10 extends UpgradeChange
{
    ///////////////////////////////////////////////////////////////////////////////////
    //
    // CONSTANTS
    //
    ///////////////////////////////////////////////////////////////////////////////////

    private static  final   String  SYNTAX_ERROR = "42X01";
    private static  final   String  HARD_UPGRADE_REQUIRED = "XCL47";
    private static  final   String  NEEDS_JAVA_STYLE = "42ZCA";
    /**
    The readme file cautioning users against touching the files in
    the database directory 
    */
    private static final String DB_README_FILE_NAME = "README_DO_NOT_TOUCH_FILES.txt";

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

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // CONSTRUCTOR
    //
    ///////////////////////////////////////////////////////////////////////////////////

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

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // JUnit BEHAVIOR
    //
    ///////////////////////////////////////////////////////////////////////////////////
    
    /**
     * Return the suite of tests to test the changes made in 10.10.
     * @param phase an integer that indicates the current phase in
     *              the upgrade test.
     * @return the test suite created.
     */
    public static Test suite(int phase) {
        BaseTestSuite suite = new BaseTestSuite("Upgrade test for 10.9");

        suite.addTestSuite(Changes10_10.class);
        
        return new SupportFilesSetup((Test) suite);
    }

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // TESTS
    //
    ///////////////////////////////////////////////////////////////////////////////////

    /**
     * Make sure that the following procedure(s) which are new to 10.10 are 
     *  only available after hard-upgrade
     *  1)invalidate stored statements 
     *    SYCS_UTIL.SYSCS_INVALIDATE_STORED_STATEMENTS
     */
    public  void    testProcsNewTo10_10()  throws Exception
    {
        Statement s = createStatement();
        String  iss = "call syscs_util.SYSCS_INVALIDATE_STORED_STATEMENTS()";
        String  rt = "call syscs_util.syscs_register_tool( 'foo', true )";
        String  rtGoodSQLState = "X0Y88";
        String  syntaxError = "42X01";
        boolean atLeastJava5 = JDBC.vmSupportsJDBC3();
        boolean oldSupportsBoolean = oldAtLeast( 10, 7 );

        switch ( getPhase() )
        {
        case PH_CREATE: // create with old version
        case PH_SOFT_UPGRADE: // boot with new version and soft-upgrade
        case PH_POST_SOFT_UPGRADE: // soft-downgrade: boot with old version after soft-upgrade
            vetProcs( s, iss, false, null );
            if ( atLeastJava5 )
            {
                vetProcs
                    ( s, rt, false,
                      oldSupportsBoolean || (getPhase() == PH_SOFT_UPGRADE ) ? null : syntaxError );
            }
            break;
            
        case PH_HARD_UPGRADE: // boot with new version and hard-upgrade
            vetProcs( s, iss, true, null );
            if ( atLeastJava5 ) { vetProcs( s, rt, true, rtGoodSQLState ); }
            break;
        }
        
        s.close();
    	
    }
    
    private void    vetProcs
        (
         Statement s,
         String procCall, 
         boolean shouldExist,
         String sqlState
         ) throws Exception
    {
        try {
            s.execute( procCall );
            
            if ( !shouldExist )
            {
                fail( "Procedure should not exist!"  );
            }
            if ( sqlState != null )
            {
                fail( "Expected to fail with SQLState " + sqlState );
            }
        } catch (SQLException se )
        {
            if ( sqlState == null ) { sqlState = "42Y03"; }
            assertSQLState( sqlState, se );
        }
    }

    /**
     * Verify upgrade behavior for user-defined aggregates.
     */
    public  void    testUDAs()  throws Exception
    {
        Statement st = createStatement();

        String  createUDA = "create derby aggregate mode for int external name 'foo.bar.Wibble'";
        String  dropUDA = "drop derby aggregate mode restrict";

        switch ( getPhase() )
        {
        case PH_CREATE: // create with old version
        case PH_POST_SOFT_UPGRADE: // soft-downgrade: boot with old version after soft-upgrade
            assertStatementError( SYNTAX_ERROR, st, createUDA );
            assertStatementError( SYNTAX_ERROR, st, dropUDA );
            break;
            
        case PH_SOFT_UPGRADE: // boot with new version and soft-upgrade
            assertStatementError( HARD_UPGRADE_REQUIRED, st, createUDA );
            assertStatementError( HARD_UPGRADE_REQUIRED, st, dropUDA );
            break;
            
        case PH_HARD_UPGRADE: // boot with new version and hard-upgrade
            st.execute( createUDA );
            st.execute( dropUDA );
            break;
        }
        
        st.close();
    }
    
    /**
     * Verify upgrade behavior for vararg routines.
     */
    public  void    testVarargss()  throws Exception
    {
        Statement st = createStatement();

        String  createVarargsProc = "create procedure vds ( a int ... ) language java parameter style derby no sql external name 'Foo.foo'";
        String  createVarargsFunc = "create function vds ( a int ... ) returns integer language java parameter style derby no sql external name 'Foo.foo'";
        String  createVarargsTableFunc = "create function vtf ( a int ... ) returns table ( b int ) language java parameter style derby_jdbc_result_set no sql external name 'Foo.foo'";
        String  createNonVarargsProcDerbyStyle = "create procedure nvds ( a int ) language java parameter style derby no sql external name 'Foo.foo'";
        String  createNonVarargsFuncDerbyStyle = "create function nvds ( a int ) returns integer language java parameter style derby no sql external name 'Foo.foo'";

        // table functions were introduced by 10.4
        boolean tableFunctionsOK = oldAtLeast( 10, 4 );       

        switch ( getPhase() )
        {
        case PH_CREATE: // create with old version
        case PH_POST_SOFT_UPGRADE: // soft-downgrade: boot with old version after soft-upgrade
            assertStatementError( SYNTAX_ERROR, st, createVarargsProc );
            assertStatementError( SYNTAX_ERROR, st, createVarargsFunc );
            if ( tableFunctionsOK ) { assertStatementError( SYNTAX_ERROR, st, createVarargsTableFunc ); }
            assertStatementError( SYNTAX_ERROR, st, createNonVarargsProcDerbyStyle );
            assertStatementError( SYNTAX_ERROR, st, createNonVarargsFuncDerbyStyle );
            break;
            
        case PH_SOFT_UPGRADE: // boot with new version and soft-upgrade
            assertStatementError( HARD_UPGRADE_REQUIRED, st, createVarargsProc );
            assertStatementError( HARD_UPGRADE_REQUIRED, st, createVarargsFunc );
            if ( tableFunctionsOK ) { assertStatementError( HARD_UPGRADE_REQUIRED, st, createVarargsTableFunc ); }
            assertStatementError( HARD_UPGRADE_REQUIRED, st, createNonVarargsProcDerbyStyle );
            assertStatementError( HARD_UPGRADE_REQUIRED, st, createNonVarargsFuncDerbyStyle );
            break;
            
        case PH_HARD_UPGRADE: // boot with new version and hard-upgrade
            st.execute( createVarargsProc );
            st.execute( createVarargsFunc );
            st.execute( createVarargsTableFunc );
            assertStatementError( NEEDS_JAVA_STYLE, st, createNonVarargsProcDerbyStyle );
            assertStatementError( NEEDS_JAVA_STYLE, st, createNonVarargsFuncDerbyStyle );
            break;
        }
        
        st.close();
    }
    
    /**
     * DERBY-5996(Create readme files (cautioning users against modifying 
     *  database files) at database hard upgrade time)
     * Simple test to make sure readme files are getting created
     */
    public void testReadMeFiles() throws SQLException, IOException
    {
        Statement s = createStatement();
        s.close();
        TestConfiguration currentConfig = TestConfiguration.getCurrent();
        String dbPath = currentConfig.getDatabasePath(currentConfig.getDefaultDatabaseName());
        switch (getPhase())
        {
        case PH_CREATE:
        case PH_SOFT_UPGRADE:
        case PH_POST_SOFT_UPGRADE:
            // DERBY-5995 Pre 10.10 databases would not have readme files
            lookForReadmeFile(dbPath, false);
            lookForReadmeFile(dbPath+File.separator+"seg0", false);
            lookForReadmeFile(dbPath+File.separator+"log", false);
            break;
        case PH_HARD_UPGRADE:
        case PH_POST_HARD_UPGRADE:
            // DERBY-5995 Hard upgrade to 10.10 will create readme files
            lookForReadmeFile(dbPath, true);
            lookForReadmeFile(dbPath+File.separator+"seg0", true);
            lookForReadmeFile(dbPath+File.separator+"log", true);
            break;
        }
    }

    /**
     * For pre-10.10 database, fileShouldExist will be false. For hard upgraded
     *  databases to 10.10, fileShouldExist will be true
     * @param path - this can be root database directory, log or seg0 directory
     * @param fileShouldExist
     * @throws IOException
     */
    private void lookForReadmeFile(String path, boolean fileShouldExist) throws IOException {
        File readmeFile = new File(path,
            DB_README_FILE_NAME);
        if (fileShouldExist)
        {
            assertTrue(readmeFile + "doesn't exist", 
                PrivilegedFileOpsForTests.exists(readmeFile));
        } else 
        {
            assertFalse(readmeFile + "exists", 
                PrivilegedFileOpsForTests.exists(readmeFile));
        
        }
    }

    // Old DB2 constants in Limits.java:
    // static final float DB2_SMALLEST_REAL = -3.402E+38f;
    // static final float DB2_LARGEST_REAL  = +3.402E+38f;
    // static final float DB2_SMALLEST_POSITIVE_REAL = +1.175E-37f;
    // static final float DB2_LARGEST_NEGATIVE_REAL  = -1.175E-37f;

    // static final double DB2_SMALLEST_DOUBLE = -1.79769E+308d;
    // static final double DB2_LARGEST_DOUBLE  = +1.79769E+308d;
    // static final double DB2_SMALLEST_POSITIVE_DOUBLE = +2.225E-307d;
    // static final double DB2_LARGEST_NEGATIVE_DOUBLE  = -2.225E-307d;

    static final float[] beyondDB2Real = new float[] {
        Float.MIN_VALUE,
        Float.MAX_VALUE,
        +1.174E-37f,
        -1.174E-37f,
        1.17549435E-38f, // Float.MIN_NORMAL
        -1.17549435E-38f // -Float.MIN_NORMAL
    };

    static final double[] beyondDB2Double = new double[] {
        Double.MIN_VALUE,
        Double.MAX_VALUE,
        +2.224E-307d,
        -2.224E-307d,
        2.2250738585072014E-308, // Double.MIN_NORMAL
        -2.2250738585072014E-308 // -Double.MIN_NORMAL
    };

    /**
     * Verify upgrade behavior DERBY-3398: removing DB2 float limits
     */
    public  void    testFloatLimits()  throws Exception
    {
        Statement st = createStatement();
        st.execute("create table d3398(r real, d double)");

        PreparedStatement psInsertReal =
            prepareStatement("insert into d3398(r) values (?)");
        PreparedStatement psInsertDouble =
            prepareStatement("insert into d3398(d) values (?)");
        PreparedStatement psSelect =
            prepareStatement("select * from d3398",
                ResultSet.TYPE_FORWARD_ONLY,
                ResultSet.CONCUR_UPDATABLE);

        st.execute("insert into d3398 values (0.0, 0.0)");


        switch ( getPhase() )
        {
        case PH_CREATE: // create with old version
            verifyDB2Behavior(psSelect, psInsertReal, psInsertDouble, false);

            break;
        case PH_POST_SOFT_UPGRADE: // soft-downgrade: boot with old
                                   // version after soft-upgrade
            verifyDB2Behavior(psSelect, psInsertReal, psInsertDouble, false);

            break;
        case PH_SOFT_UPGRADE: // boot with new version and soft-upgrade

            verifyDB2Behavior(psSelect, psInsertReal, psInsertDouble, true);

            break;
        case PH_HARD_UPGRADE: // boot with new version and hard-upgrade

            for (int i = 0; i < beyondDB2Real.length; i++) {
                psInsertReal.setFloat(1, beyondDB2Real[i]);
                psInsertReal.execute();

                ResultSet rs = psSelect.executeQuery();
                rs.next();
                rs.updateFloat(1, beyondDB2Real[i]);
                rs.updateRow();
                rs.close();
            }

            for (int i = 0; i < beyondDB2Double.length; i++) {
                psInsertDouble.setDouble(1, beyondDB2Double[i]);
                psInsertDouble.execute();

                ResultSet rs = psSelect.executeQuery();
                rs.next();
                rs.updateDouble(2, beyondDB2Double[i]);
                rs.updateRow();
                rs.close();
            }

            break;
        }

        st.executeUpdate("drop table d3398");
        st.close();
    }

    private void assertSetError(PreparedStatement ps, float fv, boolean defer)
            throws SQLException {
        try {
            ps.setFloat(1, fv);

            if (!defer) {
                fail();
            }

            ps.executeUpdate();
            fail();
        } catch (SQLException e) {
            assertSQLState("22003", e);
        }
    }
    
    private void assertSetError(PreparedStatement ps, double dv, boolean defer)
            throws SQLException {
        try {
            ps.setDouble(1, dv);

            if (!defer) {
                fail();
            }

            ps.executeUpdate();
            fail();
        } catch (SQLException e) {
            assertSQLState("22003", e);
        }
    }


    private void assertUpdateError(
            PreparedStatement ps,
            float fv,
            boolean defer) throws SQLException {

        boolean supportsForwardUpdatableResultSet = oldAtLeast( 10, 2 );

        if (!supportsForwardUpdatableResultSet) {
            return;
        }

        ResultSet rs = ps.executeQuery();
        rs.next();

        try {
            rs.updateFloat(1, fv);

            if (!defer) {
                fail();
            }

            rs.updateRow();
            fail();
        } catch (SQLException e) {
            assertSQLState("22003", e);
        } finally {
            rs.close();
        }
    }

    private void assertUpdateError(
            PreparedStatement ps,
            double fv,
            boolean defer) throws SQLException {

        boolean supportsForwardUpdatableResultSet = oldAtLeast( 10, 2 );

        if (!supportsForwardUpdatableResultSet) {
            return;
        }

        ResultSet rs = ps.executeQuery();
        rs.next();

        try {
            rs.updateDouble(1, fv);

            if (!defer) {
                fail();
            }

            rs.updateRow();
            fail();
        } catch (SQLException e) {
            assertSQLState("22003", e);
        } finally {
            rs.close();
        }
    }

    /**
     * Check that the old DB2 limits are (still) enforced.
     *
     * @param defer In soft upgrade mode, the checking if deferred after
     * DERBY-3398, i.e. instead of throwing on the DB2 limits when calling
     * {@code ResultSet#updateXXX} or {@code PreparedStatement#setXXX}, the
     * check throws on {@code ResultSet#updateRow}, or {#insertRow}, and
     * similarly on {@code PreparedStatement#execute} or {@code #executeUpdate}.
     *
     * @throws SQLException if we see some expected error.
     */
    private void verifyDB2Behavior (
            PreparedStatement psSelect,
            PreparedStatement psInsertReal,
            PreparedStatement psInsertDouble,
            boolean defer) throws SQLException {

        for (int i = 0; i < beyondDB2Real.length; i++) {
            assertSetError(psInsertReal, beyondDB2Real[i], defer);
            assertUpdateError(psSelect, beyondDB2Real[i], defer);
        }

        for (int i = 0; i < beyondDB2Double.length; i++) {
            assertSetError(psInsertDouble, beyondDB2Double[i], defer);
            assertUpdateError(psSelect, beyondDB2Double[i], defer);
        }
    }
}