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;
022:
023: import javax.management.DynamicMBean;
024: import javax.naming.Context;
025: import javax.naming.InitialContext;
026: import javax.naming.NamingException;
027: import javax.naming.Reference;
028: import javax.naming.spi.ObjectFactory;
029: import javax.sql.CommonDataSource;
030:
031: import org.easymock.EasyMock;
032: import org.testng.annotations.AfterMethod;
033: import org.testng.annotations.DataProvider;
034: import org.testng.annotations.Test;
035:
036: /**
037: * @author Paul Ferraro
038: *
039: */
040: public abstract class TestCommonDataSourceDatabase<C extends CommonDataSourceDatabase<D>, D extends CommonDataSource>
041: extends AbstractTestDatabase<C, D> implements
042: InactiveDataSourceDatabaseMBean {
043: protected D dataSource;
044:
045: protected TestCommonDataSourceDatabase(D dataSource) {
046: this .dataSource = dataSource;
047: }
048:
049: protected Object[] objects() {
050: return new Object[] { this .dataSource };
051: }
052:
053: protected void replay() {
054: EasyMock.replay(this .objects());
055: }
056:
057: protected void verify() {
058: EasyMock.verify(this .objects());
059: }
060:
061: @AfterMethod
062: protected void reset() {
063: EasyMock.reset(this .objects());
064: }
065:
066: /**
067: * @see net.sf.hajdbc.sql.AbstractTestDatabase#createDatabase(java.lang.String)
068: */
069: @Override
070: protected final C createDatabase(String id) {
071: C database = this .createDatabase();
072:
073: database.setId(id);
074:
075: return database;
076: }
077:
078: protected abstract C createDatabase();
079:
080: @DataProvider(name="datasource")
081: Object[][] dataSourceProvider() {
082: return new Object[][] { new Object[] { this .dataSource } };
083: }
084:
085: /**
086: * @see net.sf.hajdbc.Database#createConnectionFactory()
087: */
088: @Test
089: public final D createConnectionFactory() {
090: C database = this .createDatabase("1");
091: database.setName("test");
092: database.setProperty(Context.INITIAL_CONTEXT_FACTORY,
093: MockInitialContextFactory.class.getName());
094:
095: try {
096: Reference reference = new Reference(this .dataSource
097: .getClass().getName(), this .objectFactoryClass()
098: .getName(), null);
099:
100: Context context = new InitialContext(database
101: .getProperties());
102:
103: context.rebind("test", reference);
104: } catch (NamingException e) {
105: assert false;
106: }
107:
108: D dataSource = database.createConnectionFactory();
109:
110: assert dataSource.getClass().equals(this .mockDataSourceClass()) : dataSource
111: .getClass();
112:
113: return dataSource;
114: }
115:
116: protected abstract Class<? extends ObjectFactory> objectFactoryClass();
117:
118: protected abstract Class<? extends D> mockDataSourceClass();
119:
120: /**
121: * @see net.sf.hajdbc.Database#getActiveMBean()
122: */
123: @Test
124: public final DynamicMBean getActiveMBean() {
125: C database = this .createDatabase("1");
126:
127: DynamicMBean mbean = database.getActiveMBean();
128:
129: String className = mbean.getMBeanInfo().getClassName();
130:
131: try {
132: assert ActiveDataSourceDatabaseMBean.class
133: .isAssignableFrom(Class.forName(className)) : className;
134: } catch (ClassNotFoundException e) {
135: assert false : e;
136: }
137:
138: return mbean;
139: }
140:
141: /**
142: * @see net.sf.hajdbc.Database#getInactiveMBean()
143: */
144: @Test
145: public final DynamicMBean getInactiveMBean() {
146: C database = this .createDatabase("1");
147:
148: DynamicMBean mbean = database.getInactiveMBean();
149:
150: String className = mbean.getMBeanInfo().getClassName();
151:
152: try {
153: assert InactiveDataSourceDatabaseMBean.class
154: .isAssignableFrom(Class.forName(className)) : className;
155: } catch (ClassNotFoundException e) {
156: assert false : e;
157: }
158:
159: return mbean;
160: }
161:
162: /**
163: * @see net.sf.hajdbc.sql.ActiveDataSourceDatabaseMBean#getName()
164: */
165: @Test
166: public final String getName() {
167: C database = this .createDatabase("1");
168:
169: String name = database.getName();
170:
171: assert name == null : name;
172:
173: database.setName("name");
174:
175: name = database.getName();
176:
177: assert name.equals("name") : name;
178:
179: return name;
180: }
181:
182: @Test(dataProvider="string")
183: public final void setName(String name) {
184: C database = this .createDatabase("1");
185:
186: database.setName(name);
187:
188: String value = database.getName();
189:
190: assert value.equals(name) : value;
191:
192: database.clean();
193:
194: database.setName(name);
195:
196: value = database.getName();
197:
198: assert value.equals(name) : value;
199:
200: assert !database.isDirty();
201:
202: database.setName(null);
203:
204: assert database.isDirty();
205:
206: value = database.getName();
207:
208: assert value == null : value;
209: }
210: }
|