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: TestDatasources.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.database;
009:
010: import com.uwyn.rife.config.RifeConfig;
011: import com.uwyn.rife.database.exceptions.DatasourcesException;
012: import com.uwyn.rife.resources.ResourceFinderClasspath;
013: import com.uwyn.rife.tools.ExceptionUtils;
014: import java.io.File;
015: import junit.framework.TestCase;
016:
017: public class TestDatasources extends TestCase {
018: public TestDatasources(String name) {
019: super (name);
020: }
021:
022: public void testInstantiation() {
023: Datasources datasources = null;
024: try {
025: datasources = new Datasources(
026: "xml/test_xml2datasources.xml",
027: ResourceFinderClasspath.getInstance());
028: } catch (DatasourcesException e) {
029: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
030: }
031: assertNotNull(datasources);
032: }
033:
034: public void testRepInstance() {
035: assertTrue(Datasources.hasRepInstance());
036: assertNotNull(Datasources.getRepInstance());
037: }
038:
039: public void testToXml() {
040: try {
041: Datasources datasources = new Datasources(
042: "xml/test_xml2datasources.xml",
043: ResourceFinderClasspath.getInstance());
044: assertTrue(datasources.toXml().length() > 0);
045: assertEquals(
046: datasources.toXml(),
047: "<datasources>\n"
048: + "\t<datasource name=\"datasource1\">\n"
049: + "\t\t<driver>org.postgresql.Driver</driver>\n"
050: + "\t\t<url>jdbc:postgresql://localhost:5432/thedb</url>\n"
051: + "\t\t<user>unittests1</user>\n"
052: + "\t\t<password>password1</password>\n"
053: + "\t\t<poolsize>5</poolsize>\n"
054: + "\t</datasource>\n"
055: + "\t<datasource name=\"datasource2\">\n"
056: + "\t\t<driver>com.mysql.jdbc.Driver</driver>\n"
057: + "\t\t<url>jdbc:mysql://localhost:3306/thedb</url>\n"
058: + "\t\t<user>user2</user>\n"
059: + "\t\t<password>pw2</password>\n"
060: + "\t\t<poolsize>15</poolsize>\n"
061: + "\t</datasource>\n"
062: + "\t<datasource name=\"datasource3\">\n"
063: + "\t\t<driver>oracle.jdbc.driver.OracleDriver</driver>\n"
064: + "\t\t<url>jdbc:oracle:thin:@10.1.1.2:1521:database</url>\n"
065: + "\t\t<user>unittests</user>\n"
066: + "\t\t<password>password</password>\n"
067: + "\t</datasource>\n"
068: + "\t<datasource name=\"datasource4\">\n"
069: + "\t\t<driver>org.gjt.mm.mysql.Driver</driver>\n"
070: + "\t\t<url>jdbc:mysql://localhost:3306/thedb</url>\n"
071: + "\t\t<user>user2</user>\n"
072: + "\t\t<password>pw2</password>\n"
073: + "\t\t<poolsize>15</poolsize>\n"
074: + "\t</datasource>\n" + "</datasources>\n");
075: } catch (DatasourcesException e) {
076: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
077: }
078: }
079:
080: public void testStoreXml() {
081: try {
082: Datasources datasources = new Datasources();
083:
084: Datasource datasource1 = new Datasource("driver1", "url1",
085: "user1", "password1", 7);
086: Datasource datasource2 = new Datasource();
087: datasource2.setDriver("driver2");
088: datasource2.setUrl("url2");
089: datasource2.setUser("user2");
090: datasource2.setPassword("password2");
091: Datasource datasource3 = new Datasource();
092: datasource3.setDriver("driver3");
093: datasource3.setUrl("url3");
094: datasource3.setPoolsize(3);
095:
096: datasources.setDatasource("datasource1", datasource1);
097: datasources.setDatasource("datasource2", datasource2);
098: datasources.setDatasource("datasource3", datasource3);
099:
100: String xml_filename = "datasources_storexml_test.xml";
101: String xml_path = RifeConfig.Global.getTempPath()
102: + File.separator + xml_filename;
103: File xml_file = new File(xml_path);
104: datasources.storeToXml(xml_file);
105:
106: Datasources datasources_stored = new Datasources(
107: xml_filename, ResourceFinderClasspath.getInstance());
108: assertEquals(datasource1, datasources_stored
109: .getDatasource("datasource1"));
110: assertEquals(datasource2, datasources_stored
111: .getDatasource("datasource2"));
112: assertEquals(datasource3, datasources_stored
113: .getDatasource("datasource3"));
114: Datasource datasource4 = new Datasource("driver4", "url4",
115: "user4", "password4", 11);
116: datasources_stored
117: .setDatasource("datasource4", datasource4);
118: datasources_stored.storeToXml();
119:
120: Datasources datasources_stored2 = new Datasources(
121: xml_filename, ResourceFinderClasspath.getInstance());
122: assertEquals(datasource1, datasources_stored2
123: .getDatasource("datasource1"));
124: assertEquals(datasource2, datasources_stored2
125: .getDatasource("datasource2"));
126: assertEquals(datasource3, datasources_stored2
127: .getDatasource("datasource3"));
128: assertEquals(datasource4, datasources_stored2
129: .getDatasource("datasource4"));
130:
131: xml_file.delete();
132: } catch (DatasourcesException e) {
133: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
134: }
135: }
136: }
|