Now using the new database support functionality.
authorErik Brakkee <erik@brakkee.org>
Sun, 4 Apr 2010 20:52:28 +0000 (20:52 +0000)
committerErik Brakkee <erik@brakkee.org>
Sun, 4 Apr 2010 20:52:28 +0000 (20:52 +0000)
security/pom.xml
security/src/test/resources/spring/test.org.wamblee.security.datasource.xml
support/general/src/test/java/org/wamblee/persistence/Database.java [deleted file]
support/general/src/test/java/org/wamblee/persistence/DerbyDatabase.java [deleted file]
support/general/src/test/java/org/wamblee/persistence/OracleDatabase.java [deleted file]
support/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseStarter.java [moved from support/general/src/test/java/org/wamblee/persistence/DatabaseStarter.java with 93% similarity]
system/general/pom.xml
system/general/src/test/java/org/wamblee/system/components/DatabaseComponent.java
system/general/src/test/java/org/wamblee/system/components/DatabaseComponentFactory.java
system/spring/src/main/java/org/wamblee/system/spring/component/DatasourceComponent.java

index 1e3557227681a8d63f6f14ed16196773c53d1f8e..e878f6748c37dc929df50c441ad929ad325ba9d7 100644 (file)
       <type>test-jar</type>
       <version>0.2-SNAPSHOT</version>
     </dependency>
+    <dependency>
+      <groupId>org.wamblee</groupId>
+      <artifactId>wamblee-support-test-enterprise</artifactId>
+      <version>0.2-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.wamblee</groupId>
       <artifactId>wamblee-system-spring</artifactId>
index 5a75178e1d4c8d3a2615fe50c0a72145f373f64e..d7183342175fc9e2226da7126536f3a1d166fa11 100644 (file)
@@ -7,9 +7,11 @@
     <!-- This is the Spring configuration to define the database-related stuff for the
     all persistence tests.  -->
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
+        <!-- driver classname no longer needed because ServiceLoader is used these days
         <property name="driverClassName">
             <value>${database.driver}</value>
         </property>
+        --> 
         <property name="url">
             <value>${database.url}</value>
         </property>
