File: StatementEventsTest.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 (624 lines) | stat: -rw-r--r-- 22,945 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
/*
 
   Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.StatementEventsTest
 
   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.jdbc4;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLNonTransientConnectionException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Enumeration;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;
import javax.sql.StatementEvent;
import javax.sql.StatementEventListener;
import javax.sql.XADataSource;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.J2EEDataSource;
import org.apache.derbyTesting.junit.TestConfiguration;

/*
    This class is used to test the JDBC4 statement event 
    support 
*/
public class StatementEventsTest extends BaseJDBCTestCase {

    /**
     * Type of data source to use. If <code>true</code>, use
     * <code>XADataSource</code>; otherwise, use
     * <code>ConnectionPoolDataSource</code>.
     */
    private boolean xa;
    /**
     * Type of statement to use. If <code>true</code>, use
     * <code>CallableStatement</code>; otherwise, use
     * <code>PreparedStatement</code>.
     */
    private boolean callable;

    /** The statement that caused the last statementClosed event. */
    private Statement closedStatement;
    /** Number of times statementClosed events have been raised. */
    private int closedCount;
    /** The statement that caused the last statementError event. */
    private Statement errorStatement;
    /** Number of times statementError events have been raised. */
    private int errorCount;

    /**
     * The pooled connection to use in the test (could also be an XA
     * connection).
     */
    private PooledConnection pooledConnection;
    /** The connection object to use in the test. */
    private Connection connection;

    /**
     * Create a test with the given name.
     *
     * @param name name of the test.
     */
    public StatementEventsTest(String name) {
        super(name);
    }

    /**
     * Set whether the test should use <code>XADataSource</code> or
     * <code>ConnectionPoolDataSource</code>.
     *
     * @param xa if <code>true</code>, use XA
     */
    private void setXA(boolean xa) {
        this.xa = xa;
    }

    /**
     * Set whether the test should use <code>CallableStatement</code> or
     * <code>PreparedStatement</code>.
     *
     * @param callable if <code>true</code>, use callable statement; otherwise,
     * use prepared statement
     */
    private void setCallable(boolean callable) {
        this.callable = callable;
    }

    /**
     * Return the name of the test.
     *
     * @return name of the test
     */
    public String getName() {
        return super.getName() + (xa ? "_xa" : "_pooled") +
            (callable ? "_callable" : "_prepared");
    }

    // TEST SETUP

    /**
     * Set up the connection to the database and register a statement event
     * listener.
     *
     * @exception SQLException if a database error occurs
     */
    public void setUp() throws SQLException {
        if (xa) {
            XADataSource ds = J2EEDataSource.getXADataSource();
            J2EEDataSource.setBeanProperty(ds, "createDatabase", "create");
            pooledConnection = ds.getXAConnection();
        } else {
            ConnectionPoolDataSource ds =
                J2EEDataSource.getConnectionPoolDataSource();
            J2EEDataSource.setBeanProperty(ds, "createDatabase", "create");
            pooledConnection = ds.getPooledConnection();
        }
        StatementEventListener listener = new StatementEventListener() {
                public void statementClosed(StatementEvent event) {
                    closedStatement = event.getStatement();
                    closedCount++;
                }
                public void statementErrorOccurred(StatementEvent event) {
                    errorStatement = event.getStatement();
                    errorCount++;
                }
            };
        pooledConnection.addStatementEventListener(listener);
        connection = pooledConnection.getConnection();
    }

    /**
     * Free resources used in the test.
     *
     * @exception Exception if an error occurs
     */
    protected void tearDown() throws Exception {
        connection.close();
        pooledConnection.close();
        connection = null;
        pooledConnection = null;
        closedStatement = null;
        errorStatement = null;
        super.tearDown();
    }

    /**
     * Return suite with all tests of the class for all combinations of
     * pooled/xa connection and prepared/callable statement.
     *
     * @param name name of the test suite
     * @return a test suite
     */
    private static Test baseSuite(String name) {
        BaseTestSuite suites = new BaseTestSuite(name);
        boolean[] truefalse = new boolean[] { true, false };
        for (boolean xa : truefalse) {
            for (boolean callable : truefalse) {
                suites.addTest(new Suite(xa, callable));
            }
        }
        return suites;
    }

    /** Create a test suite with all tests in the class. */
    public static Test suite() {
        BaseTestSuite suite = new BaseTestSuite("StatementEventsTest suite");
        suite.addTest(baseSuite("StatementEventsTest:embedded"));
        suite.addTest(TestConfiguration.clientServerDecorator(
              baseSuite("StatementEventsTest:client")));
        return suite;
    }

