001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestDatasource.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.database;
009:
010: import junit.framework.TestCase;
011:
012: import org.postgresql.ds.PGSimpleDataSource;
013:
014: import com.uwyn.rife.database.exceptions.DatabaseException;
015: import com.uwyn.rife.tools.ExceptionUtils;
016:
017: public class TestDatasource extends TestCase {
018: public TestDatasource(String name) {
019: super (name);
020: }
021:
022: public void testInstantiation() {
023: Datasource datasource1 = new Datasource();
024: assertNotNull(datasource1);
025: assertNull(datasource1.getDriver());
026: assertNull(datasource1.getDataSource());
027: assertNull(datasource1.getUrl());
028: assertNull(datasource1.getUser());
029: assertNull(datasource1.getPassword());
030: assertEquals(datasource1.getPoolsize(), 0);
031: assertTrue(!datasource1.isPooled());
032:
033: String driver = "driver";
034: String url = "url";
035: String user = "user";
036: String password = "password";
037: int poolsize = 5;
038:
039: Datasource datasource2 = new Datasource(driver, url, user,
040: password, poolsize);
041: assertNotNull(datasource2);
042: assertEquals(datasource2.getDriver(), driver);
043: assertEquals(datasource2.getUrl(), url);
044: assertEquals(datasource2.getUser(), user);
045: assertEquals(datasource2.getPassword(), password);
046: assertEquals(datasource2.getPoolsize(), poolsize);
047: assertTrue(datasource2.isPooled());
048:
049: PGSimpleDataSource pgdatasource = new PGSimpleDataSource();
050: Datasource datasource3 = new Datasource(pgdatasource, driver,
051: user, password, poolsize);
052: assertNotNull(datasource3);
053: assertEquals(datasource3.getDriver(), driver);
054: assertSame(datasource3.getDataSource(), pgdatasource);
055: assertEquals(datasource3.getUser(), user);
056: assertEquals(datasource3.getPassword(), password);
057: assertEquals(datasource3.getPoolsize(), poolsize);
058: assertTrue(datasource3.isPooled());
059: }
060:
061: public void testPopulation() {
062: String driver = "driver";
063: String url = "url";
064: String user = "user";
065: String password = "password";
066: int poolsize = 5;
067: PGSimpleDataSource pgdatasource = new PGSimpleDataSource();
068:
069: Datasource datasource = new Datasource();
070: datasource.setDriver(driver);
071: datasource.setDataSource(pgdatasource);
072: datasource.setUrl(url);
073: datasource.setUser(user);
074: datasource.setPassword(password);
075: datasource.setPoolsize(poolsize);
076: assertEquals(datasource.getDriver(), driver);
077: assertSame(datasource.getDataSource(), pgdatasource);
078: assertEquals(datasource.getUrl(), url);
079: assertEquals(datasource.getUser(), user);
080: assertEquals(datasource.getPassword(), password);
081: assertEquals(datasource.getPoolsize(), poolsize);
082: assertTrue(datasource.isPooled());
083: }
084:
085: public void testDriverAlias() {
086: String driver_aliased = "org.gjt.mm.mysql.Driver";
087: String driver_unaliased = "com.mysql.jdbc.Driver";
088:
089: Datasource datasource = new Datasource();
090:
091: datasource.setDriver(driver_aliased);
092: assertEquals(datasource.getDriver(), driver_aliased);
093: assertEquals(datasource.getAliasedDriver(), driver_unaliased);
094:
095: datasource.setDriver(driver_unaliased);
096: assertEquals(datasource.getDriver(), driver_unaliased);
097: assertEquals(datasource.getAliasedDriver(), driver_unaliased);
098: }
099:
100: public void testEquality() {
101: String driver = "driver";
102: String url = "url";
103: String user = "user";
104: String password = "password";
105: int poolsize = 5;
106:
107: Datasource datasource1 = new Datasource();
108: datasource1.setDriver(driver);
109: datasource1.setUrl(url);
110: datasource1.setUser(user);
111: datasource1.setPassword(password);
112: datasource1.setPoolsize(poolsize);
113:
114: Datasource datasource2 = new Datasource(driver, url, user,
115: password, poolsize);
116:
117: assertNotSame(datasource1, datasource2);
118:
119: assertEquals(datasource1.getDriver(), datasource2.getDriver());
120: assertEquals(datasource1.getUrl(), datasource2.getUrl());
121: assertEquals(datasource1.getUser(), datasource2.getUser());
122: assertEquals(datasource1.getPassword(), datasource2
123: .getPassword());
124: assertEquals(datasource1.getPoolsize(), datasource2
125: .getPoolsize());
126: assertEquals(datasource1.isPooled(), datasource2.isPooled());
127: assertEquals(datasource1, datasource2);
128:
129: datasource2.setDriver("otherdriver");
130: assertTrue(!datasource1.equals(datasource2));
131: datasource2.setDriver(driver);
132: assertTrue(datasource1.equals(datasource2));
133:
134: datasource2.setUrl("otherurl");
135: assertTrue(!datasource1.equals(datasource2));
136: datasource2.setUrl(url);
137: assertTrue(datasource1.equals(datasource2));
138:
139: datasource2.setUser("otheruser");
140: assertTrue(!datasource1.equals(datasource2));
141: datasource2.setUser(user);
142: assertTrue(datasource1.equals(datasource2));
143:
144: datasource2.setPassword("otherpassword");
145: assertTrue(!datasource1.equals(datasource2));
146: datasource2.setPassword(password);
147: assertTrue(datasource1.equals(datasource2));
148:
149: datasource2.setPoolsize(poolsize + 1);
150: assertTrue(datasource1.equals(datasource2));
151:
152: PGSimpleDataSource pgdatasource = new PGSimpleDataSource();
153:
154: Datasource datasource3 = new Datasource();
155: datasource3.setDriver(driver);
156: datasource3.setDataSource(pgdatasource);
157: datasource3.setUser(user);
158: datasource3.setPassword(password);
159: datasource3.setPoolsize(poolsize);
160:
161: Datasource datasource4 = new Datasource(pgdatasource, driver,
162: user, password, poolsize);
163:
164: assertNotSame(datasource3, datasource4);
165:
166: assertEquals(datasource3.getDriver(), datasource4.getDriver());
167: assertEquals(datasource3.getUrl(), datasource4.getUrl());
168: assertEquals(datasource3.getUser(), datasource4.getUser());
169: assertEquals(datasource3.getPassword(), datasource4
170: .getPassword());
171: assertEquals(datasource3.getPoolsize(), datasource4
172: .getPoolsize());
173: assertEquals(datasource3.isPooled(), datasource4.isPooled());
174: assertEquals(datasource3, datasource4);
175:
176: datasource4.setDriver("otherdriver");
177: assertTrue(!datasource3.equals(datasource4));
178: datasource4.setDriver(driver);
179: assertTrue(datasource3.equals(datasource4));
180:
181: datasource4.setDataSource(new PGSimpleDataSource());
182: assertTrue(!datasource3.equals(datasource4));
183: datasource4.setDataSource(pgdatasource);
184: assertTrue(datasource3.equals(datasource4));
185:
186: datasource4.setUser("otheruser");
187: assertTrue(!datasource3.equals(datasource4));
188: datasource4.setUser(user);
189: assertTrue(datasource3.equals(datasource4));
190:
191: datasource4.setPassword("otherpassword");
192: assertTrue(!datasource3.equals(datasource4));
193: datasource4.setPassword(password);
194: assertTrue(datasource3.equals(datasource4));
195:
196: datasource4.setPoolsize(poolsize + 1);
197: assertTrue(datasource3.equals(datasource4));
198: }
199:
200: public void testConnection() {
201: Datasource datasource = Datasources.getRepInstance()
202: .getDatasource("unittestspgsql");
203: if (null == datasource) {
204: System.out
205: .println("WARNING : Datasource testConnection test not executed (postgresql missing)");
206: return;
207: }
208:
209: DbConnection connection = null;
210: try {
211: connection = datasource.getConnection();
212: } catch (DatabaseException e) {
213: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
214: }
215: assertNotNull(connection);
216: try {
217: connection.close();
218: } catch (DatabaseException e) {
219: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
220: }
221: }
222:
223: public void testConnectionDataSource1() {
224: Datasource declared_datasource = Datasources.getRepInstance()
225: .getDatasource("unittestspgsql");
226: if (null == declared_datasource) {
227: System.out
228: .println("WARNING : Datasource testConnectionDataSource test not executed (postgresql missing)");
229: return;
230: }
231: PGSimpleDataSource pgdatasource = new PGSimpleDataSource();
232: pgdatasource.setDatabaseName("unittests");
233: pgdatasource.setServerName("localhost");
234: pgdatasource.setPortNumber(5432);
235: pgdatasource.setUser("unittests");
236: pgdatasource.setPassword("password");
237: Datasource datasource = new Datasource(pgdatasource,
238: declared_datasource.getDriver(), "unittests",
239: "password", 5);
240:
241: DbConnection connection = null;
242: try {
243: connection = datasource.getConnection();
244: } catch (DatabaseException e) {
245: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
246: }
247: assertNotNull(connection);
248: try {
249: connection.close();
250: } catch (DatabaseException e) {
251: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
252: }
253: }
254:
255: public void testConnectionDataSource2() {
256: Datasource declared_datasource = Datasources.getRepInstance()
257: .getDatasource("unittestspgsql");
258: if (null == declared_datasource) {
259: System.out
260: .println("WARNING : Datasource testConnectionDataSource test not executed (postgresql missing)");
261: return;
262: }
263: PGSimpleDataSource pgdatasource = new PGSimpleDataSource();
264: pgdatasource.setDatabaseName("unittests");
265: pgdatasource.setServerName("localhost");
266: pgdatasource.setPortNumber(5432);
267: pgdatasource.setUser("unittests");
268: pgdatasource.setPassword("password");
269: Datasource datasource = new Datasource(pgdatasource, 5);
270:
271: DbConnection connection = null;
272: try {
273: connection = datasource.getConnection();
274: } catch (DatabaseException e) {
275: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
276: }
277: assertNotNull(connection);
278: try {
279: connection.close();
280: } catch (DatabaseException e) {
281: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
282: }
283: }
284:
285: public void testConnectionDataSourceJndi() {
286: Datasource datasource = Datasources.getRepInstance()
287: .getDatasource("jndipgsql");
288:
289: assertNotNull(datasource.getSqlConversion());
290:
291: Datasource datasource2 = Datasources.getRepInstance()
292: .getDatasource("jndipgsql2");
293: assertNotNull(datasource2.getCapabilitiesCompensator());
294:
295: DbConnection connection = null;
296: try {
297: connection = datasource.getConnection();
298: } catch (DatabaseException e) {
299: fail(ExceptionUtils.getExceptionStackTrace(e));
300: }
301: assertNotNull(connection);
302: try {
303: connection.close();
304: } catch (DatabaseException e) {
305: fail(ExceptionUtils.getExceptionStackTrace(e));
306: }
307: }
308:
309: public void testConnectionPreservation() {
310: Datasource datasource = Datasources.getRepInstance()
311: .getDatasource("unittestspgsql");
312: if (null == datasource) {
313: System.out
314: .println("WARNING : Datasource testConnectionPreservation test not executed (postgresql missing)");
315: return;
316: }
317:
318: try {
319: DbConnection connection1 = null;
320: DbConnection connection2 = null;
321:
322: connection1 = datasource.getConnection();
323: connection2 = datasource.getConnection();
324: assertTrue(connection1 != connection2);
325: connection1.close();
326: connection2.close();
327:
328: connection1 = datasource.getConnection();
329: connection1.beginTransaction();
330: connection2 = datasource.getConnection();
331: assertTrue(connection1 == connection2);
332: connection1.rollback();
333: connection1.close();
334: } catch (DatabaseException e) {
335: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
336: }
337: }
338:
339: public void testConnectionPreservationDatasource1() {
340: Datasource declared_datasource = Datasources.getRepInstance()
341: .getDatasource("unittestspgsql");
342: if (null == declared_datasource) {
343: System.out
344: .println("WARNING : Datasource testConnectionPreservationDatasource test not executed (postgresql missing)");
345: return;
346: }
347: PGSimpleDataSource pgdatasource = new PGSimpleDataSource();
348: pgdatasource.setDatabaseName("unittests");
349: pgdatasource.setServerName("localhost");
350: pgdatasource.setPortNumber(5432);
351: pgdatasource.setUser("unittests");
352: pgdatasource.setPassword("password");
353: Datasource datasource = new Datasource(pgdatasource,
354: declared_datasource.getDriver(), "unittests",
355: "password", 5);
356:
357: try {
358: DbConnection connection1 = null;
359: DbConnection connection2 = null;
360:
361: connection1 = datasource.getConnection();
362: connection2 = datasource.getConnection();
363: assertTrue(connection1 != connection2);
364: connection1.close();
365: connection2.close();
366:
367: connection1 = datasource.getConnection();
368: connection1.beginTransaction();
369: connection2 = datasource.getConnection();
370: assertTrue(connection1 == connection2);
371: connection1.rollback();
372: connection1.close();
373: } catch (DatabaseException e) {
374: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
375: }
376: }
377:
378: public void testConnectionPreservationDatasource2() {
379: Datasource declared_datasource = Datasources.getRepInstance()
380: .getDatasource("unittestspgsql");
381: if (null == declared_datasource) {
382: System.out
383: .println("WARNING : Datasource testConnectionPreservationDatasource test not executed (postgresql missing)");
384: return;
385: }
386: PGSimpleDataSource pgdatasource = new PGSimpleDataSource();
387: pgdatasource.setDatabaseName("unittests");
388: pgdatasource.setServerName("localhost");
389: pgdatasource.setPortNumber(5432);
390: pgdatasource.setUser("unittests");
391: pgdatasource.setPassword("password");
392: Datasource datasource = new Datasource(pgdatasource, 5);
393:
394: try {
395: DbConnection connection1 = null;
396: DbConnection connection2 = null;
397:
398: connection1 = datasource.getConnection();
399: connection2 = datasource.getConnection();
400: assertTrue(connection1 != connection2);
401: connection1.close();
402: connection2.close();
403:
404: connection1 = datasource.getConnection();
405: connection1.beginTransaction();
406: connection2 = datasource.getConnection();
407: assertTrue(connection1 == connection2);
408: connection1.rollback();
409: connection1.close();
410: } catch (DatabaseException e) {
411: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
412: }
413: }
414: }
|