1 21 package net.sf.hajdbc.sql; 22 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.sql.Connection ; 26 27 import javax.naming.NamingException ; 28 import javax.naming.Reference ; 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 41 @Test 42 public class TestDataSource extends DatabaseClusterTestCase 43 { 44 @BeforeClass 45 public void setUp() throws Exception 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 58 { 59 this.context.unbind("datasource"); 60 61 super.tearDown(); 62 } 63 64 private DataSource getDataSource() throws NamingException 65 { 66 return (DataSource) this.context.lookup("datasource"); 67 } 68 69 72 public void testGetLoginTimeout() 73 { 74 try 75 { 76 int timeout = this.getDataSource().getLoginTimeout(); 77 78 assert timeout == 0 : timeout; 79 } 80 catch (Exception e) 81 { 82 assert false : e; 83 } 84 } 85 86 89 public void testSetLoginTimeout() 90 { 91 try 92 { 93 this.getDataSource().setLoginTimeout(1000); 94 } 95 catch (Exception e) 96 { 97 assert false : e; 98 } 99 } 100 101 104 public void testGetLogWriter() 105 { 106 try 107 { 108 PrintWriter writer = this.getDataSource().getLogWriter(); 109 110 assert writer != null; 111 } 112 catch (Exception e) 113 { 114 assert false : e; 115 } 116 } 117 118 121 public void testSetLogWriter() 122 { 123 try 124 { 125 PrintWriter writer = new PrintWriter (new StringWriter ()); 126 127 this.getDataSource().setLogWriter(writer); 128 } 129 catch (Exception e) 130 { 131 assert false : e; 132 } 133 } 134 135 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 e) 148 { 149 assert false : e; 150 } 151 } 152 153 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 e) 166 { 167 assert false : e; 168 } 169 } 170 171 174 public void testGetCluster() 175 { 176 try 177 { 178 String name = this.getDataSource().getCluster(); 179 180 assert name.equals("test-datasource-cluster"); 181 } 182 catch (Exception e) 183 { 184 assert false : e; 185 } 186 } 187 188 191 public void testSetName() 192 { 193 try 194 { 195 this.getDataSource().setCluster("test"); 196 } 197 catch (Exception e) 198 { 199 assert false : e; 200 } 201 } 202 203 206 public void testGetReference() 207 { 208 try 209 { 210 Reference 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 e) 216 { 217 assert false : e; 218 } 219 } 220 } 221 | Popular Tags |