File: Changes10_5.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 (427 lines) | stat: -rw-r--r-- 13,550 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
/*

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

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.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBCDataSource;
import org.apache.derbyTesting.junit.SupportFilesSetup;

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

    <UL>
    <LI> testUpdateStatisticsProcdure - DERBY-269
    Make sure that SYSCS_UTIL.SYSCS_UPDATE_STATISTICS can only be run in Derby
    10.5 and higher.
    </UL>

 */
public class Changes10_5 extends UpgradeChange {

    private static  final   String  BAD_SYNTAX = "42X01";

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

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

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

    /**
     * Make sure that SYSCS_UTIL.SYSCS_UPDATE_STATISTICS can only be run in 
     * Derby 10.5 and higher. 
     * DERBY-269
     * Test added for 10.5.
     * @throws SQLException
     *
     */
    public void testUpdateStatisticsProcdure() throws SQLException
    {
    	Statement s;
        switch (getPhase())
        {
        case PH_CREATE:
            s = createStatement();
            s.execute("CREATE TABLE DERBY_269(c11 int, c12 char(20))");
            s.execute("INSERT INTO DERBY_269 VALUES(1, 'DERBY-269')");
            s.execute("CREATE INDEX I1 ON DERBY_269(c12)");
            s.close();
            break;

        case PH_SOFT_UPGRADE:
        case PH_POST_SOFT_UPGRADE:
            // new update statistics procedure should not be found
            // on soft-upgrade.
            s = createStatement();
            assertStatementError("42Y03", s,
                    "call SYSCS_UTIL.SYSCS_UPDATE_STATISTICS" +
                    "('APP', 'DERBY_269', null)");
            assertStatementError("42Y03", s,
                    "call SYSCS_UTIL.SYSCS_UPDATE_STATISTICS" +
                    "('APP', 'DERBY_269', 'I1')");
            s.close();
            break;

        case PH_HARD_UPGRADE:
        	//We are at Derby 10.5 release and hence should find the
        	//update statistics procedure
            s = createStatement();
            s.execute("call SYSCS_UTIL.SYSCS_UPDATE_STATISTICS" +
            		"('APP', 'DERBY_269', null)");
            s.execute("call SYSCS_UTIL.SYSCS_UPDATE_STATISTICS" +
            		"('APP', 'DERBY_269', 'I1')");
            s.close();
            break;
        }
    }

    /**
     * Test that the DETERMINISTIC keyword is not allowed until you
     * hard-upgrade to 10.5.
     *
     */
    public void testDeterminismKeyword() throws SQLException
    {
        String  sqlstate = null;
        
        switch (getPhase())
        {
        case PH_SOFT_UPGRADE:
            sqlstate = SQLSTATE_NEED_UPGRADE;
            break;
            
        case PH_POST_SOFT_UPGRADE:
            sqlstate = BAD_SYNTAX;
            break;

        case PH_HARD_UPGRADE:
            sqlstate = null;
            break;

        default:
            return;
        }
        
        possibleError
            (
             sqlstate,
             "create function f_3570_12()\n" +
             "returns int\n" +
             "language java\n" +
             "parameter style java\n" +
             "deterministic\n" +
             "no sql\n" +
             "external name 'foo.bar.wibble'\n"
             );
        possibleError
            (
             sqlstate,
             "create procedure p_3570_13()\n" +
             "language java\n" +
             "not deterministic\n" +
             "parameter style java\n" +
             "modifies sql data\n" +
             "external name 'foo.bar.wibble'\n"
             );
    }

    /**
     * Test that generation clauses are not allowed until you
     * hard-upgrade to 10.5.
     *
     */
    public void testGenerationClauses() throws SQLException
    {
        String  sqlstate = null;
        
        switch (getPhase())
        {
        case PH_SOFT_UPGRADE:
            sqlstate = SQLSTATE_NEED_UPGRADE;
            break;
            
        case PH_POST_SOFT_UPGRADE:
            sqlstate = BAD_SYNTAX;
            break;

        case PH_HARD_UPGRADE:
            sqlstate = null;
            break;

        default:
            return;
        }
        
        possibleError
            (
             sqlstate,
             "create table t_genCol_2( a int, b int generated always as ( -a ), c int )"
             );
    }

    /**
     * <p>
     * Run a statement. If the sqlstate is not null, then we expect that error.
     * </p>
     */
    private void    possibleError( String sqlstate, String text )
        throws SQLException
    {
        if ( sqlstate != null )
        {
            assertCompileError( sqlstate, text );
        }
        else
        {
            Statement   s = createStatement();
            s.execute( text );
            s.close();
        }
    }

    /**
     * Check that you must be hard-upgraded to 10.5 or later in order to use
     * SQL roles
     * @throws SQLException
     *
     */
    public void testSQLRolesBasic() throws SQLException
    {
        // The standard upgrade database doesn't have sqlAuthorization
        // set, so we can only check if the system tables for roles is
        // present.

        Statement s = createStatement();
        String createRoleText = "create role foo";

        if (getOldMajor() == 10 && getOldMinor() == 4) {
            // In 10.4 the roles commands were present but just gave "not
            // implemented".
            switch (getPhase()) {
            case PH_CREATE:
                assertStatementError("0A000", s, createRoleText );
                break;

            case PH_SOFT_UPGRADE:
                // needs hard upgrade
                assertStatementError("XCL47", s, createRoleText );
                break;

            case PH_POST_SOFT_UPGRADE:
                assertStatementError("0A000", s, createRoleText );
                break;

            case PH_HARD_UPGRADE:
                // not supported because SQL authorization not set
                assertStatementError("42Z60", s, createRoleText );
                break;
            }

        } else {
            switch (getPhase()) {
                case PH_CREATE:
                    assertStatementError("42X01", s, createRoleText );
                    break;

                case PH_SOFT_UPGRADE:
                    // needs hard upgrade
                    assertStatementError("XCL47", s, createRoleText );
                    break;

                case PH_POST_SOFT_UPGRADE:
                    assertStatementError("42X01", s, createRoleText );
                    break;

                case PH_HARD_UPGRADE:
                    // not supported because SQL authorization not set
                    assertStatementError("42Z60", s, createRoleText );
                    break;
            }
        }


        s.close();
    }

