001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.sql.connection.test;
019:
020: import java.sql.Connection;
021:
022: import javax.sql.DataSource;
023:
024: import org.sape.carbon.core.component.Lookup;
025: import org.sape.carbon.services.sql.connection.ConnectionFactory;
026:
027: import junit.extensions.ActiveTestSuite;
028: import junit.framework.Test;
029: import junit.framework.TestCase;
030: import junit.framework.TestSuite;
031:
032: public class NamedDataSourceConnectionFactoryTest extends TestCase {
033: public NamedDataSourceConnectionFactoryTest(String name) {
034: super (name);
035: }
036:
037: /**
038: * Test establishing a connection to a data source
039: */
040: public void testObtainConnection() throws Exception {
041: Connection conn = null;
042: try {
043: ConnectionFactory connFac = (ConnectionFactory) Lookup
044: .getInstance().fetchComponent(
045: NAMED_DATASOURCE_CONNECTION_FACTORY);
046: conn = connFac.getConnection();
047: super .assertNotNull(
048: "Null connection returned from DataSource at: "
049: + "[" + NAMED_DATASOURCE_CONNECTION_FACTORY
050: + "]", conn);
051: } finally {
052: if (null != conn) {
053: conn.close();
054: }
055: }
056: }
057:
058: /**
059: * Test establishing a connection to a data source, overriding the
060: * configured dataSourceUserId.
061: */
062: public void testOverriddenUserId() throws Exception {
063: Connection conn = null;
064: try {
065: DataSource ds = (DataSource) Lookup
066: .getInstance()
067: .fetchComponent(NAMED_DATASOURCE_CONNECTION_FACTORY);
068: conn = ds.getConnection("j2ee", "j2ee");
069: super .assertNotNull(
070: "Null connection returned from DataSource at: "
071: + "[" + NAMED_DATASOURCE_CONNECTION_FACTORY
072: + "]", conn);
073: } finally {
074: if (null != conn) {
075: conn.close();
076: }
077: }
078: }
079:
080: public static final String NAMED_DATASOURCE_CONNECTION_FACTORY = "/sql/connection/test/NamedDataSourceConnectionFactory";
081:
082: /**
083: * Method called by jUnit to get all the tests in this test case.
084: * @return Test the suite of tests in this test case
085: */
086: public static Test suite() {
087: TestSuite masterSuite = new TestSuite();
088: // add single threaded tests
089: Test singleThreadedTests = getSingleThreadedTests();
090: if (singleThreadedTests != null) {
091: masterSuite.addTest(singleThreadedTests);
092: }
093: // add multi threaded tests
094: Test multiThreadedTests = getMultiThreadedTests();
095: if (multiThreadedTests != null) {
096: masterSuite.addTest(multiThreadedTests);
097: }
098: return masterSuite;
099: }
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: }
|