    /**
     * Test suite class which contains all test cases in
     * <code>StatementEventsTest</code> for a given configuration.
     */
    private static class Suite extends BaseTestSuite {
        private Suite(boolean xa, boolean callable) {
            super(StatementEventsTest.class);

            // Iterate over the sorted fixtures and tinker with them
            for (Enumeration<?> i = tests(); i.hasMoreElements(); ) {
                StatementEventsTest test =
                        (StatementEventsTest) i.nextElement();
                test.setXA(xa);
                test.setCallable(callable);
            }
        }
    }

    // UTILITIES

    /**
     * Prepare a statement.
     *
     * @param sql SQL text
     * @return a <code>PreparedStatement</code> or
     * <code>CallableStatement</code> object
     * @exception SQLException if a database error occurs
     */
    private PreparedStatement prepare(String sql) throws SQLException {
        if (callable) {
            return connection.prepareCall(sql);
        }
        return connection.prepareStatement(sql);
    }

    // TEST CASES

    /**
     * Test that a close event is raised when a statement is closed.
     *
     * @exception SQLException if a database error occurs
     */
    public void testCloseEvent() throws SQLException {
        PreparedStatement ps = prepare("VALUES (1)");
        ps.close();
        assertSame("Close event raised on wrong statement.",
                   ps, closedStatement);
        assertEquals("Incorrect close count.", 1, closedCount);
    }

    /**
     * Test that you don't get a NullPointerException when the listeners are
     * triggered and one of them is null. DERBY-3695
     */
    public void testAddNullListener() throws SQLException {
        pooledConnection.addStatementEventListener(null);
        PreparedStatement ps = prepare("VALUES (1)");
        ps.close(); // trigger close event
        assertEquals(1, closedCount);
        assertEquals(0, errorCount);
        ps = prepare("VALUES (1)");
        connection.close();
        try {
            ps.execute();
            fail("Execute should fail on closed connection");
        } catch (SQLNonTransientConnectionException e) {
            assertSQLState("08003", e);
        }
        assertEquals(1, errorCount);
    }

    /**
     * Test that you can call {@code removeStatementEventListener()} with a
     * {@code null} argument.
     */
    public void testRemoveNullListener() throws SQLException {
        pooledConnection.removeStatementEventListener(null);
    }

    /**
     * Test how the listener behaves if it is added twice.
     */
    public void testDoubleAddListener() throws SQLException {
        SimpleListener listener = new SimpleListener();
        pooledConnection.addStatementEventListener(listener);
        pooledConnection.addStatementEventListener(listener);

        // Generate a close event.
        prepare("VALUES (1)").close();

        // The listener will have been invoked twice.
        assertEquals(2, listener.getCloseCount());
        assertEquals(0, listener.getErrorCount());

        listener.resetCounts();

        // Generate an error event.
        PreparedStatement ps = prepare("VALUES (1)");
        connection.close();
        assertStatementError("08003", ps);

        // The listener will have been invoked twice with an error event.
        // On the client, it will also have been invoked twice with a close
        // event - see DERBY-3851.
        assertEquals(2, listener.getErrorCount());
        assertEquals(usingDerbyNetClient() ? 2 : 0, listener.getCloseCount());
    }

    /**
     * Test whether a close event is raised when a connection is
     * closed. (Client should raise a close event since the connection calls
     * <code>close()</code> on its statements. Embedded should not raise a
     * close event since the connection does not call <code>close()</code> on
     * its statements.)
     *
     * @exception SQLException if a database error occurs
     */
    public void testCloseEventOnClosedConnection() throws SQLException {
        PreparedStatement ps = prepare("VALUES (1)");
        connection.close();
        if (usingDerbyNetClient()) {
            assertSame("Close event raised on wrong statement.",
                       ps, closedStatement);
            assertEquals("Incorrect close count.", 1, closedCount);
        } else if (usingEmbedded()) {
            assertNull("Didn't expect close event.", closedStatement);
            assertEquals("Incorrect close count.", 0, closedCount);
        } else {
            fail("Unknown framework.");
        }
    }

