1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.apache.harmony.sql.tests.javax.sql;
19
20import java.io.Serializable;
21import java.sql.SQLException;
22import javax.sql.ConnectionEvent;
23
24import org.apache.harmony.testframework.serialization.SerializationTest;
25import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
26
27import junit.framework.TestCase;
28
29public class ConnectionEventTest extends TestCase {
30
31    public void testConstructorConnection() {
32        try {
33            new ConnectionEvent(null);
34            fail("illegal argument exception expected");
35        } catch (IllegalArgumentException e) {
36        }
37
38        Impl_PooledConnection ipc = new Impl_PooledConnection();
39        ConnectionEvent ce = new ConnectionEvent(ipc);
40        assertSame(ipc, ce.getSource());
41        assertNull(ce.getSQLException());
42    }
43
44    public void testConstructorConnectionSQLException() {
45        try {
46            new ConnectionEvent(null, null);
47            fail("illegal argument exception expected");
48        } catch (IllegalArgumentException e) {
49        }
50
51        Impl_PooledConnection ipc = new Impl_PooledConnection();
52        ConnectionEvent ce = new ConnectionEvent(ipc, null);
53        assertSame(ipc, ce.getSource());
54        assertNull(ce.getSQLException());
55
56        SQLException e = new SQLException();
57        ce = new ConnectionEvent(ipc, e);
58        assertSame(ipc, ce.getSource());
59        assertSame(e, ce.getSQLException());
60    }
61
62    /**
63     * @tests serialization/deserialization compatibility.
64     */
65    public void testSerializationSelf() throws Exception {
66        Impl_PooledConnection ipc = new Impl_PooledConnection();
67        SQLException e = new SQLException();
68        ConnectionEvent ce = new ConnectionEvent(ipc, e);
69        SerializationTest.verifySelf(ce, CONNECTIONEVENT_COMPARATOR);
70    }
71
72    /**
73     * @tests serialization/deserialization compatibility with RI.
74     */
75    public void testSerializationCompatibility() throws Exception {
76        Impl_PooledConnection ipc = new Impl_PooledConnection();
77        SQLException nextSQLException = new SQLException("nextReason",
78                "nextSQLState", 33);
79
80        int vendorCode = 10;
81        SQLException sqlException = new SQLException("reason", "SQLState",
82                vendorCode);
83
84        sqlException.setNextException(nextSQLException);
85
86        ConnectionEvent ce = new ConnectionEvent(ipc, sqlException);
87
88        SerializationTest.verifyGolden(this, ce, CONNECTIONEVENT_COMPARATOR);
89    }
90
91    private static final SerializableAssert CONNECTIONEVENT_COMPARATOR = new SerializableAssert() {
92
93        public void assertDeserialized(Serializable initial,
94                Serializable deserialized) {
95            ConnectionEvent ceInitial = (ConnectionEvent) initial;
96            ConnectionEvent ceDeser = (ConnectionEvent) deserialized;
97
98            SQLException initThr = ceInitial.getSQLException();
99            SQLException dserThr = ceDeser.getSQLException();
100
101            // verify SQLState
102            assertEquals(initThr.getSQLState(), dserThr.getSQLState());
103
104            // verify vendorCode
105            assertEquals(initThr.getErrorCode(), dserThr.getErrorCode());
106
107            // verify next
108            if (initThr.getNextException() == null) {
109                assertNull(dserThr.getNextException());
110            }
111        }
112
113    };
114}
115