KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > sql > connection > test > StandaloneConnectionFactoryTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.sql.connection.test;
19
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 import org.sape.carbon.core.component.Lookup;
24 import org.sape.carbon.services.sql.connection.ConnectionFactory;
25
26 import junit.extensions.ActiveTestSuite;
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30
31 public class StandaloneConnectionFactoryTest extends TestCase {
32     public StandaloneConnectionFactoryTest(String JavaDoc name){
33         super(name);
34     }
35
36     /**
37     * Test establishing a connection to a data source
38     */

39     public void testObtainConnection() throws Exception JavaDoc {
40         Connection JavaDoc conn = null;
41         try {
42            ConnectionFactory connFac =
43                (ConnectionFactory) Lookup.getInstance().fetchComponent(
44                    STANDALONE_CONNECTION_FACTORY);
45            conn = connFac.getConnection();
46            super.assertNotNull("Null connection returned from config at: "+
47                 "["+STANDALONE_CONNECTION_FACTORY+"]", conn);
48         }
49         finally{
50             if(null != conn){
51                 conn.close();
52             }
53         }
54     }
55     
56     /**
57      * tests retrieving connections from a connection factory configured with additional properties
58      * Since every driver has a different additional properties which can be set for the driver,
59      * there is no easy way to have the same test case which can work with all databases. This test case
60      * configures a invalid password for the connection and hence retrieve connection will fail
61      */

62     public void testObtainConnectionWithAdditionalProperties() throws Exception JavaDoc {
63         Connection JavaDoc conn = null;
64         try {
65             ConnectionFactory factory =
66                 (ConnectionFactory)Lookup.getInstance().fetchComponent(STANDALONE_CONNECTION_FACTORY_WITH_PROPERTIES);
67             factory.getConnection();
68             fail("Retrieve connection should have failed, as the connection factory has been "+
69                 " configured with invalid additional properties ");
70         } catch(SQLException JavaDoc se) {
71             //expected as the additional properties contains a invalid password
72
} finally {
73             if ( conn != null ) {
74                 conn.close();
75             }
76         }
77     }
78
79
80     public static final String JavaDoc STANDALONE_CONNECTION_FACTORY =
81         "/sql/connection/test/StandaloneConnectionFactory";
82
83     public static final String JavaDoc STANDALONE_CONNECTION_FACTORY_WITH_PROPERTIES =
84         "/sql/connection/test/StandaloneConnectionFactoryWithProperties";
85
86     /**
87      * Method called by jUnit to get all the tests in this test case.
88      * @return Test the suite of tests in this test case
89      */

90     public static Test suite() {
91         TestSuite masterSuite = new TestSuite();
92         // add single threaded tests
93
Test singleThreadedTests = getSingleThreadedTests();
94         if (singleThreadedTests != null) {
95             masterSuite.addTest(singleThreadedTests);
96         }
97         // add multi threaded tests
98
Test multiThreadedTests = getMultiThreadedTests();
99         if (multiThreadedTests != null) {
100             masterSuite.addTest(multiThreadedTests);
101         }
102         return masterSuite;
103     }
104
105     /**
106      * This method is used within the suite method to get all of the single threaded tests.
107      * Add all your single threaded tests in this method with a line like:
108      * suite.addTest(new SchedulerServiceTest("testFunction1"));
109      * @return Test the suite of single threaded tests in this test case
110      */

111     private static Test getSingleThreadedTests() {
112         TestSuite suite = new TestSuite();
113
114         suite.addTest(new StandaloneConnectionFactoryTest("testObtainConnection"));
115         suite.addTest(new StandaloneConnectionFactoryTest("testObtainConnectionWithAdditionalProperties"));
116
117         return suite;
118     }
119
120     /**
121      * This method is used within the suite method to get all of the multi threaded tests.
122      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
123      * @return Test the suite of multi-threaded tests in this test case
124      */

125     private static Test getMultiThreadedTests() {
126         TestSuite suite = new ActiveTestSuite();
127
128         /*
129          * add your tests here following these examples:
130          *
131          * addTest(suite, "testFunction1", 5);
132          * addTest(suite, "testFunction2", 10);
133          */

134
135         return suite;
136     }
137
138
139 }
140
Popular Tags