KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import javax.sql.DataSource JavaDoc;
23
24 import org.sape.carbon.core.component.Lookup;
25 import org.sape.carbon.services.sql.connection.ConnectionFactory;
26
27 import junit.extensions.ActiveTestSuite;
28 import junit.framework.Test;
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
32 public class NamedDataSourceConnectionFactoryTest extends TestCase {
33     public NamedDataSourceConnectionFactoryTest(String JavaDoc name){
34         super(name);
35     }
36
37     /**
38     * Test establishing a connection to a data source
39     */

40     public void testObtainConnection() throws Exception JavaDoc {
41         Connection JavaDoc conn = null;
42         try {
43            ConnectionFactory connFac =
44                (ConnectionFactory) Lookup.getInstance().fetchComponent(
45                    NAMED_DATASOURCE_CONNECTION_FACTORY);
46            conn = connFac.getConnection();
47            super.assertNotNull("Null connection returned from DataSource at: "+
48                 "["+NAMED_DATASOURCE_CONNECTION_FACTORY+"]", conn);
49         }
50         finally{
51             if(null != conn){
52                 conn.close();
53             }
54         }
55     }
56
57
58     /**
59     * Test establishing a connection to a data source, overriding the
60     * configured dataSourceUserId.
61     */

62     public void testOverriddenUserId() throws Exception JavaDoc {
63         Connection JavaDoc conn = null;
64         try {
65            DataSource JavaDoc ds =
66                (DataSource JavaDoc) Lookup.getInstance().fetchComponent(
67                    NAMED_DATASOURCE_CONNECTION_FACTORY);
68            conn = ds.getConnection("j2ee", "j2ee");
69            super.assertNotNull("Null connection returned from DataSource at: "+
70                 "["+NAMED_DATASOURCE_CONNECTION_FACTORY+"]", conn);
71         }
72         finally{
73             if(null != conn){
74                 conn.close();
75             }
76         }
77     }
78
79     public static final String JavaDoc NAMED_DATASOURCE_CONNECTION_FACTORY =
80         "/sql/connection/test/NamedDataSourceConnectionFactory";
81
82     /**
83      * Method called by jUnit to get all the tests in this test case.
84      * @return Test the suite of tests in this test case
85      */

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

107     private static Test getSingleThreadedTests() {
108         TestSuite suite = new TestSuite();
109
110         //Commented-out because test requires a running AppServer (for JNDI),
111
//with a configured DataSource, and running Database
112
//suite.addTest(
113
// new NamedDataSourceConnectionFactoryTest("testObtainConnection"));
114

115         //Uses hardcoded userid and password, that do not belong in the
116
//Component's config at any time. Tweak if you need to use this
117
//test on your JNDI/JDBC/DB setup.
118
//suite.addTest(
119
// new NamedDataSourceConnectionFactoryTest("testOverriddenUserId"));
120
return suite;
121     }
122
123     /**
124      * This method is used within the suite method to get all of the multi threaded tests.
125      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
126      * @return Test the suite of multi-threaded tests in this test case
127      */

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

137
138         return suite;
139     }
140
141
142 }
143
Popular Tags