diff --git a/support/general/src/test/java/org/wamblee/persistence/Database.java b/support/general/src/test/java/org/wamblee/persistence/Database.java
deleted file mode 100644 (file)
index 5b0ae98..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Copyright 2005 the original author or authors.
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.wamblee.persistence;
-
-import java.sql.Connection;
-import java.sql.SQLException;
-
-/**
- * Represents a database.
- * 
- * @author Erik Brakkee
- */
-public interface Database {
-    /**
-     * Starts a database. This call should not block and return as soon as the
-     * database has been started.
-     */
-    void start();
-
-    /**
-     * Gets the Jdbc Url to connect to this database.
-     * 
-     * @return Jdbc Url.
-     */
-    String getJdbcUrl();
-
-    /**
-     * Gets the external Jdbc URL to connect to this database from other JVMs.
-     */
-    String getExternalJdbcUrl();
-
-    /**
-     * Gets the driver class name used to connect to the database from another
-     * JVM.
-     * 
-     * @return Driver class name.
-     */
-    String getDriverClassName();
-
-    /**
-     * Gets the username to connect to the database.
-     * 
-     * @return username.
-     */
-    String getUsername();
-
-    /**
-     * Gets the password to connect to the database.
-     * 
-     * @return password.
-     */
-    String getPassword();
-
-    /**
-     * Creates a new database connection. It is the caller's responsibility to
-     * close this connection.
-     * 
-     * @return Newly created database connection.
-     */
-    Connection createConnection() throws SQLException;
-
-    /**
-     * Stops a database.
-     */
-    void stop();
-}
diff --git a/support/general/src/test/java/org/wamblee/persistence/DerbyDatabase.java b/support/general/src/test/java/org/wamblee/persistence/DerbyDatabase.java
deleted file mode 100644 (file)
index f8d09d2..0000000
+++ /dev/null
@@ -1,298 +0,0 @@
-/*
- * Copyright 2005 the original author or authors.
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.wamblee.persistence;
-
-import java.io.File;
-import java.io.PrintWriter;
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-import java.util.Properties;
-
-import junit.framework.TestCase;
-
-import org.apache.derby.drda.NetworkServerControl;
-import org.apache.derby.jdbc.ClientDriver;
-import org.apache.log4j.Logger;
-import org.wamblee.io.FileSystemUtils;
-
-/**
- * Derby database setup. The external JDBC url used to connect to a running
- * instance is
- * 
- * <pre>
- *     jdbc:derby:net://localhost:1527/testdb 
- * </pre>
- * 
- * and the driver class is
- * 
- * <pre>
- * com.ibm.db2.jcc.DB2Driver
- * </pre>
- * 
- * The following jars will have to be used <code>db2jcc.jar</code> and
- * <code>db2jcc_license_c.jar</code>.
- */
-public class DerbyDatabase implements Database {
-
-    /**
-     * Logger.
-     */
-    private static final Logger LOGGER = Logger.getLogger(DerbyDatabase.class);
-
-    /**
-     * Database user name.
-     */
-    private static final String USERNAME = "sa";
-
-    /**
-     * Database password.
-     */
-    private static final String PASSWORD = "123";
-
-    /**
-     * Poll interval for the checking the server status.
-     */
-    private static final int POLL_INTERVAL = 100;
-
-    /**
-     * Maximum time to wait until the server has started or stopped.
-     */
-    private static final int MAX_WAIT_TIME = 10000;
-
-    /**
-     * Database name to use.
-     */
-    private static final String DATABASE_NAME = "testdb";
-
-    /**
-     * Path on the file system where derby files are stored.
-     */
-    private static final String DATABASE_PATH = "target/db/persistence/derby";
-
-    /**
-     * Derby property required to set the file system path
-     * {@link #DATABASE_PATH}.
-     */
-    private static final String SYSTEM_PATH_PROPERTY = "derby.system.home";
-
-    /**
-     * Constructs derby database class to allow creation of derby database
-     * instances.
-     */
-    public DerbyDatabase() {
-        // Empty
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.wamblee.persistence.Database#start()
-     */
-    public void start() {
-        try {
-            // just in case a previous run was killed without the
-            // cleanup
-            cleanPersistentStorage();
-
-            // set database path.
-            Properties lProperties = System.getProperties();
-            lProperties.put(SYSTEM_PATH_PROPERTY, DATABASE_PATH);
-
-            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
-
-            runDatabase();
-
-            waitUntilStartedOrStopped(true);
-
-            // Force creation of the database.
-            Connection lConnection = createConnection();
-            lConnection.close();
-        } catch (Exception e) {
-            throw new RuntimeException("Problem starting database", e);
-        }
-    }
-
-    /**
-     * Waits until the database server has started or stopped.
-     * 
-     * @param aStarted
-     *            If true, waits until the server is up, if false, waits until
-     *            the server is down.
-     * @throws InterruptedException
-     */
-    private void waitUntilStartedOrStopped(boolean aStarted)
-            throws InterruptedException {
-        long lWaited = 0;
-
-        while (aStarted != isStarted()) {
-            Thread.sleep(POLL_INTERVAL);
-            lWaited += POLL_INTERVAL;
-
-            if (lWaited > MAX_WAIT_TIME) {
-                throw new RuntimeException(
-                        "Derby database did not start within " + MAX_WAIT_TIME
-                                + "ms");
-            }
-        }
-    }
-
-    /**
-     * Checks if the database server has started or not.
-     * 
-     * @return True if started, false otherwise.
-     */
-    private boolean isStarted() {
-        try {
-            getControl().ping();
-
-            return true;
-        } catch (Exception e) {
-            return false;
-        }
-    }
-
-    /**
-     * Gets the controller for the database server.
-     * 
-     * @return Controller.
-     * @throws Exception
-     */
-    private NetworkServerControl getControl() throws Exception {
-        return new NetworkServerControl();
-    }
-
-    /**
-     * Runs the database.
-     * 
-     */
-    private void runDatabase() {
-        try {
-            getControl().start(new PrintWriter(System.out));
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.wamblee.persistence.Database#getJdbcUrl()
-     */
-    public String getJdbcUrl() {
-        return getBaseJdbcUrl()
-                + ";create=true;retrieveMessagesFromServerOnGetMessage=true;";
-    }
-
-    private String getBaseJdbcUrl() {
-        return "jdbc:derby:" + DATABASE_NAME;
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.wamblee.persistence.Database#getExternalJdbcUrl()
-     */
-    public String getExternalJdbcUrl() {
-        return "jdbc:derby:net://localhost:1527/" + DATABASE_NAME;
-    }
-
-    /**
-     * Shuts down the derby database and cleans up all created files.
-     * 
-     */
-    private void shutdownDerby() {
-        try {
-            DriverManager.getConnection("jdbc:derby:;shutdown=true");
-            throw new RuntimeException("Derby did not shutdown, "
-                    + " should always throw exception at shutdown");
-        } catch (Exception e) {
-            LOGGER.info("Derby has been shut down.");
-        }
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.wamblee.persistence.Database#getDriverClassName()
-     */
-    public String getDriverClassName() {
-        return ClientDriver.class.getName();
-    }
-
-    /**
-     * Gets the user name.
-     */
-    public String getUsername() {
-        return USERNAME;
-    }
-
-    /**
-     * Gets the password.
-     * 
-     * @return
-     */
-    public String getPassword() {
-        return PASSWORD;
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.wamblee.persistence.Database#createConnection()
-     */
-    public Connection createConnection() throws SQLException {
-        Connection c = DriverManager.getConnection(getJdbcUrl(), getUsername(),
-                getPassword());
-
-        return c;
-    }
-
-    /**
-     * Stops the derby database and cleans up all derby files.
-     */
-    public void stop() {
-        try {
-            // shutdown network server.
-            getControl().shutdown();
-            waitUntilStartedOrStopped(false);
-
-            // shutdown inmemory access.
-            shutdownDerby();
-            cleanPersistentStorage();
-        } catch (Exception e) {
-            LOGGER.warn("Problem stopping database", e);
-        }
-    }
-
-    /**
-     * Cleans up persistent storage of Derby.
-     */
-    private void cleanPersistentStorage() {
-        File lFile = new File(DATABASE_PATH);
-
-        if (lFile.isFile()) {
-            TestCase.fail("A regular file by the name " + DATABASE_PATH
-                    + " exists, clean this up first");
-        }
-
-        if (!lFile.isDirectory()) {
-            return; // no-op already cleanup up.
-        }
-
-        FileSystemUtils.deleteDirRecursively(DATABASE_PATH);
-    }
-}
diff --git a/support/general/src/test/java/org/wamblee/persistence/OracleDatabase.java b/support/general/src/test/java/org/wamblee/persistence/OracleDatabase.java
deleted file mode 100644 (file)
index 6b82c63..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Copyright 2005 the original author or authors.
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.wamblee.persistence;
-
-import java.sql.Connection;
-import java.sql.SQLException;
-
-public class OracleDatabase implements Database {
-
-    public OracleDatabase() {
-        super();
-    }
-
-    public void start() {
-        System.out.println("Oracle must be started externally");
-    }
-
-    public String getJdbcUrl() {
-        return null;
-    }
-
-    public String getExternalJdbcUrl() {
-        return null;
-    }
-
-    public String getDriverClassName() {
-        return null;
-    }
-
-    public String getUsername() {
-        return null;
-    }
-
-    public String getPassword() {
-        return null;
-    }
-
-    public Connection createConnection() throws SQLException {
-        return null;
-    }
-
-    public void stop() {
-        System.out.println("Oracle must be stopped externally");
-    }
-
-}
similarity index 93%
rename from support/general/src/test/java/org/wamblee/persistence/DatabaseStarter.java
rename to support/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseStarter.java
index c0bdcc2f97b4180e02290ce0de8058732c90284b..44294e351916cd335f4deb019fe3a9ea7b1c7142 100644 (file)
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-package org.wamblee.persistence;
+package org.wamblee.support.persistence;
 
 
 /**
@@ -47,7 +47,7 @@ public class DatabaseStarter {
         } catch ( Exception e ) {
             e.printStackTrace( );
             System.out
-                    .println( "\nUsage: ant -Ddatabase=Derby|Hypersonic startdb" );
+                    .println( "\nUsage: ant dbClass ");
         }
     }
 
@@ -88,8 +88,8 @@ public class DatabaseStarter {
         System.out.println( );
         System.out.println("=======================================================");
         System.out.println( "Connection details:" );
-        System.out.println( "  Driver class: "
-                + lDatabase.getDriverClassName( ) );
+       // System.out.println( "  Driver class: "
+       //         + lDatabase.getDriverClassName( ) );
         System.out.println( "  JDBC URL:     "
                 + lDatabase.getExternalJdbcUrl( ) );
         System.out.println( "  username:     '" + lDatabase.getUsername( )
index 70546d7d0fbadfaa118fc0a4540e3fa61ed68158..615f4c4eb40bed6b7fd69c15dd74c5047a0138fa 100644 (file)
@@ -1,6 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    
   <parent>
     <groupId>org.wamblee</groupId>
     <artifactId>wamblee-utils</artifactId>
       <type>test-jar</type>
       <version>0.2-SNAPSHOT</version>
     </dependency>
+    <dependency>
+      <groupId>org.wamblee</groupId>
+      <artifactId>wamblee-support-test-enterprise</artifactId>
+      <version>0.2-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
index 5ec58512e87cdef6b0cbd72665da1dfb06a54cb6..afcc581aba29c2a328ab35962cf8c180ea3ab15d 100644 (file)
@@ -17,8 +17,8 @@ package org.wamblee.system.components;
 
 import java.util.Properties;
 
-import org.wamblee.persistence.Database;
-import org.wamblee.persistence.DerbyDatabase;
+import org.wamblee.support.persistence.Database;
+import org.wamblee.support.persistence.DerbyDatabase;
 import org.wamblee.system.components.ORMappingConfig.DatabaseType;
 import org.wamblee.system.core.AbstractComponent;
 import org.wamblee.system.core.DefaultProvidedInterface;
@@ -47,7 +47,7 @@ public class DatabaseComponent extends AbstractComponent<Database> {
         } else { 
             throw new IllegalArgumentException("Unknown database type " + _database); 
         }
-        props.put("database.driver", _database.getDriverClassName());
+        //props.put("database.driver", _database.getDriverClassName());
         props.put("database.url", _database.getJdbcUrl());
         props.put("database.username", _database.getUsername());
         props.put("database.password", _database.getPassword());
index b8ca2596e4e4c034f985e6196cdcd9e5440562fa..4c100fcca9bd459cf772268c2a4d6d3d815efaea 100644 (file)
  */
 package org.wamblee.system.components;
 
-import org.wamblee.persistence.DerbyDatabase;
+import org.wamblee.support.persistence.DatabaseBuilder;
 import org.wamblee.system.container.Container;
 
 public class DatabaseComponentFactory {
 
     public static void addDatabaseConfig(Container aContainer) {
         try {
-            /*
-            aContainer
-                    .addComponent(new PropertyComponent(
-                            "dbProps",
-                            new ClassPathResource(
-                                    "properties/test.org.wamblee.security.database.properties")));
-            */
             aContainer.addComponent(new DatabaseComponent("db",
-                    new DerbyDatabase())); 
+                    DatabaseBuilder.getDatabase())); 
         } catch (Exception e) {
             throw new RuntimeException("Could not add database configuration",
                     e);
index 96d7243685ec3a9ec849abb87d83b7e13d50261f..a8d53d4644f6b2c8957acafba6478b62c94ade1e 100644 (file)
@@ -56,7 +56,6 @@ public class DatasourceComponent extends AbstractComponent<DataSource> {
         Properties dbProps = aScope.getInterfaceImplementation(
                 PROPS.getProvider(), Properties.class);
         DriverManagerDataSource ds = new DriverManagerDataSource(
-                dbProps.getProperty("database.driver"),
                 dbProps.getProperty("database.url"), 
                 dbProps.getProperty("database.username"),
                 dbProps.getProperty("database.password"));