summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
726e168)
As a result, no external Mysql database must be setup before running
the unit tests. It is now checkout and go!!
* 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.
* 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;
package org.wamblee.persistence;
import java.sql.Connection;
import java.sql.SQLException;
/**
* Represents a database.
/**
* Represents a database.
* @author Erik Brakkee
*/
public interface 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.
- * @throws Exception
+ * Starts a database. This call should not block and return as soon as the
+ * database has been started.
- void start( ) throws Exception;
/**
* Gets the Jdbc Url to connect to this database.
/**
* Gets the Jdbc Url to connect to this database.
- String getJdbcUrl( );
-
- * Gets the external Jdbc URL to connect to this database from other JVMs.
+ * Gets the external Jdbc URL to connect to this database from other JVMs.
*/
String getExternalJdbcUrl();
*/
String getExternalJdbcUrl();
- * Gets the driver class name used to connect to the database from another JVM.
- * @return Driver class name.
+ * Gets the driver class name used to connect to the database from another
+ * JVM.
+ *
+ * @return Driver class name.
- String getDriverClassName();
-
+ String getDriverClassName();
+
- * Gets the username to connect to the database.
- * @return username.
+ * Gets the username to connect to the database.
+ *
+ * @return username.
- String getUsername();
-
+ String getUsername();
+
- * Gets the password to connect to the database.
- * @return password.
+ * Gets the password to connect to the database.
+ *
+ * @return password.
- * Creates a new database connection. It is the caller's
- * responsibility to close this connection.
- *
+ * Creates a new database connection. It is the caller's responsibility to
+ * close this connection.
+ *
* @return Newly created database connection.
*/
* @return Newly created database connection.
*/
- Connection createConnection( ) throws SQLException;
+ Connection createConnection() throws SQLException;
- void stop( ) throws Exception;
* 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.
* 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;
package org.wamblee.persistence;
import java.io.File;
import org.apache.log4j.Logger;
import org.wamblee.io.FileSystemUtils;
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
+ * Derby database setup. The external JDBC url used to connect to a running
+ * instance is
+ *
* <pre>
* jdbc:derby:net://localhost:1527/testdb
* </pre>
* <pre>
* jdbc:derby:net://localhost:1527/testdb
* </pre>
* and the driver class is
* and the driver class is
- * com.ibm.db2.jcc.DB2Driver
+ * com.ibm.db2.jcc.DB2Driver
- * The following jars will have to be used
- * <code>db2jcc.jar</code> and <code>db2jcc_license_c.jar</code>.
+ *
+ * The following jars will have to be used <code>db2jcc.jar</code> and
+ * <code>db2jcc_license_c.jar</code>.
*/
public class DerbyDatabase implements Database {
*/
public class DerbyDatabase implements Database {
- private static final Logger LOGGER
- = Logger.getLogger( DerbyDatabase.class );
-
+ private static final Logger LOGGER = Logger.getLogger(DerbyDatabase.class);
+
/**
* Database user name.
*/
/**
* Database user name.
*/
* {@link #DATABASE_PATH}.
*/
private static final String SYSTEM_PATH_PROPERTY = "derby.system.home";
* {@link #DATABASE_PATH}.
*/
private static final String SYSTEM_PATH_PROPERTY = "derby.system.home";
- * Constructs derby database class to allow creation of derby
- * database instances.
+ * Constructs derby database class to allow creation of derby database
+ * instances.
- public DerbyDatabase( ) {
+ public DerbyDatabase() {
// Empty
}
/*
* (non-Javadoc)
// Empty
}
/*
* (non-Javadoc)
* @see org.wamblee.persistence.Database#start()
*/
* @see org.wamblee.persistence.Database#start()
*/
- public void start( ) throws Exception {
- // 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( );
+ 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.
}
/**
* 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
*/
* @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 {
+ private void waitUntilStartedOrStopped(boolean aStarted)
+ throws InterruptedException {
- while ( aStarted != isStarted( ) ) {
- Thread.sleep( POLL_INTERVAL );
+ while (aStarted != isStarted()) {
+ Thread.sleep(POLL_INTERVAL);
lWaited += POLL_INTERVAL;
lWaited += POLL_INTERVAL;
- if ( lWaited > MAX_WAIT_TIME ) {
- throw new RuntimeException(
- "Derby database did not start within " + MAX_WAIT_TIME
- + "ms" );
+ 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.
}
}
}
/**
* Checks if the database server has started or not.
* @return True if started, false otherwise.
*/
* @return True if started, false otherwise.
*/
- private boolean isStarted( ) {
+ private boolean isStarted() {
- } catch ( Exception e ) {
+ } catch (Exception e) {
return false;
}
}
/**
* Gets the controller for the database server.
return false;
}
}
/**
* Gets the controller for the database server.
* @return Controller.
* @throws Exception
*/
* @return Controller.
* @throws Exception
*/
- private NetworkServerControl getControl( ) throws Exception {
- return new NetworkServerControl( );
+ private NetworkServerControl getControl() throws Exception {
+ return new NetworkServerControl();
}
/**
* Runs the database.
}
/**
* Runs the database.
- private void runDatabase( ) {
+ private void runDatabase() {
- getControl( ).start( new PrintWriter( System.out ) );
- } catch ( Exception e ) {
- throw new RuntimeException( e );
+ 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;";
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.persistence.Database#getJdbcUrl()
+ */
+ public String getJdbcUrl() {
+ return getBaseJdbcUrl()
+ + ";create=true;retrieveMessagesFromServerOnGetMessage=true;";
- private String getBaseJdbcUrl( ) {
+ private String getBaseJdbcUrl() {
return "jdbc:derby:" + DATABASE_NAME;
}
return "jdbc:derby:" + DATABASE_NAME;
}
* @see org.wamblee.persistence.Database#getExternalJdbcUrl()
*/
* @see org.wamblee.persistence.Database#getExternalJdbcUrl()
*/
- public String getExternalJdbcUrl( ) {
+ public String getExternalJdbcUrl() {
return "jdbc:derby:net://localhost:1527/" + DATABASE_NAME;
}
/**
* Shuts down the derby database and cleans up all created files.
return "jdbc:derby:net://localhost:1527/" + DATABASE_NAME;
}
/**
* Shuts down the derby database and cleans up all created files.
- private void shutdownDerby( ) {
+ private void shutdownDerby() {
- 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." );
+ 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( ) {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.persistence.Database#getDriverClassName()
+ */
+ public String getDriverClassName() {
return ClientDriver.class.getName();
}
/**
* Gets the user name.
*/
return ClientDriver.class.getName();
}
/**
* Gets the user name.
*/
- public String getUsername( ) {
+ public String getUsername() {
return USERNAME;
}
/**
* Gets the password.
return USERNAME;
}
/**
* Gets the password.
- public String getPassword( ) {
+ public String getPassword() {
- /*
- * (non-Javadoc)
- * @see org.wamblee.persistence.Database#createConnection()
- */
- public Connection createConnection( ) throws SQLException {
- Connection c =
- DriverManager.getConnection( getJdbcUrl( ), getUsername( ),
- getPassword( ) );
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.persistence.Database#createConnection()
+ */
+ public Connection createConnection() throws SQLException {
+ Connection c = DriverManager.getConnection(getJdbcUrl(), getUsername(),
+ getPassword());
/**
* Stops the derby database and cleans up all derby files.
*/
/**
* Stops the derby database and cleans up all derby files.
*/
- public void stop( ) throws Exception {
- // shutdown network server.
- getControl( ).shutdown( );
- waitUntilStartedOrStopped( false );
-
- // shutdown inmemory access.
- shutdownDerby( );
- cleanPersistentStorage( );
+ 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.
*/
}
/**
* Cleans up persistent storage of Derby.
*/
- private void cleanPersistentStorage( ) {
- File lFile = new File( DATABASE_PATH );
+ 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.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.
+ if (!lFile.isDirectory()) {
+ return; // no-op already cleanup up.
- FileSystemUtils.deleteDirRecursively( DATABASE_PATH );
+ FileSystemUtils.deleteDirRecursively(DATABASE_PATH);
* 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.
* 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 {
package org.wamblee.persistence;
import java.sql.Connection;
import java.sql.SQLException;
public class OracleDatabase implements Database {
public OracleDatabase() {
super();
}
public OracleDatabase() {
super();
}
- public void start( ) throws Exception {
- System.out.println("Oracle must be started externally");
+ public void start() {
+ System.out.println("Oracle must be started externally");
- public String getJdbcUrl( ) {
+ public String getJdbcUrl() {
- public String getExternalJdbcUrl( ) {
+ public String getExternalJdbcUrl() {
- public String getDriverClassName( ) {
+ public String getDriverClassName() {
- public String getUsername( ) {
+ public String getUsername() {
- public String getPassword( ) {
+ public String getPassword() {
- public Connection createConnection( ) throws SQLException {
+ public Connection createConnection() throws SQLException {
- public void stop( ) throws Exception {
System.out.println("Oracle must be stopped externally");
}
System.out.println("Oracle must be stopped externally");
}