File: Changes10_7.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 (728 lines) | stat: -rw-r--r-- 36,192 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*

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

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.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.SupportFilesSetup;


/**
 * Upgrade test cases for 10.7.
 * If the old version is 10.7 or later then these tests
 * will not be run.
 * <BR>
    10.7 Upgrade issues

    <UL>
    <LI>BOOLEAN data type support expanded.</LI>
    </UL>

 */
public class Changes10_7 extends UpgradeChange
{
    ///////////////////////////////////////////////////////////////////////////////////
    //
    // CONSTANTS
    //
    ///////////////////////////////////////////////////////////////////////////////////

    private static final String SYNTAX_ERROR = "42X01";
    private static final String  UPGRADE_REQUIRED = "XCL47";
    private static final String  GRANT_REVOKE_WITH_LEGACY_ACCESS = "42Z60";

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

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

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

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // JUnit BEHAVIOR
    //
    ///////////////////////////////////////////////////////////////////////////////////

    /**
     * Return the suite of tests to test the changes made in 10.7.
     * @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.7");

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

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

    /**
     * Make sure that that database is at level 10.7 in order to enjoy
     * extended support for the BOOLEAN datatype.
     */
    public void testBoolean() throws SQLException
    {
        String booleanValuedFunction =
            "create function f_4655( a varchar( 100 ) ) returns boolean\n" +
            "language java parameter style java no sql deterministic\n" +
            "external name 'Z.getBooleanValue'\n";

        Statement s = createStatement();

        switch ( getPhase() )
        {
        case PH_CREATE: // create with old version
        case PH_POST_SOFT_UPGRADE: // soft-downgrade: boot with old version after soft-upgrade
            assertFalse(getSupportedTypes().contains("BOOLEAN"));
            assertStatementError(  SYNTAX_ERROR, s, booleanValuedFunction );
            break;

        case PH_SOFT_UPGRADE: // boot with new version and soft-upgrade
            assertFalse(getSupportedTypes().contains("BOOLEAN"));
            assertStatementError( UPGRADE_REQUIRED, s, booleanValuedFunction );
            break;
            
        case PH_HARD_UPGRADE: // boot with new version and hard-upgrade
            assertTrue(getSupportedTypes().contains("BOOLEAN"));
            s.execute( booleanValuedFunction );
            break;
        }
        
        s.close();
    }

    /**
     * Get the names of all supported types, as reported by
     * {@code DatabaseMetaData.getTypeInfo()}.
     *
     * @return a set with the names of all supported types in the loaded
     * version of Derby
     */
    private Set<String> getSupportedTypes() throws SQLException {
        HashSet<String> types = new HashSet<String>();
        ResultSet rs = getConnection().getMetaData().getTypeInfo();
        while (rs.next()) {
            types.add(rs.getString("TYPE_NAME"));
        }
        rs.close();
        return types;
    }
    
