Skip to content

Commit 044baef

Browse files
author
Igor Polevoy
committed
#279 Improve database.properties configuration for tests
1 parent bb3e090 commit 044baef

File tree

5 files changed

+87
-16
lines changed

5 files changed

+87
-16
lines changed

activeweb/src/main/java/org/javalite/activeweb/AbstractDBConfig.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ public void configFile(String file) {
145145
String userName = props.getProperty(env + ".username");
146146
String password = props.getProperty(env + ".password");
147147
String url = props.getProperty(env + ".url");
148-
if (driver == null || userName == null || password == null || url == null) {
149-
throw new InitException("Four JDBC properties are expected: driver, username, password, url for environment: " + env);
150-
}
148+
checkProps(driver, userName, password, url, env);
151149
createJdbcWrapper(env, driver, url, userName, password);
152150
}
153151
}
@@ -158,11 +156,20 @@ public void configFile(String file) {
158156
}
159157
}
160158

159+
private void checkProps(String driver, String userName, String password, String url, String env){
160+
if (driver == null || userName == null || password == null || url == null){
161+
throw new InitException("Four JDBC properties are expected: driver, username, password, url for environment: " + env);
162+
}
163+
}
164+
161165
private void createJdbcWrapper(String env, String driver, String url, String userName, String password) {
162166
ConnectionSpecWrapper wrapper = new ConnectionSpecWrapper();
163167
if(env.equals("test")){
164168
wrapper.setEnvironment("development");
165169
wrapper.setTesting(true);
170+
} else if(env.endsWith(".test")) {
171+
wrapper.setEnvironment(env.split("\\.")[0]);
172+
wrapper.setTesting(true);
166173
}else{
167174
wrapper.setEnvironment(env);
168175
}
@@ -181,12 +188,18 @@ private void createJndiWrapper(String env, String jndiName) {
181188

182189

183190
private Set<String> getEnvironments(Properties props) {
184-
Set<String> environments = new HashSet<String>();
185-
for (Object k : props.keySet()) {
186-
String environment = k.toString().split("\\.")[0];
187-
environments.add(environment);
191+
Set<String> environments = new HashSet<>();
192+
for (String prop : props.stringPropertyNames()) {
193+
String[] parts = prop.split("\\.");
194+
if(parts.length == 2){
195+
environments.add(parts[0]);
196+
}else if(parts.length == 3 && parts[1].equals("test")){
197+
environments.add(parts[0] + ".test");
198+
}else {
199+
throw new ConfigurationException("Incorrect property: " + prop);
200+
}
188201
}
189-
return new TreeSet<String>(environments);
202+
return environments;
190203
}
191204

192205
private Properties readPropertyFile(String file) throws IOException {

activeweb/src/main/java/org/javalite/activeweb/Configuration.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ enum Params {
3939
freeMarkerConfig, route_config, maxUploadSize
4040
}
4141

42-
private static final Configuration instance = new Configuration();
4342
private static Properties props = new Properties();
4443
private static TemplateManager templateManager;
45-
private static HashMap<String, List<ConnectionSpecWrapper>> connectionWrappers = new HashMap<String, List<ConnectionSpecWrapper>>();
44+
private static HashMap<String, List<ConnectionSpecWrapper>> connectionWrappers = new HashMap<>();
4645
private static boolean testing = false;
4746
private static String ENV;
4847
private static boolean activeReload = !blank(System.getProperty("active_reload")) && System.getProperty("active_reload").equals("true");
@@ -254,7 +253,7 @@ protected static void addConnectionWrapper(ConnectionSpecWrapper connectionWrapp
254253
String connectionWrapperEnv = connectionWrapper.getEnvironment();
255254
List<ConnectionSpecWrapper> envConnectionWrappers = connectionWrappers.get(connectionWrapperEnv);
256255
if(envConnectionWrappers == null || override) {
257-
envConnectionWrappers = new ArrayList<ConnectionSpecWrapper>();
256+
envConnectionWrappers = new ArrayList<>();
258257
connectionWrappers.put(connectionWrapperEnv, envConnectionWrappers);
259258
}
260259
envConnectionWrappers.add(connectionWrapper);
@@ -286,7 +285,7 @@ protected static void clearConnectionWrappers() {
286285

287286
//for tests only
288287
protected static void resetConnectionWrappers() {
289-
connectionWrappers = new HashMap<String, List<ConnectionSpecWrapper>>();
288+
connectionWrappers = new HashMap<>();
290289
}
291290

292291
protected static void clearConnectionWrappers(String env) {
@@ -306,6 +305,4 @@ public static File getTmpDir() {
306305
return new File(System.getProperty("java.io.tmpdir"));
307306
}
308307

309-
private static List<IgnoreSpec> ignoreSpecs = new ArrayList<IgnoreSpec>();
310-
311308
}

activeweb/src/test/java/org/javalite/activeweb/AbstractConnectionBuilderSpec.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void shouldOverrideConnectionSpecForTheSameEnvironment(){
135135

136136
class DBConfig extends AbstractDBConfig{
137137
public void init(AppContext appContext) {
138-
configFile("/database_new.properties");
138+
configFile("/database1.properties");
139139
environment("production", true).jndi("java:comp/env/jdbc/prod_new");
140140
}
141141
}
@@ -145,10 +145,51 @@ public void init(AppContext appContext) {
145145

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

148-
//we configured two, one in file, one in class. But the class config overrides one in file.
148+
//we configured two for production, one in file, one in class. But the class config overrides one in file.
149149
the(wrappers.size()).shouldBeEqual(1);
150150

151151
ConnectionJndiSpec connectionSpec = (ConnectionJndiSpec) wrappers.get(0).getConnectionSpec();
152152
the(connectionSpec.getDataSourceJndiName()).shouldBeEqual("java:comp/env/jdbc/prod_new");
153153
}
154+
155+
156+
/**
157+
* This feature is needed because often times, you have different configuration locally vs another
158+
* environment where you need to run tests. It is possible to achieve with Maven profiles, but kind of hacky.
159+
* Example: development env requires to connect to localhost. Jenkins when running tests, requires to connect to jenkins_db host.
160+
* This feature is not specific to Jenkins :)
161+
*
162+
* The new solution is clean.
163+
*/
164+
@Test
165+
public void should_configure_different_test_configs_for_development_and_jenkins(){
166+
167+
class DBConfig extends AbstractDBConfig{
168+
public void init(AppContext appContext) {
169+
configFile("/database2.properties");
170+
}
171+
}
172+
173+
DBConfig config = new DBConfig();
174+
config.init(null);
175+
176+
List<ConnectionSpecWrapper> wrappers = Configuration.getConnectionSpecWrappers("development");
177+
178+
the(wrappers.size()).shouldBeEqual(2);
179+
180+
181+
ConnectionSpecWrapper dev = null;
182+
ConnectionSpecWrapper test = null;
183+
184+
//have to do this because the order of specs is not deterministic
185+
for (ConnectionSpecWrapper connectionSpecWrapper : wrappers) {
186+
if(connectionSpecWrapper.isTesting()){
187+
test = connectionSpecWrapper;
188+
}else {
189+
dev = connectionSpecWrapper;
190+
}
191+
}
192+
the(((ConnectionJdbcSpec)test.getConnectionSpec()).getUrl()).shouldBeEqual("jdbc:mysql://localhost/test");
193+
the(((ConnectionJdbcSpec)dev.getConnectionSpec()).getUrl()).shouldBeEqual("jdbc:mysql://localhost/dev");
194+
}
154195
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
development.driver=com.mysql.jdbc.Driver
2+
development.username=localdev
3+
development.password=pwd
4+
development.url=jdbc:mysql://localhost/dev
5+
6+
7+
development.test.driver=com.mysql.jdbc.Driver
8+
development.test.username=localtest
9+
development.test.password=pwd1
10+
development.test.url=jdbc:mysql://localhost/test
11+
12+
jenkins.test.driver=com.mysql.jdbc.Driver
13+
jenkins.test.username=jenkins
14+
jenkins.test.password=pwd1
15+
jenkins.test.url=jdbc:mysql://localhost/test_jenkins
16+
17+
production.driver=com.mysql.jdbc.Driver
18+
production.username=mary
19+
production.password=pwd1
20+
production.url=jdbc:mysql://localhost/prod

0 commit comments

Comments
 (0)