Skip to content

Commit

Permalink
#220 Direct connection configuration in DbConfig...
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Polevoy committed May 15, 2015
1 parent 7abc7bc commit 57612cc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
Expand Up @@ -72,18 +72,31 @@
* <li>Line 5: This is a configuration of DB connection for "hudson" environment, but for "testing" mode. The "hudson"
* environment is a computer where this project is built by Hudson - the continuous integration server. Since Hudson
* computer is fully automated, and this project is not running there in "standard" mode, there is no standard configuration
* for hudson environment, just one for testing.
* for Jenkins environment, just one for testing.
* <li>Line 6: This is configuration similar to one on line 3, but for "production" environment.
* </ul>
*
* @author Igor Polevoy
*/
public abstract class AbstractDBConfig extends AppConfig {

/**
* @param environment name of environment (corresponds to env var ACTIVE_ENV)
* @return builder instance
*/
public ConnectionBuilder environment(String environment) {
return new ConnectionBuilder(environment);
}

/**
* @param environment name of environment (corresponds to env var ACTIVE_ENV)
* @param override true to override any previous configuration for the same environment.
* @return builder instance
*/
public ConnectionBuilder environment(String environment, boolean override) {
return new ConnectionBuilder(environment, override);
}

/**
* Configures multiple database connections from a single property file. Example content for such file:
*
Expand Down Expand Up @@ -155,15 +168,15 @@ private void createJdbcWrapper(String env, String driver, String url, String use
}
ConnectionJdbcSpec connectionSpec = new ConnectionJdbcSpec(driver, url, userName, password);
wrapper.setConnectionSpec(connectionSpec);
Configuration.addConnectionWrapper(wrapper);
Configuration.addConnectionWrapper(wrapper, false);
}

private void createJndiWrapper(String env, String jndiName) {
ConnectionSpecWrapper wrapper = new ConnectionSpecWrapper();
wrapper.setEnvironment(env);
ConnectionJndiSpec connectionSpec = new ConnectionJndiSpec(jndiName);
wrapper.setConnectionSpec(connectionSpec);
Configuration.addConnectionWrapper(wrapper);
Configuration.addConnectionWrapper(wrapper, false);
}


Expand Down
Expand Up @@ -222,10 +222,10 @@ public static boolean rollback() {
return Boolean.parseBoolean(get(Params.rollback.toString().trim()));
}

protected static void addConnectionWrapper(ConnectionSpecWrapper connectionWrapper) {
protected static void addConnectionWrapper(ConnectionSpecWrapper connectionWrapper, boolean override) {
String connectionWrapperEnv = connectionWrapper.getEnvironment();
List<ConnectionSpecWrapper> envConnectionWrappers = connectionWrappers.get(connectionWrapperEnv);
if(envConnectionWrappers == null) {
if(envConnectionWrappers == null || override) {
envConnectionWrappers = new ArrayList<ConnectionSpecWrapper>();
connectionWrappers.put(connectionWrapperEnv, envConnectionWrappers);
}
Expand Down
Expand Up @@ -33,7 +33,13 @@ public class ConnectionBuilder {
ConnectionBuilder(String environment) {
connectionWrapper = new ConnectionSpecWrapper();
connectionWrapper.setEnvironment(environment);
Configuration.addConnectionWrapper(connectionWrapper);
Configuration.addConnectionWrapper(connectionWrapper, false);
}

ConnectionBuilder(String environment, boolean override) {
connectionWrapper = new ConnectionSpecWrapper();
connectionWrapper.setEnvironment(environment);
Configuration.addConnectionWrapper(connectionWrapper, override);
}

/**
Expand Down
Expand Up @@ -5,7 +5,10 @@
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.javalite.test.jspec.JSpec.a;
import static org.javalite.test.jspec.JSpec.the;

/**
* @author Igor Polevoy on 12/1/14.
Expand Down Expand Up @@ -124,4 +127,28 @@ public void init(AppContext appContext) {
a(connectionSpec.getUser()).shouldBeEqual("mary");
a(connectionSpec.getPassword()).shouldBeEqual("pwd1");
}



@Test
public void shouldOverrideConnectionSpecForTheSameEnvironment(){

class DBConfig extends AbstractDBConfig{
public void init(AppContext appContext) {
configFile("/database_new.properties");
environment("production", true).jndi("java:comp/env/jdbc/prod_new");
}
}

DBConfig config = new DBConfig();
config.init(null);

List<ConnectionSpecWrapper> wrappers = Configuration.getConnectionSpecWrappers("production");

//we configured two, one in file, one in class. But the class config overrides one in file.
the(wrappers.size()).shouldBeEqual(1);

ConnectionJndiSpec connectionSpec = (ConnectionJndiSpec) wrappers.get(0).getConnectionSpec();
the(connectionSpec.getDataSourceJndiName()).shouldBeEqual("java:comp/env/jdbc/prod_new");
}
}
15 changes: 15 additions & 0 deletions activeweb/src/test/resources/database_new.properties
@@ -0,0 +1,15 @@
development.driver=com.mysql.jdbc.Driver
development.username=john
development.password=pwd
development.url=jdbc:mysql://localhost/proj_dev


test.driver=com.mysql.jdbc.Driver
test.username=mary
test.password=pwd1
test.url=jdbc:mysql://localhost/test

production.driver=com.mysql.jdbc.Driver
production.username=mary
production.password=pwd1
production.url=jdbc:mysql://localhost/prod

0 comments on commit 57612cc

Please sign in to comment.