1 21 package net.sf.hajdbc; 22 23 import java.sql.DriverManager ; 24 import java.util.Properties ; 25 26 import javax.management.MBeanServer ; 27 import javax.management.MBeanServerFactory ; 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 import javax.naming.Reference ; 31 import javax.sql.ConnectionPoolDataSource ; 32 import javax.sql.DataSource ; 33 import javax.sql.XADataSource ; 34 35 import net.sf.hajdbc.sql.MockDriver; 36 37 import org.easymock.EasyMock; 38 import org.easymock.IMocksControl; 39 import org.testng.annotations.AfterClass; 40 import org.testng.annotations.AfterMethod; 41 import org.testng.annotations.BeforeClass; 42 43 47 public abstract class DatabaseClusterTestCase 48 { 49 protected IMocksControl control = EasyMock.createStrictControl(); 50 protected Context context; 51 private MBeanServer server; 52 53 @BeforeClass 54 public void setUp() throws Exception 55 { 56 this.server = MBeanServerFactory.createMBeanServer(); 57 58 DriverManager.registerDriver(new MockDriver()); 59 60 Properties properties = new Properties (); 61 62 properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "net.sf.hajdbc.sql.MockInitialContextFactory"); 63 64 this.context = new InitialContext (properties); 65 66 Reference reference = new Reference (DataSource .class.toString(), "net.sf.hajdbc.sql.MockDataSourceFactory", null); 67 68 this.context.rebind("datasource1", reference); 69 this.context.rebind("datasource2", reference); 70 71 reference = new Reference (ConnectionPoolDataSource .class.toString(), "net.sf.hajdbc.sql.pool.MockConnectionPoolDataSourceFactory", null); 72 73 this.context.rebind("pool-datasource1", reference); 74 this.context.rebind("pool-datasource2", reference); 75 76 reference = new Reference (XADataSource .class.toString(), "net.sf.hajdbc.sql.pool.xa.MockXADataSourceFactory", null); 77 78 this.context.rebind("xa-datasource1", reference); 79 this.context.rebind("xa-datasource2", reference); 80 } 81 82 @AfterClass 83 public void tearDown() throws Exception 84 { 85 DriverManager.deregisterDriver(new MockDriver()); 86 87 this.context.unbind("datasource1"); 88 this.context.unbind("datasource2"); 89 this.context.unbind("pool-datasource1"); 90 this.context.unbind("pool-datasource2"); 91 this.context.unbind("xa-datasource1"); 92 this.context.unbind("xa-datasource2"); 93 94 MBeanServerFactory.releaseMBeanServer(this.server); 95 } 96 97 @AfterMethod 98 public void reset() 99 { 100 this.control.reset(); 101 } 102 } 103 | Popular Tags |