    /**
     * This test creates 2 kinds of triggers in old release for each of the
     * three phase of upgrade. The triggers are of following 2 types
     * 1)trigger action using columns available through the REFERENCING clause.
     * 2)trigger action using columns without the REFERENCING clause.
     * For both kinds of triggers, there is test case which drops the column 
     * being used in the trigger action column. 
     * 
     * In all three modes of upgrade, soft upgrade, post soft upgrade, and 
     * hard upgrade, ALTER TABLE DROP COLUMN will detect the trigger 
     * dependency.
     */
    public void testAlterTableDropColumnAndTriggerAction() throws Exception
    {
    	// ALTER TABLE DROP COLUMN was introduced in 10.3 so no point running
    	// this test with earlier releases
    	if (!oldAtLeast(10, 3)) return;
    	
        Statement s = createStatement();
        ResultSet rs;

        switch ( getPhase() )
        {
        case PH_CREATE: // create with old version
        	// Create 4 tables for each of the upgrade phases
        	//
        	// There will be 2 tests in each upgrade phase. 
        	// 1)One test will use the column being dropped as part of the
        	//   trigger action column through the REFERENCING clause
        	// 2)Second test will use the column being dropped as part of the
        	//   trigger action sql without the REFERENCING clause
        	//
        	//For each of the two tests, one table will be used for 
        	//ALTER TABLE DROP COLUMN RESTRICT and the second table will
        	//be used for ALTER TABLE DROP COLUMN CASCADE

        	//Following 4 tables and triggers will be used in soft upgrade mode
        	// The trigger actions on following 2 table use a column through 
        	// REFERENCING clause
        	createTableAndTrigger("TAB1_SOFT_UPGRADE_RESTRICT", 
        			"TAB1_SOFT_UPGRADE_RESTRICT_TR1", true);
        	createTableAndTrigger("TAB1_SOFT_UPGRADE_CASCADE",
        			"TAB1_SOFT_UPGRADE_CASCADE_TR1", true);
        	// The trigger actions on following 2 table use a column without
        	// the REFERENCING clause
        	createTableAndTrigger("TAB2_SOFT_UPGRADE_RESTRICT",
        			"TAB2_SOFT_UPGRADE_RESTRICT_TR1", false);
        	createTableAndTrigger("TAB2_SOFT_UPGRADE_CASCADE",
        			"TAB2_SOFT_UPGRADE_CASCADE_TR1", false);

        	//Following 4 tables and triggers will be used in post-soft 
        	// upgrade mode
        	// The trigger actions on following 2 table use a column through 
        	// REFERENCING clause
        	createTableAndTrigger("TAB1_POSTSOFT_UPGRADE_RESTRICT", 
        			"TAB1_POSTSOFT_UPGRADE_RESTRICT_TR1", true);
        	createTableAndTrigger("TAB1_POSTSOFT_UPGRADE_CASCADE",
        			"TAB1_POSTSOFT_UPGRADE_CASCADE_TR1", true);
        	// The trigger actions on following 2 table use a column without
        	// the REFERENCING clause
        	createTableAndTrigger("TAB2_POSTSOFT_UPGRADE_RESTRICT",
        			"TAB2_POSTSOFT_UPGRADE_RESTRICT_TR1", false);
        	createTableAndTrigger("TAB2_POSTSOFT_UPGRADE_CASCADE",
        			"TAB2_POSTSOFT_UPGRADE_CASCADE_TR1", false);

        	//Following 4 tables and triggers will be used in hard 
        	// upgrade mode
        	// The trigger actions on following 2 table use a column through 
        	// REFERENCING clause
        	createTableAndTrigger("TAB1_HARD_UPGRADE_RESTRICT", 
        			"TAB1_HARD_UPGRADE_RESTRICT_TR1", true);
        	createTableAndTrigger("TAB1_HARD_UPGRADE_CASCADE",
        			"TAB1_HARD_UPGRADE_CASCADE_TR1", true);
        	// The trigger actions on following 2 table use a column without
        	// the REFERENCING clause
        	createTableAndTrigger("TAB2_HARD_UPGRADE_RESTRICT",
        			"TAB2_HARD_UPGRADE_RESTRICT_TR1", false);
        	createTableAndTrigger("TAB2_HARD_UPGRADE_CASCADE",
        			"TAB2_HARD_UPGRADE_CASCADE_TR1", false);

            break;

        case PH_SOFT_UPGRADE: // boot with new version and soft-upgrade
        	// The trigger has trigger action using the column being dropped
            // through the REFERENCING clause. Because of this, 
        	// DROP COLUMN RESTRICT will fail.
            assertStatementError("X0Y25", s,
            		" alter table TAB1_SOFT_UPGRADE_RESTRICT " +
            		" drop column c11 restrict");
            //Verify that trigger still exists in the system
            rs = s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB1_SOFT_UPGRADE_RESTRICT_TR1'");
            JDBC.assertFullResultSet(rs, 
               		new String[][]{{"TAB1_SOFT_UPGRADE_RESTRICT_TR1"}});
                           	
        	// The trigger has trigger action using the column being dropped
            // through the REFERENCING clause. Because of this, 
        	// DROP COLUMN CASCADE will drop the dependent trigger.
            s.executeUpdate("alter table TAB1_SOFT_UPGRADE_CASCADE " +
            		" drop column c11 CASCADE");
            checkWarning(s, "01502");
            //Verify that the trigger does not exist in the system anymore
            JDBC.assertEmpty(s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB1_SOFT_UPGRADE_CASCADE_TR1'"));

        	// The trigger has trigger action using the column being dropped
        	// (not through the REFERENCING clause). Because of this, 
        	// DROP COLUMN RESTRICT will fail.
            assertStatementError("X0Y25", s,
            		" alter table TAB2_SOFT_UPGRADE_RESTRICT " +
            		" drop column c11 restrict");
            //Verify that trigger still exists in the system
            rs = s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB2_SOFT_UPGRADE_RESTRICT_TR1'");
            JDBC.assertFullResultSet(rs, 
               		new String[][]{{"TAB2_SOFT_UPGRADE_RESTRICT_TR1"}});
                           	
        	// The trigger has trigger action using the column being dropped
            // (not through the REFERENCING clause). Because of this, 
        	// DROP COLUMN CASCADE will drop the dependent trigger.
            s.executeUpdate("alter table TAB2_SOFT_UPGRADE_CASCADE " +
            		" drop column c11 CASCADE");
            checkWarning(s, "01502");
            //Verify that the trigger does not exist in the system anymore
            JDBC.assertEmpty(s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB2_SOFT_UPGRADE_CASCADE_TR1'"));

            // Same behavior can be seen with tables and triggers created
            // in soft upgrade mode using Derby 10.7 release,
            // The trigger actions in this test case uses a column through 
        	// REFERENCING clause. Because of this, 
        	// DROP COLUMN RESTRICT will fail.
        	createTableAndTrigger("TAB1_SOFT_UPGRADE_NEW_TABLE_RESTRICT", 
        			"TAB1_SOFT_UPGRADE_NEW_TABLE_RESTRICT_TR1", true);
            assertStatementError("X0Y25", s,
            		" alter table TAB1_SOFT_UPGRADE_NEW_TABLE_RESTRICT " +
            		" drop column c11 restrict");
            //Verify that trigger still exists in the system
            rs = s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB1_SOFT_UPGRADE_NEW_TABLE_RESTRICT_TR1'");
            JDBC.assertFullResultSet(rs,
            		new String[][]{{"TAB1_SOFT_UPGRADE_NEW_TABLE_RESTRICT_TR1"}});
            
            // Same behavior can be seen with tables and triggers created
            // in soft upgrade mode using Derby 10.7 release,
            // The trigger actions in this test case uses a column through 
        	// REFERENCING clause. Because of this, 
        	// DROP COLUMN CASCADE will drop the dependent trigger.
        	createTableAndTrigger("TAB1_SOFT_UPGRADE_NEW_TABLE_CASCADE",
        			"TAB1_SOFT_UPGRADE_NEW_TABLE_CASCADE_TR1", true);
            s.executeUpdate("alter table TAB1_SOFT_UPGRADE_NEW_TABLE_CASCADE " +
            		" drop column c11 CASCADE");
            checkWarning(s, "01502");
            //Verify that the trigger does not exist in the system anymore
            JDBC.assertEmpty(s.executeQuery(
            		" select triggername from sys.systriggers where " +
            		"triggername='TAB1_SOFT_UPGRADE_NEW_TABLE_CASCADE_TR1'"));

            // Same behavior can be seen with tables and triggers created
            // in soft upgrade mode using Derby 10.7 release,
            // The trigger actions in this test case uses a column  
        	// (not through the REFERENCING clause). Because of this, 
        	// DROP COLUMN RESTRICT will fail.
        	createTableAndTrigger("TAB2_SOFT_UPGRADE_NEW_TABLE_RESTRICT", 
        			"TAB2_SOFT_UPGRADE_NEW_TABLE_RESTRICT_TR1", false);
            assertStatementError("X0Y25", s,
            		" alter table TAB2_SOFT_UPGRADE_NEW_TABLE_RESTRICT " +
            		" drop column c11 restrict");
            //Verify that trigger still exists in the system
            rs = s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB2_SOFT_UPGRADE_NEW_TABLE_RESTRICT_TR1'");
            JDBC.assertFullResultSet(rs,
            		new String[][]{{"TAB2_SOFT_UPGRADE_NEW_TABLE_RESTRICT_TR1"}});

            // Same behavior can be seen with tables and triggers created
            // in soft upgrade mode using Derby 10.7 release,
            // The trigger actions in this test case uses a column  
        	// (not through the REFERENCING clause). Because of this, 
        	// DROP COLUMN RESTRICT will fail.
        	createTableAndTrigger("TAB2_SOFT_UPGRADE_NEW_TABLE_CASCADE", 
        			"TAB2_SOFT_UPGRADE_NEW_TABLE_CASCADE_TR1", false);
            s.executeUpdate("alter table TAB2_soft_upgrade_NEW_TABLE_cascade " +
            		" drop column c11 CASCADE");
            checkWarning(s, "01502");
            //Verify that the trigger does not exist in the system anymore
            JDBC.assertEmpty(s.executeQuery(
            		" select triggername from sys.systriggers where " +
            		"triggername='TAB2_SOFT_UPGRADE_NEW_TABLE_CASCADE_TR1'"));
            break;

        case PH_POST_SOFT_UPGRADE: 
        	// soft-downgrade: boot with old version after soft-upgrade

        	//The tables created with 10.6 and prior versions will exhibit
        	// incorrect behavior because changes for DERBY-4887/DERBY-4984 
        	// have not been backported to 10.6 and earlier yet
        	//
        	//ALTER TABLE DROP COLUMN will not detect column being dropped
        	// in trigger action of dependent triggers.
        	incorrectBehaviorForDropColumn("TAB1_POSTSOFT_UPGRADE_RESTRICT",
        			"TAB1_POSTSOFT_UPGRADE_RESTRICT_TR1", "RESTRICT");
        	incorrectBehaviorForDropColumn("TAB1_POSTSOFT_UPGRADE_CASCADE",
        			"TAB1_POSTSOFT_UPGRADE_CASCADE_TR1", "CASCADE");
        	incorrectBehaviorForDropColumn("TAB2_POSTSOFT_UPGRADE_RESTRICT",
        			"TAB2_POSTSOFT_UPGRADE_RESTRICT_TR1", "RESTRICT");
        	incorrectBehaviorForDropColumn("TAB2_POSTSOFT_UPGRADE_CASCADE",
        			"TAB2_POSTSOFT_UPGRADE_CASCADE_TR1", "CASCADE");

        	//We are back to pre-10.7 version after the soft upgrade. 
        	//ALTER TABLE DROP COLUMN will continue to behave incorrectly
        	//and will not detect the trigger actions referencing the column
        	//being dropped through the REFERENCING clause
        	createTableAndTrigger("TAB1_POST_SOFT_UPGRADE_NEW_TABLE_RESTRICT",
        			"TAB1_POST_SOFT_UPGRADE_NEW_TABLE_RESTRICT_TR1", true);
        	incorrectBehaviorForDropColumn("TAB1_POST_SOFT_UPGRADE_NEW_TABLE_RESTRICT",
        			"TAB1_POST_SOFT_UPGRADE_NEW_TABLE_RESTRICT_TR1", "RESTRICT");
        	createTableAndTrigger("TAB1_POST_SOFT_UPGRADE_NEW_TABLE_CASCADE",
        			"TAB1_POST_SOFT_UPGRADE_NEW_TABLE_CASCADE_TR1", true);
        	incorrectBehaviorForDropColumn("TAB1_POST_SOFT_UPGRADE_NEW_TABLE_CASCADE",
        			"TAB1_POST_SOFT_UPGRADE_NEW_TABLE_CASCADE_TR1", "RESTRICT");

        	//We are back to pre-10.7 version after the soft upgrade. 
        	//ALTER TABLE DROP COLUMN will continue to behave incorrectly
        	//and will not detect the trigger actions referencing the column
        	//being dropped directly (ie without the REFERENCING clause)
        	createTableAndTrigger("TAB2_POST_SOFT_UPGRADE_NEW_TABLE_RESTRICT",
        			"TAB2_POST_SOFT_UPGRADE_NEW_TABLE_RESTRICT_TR1", false);
        	incorrectBehaviorForDropColumn("TAB2_POST_SOFT_UPGRADE_NEW_TABLE_RESTRICT",
        			"TAB2_POST_SOFT_UPGRADE_NEW_TABLE_RESTRICT_TR1", "RESTRICT");
        	
        	createTableAndTrigger("TAB2_POST_SOFT_UPGRADE_NEW_TABLE_CASCADE",
        			"TAB2_POST_SOFT_UPGRADE_NEW_TABLE_CASCADE_TR1", false);
        	incorrectBehaviorForDropColumn("TAB2_POST_SOFT_UPGRADE_NEW_TABLE_CASCADE",
        			"TAB2_POST_SOFT_UPGRADE_NEW_TABLE_CASCADE_TR1", "RESTRICT");
        	
        	break;

        case PH_HARD_UPGRADE: // boot with new version and hard-upgrade
        	// The trigger has trigger action using the column being dropped
            // through the REFERENCING clause. Because of this, 
        	// DROP COLUMN RESTRICT will fail.
            assertStatementError("X0Y25", s,
            		" alter table TAB1_HARD_UPGRADE_RESTRICT " +
            		" drop column c11 restrict");
            //Verify that trigger still exists in the system
            rs = s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB1_HARD_UPGRADE_RESTRICT_TR1'");
            JDBC.assertFullResultSet(rs, 
                    new String[][]{{"TAB1_HARD_UPGRADE_RESTRICT_TR1"}});

        	// The trigger has trigger action using the column being dropped
            // through the REFERENCING clause. Because of this, 
        	// DROP COLUMN CASCADE will drop the dependent trigger.
            s.executeUpdate("alter table TAB1_HARD_UPGRADE_CASCADE " +
                    " drop column c11 CASCADE");
            checkWarning(s, "01502");
            //Verify that the trigger does not exist in the system anymore
            JDBC.assertEmpty(s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB1_HARD_UPGRADE_CASCADE_TR1'"));
        	
        	// The trigger has trigger action using the column being dropped
        	// (not through the REFERENCING clause). Because of this, 
        	// DROP COLUMN RESTRICT will fail.
            assertStatementError("X0Y25", s,
            		" alter table TAB2_HARD_UPGRADE_RESTRICT " +
            		" drop column c11 restrict");
            //Verify that trigger still exists in the system
            rs = s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB2_HARD_UPGRADE_RESTRICT_TR1'");
            JDBC.assertFullResultSet(rs, 
                    new String[][]{{"TAB2_HARD_UPGRADE_RESTRICT_TR1"}});

        	// The trigger has trigger action using the column being dropped
            // (not through the REFERENCING clause). Because of this, 
        	// DROP COLUMN CASCADE will drop the dependent trigger.
            s.executeUpdate("alter table TAB2_HARD_UPGRADE_CASCADE " +
                    " drop column c11 CASCADE");
            checkWarning(s, "01502");
            //Verify that the trigger does not exist in the system anymore
            JDBC.assertEmpty(s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB2_HARD_UPGRADE_CASCADE_TR1'"));

            //Create 2 new tables now that the database has been upgraded.
        	//Notice that newly created tables will be able to detect
        	//trigger action reference to column through REFERENCING clause.
        	createTableAndTrigger("TAB1_HARD_UPGRADE_NEW_TABLE_RESTRICT",
        			"TAB1_HARD_UPGRADE_NEW_TABLE_RESTRICT_TR1", true);
            assertStatementError("X0Y25", s,
            		" alter table TAB1_HARD_UPGRADE_NEW_TABLE_RESTRICT " +
            		" drop column c11 restrict");
            //Verify that trigger still exists in the system
            rs = s.executeQuery(
            " select triggername from sys.systriggers where " +
            "triggername='TAB1_HARD_UPGRADE_NEW_TABLE_RESTRICT_TR1'");
            JDBC.assertFullResultSet(rs, 
            		new String[][]{{"TAB1_HARD_UPGRADE_NEW_TABLE_RESTRICT_TR1"}});

        	// The trigger has trigger action using the column being dropped
            // through the REFERENCING clause. Because of this, 
        	// DROP COLUMN CASCADE will drop the dependent trigger.
        	createTableAndTrigger("TAB1_HARD_UPGRADE_NEW_TABLE_CASCADE",
        			"TAB1_HARD_UPGRADE_NEW_TABLE_CASCADE_TR1", true);
            s.executeUpdate("alter table TAB1_HARD_UPGRADE_NEW_TABLE_CASCADE " +
            		" drop column c11 CASCADE");
            checkWarning(s, "01502");
            //Verify that the trigger does not exist in the system anymore
            JDBC.assertEmpty(s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB1_HARD_UPGRADE_NEW_TABLE_CASCADE_TR1'"));

            //Create 2 new tables now that the database has been upgraded.
        	// Notice that newly created tables will be able to detect
        	// trigger action column (without the REFERENCING clause.)
        	//Because of this, DROP COLUMN RESTRICT will fail.
        	createTableAndTrigger("TAB2_HARD_UPGRADE_NEW_TABLE_RESTRICT",
        			"TAB2_HARD_UPGRADE_NEW_TABLE_RESTRICT_TR1", false);
            //Verify that trigger still exists in the system
            assertStatementError("X0Y25", s,
            		" alter table TAB2_HARD_UPGRADE_NEW_TABLE_RESTRICT " +
            		" drop column c11 restrict");
            rs = s.executeQuery(
            " select triggername from sys.systriggers where " +
            "triggername='TAB2_HARD_UPGRADE_NEW_TABLE_RESTRICT_TR1'");
            JDBC.assertFullResultSet(rs, 
            		new String[][]{{"TAB2_HARD_UPGRADE_NEW_TABLE_RESTRICT_TR1"}});

        	// The trigger has trigger action using the column being dropped
            // (not through the REFERENCING clause). Because of this, 
        	// DROP COLUMN CASCADE will drop the dependent trigger.
        	createTableAndTrigger("TAB2_HARD_UPGRADE_NEW_TABLE_CASCADE",
        			"TAB2_HARD_UPGRADE_NEW_TABLE_CASCADE_TR1", false);
            s.executeUpdate("alter table TAB2_HARD_UPGRADE_NEW_TABLE_CASCADE " +
            		" drop column c11 CASCADE");
            checkWarning(s, "01502");
            //Verify that the trigger does not exist in the system anymore
            JDBC.assertEmpty(s.executeQuery(
                    " select triggername from sys.systriggers where " +
                    "triggername='TAB2_HARD_UPGRADE_NEW_TABLE_CASCADE_TR1'"));
            break;
        }
    }

    //Create the table and trigger necessary for ALTER TABLE DROP COLUMN test
    private void createTableAndTrigger(String tableName,
    		String triggerName, boolean usesReferencingClause) 
    throws SQLException {
        Statement s = createStatement();
        ResultSet rs;
        
        s.execute("CREATE TABLE " + tableName + " (c11 int, c12 int) ");
        s.execute("INSERT INTO " + tableName + " VALUES (1,10)");
        s.execute("CREATE TRIGGER " + triggerName + 
        		" AFTER UPDATE OF c12 ON " + tableName +
        		(usesReferencingClause ? " REFERENCING OLD AS oldt" : "" )+
        		" FOR EACH ROW SELECT " +
        		(usesReferencingClause ? "oldt.c11 " : "c11 " )+
                "FROM " + tableName);
        s.executeUpdate("UPDATE " + tableName + " SET c12=c12+1");
    }
    

    //ALTER TABLE DROP COLUMN in not detected the trigger column dependency for
    //columns being used through the REFERENCING clause for triggers created
    //prior to 10.7 release
    private void incorrectBehaviorForDropColumn(String tableName,
    		String triggerName, String restrictOrCascade) throws SQLException {
        Statement s = createStatement();
        ResultSet rs;
        
        //ALTER TABLE DROP COLUMN of a column used in the trigger action
        //through REFERENCING clause does not detect the trigger 
        //dependency in older releases.
        //RESTRICT won't give any error for dependent trigger and will
        //drop column c11 even though it is getting used in trigger action
        //and will leave the invalid trigger in the system. 
        //CASCADE won't give any warning for dependent trigger and will
        //drop column c11 even though it is getting used in trigger action
        //and will leave the invalid trigger in the system. 
        s.executeUpdate("ALTER TABLE " + tableName + " DROP COLUMN c11 " +
        		restrictOrCascade);
        rs =
            s.executeQuery(
            " select triggername from sys.systriggers where " +
            "triggername='" + triggerName + "'");
        JDBC.assertFullResultSet(rs, new String[][]{{triggerName}});
    }

    private void checkWarning(Statement st, String expectedWarning)
            throws Exception {
        SQLWarning sqlWarn = null;

        sqlWarn = st.getWarnings();
        if (sqlWarn == null) {
            sqlWarn = getConnection().getWarnings();
        }
        assertNotNull("Expected warning but found none", sqlWarn);
        assertSQLState(expectedWarning, sqlWarn);
    }

    /**
     * Make sure that DERBY-1482 changes do not break backward compatibility
     */
    public void testTriggers() throws SQLException
    {
        Statement s = createStatement();
        ResultSet rs;
        boolean modeDb2SqlOptional = oldAtLeast(10, 3);

        switch ( getPhase() )
        {
        case PH_CREATE: // create with old version
            s.execute("CREATE TABLE DERBY1482_table1(c11 int, c12 int)");
            s.execute("INSERT INTO DERBY1482_table1 VALUES (1,10)");
            s.execute("CREATE TABLE DERBY1482_table2(c21 int, c22 int)");
            s.execute("CREATE TABLE DERBY1482_table3(c31 int, c32 int)");
            s.execute("CREATE TABLE DERBY1482_table4(c41 int, c42 int)");
            s.execute("CREATE TABLE DERBY1482_table5(c51 int, c52 int)");
            //Create the first trigger in the older release where the
            //database has been created. Every update of DERBY1482_table1.c12
            //will cause an insert into DERBY1482_table2 through this trigger tr1.
            s.execute("CREATE TRIGGER tr1 AFTER UPDATE OF c12 " +
            		"ON DERBY1482_table1 REFERENCING OLD AS oldt " +
            		"FOR EACH ROW " +
                    (modeDb2SqlOptional?"":"MODE DB2SQL ") +
                    "INSERT INTO DERBY1482_table2 VALUES(-1, oldt.c12)");
            
            //Now do an update which will fire trigger tr1
            s.executeUpdate("UPDATE DERBY1482_table1 SET c12=-1 WHERE c11=1");
            //Verify that trigger tr1 has inserted one row in DERBY1482_table2
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table2");
            JDBC.assertFullResultSet(rs, new String[][]{{"1"}});
            break;

        case PH_SOFT_UPGRADE: // boot with new version and soft-upgrade
            //Now do an update while in the soft upgrade. This should
        	//fire trigger tr1
            s.executeUpdate("UPDATE DERBY1482_table1 SET c12=-1 WHERE c11=1");
            //Verify that now we have 2 rows in DERBY1482_table2 because trigger tr1
            //has fired twice so far. Once in PH_CREATE phase and once
            //in PH_SOFT_UPGRADE phase
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table2");
            JDBC.assertFullResultSet(rs, new String[][]{{"2"}});
            //Create trigger tr2 in soft upgrade mode. DERBY-1482 changes
            //will not put anything about trigger action columns in 
            //SYSTRIGGERS to maintain backward compatibility. Only 10.7
            //and up recognize additional information about trigger action
            //columns in SYSTRIGGERS.
            s.execute("CREATE TRIGGER tr2 AFTER UPDATE OF c12 ON DERBY1482_table1 " +
            		"REFERENCING OLD AS oldt FOR EACH ROW " +
                    (modeDb2SqlOptional?"":"MODE DB2SQL ") +
            		"INSERT INTO DERBY1482_table3 VALUES(-1, oldt.c12)");
            //Now do an update which will fire triggers tr1 and tr2
            s.executeUpdate("UPDATE DERBY1482_table1 SET c12=-1 WHERE c11=1");
            //Verify that trigger tr1 has inserted one more row in DERBY1482_table2
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table2");
            JDBC.assertFullResultSet(rs, new String[][]{{"3"}});
            //Verify that trigger tr2 has inserted one row in DERBY1482_table3
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table3");
            JDBC.assertFullResultSet(rs, new String[][]{{"1"}});
            break;

        case PH_POST_SOFT_UPGRADE: // soft-downgrade: boot with old version after soft-upgrade
            //Now do an update when we are back with the older release
        	//after the soft upgrade. This should fire trigger tr1 and tr2
            s.executeUpdate("UPDATE DERBY1482_table1 SET c12=-1 WHERE c11=1");
            //Verify that now we have 4 rows in DERBY1482_table2 and 2 rows in DERBY1482_table3
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table2");
            JDBC.assertFullResultSet(rs, new String[][]{{"4"}});
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table3");
            JDBC.assertFullResultSet(rs, new String[][]{{"2"}});
            //Create trigger tr3 with the older release. Triggers created in
            //soft-upgrade mode and with older release should work fine.
            s.execute("CREATE TRIGGER tr3 AFTER UPDATE OF c12 ON DERBY1482_table1 " +
            		"REFERENCING OLD AS oldt FOR EACH ROW " +
                    (modeDb2SqlOptional?"":"MODE DB2SQL ") +
            		"INSERT INTO DERBY1482_table4 VALUES(-1, oldt.c12)");
            //Now do an update which will fire triggers tr1, tr2 and tr3
            s.executeUpdate("UPDATE DERBY1482_table1 SET c12=-1 WHERE c11=1");
            //Verify that trigger tr1 has inserted one more row in DERBY1482_table2
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table2");
            JDBC.assertFullResultSet(rs, new String[][]{{"5"}});
            //Verify that trigger tr2 has inserted one more row in DERBY1482_table3
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table3");
            JDBC.assertFullResultSet(rs, new String[][]{{"3"}});
            //Verify that trigger tr3 has inserted one row in DERBY1482_table4
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table4");
            JDBC.assertFullResultSet(rs, new String[][]{{"1"}});
            break;

        case PH_HARD_UPGRADE: // boot with new version and hard-upgrade
        	//Do an update after we have hard upgraded to 10.7 and make sure
        	//that all the triggers (created with older release and created
        	//in soft-upgrade mode) work fine.
            s.executeUpdate("UPDATE DERBY1482_table1 SET c12=-1 WHERE c11=1");
            //Verify that now we have 6 rows in DERBY1482_table2, 4 rows in DERBY1482_table3, 2 rows in DERBY1482_table4
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table2");
            JDBC.assertFullResultSet(rs, new String[][]{{"6"}});
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table3");
            JDBC.assertFullResultSet(rs, new String[][]{{"4"}});
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table4");
            JDBC.assertFullResultSet(rs, new String[][]{{"2"}});
            //Create trigger DERBY1482_table4 in the hard-upgraded db.
            s.execute("CREATE TRIGGER tr4 AFTER UPDATE OF c12 ON DERBY1482_table1 " +
            		"REFERENCING OLD AS oldt FOR EACH ROW " +
                    (modeDb2SqlOptional?"":"MODE DB2SQL ") +
            		"INSERT INTO DERBY1482_table5 VALUES(-1, oldt.c12)");
            //All 4 triggers tr1, tr2, tr3 and tr4 should fire 
            //Now do an update which will fire all 4 triggers tr1,tr2,tr3,tr4
            s.executeUpdate("UPDATE DERBY1482_table1 SET c12=-1 WHERE c11=1");
            //Verify that trigger tr1 has inserted one more row in DERBY1482_table2
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table2");
            JDBC.assertFullResultSet(rs, new String[][]{{"7"}});
            //Verify that trigger tr2 has inserted one more row in DERBY1482_table3
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table3");
            JDBC.assertFullResultSet(rs, new String[][]{{"5"}});
            //Verify that trigger tr3 has inserted one more row in DERBY1482_table4
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table4");
            JDBC.assertFullResultSet(rs, new String[][]{{"3"}});
            //Verify that trigger tr4 has inserted one row in DERBY1482_table5
            rs = s.executeQuery("SELECT COUNT(*) FROM DERBY1482_table5");
            JDBC.assertFullResultSet(rs, new String[][]{{"1"}});
            break;
        }
        s.close();
    }

    /**
     * Make sure that that database is at level 10.7 in order to enjoy
     * routines with specified EXTERNAL SECURITY INVOKER or DEFINER.
     */
    public void testExternalSecuritySpecification() throws SQLException
    {
        String functionWithDefinersRights =
            "create function f_4551( a varchar( 100 ) ) returns int\n" +
            "language java parameter style java reads sql data\n" +
            "external security definer\n" +
            "external name 'Z.getIntValue'\n";

        Statement s = createStatement();

        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, s, functionWithDefinersRights );
            break;

        case PH_SOFT_UPGRADE: // boot with new version and soft-upgrade
            assertStatementError(
                UPGRADE_REQUIRED, s, functionWithDefinersRights );
            break;

        case PH_HARD_UPGRADE: // boot with new version and hard-upgrade.
            // Syntax now accepted and dictionary level ok, but
            // sqlAuthorization not enabled (a priori) - expected.
            assertStatementError(GRANT_REVOKE_WITH_LEGACY_ACCESS,
                                 s, functionWithDefinersRights );
            break;
        }

        s.close();
    }

}