    /**
     * Test that an error event is raised when <code>execute()</code> fails
     * because the connection is closed.
     *
     * @exception SQLException if a database error occurs
     */
    public void testErrorEventOnClosedConnection() throws SQLException {
        PreparedStatement ps = prepare("VALUES (1)");
        connection.close();
        try {
            ps.execute();
            fail("No exception thrown.");
        } catch (SQLException e) {
            assertSQLState("Unexpected SQL state.", "08003", e);
            assertSame("Error event raised on wrong statement.",
                       ps, errorStatement);
            assertEquals("Incorrect error count.", 1, errorCount);
        }
    }

    /**
     * Test that removing a listener from a listener works. (DERBY-3401)
     */
    public void testRemoveListenerFromListener() throws SQLException {

        // First element is number of times the close listeners below have
        // been triggered, second element is number of times the error
        // listeners have been triggered.
        final int[] counters = new int[2];

        // Add some listeners that remove themselves
        for (int i = 0; i < 5; i++) {
            StatementEventListener close = new StatementEventListener() {

                public void statementClosed(StatementEvent event) {
                    pooledConnection.removeStatementEventListener(this);
                    counters[0]++;
                }

                public void statementErrorOccurred(StatementEvent event) {
                }
            };
            pooledConnection.addStatementEventListener(close);

            StatementEventListener error = new StatementEventListener() {

                public void statementClosed(StatementEvent event) {
                }

                public void statementErrorOccurred(StatementEvent event) {
                    pooledConnection.removeStatementEventListener(this);
                    counters[1]++;
                }
            };
            pooledConnection.addStatementEventListener(error);
        }

        // Generate close event twice. The close listeners remove themselves
        // in the first iteration, so no updates of the counters are expected
        // in the second iteration.
        for (int i = 0; i < 2; i++) {
            prepare("VALUES (1)").close();
            assertEquals("unexpected number of close events", 5, counters[0]);
            assertEquals("unexpected number of error events", 0, counters[1]);
        }

        // reset counters
        Arrays.fill(counters, 0);

        // Generate error event twice. Only expect counters to be updated in
        // the first iteration since the listeners remove themselves.
        for (int i = 0; i < 2; i++) {
            PreparedStatement ps = prepare("VALUES (1)");
            connection.close();
            try {
                ps.execute();
                fail("Execute on closed connection should fail");
            } catch (SQLNonTransientConnectionException e) {
                assertSQLState("08003", e);
            }
            assertEquals("unexpected number of close events", 0, counters[0]);
            assertEquals("unexpected number of error events", 5, counters[1]);
            connection = pooledConnection.getConnection();
        }

        // The listeners that are automatically added for all test cases have
        // been active all the time.
        assertEquals("Incorrect error count", 2, errorCount);
        // Embedded doesn't receive close events when the connection is
        // closed, whereas the client driver does. This is therefore an
        // expected difference.
        if (usingEmbedded()) {
            assertEquals("Incorrect close count", 2, closedCount);
        } else if (usingDerbyNetClient()) {
            assertEquals("Incorrect close count", 4, closedCount);
        } else {
            fail("unknown framework");
        }
    }

