KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > sql > TestDataSource


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.sql;
22
23 import java.io.PrintWriter JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.sql.Connection JavaDoc;
26
27 import javax.naming.NamingException JavaDoc;
28 import javax.naming.Reference JavaDoc;
29
30 import net.sf.hajdbc.DatabaseClusterTestCase;
31
32 import org.testng.annotations.AfterClass;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
35
36 /**
37  * Unit test for {@link TestDataSource}.
38  * @author Paul Ferraro
39  * @since 1.1
40  */

41 @Test
42 public class TestDataSource extends DatabaseClusterTestCase
43 {
44     @BeforeClass
45     public void setUp() throws Exception JavaDoc
46     {
47         super.setUp();
48         
49         DataSource dataSource = new DataSource();
50         
51         dataSource.setCluster("test-datasource-cluster");
52         
53         this.context.bind("datasource", dataSource);
54     }
55
56     @AfterClass
57     public void tearDown() throws Exception JavaDoc
58     {
59         this.context.unbind("datasource");
60         
61         super.tearDown();
62     }
63
64     private DataSource getDataSource() throws NamingException JavaDoc
65     {
66         return (DataSource) this.context.lookup("datasource");
67     }
68     
69     /**
70      * Test method for {@link DataSource#getLoginTimeout()}
71      */

72     public void testGetLoginTimeout()
73     {
74         try
75         {
76             int timeout = this.getDataSource().getLoginTimeout();
77             
78             assert timeout == 0 : timeout;
79         }
80         catch (Exception JavaDoc e)
81         {
82             assert false : e;
83         }
84     }
85
86     /**
87      * Test method for {@link DataSource#setLoginTimeout(int)}
88      */

89     public void testSetLoginTimeout()
90     {
91         try
92         {
93             this.getDataSource().setLoginTimeout(1000);
94         }
95         catch (Exception JavaDoc e)
96         {
97             assert false : e;
98         }
99     }
100
101     /**
102      * Test method for {@link DataSource#getLogWriter()}
103      */

104     public void testGetLogWriter()
105     {
106         try
107         {
108             PrintWriter JavaDoc writer = this.getDataSource().getLogWriter();
109             
110             assert writer != null;
111         }
112         catch (Exception JavaDoc e)
113         {
114             assert false : e;
115         }
116     }
117
118     /**
119      * Test method for {@link DataSource#setLogWriter(PrintWriter)}
120      */

121     public void testSetLogWriter()
122     {
123         try
124         {
125             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new StringWriter JavaDoc());
126             
127             this.getDataSource().setLogWriter(writer);
128         }
129         catch (Exception JavaDoc e)
130         {
131             assert false : e;
132         }
133     }
134
135     /**
136      * Test method for {@link DataSource#getConnection()}
137      */

138     public void testGetConnection()
139     {
140         try
141         {
142             Connection connection = this.getDataSource().getConnection();
143             
144             assert connection != null;
145             assert net.sf.hajdbc.sql.Connection.class.equals(connection.getClass()) : connection.getClass().getName();
146         }
147         catch (Exception JavaDoc e)
148         {
149             assert false : e;
150         }
151     }
152
153     /**
154      * Test method for {@link DataSource#getConnection(String, String)}
155      */

156     public void testGetConnectionStringString()
157     {
158         try
159         {
160             Connection connection = this.getDataSource().getConnection("sa", "");
161             
162             assert connection != null;
163             assert net.sf.hajdbc.sql.Connection.class.equals(connection.getClass()) : connection.getClass().getName();
164         }
165         catch (Exception JavaDoc e)
166         {
167             assert false : e;
168         }
169     }
170
171     /**
172      * Test method for {@link DataSource#getName()}
173      */

174     public void testGetCluster()
175     {
176         try
177         {
178             String JavaDoc name = this.getDataSource().getCluster();
179             
180             assert name.equals("test-datasource-cluster");
181         }
182         catch (Exception JavaDoc e)
183         {
184             assert false : e;
185         }
186     }
187
188     /**
189      * Test method for {@link DataSource#setName(String)}
190      */

191     public void testSetName()
192     {
193         try
194         {
195             this.getDataSource().setCluster("test");
196         }
197         catch (Exception JavaDoc e)
198         {
199             assert false : e;
200         }
201     }
202
203     /**
204      * Test method for {@link DataSource#getReference()}
205      */

206     public void testGetReference()
207     {
208         try
209         {
210             Reference JavaDoc reference = this.getDataSource().getReference();
211             
212             assert reference.getClassName().equals("net.sf.hajdbc.sql.DataSource");
213             assert reference.getFactoryClassName().equals("net.sf.hajdbc.sql.DataSourceFactory");
214         }
215         catch (Exception JavaDoc e)
216         {
217             assert false : e;
218         }
219     }
220 }
221
Popular Tags