KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > DatabaseClusterTestCase


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc;
22
23 import java.sql.DriverManager JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.Reference JavaDoc;
31 import javax.sql.ConnectionPoolDataSource JavaDoc;
32 import javax.sql.DataSource JavaDoc;
33 import javax.sql.XADataSource JavaDoc;
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 /**
44  * @author Paul Ferraro
45  * @since 1.0
46  */

47 public abstract class DatabaseClusterTestCase
48 {
49     protected IMocksControl control = EasyMock.createStrictControl();
50     protected Context JavaDoc context;
51     private MBeanServer JavaDoc server;
52     
53     @BeforeClass
54     public void setUp() throws Exception JavaDoc
55     {
56         this.server = MBeanServerFactory.createMBeanServer();
57         
58         DriverManager.registerDriver(new MockDriver());
59         
60         Properties JavaDoc properties = new Properties JavaDoc();
61         
62         properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "net.sf.hajdbc.sql.MockInitialContextFactory");
63         
64         this.context = new InitialContext JavaDoc(properties);
65         
66         Reference JavaDoc reference = new Reference JavaDoc(DataSource JavaDoc.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 JavaDoc(ConnectionPoolDataSource JavaDoc.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 JavaDoc(XADataSource JavaDoc.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 JavaDoc
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