Skip to content

Commit

Permalink
Refactored packages and updated documentation
Browse files Browse the repository at this point in the history
Created and re-factored classes to the `utils` and `datasource`
sub-packages. Updated and re-factored documentation. Moved up to Apache
Derby 10.13.x and other dependencies (with deprecated code cleanup).
Javadoc and other documentation updates/cleanup.

Change-Id: I204b7fc93c2d3379d90c1e9f91ac81bcfd5832a1
Signed-off-by: Bindul Bhowmik <bindulbhowmik@gmail.com>
  • Loading branch information
bindul committed Dec 22, 2016
1 parent e3f7cbe commit 77acce6
Show file tree
Hide file tree
Showing 55 changed files with 936 additions and 444 deletions.
16 changes: 14 additions & 2 deletions .gitattributes
@@ -1,8 +1,20 @@
# Auto detect text files and perform LF normalization
* text=auto

# Custom for Visual Studio
*.cs diff=csharp
# Known text files
*.java text
*.properties text
*.xml text
*.yml text
*.md text
*.sh text
*.css text
*.txt text
*.nojekyll text

# Known binary files
*.jar binary
*.png binary

# Standard to msysgit
*.doc diff=astextplain
Expand Down
5 changes: 5 additions & 0 deletions NOTICE
@@ -0,0 +1,5 @@
Development Entropy JUnit Helper
Copyright (c) 2015-2016 Development Entropy (deventropy.org) Contributors

