001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 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;
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.DataSource;
038:
039: import net.sf.hajdbc.local.LocalStateManager;
040:
041: import org.testng.annotations.AfterClass;
042: import org.testng.annotations.BeforeClass;
043: import org.testng.annotations.DataProvider;
044: import org.testng.annotations.Test;
045:
046: /**
047: * Unit test for {@link DataSourceFactory}.
048: *
049: * @author Paul Ferraro
050: * @since 1.1
051: */
052: @SuppressWarnings("nls")
053: public class TestDataSourceFactory implements ObjectFactory {
054: private DataSourceFactory factory = new DataSourceFactory();
055: private Context context;
056:
057: @BeforeClass
058: protected void setUp() throws Exception {
059: Preferences.userNodeForPackage(LocalStateManager.class).put(
060: "test-datasource-cluster", "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(DataSourceFactory.class
070: .toString(), MockDataSourceFactory.class.getName(),
071: null);
072:
073: this .context.bind("datasource1", reference);
074: this .context.bind("datasource2", reference);
075:
076: this .context.bind("datasource", new DataSourceReference(
077: "test-datasource-cluster"));
078: }
079:
080: @AfterClass
081: protected void tearDown() throws Exception {
082: this .context.unbind("datasource");
083:
084: this .context.unbind("datasource1");
085: this .context.unbind("datasource2");
086:
087: Preferences.userNodeForPackage(LocalStateManager.class).remove(
088: "test-datasource-cluster");
089: }
090:
091: @DataProvider(name="factory")
092: Object[][] objectInstanceProvider() {
093: return new Object[][] {
094: new Object[] { null, null, null, null },
095: new Object[] { new Object(), null, null, null },
096: new Object[] {
097: new Reference(DataSource.class.getName(),
098: new StringRefAddr("cluster",
099: "test-datasource-cluster")),
100: null, null, null },
101: new Object[] {
102: new Reference(DataSource.class.getName(),
103: new StringRefAddr("cluster", null)),
104: null, null, null },
105: new Object[] {
106: new Reference(Driver.class.getName(),
107: new StringRefAddr("cluster",
108: "test-datasource-cluster")),
109: null, null, null },
110: new Object[] {
111: new Reference(DataSource.class.getName()),
112: null, null, null },
113: new Object[] {
114: new Reference(DataSource.class.getName(),
115: new StringRefAddr("cluster",
116: "invalid-cluster")), null,
117: null, null } };
118: }
119:
120: /**
121: * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
122: */
123: @Test(dataProvider="factory")
124: public Object getObjectInstance(Object obj, Name name,
125: Context nameCtx, Hashtable<?, ?> environment) {
126: try {
127: Object result = this .factory.getObjectInstance(obj, name,
128: nameCtx, environment);
129:
130: if ((obj == null) || !Reference.class.isInstance(obj)) {
131: assert result == null;
132:
133: return result;
134: }
135:
136: Reference reference = (Reference) obj;
137:
138: if (!reference.getClassName().equals(
139: DataSource.class.getName())) {
140: assert result == null;
141:
142: return result;
143: }
144:
145: RefAddr addr = reference.get("cluster");
146:
147: if ((addr == null) || (addr.getContent() == null)) {
148: assert result == null;
149:
150: return result;
151: }
152:
153: String id = (String) addr.getContent();
154:
155: if ((id == null) || !id.equals("test-datasource-cluster")) {
156: assert result == null;
157: } else {
158: assert result != null;
159: assert Proxy.isProxyClass(result.getClass()) : result
160: .getClass().getName();
161: }
162:
163: return result;
164: } catch (SQLException e) {
165: assert ((Reference) obj).get("cluster").getContent()
166: .equals("invalid-cluster");
167:
168: return null;
169: } catch (Exception e) {
170: assert false : e;
171:
172: return null;
173: }
174: }
175: }
|