KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > derbynet > SqlExceptionTest


1 /*
2    Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.SqlExceptionTest
3  
4    Licensed to the Apache Software Foundation (ASF) under one or more
5    contributor license agreements. See the NOTICE file distributed with
6    this work for additional information regarding copyright ownership.
7    The ASF licenses this file to You under the Apache License, Version 2.0
8    (the "License"); you may not use this file except in compliance with
9    the License. You may obtain a copy of the License at
10  
11       http://www.apache.org/licenses/LICENSE-2.0
12  
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18  
19  */

20 package org.apache.derbyTesting.functionTests.tests.derbynet;
21
22 import org.apache.derbyTesting.junit.BaseTestCase;
23 import org.apache.derby.client.am.SqlException;
24 import org.apache.derby.client.am.ClientMessageId;
25 import org.apache.derby.shared.common.reference.SQLState;
26 import java.sql.SQLException JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 /**
30  * This is used for testing the SqlException class. This test can be added
31  * to. My itch right now is to verify that exception chaining is working
32  * correctly.
33  */

34
35 public class SqlExceptionTest extends BaseTestCase
36 {
37     public SqlExceptionTest(String JavaDoc name)
38     {
39         super(name);
40     }
41     
42     /**
43      * Makes sure exception chaining works correctly (DERBY-1117)
44      */

45     public void testChainedException() {
46         IOException JavaDoc ioe = new IOException JavaDoc("Test exception");
47         SqlException sqle = new SqlException(null,
48             new ClientMessageId(SQLState.NOGETCONN_ON_CLOSED_POOLED_CONNECTION),
49             ioe);
50         SQLException JavaDoc javae = sqle.getSQLException();
51         
52         // The underlying SqlException is the first cause; the IOException
53
// should be the second cause
54
assertEquals(sqle, javae.getCause());
55         assertEquals(ioe, javae.getCause().getCause());
56         assertNull(sqle.getNextException());
57     }
58     
59     /**
60      * Make sure a SQLException is chained as a nextSQLException()
61      * rather than as a chained exception
62      */

63     public void testNextException() {
64         SQLException JavaDoc nexte = new SQLException JavaDoc("test");
65         SqlException sqle = new SqlException(null,
66             new ClientMessageId(SQLState.NOGETCONN_ON_CLOSED_POOLED_CONNECTION),
67             nexte);
68         SQLException JavaDoc javae = sqle.getSQLException();
69         
70         assertEquals(sqle, javae.getCause());
71         assertNull(javae.getCause().getCause());
72         assertEquals(nexte, javae.getNextException());
73         
74         // Make sure exception chaining works with Derby's SqlException
75
// just as well as java.sql.SQLException
76
SqlException internalException =
77             new SqlException(null,
78                 new ClientMessageId("08000"));
79         
80         javae = new SqlException(null,
81             new ClientMessageId(SQLState.NOGETCONN_ON_CLOSED_POOLED_CONNECTION),
82             internalException).getSQLException();
83         
84         assertNotNull(javae.getNextException());
85         assertEquals(javae.getNextException().getSQLState(), "08000");
86     }
87 }
88
Popular Tags