    /**
     * Test that adding a listener from a listener works. (DERBY-3401)
     */
    public void testAddListenerFromListener() throws SQLException {

        // First element is number of times the close listeners below have
        // been triggered, second element is number of times the error
        // listeners have been triggered. Third element is the number of
        // times listeners added by close listeners have been triggered,
        // fourth element is the number of times listeners added by error
        // listeners have been triggered.
        final int[] counters = new int[4];

        // Add some listeners that add another listener
        for (int i = 0; i < 5; i++) {
            StatementEventListener close = new StatementEventListener() {

                public void statementClosed(StatementEvent event) {
                    counters[0]++;
                    pooledConnection.addStatementEventListener(
                            new StatementEventListener() {
                        public void statementClosed(StatementEvent e) {
                            counters[2]++;
                        }
                        public void statementErrorOccurred(StatementEvent e) {
                            counters[2]++;
                        }
                    });
                }

                public void statementErrorOccurred(StatementEvent event) {
                }
            };

            pooledConnection.addStatementEventListener(close);

            StatementEventListener error = new StatementEventListener() {

                public void statementClosed(StatementEvent event) {
                }

                public void statementErrorOccurred(StatementEvent event) {
                    counters[1]++;
                    pooledConnection.addStatementEventListener(
                            new StatementEventListener() {
                        public void statementClosed(StatementEvent e) {
                            counters[3]++;
                        }
                        public void statementErrorOccurred(StatementEvent e) {
                            counters[3]++;
                        }
                    });
                }
            };

            pooledConnection.addStatementEventListener(error);
        }

        // Generate close event
        prepare("VALUES (1)").close();
        assertEquals("unexpected number of close events", 5, counters[0]);
        assertEquals("unexpected number of error events", 0, counters[1]);
        assertEquals("unexpected number of added close listeners triggered",
                     0, counters[2]);
        assertEquals("unexpected number of added error listeners triggered",
                     0, counters[3]);

        // Generate another close event
        prepare("VALUES (1)").close();
        assertEquals("unexpected number of close events", 10, counters[0]);
        assertEquals("unexpected number of error events", 0, counters[1]);
        assertEquals("unexpected number of added close listeners triggered",
                     5, counters[2]);
        assertEquals("unexpected number of added error listeners triggered",
                     0, counters[3]);

        // Generate a statement that doesn't work
        PreparedStatement ps = prepare("VALUES (1)");
        connection.close();
        // reset counters
        Arrays.fill(counters, 0);

        // Generate an error event
        try {
            ps.execute();
            fail("Execute on closed connection should fail");
        } catch (SQLNonTransientConnectionException e) {
            assertSQLState("08003", e);
        }

        assertEquals("unexpected number of close events", 0, counters[0]);
        assertEquals("unexpected number of error events", 5, counters[1]);
        // difference between embedded and client because client gets
        // statement-closed event when the connection is closed, whereas
        // embedded doesn't
        assertEquals("unexpected number of added close listeners triggered",
                     usingEmbedded() ? 10 : 15, counters[2]);
        assertEquals("unexpected number of added error listeners triggered",
                     0, counters[3]);

        // reset counters
        Arrays.fill(counters, 0);

        // Generate another error event, now with more listeners active
        try {
            ps.execute();
            fail("Execute on closed connection should fail");
        } catch (SQLNonTransientConnectionException e) {
            assertSQLState("08003", e);
        }

        assertEquals("unexpected number of close events", 0, counters[0]);
        assertEquals("unexpected number of error events", 5, counters[1]);
        // difference between embedded and client because client gets
        // statement-closed event when the connection is closed, whereas
        // embedded doesn't
        assertEquals("unexpected number of added close listeners triggered",
                     usingEmbedded() ? 10 : 15, counters[2]);
        assertEquals("unexpected number of added error listeners triggered",
                     5, counters[3]);

        // The listeners that are automatically added for all test cases have
        // been active all the time.
        assertEquals("Incorrect error count", 2, errorCount);
        // Embedded doesn't receive close events when the connection is
        // closed, whereas the client driver does. This is therefore an
        // expected difference.
        if (usingEmbedded()) {
            assertEquals("Incorrect close count", 2, closedCount);
        } else if (usingDerbyNetClient()) {
            assertEquals("Incorrect close count", 3, closedCount);
        } else {
            fail("unknown framework");
        }
    }

    /**
     * Test how {@code addStatementEventListener()} and
     * {@code removeStatementEventListener()} behave when they are called
     * on a closed {@code PooledConnection} or {@code XAConnection}.
     */
    public void testAddRemoveListenerOnClosedObject() throws SQLException {
        // Add a listener that we can try to remove later after closing.
        SimpleListener listener = new SimpleListener();
        pooledConnection.addStatementEventListener(listener);

        // Close the PooledConnection or XAConnection.
        pooledConnection.close();

        // Remove the listener and add a new one. No exception is thrown.
        pooledConnection.removeStatementEventListener(listener);
        pooledConnection.addStatementEventListener(new SimpleListener());
    }

    /**
     * Test that removing a listener that hasn't been added first, doesn't
     * cause any errors.
     */
    public void testRemoveListenerNotAddedFirst() throws SQLException {
        pooledConnection.removeStatementEventListener(new SimpleListener());

        // Verify that the listener that was there (added in setUp()) is
        // still there.
        prepare("VALUES (1)").close();
        assertEquals(1, closedCount);
        assertEquals(0, errorCount);
    }

    /**
     * A simple {@code StatementEventListener} that counts the number of
     * times it has been triggered.
     */
    private static class SimpleListener implements StatementEventListener {
        private int closeCount;
        private int errorCount;

        @Override
        public void statementClosed(StatementEvent event) {
            closeCount++;
        }

        @Override
        public void statementErrorOccurred(StatementEvent event) {
            errorCount++;
        }

        private int getCloseCount() {
            return closeCount;
        }

        private int getErrorCount() {
            return errorCount;
        }

        private void resetCounts() {
            closeCount = 0;
            errorCount = 0;
        }
    }
}