    /**
     * Check that when hard-upgraded to 10.5 or later SQL roles can be
     * declared if DB has sqlAuthorization.
     * @throws SQLException
     *
     */
    public void testSQLRoles() throws SQLException
    {
        // Do rudimentary sanity checking: that we can create, meaningfully use
        // and drop roles. If so, we can presume SYS.SYSROLES has been upgraded
        // correctly. If upgrading from 10.4, SYS.SYSROLES are already present,
        // but roles were not activated, cf. test in POST_SOFT_UPGRADE.

        DataSource ds = JDBCDataSource.getDataSourceLogical("ROLES_10_5");
        Connection conn = null;
        Statement s = null;
        boolean supportSqlAuthorization = oldAtLeast(10, 2);

        JDBCDataSource.setBeanProperty(ds, "user", "garfield");
        JDBCDataSource.setBeanProperty(ds, "password", "theCat");

        switch (getPhase()) {
        case PH_CREATE:
            // Create the database if it was not already created.
            JDBCDataSource.setBeanProperty(ds, "createDatabase", "create");
            conn = ds.getConnection();

            // Make the database have std security, and define
            // a database user for the database owner).
            CallableStatement cs = conn.prepareCall(
                "call syscs_util.syscs_set_database_property(?,?)");

            cs.setString(1, "derby.connection.requireAuthentication");
            cs.setString(2, "true");
            cs.execute();

            cs.setString(1, "derby.authentication.provider");
            cs.setString(2, "BUILTIN");
            cs.execute();

            cs.setString(1, "derby.database.sqlAuthorization");
            cs.setString(2, "true");
            cs.execute();

            cs.setString(1, "derby.database.propertiesOnly");
            cs.setString(2, "true");
            cs.execute();

            cs.setString(1, "derby.user.garfield");
            cs.setString(2, "theCat");
            cs.execute();

            cs.setString(1, "derby.user.jon");
            cs.setString(2, "theOwner");
            cs.execute();

            conn.close();

            JDBCDataSource.shutdownDatabase(ds);
            break;

        case PH_SOFT_UPGRADE:
            /* We can't always do soft upgrade, because when
             * sqlAuthorization is set and we are coming from a
             * pre-10.2 database, connecting will fail with a message
             * to hard upgrade before setting sqlAuthorization, so we
             * skip this step.
             */
            if (oldAtLeast(10,2)) {
                // needs hard upgrade
                conn = ds.getConnection();
                s = conn.createStatement();

                assertStatementError("XCL47", s, "create role foo" );
                conn.close();

                JDBCDataSource.shutdownDatabase(ds);
            }
            break;

        case PH_POST_SOFT_UPGRADE:
            conn = ds.getConnection();
            s = conn.createStatement();

            if (getOldMajor() == 10 && getOldMinor() == 4) {
                // not implemented
                assertStatementError("0A000", s, "create role foo" );
            } else {
                // syntax error
                assertStatementError("42X01", s, "create role foo" );
            }

            conn.close();

            JDBCDataSource.shutdownDatabase(ds);
            break;

        case PH_HARD_UPGRADE:
            JDBCDataSource.setBeanProperty(
                ds, "connectionAttributes", "upgrade=true");
            conn = ds.getConnection();
            s = conn.createStatement();

            // Roles should work; basic sanity test

            // garfield is dbo
            s.execute("create role foo");
            s.execute("create table cats(specie varchar(30))");
            s.execute("insert into cats " +
                      "values 'lynx', 'tiger', 'persian', 'garfield'");
            s.execute("grant select on cats to foo");
            s.execute("grant foo to jon");

            // Connect as jon (not owner) and employ jon's newfound role
            JDBCDataSource.clearStringBeanProperty(ds, "connectionAttributes");
            JDBCDataSource.setBeanProperty(ds, "user", "jon");
            JDBCDataSource.setBeanProperty(ds, "password", "theOwner");
            Connection jon = ds.getConnection();

            Statement jonStm = jon.createStatement();
            // Still, no privilege available for poor jon..
            assertStatementError
                ("42502", jonStm, "select * from garfield.cats");

            jonStm.execute("set role foo");
            // Now, though:
            jonStm.execute("select * from garfield.cats");
            jonStm.close();
            jon.close();

            s.execute("drop table cats");
            s.execute("drop role foo");
            conn.close();

            println("Roles work after hard upgrade");

            // Owner garfield shuts down
            JDBCDataSource.setBeanProperty(ds, "user", "garfield");
            JDBCDataSource.setBeanProperty(ds, "password", "theCat");
            JDBCDataSource.shutdownDatabase(ds);
            break;
        }
    }
}