This product includes software developed at the Deventropy Project (https://github.com/deventropy). Licensed under the
Apache License, Version 2.0. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8 changes: 8 additions & 0 deletions junit-helper-derby/NOTICE
@@ -0,0 +1,8 @@
Development Entropy JUnit Helper
Copyright (c) 2015-2016 Development Entropy (deventropy.org) Contributors

This product includes software developed at the Deventropy Project (https://github.com/deventropy). Licensed under the
Apache License, Version 2.0. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

The JNDI DataSource implementation is inspired by Randy Carver's post at
https://blogs.oracle.com/randystuph/entry/injecting_jndi_datasources_for_junit
2 changes: 1 addition & 1 deletion junit-helper-derby/pom.xml
Expand Up @@ -22,7 +22,7 @@ limitations under the License.
<version>1.0-alpha.1-SNAPSHOT</version>
</parent>
<artifactId>junit-helper-derby</artifactId>
<name>JUnit Helper :: Derby</name>
<name>Deventropy JUnit Helper :: Derby</name>
<description>JUnit resources classes for Derby DB as an embedded resource</description>
<scm>
<connection>${project.parent.scm.connection}</connection>
Expand Down
3 changes: 3 additions & 0 deletions junit-helper-derby/src/changes/changes.xml
Expand Up @@ -43,6 +43,9 @@ limitations under the License.
<action dev="bindul" type="add" issue="23" date="2016-02-23">
Added support to perform an online backup of a Derby instance
</action>
<action dev="bindul" type="add" issue="24" date="2016-02-25">
Support returning JDBC Datasources for Derby
</action>
</release>
</body>
</document>
Expand Up @@ -93,8 +93,9 @@ public class DerbyResourceConfig {
// TODO Complete configuring logging
private ErrorLoggingMode errorLoggingMode;

// Post init scripts

/**
* Post init scripts
*/
private List<String> postInitScripts;

/**
Expand Down
Expand Up @@ -19,24 +19,20 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import javax.sql.XADataSource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource;
import org.apache.derby.jdbc.EmbeddedDataSource;
import org.apache.derby.jdbc.EmbeddedDataSourceInterface;
import org.apache.derby.jdbc.EmbeddedXADataSource;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.deventropy.junithelper.derby.util.DerbyBackupOperationsHelper;
import org.deventropy.junithelper.derby.util.DerbyScriptRunner;
import org.deventropy.junithelper.derby.util.DerbyUtils;
import org.deventropy.shared.utils.ArgumentCheck;
import org.junit.rules.ExternalResource;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -99,8 +95,10 @@
public class EmbeddedDerbyResource extends ExternalResource implements Closeable {

private final Logger log = LogManager.getLogger();

private final Charset defaultCharset = Charset.defaultCharset();

private final DerbyResourceConfig config;

private File derbySystemHome;
private TemporaryFolder derbySystemHomeParent;

Expand All @@ -110,7 +108,6 @@ public class EmbeddedDerbyResource extends ExternalResource implements Closeable
private String oldDerbySystemHomeValue;

private final DerbyBackupOperationsHelper backupOperationsHelper = new DerbyBackupOperationsHelper(this);
private final EmbeddedDerbyDataSourceFactory dataSourceFactory = new EmbeddedDerbyDataSourceFactoryImpl();

/**
* Creates a new Derby resource. All configurable parameters for this resource come from the config object
Expand Down Expand Up @@ -147,13 +144,27 @@ public EmbeddedDerbyResource (final DerbyResourceConfig dbResourceConfig,
this.jdbcUrl = buildJdbcUrl();
}

/**
* Allows extending classes to use the config.
* @return the config The resource config
*/
protected DerbyResourceConfig getConfig () {
return config;
}

private String buildJdbcUrl () {
final StringBuilder jdbcUrlBldr = new StringBuilder().append(config.getSubSubProtocol().jdbcConnectionPrefix());
appendDbLocNameToUrl(jdbcUrlBldr);
return jdbcUrlBldr.toString();
}

private void appendDbLocNameToUrl (final StringBuilder jdbcUrlBldr) {
/**
* Adds the database name to the Derby JDBC Url being built, taking into consideration sub-protocol specific
* variations.
*
* @param jdbcUrlBldr The String builder to which the database name is being added
*/
protected void appendDbLocNameToUrl (final StringBuilder jdbcUrlBldr) {
if (JdbcDerbySubSubProtocol.Jar == config.getSubSubProtocol()) {
// for :jar: protocol, see http://db.apache.org/derby/docs/10.12/devguide/cdevdeploy11201.html
jdbcUrlBldr.append('(').append(config.getJarDatabaseJarFile()).append(')');
Expand Down Expand Up @@ -236,11 +247,11 @@ private void executePostInitScripts () throws IOException, SQLException {
try {
final int result = scriptRunner.executeScript(postInitScript, scriptLogFile);
if (result != 0) {
log.warn(FileUtils.readFileToString(scriptLogFile));
log.warn(FileUtils.readFileToString(scriptLogFile, defaultCharset));
throw new IOException("Exceptions exist in script. See output for details");
}
} catch (IOException e) {
log.warn(FileUtils.readFileToString(scriptLogFile));
log.warn(FileUtils.readFileToString(scriptLogFile, defaultCharset));
throw new IOException("Exceptions exist in script. See output for details");
}
}
Expand All @@ -266,7 +277,7 @@ private String buildCreateJDBCUrl () {
.append(config.getDbRecoveryLogDevice().getAbsolutePath());
}
} else if (JdbcDerbySubSubProtocol.Memory == subSubProtocol
|| (JdbcDerbySubSubProtocol.Directory == subSubProtocol && !config.isDirectoryDatabaseSkipCreate())) {
|| JdbcDerbySubSubProtocol.Directory == subSubProtocol && !config.isDirectoryDatabaseSkipCreate()) {
// Only :memory: and :directory: databases need the 'create' flag.
createDbJdbcUrl.append(DerbyConstants.URLPROP_DERBY_CREATE);
}
Expand Down Expand Up @@ -442,94 +453,4 @@ public void backupLiveDatabase (final File backupDir, final boolean waitForTrans
backupOperationsHelper.backupLiveDatabase(backupDir, waitForTransactions, enableArchiveLogging,
deleteArchivedLogs);
}

/**
* Returns the {@link EmbeddedDerbyDataSourceFactory} instance for this resource from which data sources can be
* created / cached. The factory returned supports caching <code>ataSource</code>s created. It also checks the
* state of <code>this</code> instance and will throw {@link IllegalStateException} if the resource is not
* {@link #isActive()}.
*
* @return Factory to create data sources for this instance.
*/
public EmbeddedDerbyDataSourceFactory getDataSourceFactory () {
return dataSourceFactory;
}

private class EmbeddedDerbyDataSourceFactoryImpl implements EmbeddedDerbyDataSourceFactory {

private EmbeddedDataSource embeddedDataSource;
private EmbeddedConnectionPoolDataSource embeddedConnectionPoolDataSource;
private EmbeddedXADataSource embeddedXADataSource;

/* (non-Javadoc)
* @see org.deventropy.junithelper.derby.EmbeddedDerbyDataSourceFactory#getDataSource(boolean)
*/
@Override
public DataSource getDataSource (final boolean cachedInstance) {
ensureActive();
if (cachedInstance) {
if (null == embeddedDataSource) {
embeddedDataSource = createEmbeddedDataSource();
}
return embeddedDataSource;
}
return createEmbeddedDataSource();
}

private EmbeddedDataSource createEmbeddedDataSource () {
final EmbeddedDataSource embeddedDs = new EmbeddedDataSource();
setupDataSource(embeddedDs);
return embeddedDs;
}

/* (non-Javadoc)
* @see org.deventropy.junithelper.derby.EmbeddedDerbyDataSourceFactory#getConnectionPoolDataSource(boolean)
*/
@Override
public ConnectionPoolDataSource getConnectionPoolDataSource (final boolean cachedInstance) {
ensureActive();
if (cachedInstance) {
if (null == embeddedConnectionPoolDataSource) {
embeddedConnectionPoolDataSource = createEmbeddedConnectionPoolDataSource();
}
return embeddedConnectionPoolDataSource;
}
return createEmbeddedConnectionPoolDataSource();
}

private EmbeddedConnectionPoolDataSource createEmbeddedConnectionPoolDataSource () {
final EmbeddedConnectionPoolDataSource connectionPoolDataSource = new EmbeddedConnectionPoolDataSource();
setupDataSource(connectionPoolDataSource);
return connectionPoolDataSource;
}

/* (non-Javadoc)
* @see org.deventropy.junithelper.derby.EmbeddedDerbyDataSourceFactory#getXADataSource(boolean)
*/
@Override
public XADataSource getXADataSource (final boolean cachedInstance) {
ensureActive();
if (cachedInstance) {
if (null == embeddedXADataSource) {
embeddedXADataSource = createEmbeddedXADataSource();
}
return embeddedXADataSource;
}
return createEmbeddedXADataSource();
}

private EmbeddedXADataSource createEmbeddedXADataSource () {
final EmbeddedXADataSource xaDataSource = new EmbeddedXADataSource();
setupDataSource(xaDataSource);
return xaDataSource;
}

private void setupDataSource (final EmbeddedDataSourceInterface dataSource) {
final StringBuilder dsDatabaseName = new StringBuilder()
.append(config.getSubSubProtocol().datasourceDatabaseNamePrefix());
appendDbLocNameToUrl(dsDatabaseName);
dataSource.setDatabaseName(dsDatabaseName.toString());
}

}
}
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.deventropy.junithelper.derby;
package org.deventropy.junithelper.derby.datasource;

import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
Expand Down Expand Up @@ -49,7 +49,7 @@ public interface EmbeddedDerbyDataSourceFactory {
* @return A DataSource instance
* @throws IllegalStateException If the underlying implementation does state checking and the resource is not active
*/
DataSource getDataSource (final boolean cachedInstance);
DataSource getDataSource (boolean cachedInstance);

/**
* Returns a new or cached {@link ConnectionPoolDataSource} instance with parameters set up to create connections to
Expand All @@ -62,7 +62,7 @@ public interface EmbeddedDerbyDataSourceFactory {
* @return A ConnectionPoolDataSource instance
* @throws IllegalStateException If the underlying implementation does state checking and the resource is not active
*/
ConnectionPoolDataSource getConnectionPoolDataSource (final boolean cachedInstance);
ConnectionPoolDataSource getConnectionPoolDataSource (boolean cachedInstance);

/**
* Returns a new or cached {@link XADataSource} instance with parameters set up to create connections to
Expand All @@ -75,5 +75,5 @@ public interface EmbeddedDerbyDataSourceFactory {
* @return A XADataSource instance
* @throws IllegalStateException If the underlying implementation does state checking and the resource is not active
*/
XADataSource getXADataSource (final boolean cachedInstance);
XADataSource getXADataSource (boolean cachedInstance);
}

0 comments on commit 77acce6

Please sign in to comment.