Skip to content

Commit

Permalink
Refactoring in ConnectionFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gergely-horvath committed Aug 6, 2017
1 parent ac5d356 commit 3a4cf60
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
50 changes: 41 additions & 9 deletions src/main/java/com/github/dyna4jdbc/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,57 @@ Hence, we intentionally and knowingly catch all Throwables (including
}
}

protected Connection newConnection(String connectionTypeName, String config, Properties properties)
throws Exception {

protected Connection newConnection(String connectionType, String config, Properties properties) throws Exception {
ConnectionType connectionType = ConnectionType.getByName(connectionTypeName);

switch (connectionType) {
case "process-runner":
return new ProcessRunnerConnection(config, properties);
return connectionType.newConnection(config, properties);

}

case "scriptengine":
private enum ConnectionType {
SCRIPTENGINE("scriptengine") {
@Override
protected Connection newConnection(String config, Properties properties)
throws MisconfigurationException, SQLException {
return new ScriptEngineConnection(config, properties);
}
},
PROCESS_RUNNER("process-runner") {
@Override
protected Connection newConnection(String config, Properties properties)
throws MisconfigurationException, SQLException {
return new ProcessRunnerConnection(config, properties);
}
},
NODE_JS("nodejs") {
@Override
protected Connection newConnection(String config, Properties properties)
throws MisconfigurationException, SQLException {
return new NodeJsConnection(config, properties);
}
};

private final String publicName;

case "nodejs":
return new NodeJsConnection(config, properties);
ConnectionType(String publicName) {
this.publicName = publicName;
}

private static ConnectionType getByName(String requestedPublicName) throws MisconfigurationException {
for (ConnectionType ct : ConnectionType.values()) {
if (ct.publicName.equals(requestedPublicName)) {
return ct;
}
}

default:
throw MisconfigurationException.forMessage("No such connection type: '%s'", connectionType);
throw MisconfigurationException.forMessage("No such connection type: '%s'", requestedPublicName);
}


protected abstract Connection newConnection(String config, Properties properties)
throws MisconfigurationException, SQLException;
}

}
16 changes: 8 additions & 8 deletions src/test/java/com/github/dyna4jdbc/ConnectionFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public void testConnectionFactorySplitsConfigurationCorrectly() throws SQLExcept

ConnectionFactory mockConnectionFactory = new ConnectionFactory() {
@Override
protected Connection newConnection(String connectionType, String config, Properties properties) throws Exception {
protected Connection newConnection(String connectionTypeName, String config, Properties properties) throws Exception {

assertEquals(connectionType, "foo");
assertEquals(connectionTypeName, "foo");

assertEquals(config, "bar:baz");

Expand All @@ -61,9 +61,9 @@ public void testConnectionFactoryPassesNullWhenColonIsPresent() throws SQLExcept

ConnectionFactory mockConnectionFactory = new ConnectionFactory() {
@Override
protected Connection newConnection(String connectionType, String config, Properties properties) throws Exception {
protected Connection newConnection(String connectionTypeName, String config, Properties properties) throws Exception {

assertEquals(connectionType, "foo");
assertEquals(connectionTypeName, "foo");

assertEquals(config, null);

Expand All @@ -84,9 +84,9 @@ public void testConnectionFactoryPassesEmptyStringWhenColonIsPresent() throws SQ

ConnectionFactory mockConnectionFactory = new ConnectionFactory() {
@Override
protected Connection newConnection(String connectionType, String config, Properties properties) throws Exception {
protected Connection newConnection(String connectionTypeName, String config, Properties properties) throws Exception {

assertEquals(connectionType, "foo");
assertEquals(connectionTypeName, "foo");

assertEquals(config, "");

Expand All @@ -107,9 +107,9 @@ public void testConnectionFactoryConvertsToLowerCase() throws SQLException {

ConnectionFactory mockConnectionFactory = new ConnectionFactory() {
@Override
protected Connection newConnection(String connectionType, String config, Properties properties) throws Exception {
protected Connection newConnection(String connectionTypeName, String config, Properties properties) throws Exception {

assertEquals(connectionType, "foobar");
assertEquals(connectionTypeName, "foobar");

assertEquals(config, "");

Expand Down

0 comments on commit 3a4cf60

Please sign in to comment.