X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fpersistence%2FDerbyDatabase.java;h=86d9bc5c98022cdabef0954f9ceacd543694f0d8;hb=72a3282da475e8b9e7a6c7395412ca7936c7ee8e;hp=1385defd1c0cab3646ec0feb871e2732547cf132;hpb=dc05aae40ab88d4224d37f53c35d516107a3b031;p=utils diff --git a/support/general/src/test/java/org/wamblee/persistence/DerbyDatabase.java b/support/general/src/test/java/org/wamblee/persistence/DerbyDatabase.java index 1385defd..86d9bc5c 100644 --- a/support/general/src/test/java/org/wamblee/persistence/DerbyDatabase.java +++ b/support/general/src/test/java/org/wamblee/persistence/DerbyDatabase.java @@ -12,7 +12,7 @@ * 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; @@ -30,28 +30,30 @@ import org.apache.derby.jdbc.EmbeddedDriver; 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 + * *
* jdbc:derby:net://localhost:1527/testdb *+ * * and the driver class is + * *
- * com.ibm.db2.jcc.DB2Driver + * com.ibm.db2.jcc.DB2Driver *- * The following jars will have to be used - *
db2jcc.jar
and db2jcc_license_c.jar
.
+ *
+ * The following jars will have to be used db2jcc.jar
and
+ * db2jcc_license_c.jar
.
*/
public class DerbyDatabase implements Database {
-
+
/**
* Logger.
*/
- private static final Logger LOGGER
- = Logger.getLogger( DerbyDatabase.class );
-
+ private static final Logger LOGGER = Logger.getLogger(DerbyDatabase.class);
+
/**
* Database user name.
*/
@@ -87,167 +89,175 @@ public class DerbyDatabase implements Database {
* {@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)
+ *
* @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.
- *
+ *
* @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 {
long lWaited = 0;
- while ( aStarted != isStarted( ) ) {
- Thread.sleep( POLL_INTERVAL );
+ 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" );
+ 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( ) {
+ private boolean isStarted() {
try {
- getControl( ).ping( );
+ getControl().ping();
return true;
- } catch ( Exception e ) {
+ } catch (Exception e) {
return false;
}
}
/**
* Gets the controller for the database server.
- *
+ *
* @return Controller.
* @throws Exception
*/
- private NetworkServerControl getControl( ) throws Exception {
- return new NetworkServerControl( );
+ private NetworkServerControl getControl() throws Exception {
+ return new NetworkServerControl();
}
/**
* Runs the database.
- *
+ *
*/
- private void runDatabase( ) {
+ private void runDatabase() {
try {
- 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;
}
-
+
/*
* (non-Javadoc)
+ *
* @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.
- *
+ *
*/
- private void shutdownDerby( ) {
+ 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." );
+ 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.
*/
- public String getUsername( ) {
+ public String getUsername() {
return USERNAME;
}
/**
* Gets the password.
+ *
* @return
*/
- public String getPassword( ) {
+ 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( ) );
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.persistence.Database#createConnection()
+ */
+ public Connection createConnection() throws SQLException {
+ Connection c = DriverManager.getConnection(getJdbcUrl(), getUsername(),
+ getPassword());
return c;
}
@@ -255,31 +265,35 @@ public class DerbyDatabase implements Database {
/**
* 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.
*/
- 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);
}
}