File: StatementKeyFactoryTest.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 (176 lines) | stat: -rw-r--r-- 7,277 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
/*

   Derby - Class org.apache.derbyTesting.unitTests.junit.StatementKeyFactoryTest

   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.unitTests.junit;

import java.sql.ResultSet;
import java.sql.Statement;
import junit.framework.Test;
import org.apache.derby.client.am.stmtcache.StatementKey;
import org.apache.derby.client.am.stmtcache.StatementKeyFactory;
import org.apache.derbyTesting.junit.BaseTestCase;
import org.apache.derbyTesting.junit.TestConfiguration;

/**
 * Test that statement key equality is correct, and that the factory produces
 * correct keys.
 * <p>
 * Objects implementing {@link StatementKey} is crucial for correct
 * operation of the JDBC statement object cache.
 * 
 * @see org.apache.derby.client.am.stmtcache.JDBCStatementCache
 */
public class StatementKeyFactoryTest
        extends BaseTestCase {

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

    /**
     * Creating keys with <code>null</code> for required information should
     * fail, as it can lead to NPEs in the key implementations and/or the wrong
     * statement to be fetched from the cache.
     */
    public void testCreationBasicWithNulls() {
        try {
            StatementKeyFactory.newPrepared(null, null, 0);
            fail("Creation with <null> should have failed");
        } catch (IllegalArgumentException iae) {
            // As expected
        }
        try {
            StatementKeyFactory.newPrepared(null, "app", 0);
            fail("Creation with <null> should have failed");
        } catch (IllegalArgumentException iae) {
            // As expected
        }
        try {
            StatementKeyFactory.newPrepared("values 1", null, 0);
            fail("Creation with <null> should have failed");
        } catch (IllegalArgumentException iae) {
            // As expected
        }
    }

    public void testCreationBasic() {
        StatementKey stdKey = StatementKeyFactory.newPrepared("values 1", "APP", 
                                            ResultSet.HOLD_CURSORS_OVER_COMMIT);
        StatementKey key = StatementKeyFactory.newPrepared(
                "select * from sys.systables", "APP", 
                                            ResultSet.HOLD_CURSORS_OVER_COMMIT);
        assertFalse(key.equals(stdKey));
        assertFalse(stdKey.equals(key));
    }

    public void testEqualityBasic() {
        StatementKey key1 = StatementKeyFactory.newPrepared(
                "select * from sys.systables", "APP", 
                                            ResultSet.HOLD_CURSORS_OVER_COMMIT);
        StatementKey key2 = StatementKeyFactory.newPrepared(
                "select * from sys.systables", "APP", 
                                            ResultSet.HOLD_CURSORS_OVER_COMMIT);
        StatementKey key3 = StatementKeyFactory.newPrepared(
                "select * from sys.systables", "APP", 
                                            ResultSet.HOLD_CURSORS_OVER_COMMIT);
        assertTrue(key1.equals(key2));
        assertTrue(key2.equals(key1));
        assertTrue(key2.equals(key3));
        assertTrue(key1.equals(key3));
    }

    public void testEqualityDefaultNoAutoGenKey() {
        int holdability = ResultSet.HOLD_CURSORS_OVER_COMMIT;
        StatementKey basicKey = StatementKeyFactory.newPrepared(
                "values 2", "APP", holdability);
        StatementKey simplifiedKey = StatementKeyFactory.newPrepared(
                "values 2", "APP", holdability, Statement.NO_GENERATED_KEYS);
        assertTrue(basicKey.equals(simplifiedKey));
        assertTrue(simplifiedKey.equals(basicKey));
    }

    public void testEqualityNoAutoVsAutoGenKey() {
        int holdability = ResultSet.HOLD_CURSORS_OVER_COMMIT;
        StatementKey basicKey = StatementKeyFactory.newPrepared(
                "values 2", "APP", holdability);
        StatementKey autoKey = StatementKeyFactory.newPrepared(
                "values 2", "APP", holdability, Statement.RETURN_GENERATED_KEYS);
        assertFalse(basicKey.equals(autoKey));
        assertFalse(autoKey.equals(basicKey));
    }

    public void testUnequalityVarious() {
        String sql = "select * from sys.systables";
        String schema = "APP";
        int rsh = ResultSet.HOLD_CURSORS_OVER_COMMIT;
        int rst = ResultSet.TYPE_SCROLL_INSENSITIVE;
        int rsc = ResultSet.CONCUR_UPDATABLE;
        int auto = Statement.RETURN_GENERATED_KEYS;
        // Create a one key of each type, all different from each other.
        StatementKey[] keys = new StatementKey[] {
            StatementKeyFactory.newPrepared(sql, schema, rsh),
            StatementKeyFactory.newPrepared(sql, schema, rsh, auto),
            StatementKeyFactory.newPrepared(sql, schema, rst, rsc, rsh),
            StatementKeyFactory.newCallable(sql, schema, rsh),
            StatementKeyFactory.newCallable(sql, schema, rst, rsc, rsh)};
        for (int outer=0; outer < keys.length; outer++) {
            StatementKey current = keys[outer];
            for (int inner=0; inner < keys.length; inner++) {
                if (outer != inner) {
                    if (current.equals(keys[inner])) {
                        fail("[" + current.toString() + "] should not equal [" +
                                keys[inner].toString() + "]");
                    }
                } else {
                    // Should equal itself.
                    assertTrue(current.equals(keys[inner]));
                }
            }
        }
    }

    public void testCallableVsPrepared() {
        String sql = "select colA, colB from mytable";
        String schema = "SOMEAPP";
        int holdability = ResultSet.HOLD_CURSORS_OVER_COMMIT;
        StatementKey callable =
                StatementKeyFactory.newCallable(sql, schema, holdability);
        StatementKey prepared =
                StatementKeyFactory.newPrepared(sql, schema, holdability);
        assertFalse(callable.equals(prepared));
        assertFalse(prepared.equals(callable));
    }

    /**
     * Returns the appropriate tests.
     * <p>
     * Run only client/server, because the code being tested does not live
     * in the embedded driver (yet).
     * 
     * @return A suite of tests (may be empty).
     */
    public static Test suite() {
        // Run only client/server, because the code being tested does not live
        // in the embedded driver (yet).
        return TestConfiguration.clientServerSuite(
                StatementKeyFactoryTest.class);
    }
}