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.xa;
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.XADataSource;
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 TestXADataSourceFactory implements ObjectFactory {
052: private XADataSourceFactory factory = new XADataSourceFactory();
053: private Context context;
054:
055: @BeforeClass
056: protected void setUp() throws Exception {
057: Preferences.userNodeForPackage(LocalStateManager.class)
058: .put("test-xa-datasource-cluster",
059: "datasource1,datasource2");
060:
061: Properties properties = new Properties();
062:
063: properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
064: MockInitialContextFactory.class.getName());
065:
066: this .context = new InitialContext(properties);
067:
068: Reference reference = new Reference(XADataSourceFactory.class
069: .getName(), MockXADataSourceFactory.class.getName(),
070: null);
071:
072: this .context.bind("datasource1", reference);
073: this .context.bind("datasource2", reference);
074:
075: this .context.bind("datasource", new XADataSourceReference(
076: "test-xa-datasource-cluster"));
077: }
078:
079: @AfterClass
080: protected void tearDown() throws Exception {
081: this .context.unbind("datasource");
082:
083: this .context.unbind("datasource1");
084: this .context.unbind("datasource2");
085:
086: Preferences.userNodeForPackage(LocalStateManager.class).remove(
087: "test-xa-datasource-cluster");
088: }
089:
090: @DataProvider(name="factory")
091: Object[][] objectInstanceProvider() {
092: return new Object[][] {
093: new Object[] { null, null, null, null },
094: new Object[] { new Object(), null, null, null },
095: new Object[] {
096: new Reference(XADataSource.class.getName(),
097: new StringRefAddr("cluster",
098: "test-xa-datasource-cluster")),
099: null, null, null },
100: new Object[] {
101: new Reference(XADataSource.class.getName(),
102: new StringRefAddr("cluster", null)),
103: null, null, null },
104: new Object[] {
105: new Reference(Driver.class.getName(),
106: new StringRefAddr("cluster",
107: "test-xa-datasource-cluster")),
108: null, null, null },
109: new Object[] {
110: new Reference(XADataSource.class.getName()),
111: null, null, null },
112: new Object[] {
113: new Reference(XADataSource.class.getName(),
114: new StringRefAddr("cluster",
115: "invalid-cluster")), null,
116: null, null } };
117: }
118:
119: /**
120: * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
121: */
122: @Test(dataProvider="factory")
123: public Object getObjectInstance(Object obj, Name name,
124: Context nameCtx, Hashtable<?, ?> environment) {
125: try {
126: Object result = this .factory.getObjectInstance(obj, name,
127: nameCtx, environment);
128:
129: if ((obj == null) || !Reference.class.isInstance(obj)) {
130: assert result == null;
131:
132: return result;
133: }
134:
135: Reference reference = (Reference) obj;
136:
137: if (!reference.getClassName().equals(
138: XADataSource.class.getName())) {
139: assert result == null;
140:
141: return result;
142: }
143:
144: RefAddr addr = reference.get("cluster");
145:
146: if ((addr == null) || (addr.getContent() == null)) {
147: assert result == null;
148:
149: return result;
150: }
151:
152: String id = (String) addr.getContent();
153:
154: if ((id == null)
155: || !id.equals("test-xa-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: }
|