001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2008 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql.pool;
022:
023: import java.lang.reflect.Proxy;
024: import java.sql.Driver;
025: import java.sql.SQLException;
026: import java.util.Hashtable;
027: import java.util.Properties;
028: import java.util.prefs.Preferences;
029:
030: import javax.naming.Context;
031: import javax.naming.InitialContext;
032: import javax.naming.Name;
033: import javax.naming.RefAddr;
034: import javax.naming.Reference;
035: import javax.naming.StringRefAddr;
036: import javax.naming.spi.ObjectFactory;
037: import javax.sql.ConnectionPoolDataSource;
038:
039: import net.sf.hajdbc.local.LocalStateManager;
040: import net.sf.hajdbc.sql.MockInitialContextFactory;
041:
042: import org.testng.annotations.AfterClass;
043: import org.testng.annotations.BeforeClass;
044: import org.testng.annotations.DataProvider;
045: import org.testng.annotations.Test;
046:
047: /**
048: * @author Paul Ferraro
049: *
050: */
051: public class TestConnectionPoolDataSourceFactory implements
052: ObjectFactory {
053: private ConnectionPoolDataSourceFactory factory = new ConnectionPoolDataSourceFactory();
054: private Context context;
055:
056: @BeforeClass
057: protected void setUp() throws Exception {
058: Preferences.userNodeForPackage(LocalStateManager.class).put(
059: "test-pool-datasource-cluster",
060: "datasource1,datasource2");
061:
062: Properties properties = new Properties();
063:
064: properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
065: MockInitialContextFactory.class.getName());
066:
067: this .context = new InitialContext(properties);
068:
069: Reference reference = new Reference(
070: ConnectionPoolDataSourceFactory.class.getName(),
071: MockConnectionPoolDataSourceFactory.class.getName(),
072: null);
073:
074: this .context.bind("datasource1", reference);
075: this .context.bind("datasource2", reference);
076:
077: this .context.bind("datasource",
078: new ConnectionPoolDataSourceReference(
079: "test-pool-datasource-cluster"));
080: }
081:
082: @AfterClass
083: protected void tearDown() throws Exception {
084: this .context.unbind("datasource");
085:
086: this .context.unbind("datasource1");
087: this .context.unbind("datasource2");
088:
089: Preferences.userNodeForPackage(LocalStateManager.class).remove(
090: "test-pool-datasource-cluster");
091: }
092:
093: @DataProvider(name="factory")
094: Object[][] objectInstanceProvider() {
095: return new Object[][] {
096: new Object[] { null, null, null, null },
097: new Object[] { new Object(), null, null, null },
098: new Object[] {
099: new Reference(ConnectionPoolDataSource.class
100: .getName(), new StringRefAddr(
101: "cluster",
102: "test-pool-datasource-cluster")), null,
103: null, null },
104: new Object[] {
105: new Reference(ConnectionPoolDataSource.class
106: .getName(), new StringRefAddr(
107: "cluster", null)), null, null, null },
108: new Object[] {
109: new Reference(Driver.class.getName(),
110: new StringRefAddr("cluster",
111: "test-pool-datasource-cluster")),
112: null, null, null },
113: new Object[] {
114: new Reference(ConnectionPoolDataSource.class
115: .getName()), null, null, null },
116: new Object[] {
117: new Reference(ConnectionPoolDataSource.class
118: .getName(), new StringRefAddr(
119: "cluster", "invalid-cluster")), null,
120: null, null } };
121: }
122:
123: /**
124: * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
125: */
126: @Test(dataProvider="factory")
127: public Object getObjectInstance(Object obj, Name name,
128: Context nameCtx, Hashtable<?, ?> environment) {
129: try {
130: Object result = this .factory.getObjectInstance(obj, name,
131: nameCtx, environment);
132:
133: if ((obj == null) || !Reference.class.isInstance(obj)) {
134: assert result == null;
135:
136: return result;
137: }
138:
139: Reference reference = (Reference) obj;
140:
141: if (!reference.getClassName().equals(
142: ConnectionPoolDataSource.class.getName())) {
143: assert result == null;
144:
145: return result;
146: }
147:
148: RefAddr addr = reference.get("cluster");
149:
150: if ((addr == null) || (addr.getContent() == null)) {
151: assert result == null;
152:
153: return result;
154: }
155:
156: String id = (String) addr.getContent();
157:
158: if ((id == null)
159: || !id.equals("test-pool-datasource-cluster")) {
160: assert result == null;
161: } else {
162: assert result != null;
163: assert Proxy.isProxyClass(result.getClass()) : result
164: .getClass().getName();
165: }
166:
167: return result;
168: } catch (SQLException e) {
169: assert ((Reference) obj).get("cluster").getContent()
170: .equals("invalid-cluster");
171:
172: return null;
173: } catch (Exception e) {
174: assert false : e;
175:
176: return null;
177: }
178: }
179: }
|