Skip to content

Commit

Permalink
JBERET-472: Extract database props from env-vars
Browse files Browse the repository at this point in the history
  • Loading branch information
DaScheid authored and liweinan committed Feb 16, 2024
1 parent a1798cf commit ecf9ab5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
5 changes: 5 additions & 0 deletions jberet-se/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ SPDX-License-Identifier: EPL-2.0
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
23 changes: 12 additions & 11 deletions jberet-se/src/main/java/org/jberet/se/BatchSEEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.concurrent.BlockingQueue;
Expand Down Expand Up @@ -62,7 +63,7 @@ public final class BatchSEEnvironment implements BatchEnvironment {
private final JobXmlResolver jobXmlResolver;
private final JobExecutor executor;

private static final String PROP_PATTERN_STR = "\\$\\{([0-9a-zA-Z_\\-]+)(:([0-9a-zA-Z_\\-]*))?\\}";
private static final String PROP_PATTERN_STR = "\\$\\{([0-9a-zA-Z_\\-]+)(:([0-9a-zA-Z_\\-;:=/.]*))?\\}";

private static final Pattern PROP_PATTERN = Pattern.compile(PROP_PATTERN_STR);

Expand All @@ -80,8 +81,13 @@ public final class BatchSEEnvironment implements BatchEnvironment {
static final String THREAD_POOL_REJECTION_POLICY = "thread-pool-rejection-policy";
static final String THREAD_FACTORY = "thread-factory";

static final String DB_URL_KEY = "db-url";
static final String DB_USER_KEY = "db-user";
static final String DB_PASSWORD_KEY = "db-password";
static final String DB_TABLE_PREFIX_KEY = "db-table-prefix";
static final String DB_TABLE_SUFFIX_KEY = "db-table-suffix";

static final List<String> PROP_NAMES_THAT_COULD_HAVE_A_DEFAULT_VALUE = List.of(DB_URL_KEY, DB_USER_KEY, DB_PASSWORD_KEY, DB_TABLE_PREFIX_KEY, DB_TABLE_SUFFIX_KEY);

public BatchSEEnvironment() {
configProperties = new Properties();
Expand All @@ -91,16 +97,11 @@ public BatchSEEnvironment() {
configProperties.load(configStream);
if (configProperties.getProperty(JOB_REPOSITORY_TYPE_KEY).equals(REPOSITORY_TYPE_JDBC) ||
configProperties.getProperty(JOB_REPOSITORY_TYPE_KEY).equals(REPOSITORY_TYPE_MONGODB)) {

String dbUser = configProperties.getProperty(DB_USER_KEY);
String dbPassword = configProperties.getProperty(DB_PASSWORD_KEY);

if (dbUser != null) {
configProperties.setProperty(DB_USER_KEY, parseProp(dbUser));
}

if (dbPassword != null) {
configProperties.setProperty(DB_PASSWORD_KEY, parseProp(dbPassword));
for (String propName : PROP_NAMES_THAT_COULD_HAVE_A_DEFAULT_VALUE) {
final String propVal = configProperties.getProperty(propName);
if (propVal != null) {
configProperties.setProperty(propName, parseProp(propVal));
}
}
}
} catch (final IOException e) {
Expand Down
50 changes: 27 additions & 23 deletions jberet-se/src/test/java/org/jberet/se/BatchSEEnvironmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import jakarta.batch.operations.BatchRuntimeException;

import org.jberet.repository.JdbcRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.jberet.se.BatchSEEnvironment.THREAD_FACTORY;
import static org.jberet.se.BatchSEEnvironment.THREAD_POOL_ALLOW_CORE_THREAD_TIMEOUT;
Expand Down Expand Up @@ -156,30 +161,29 @@ public void testResolveDbProps() {
.getProperty(JdbcRepository.DB_USER_KEY));
}

@ParameterizedTest
@MethodSource("provideValuesForTestingPropParsing")
public void testPropParsings(String propValue, String expectedAfterParsing) {
Assertions.assertEquals(expectedAfterParsing, BatchSEEnvironment.parseProp(propValue));
}

@Test
public void testPropParsings() {
String p1 = "${FOO:bar}";
String p2 = "${FOO}";
String p3 = "${BAR:defaultVal}";
String p4 = "${INVALID";
String p5 = "$INVALID}";
String p6 = "${{INVALID}";
String p7 = "${INVALID}}";
String p8 = "${BAR:}";
String p9 = "${}";
String p10 = "${BAR}";


Assertions.assertEquals("foo", BatchSEEnvironment.parseProp(p1));
Assertions.assertEquals("foo", BatchSEEnvironment.parseProp(p2));
Assertions.assertEquals("defaultVal", BatchSEEnvironment.parseProp(p3));
Assertions.assertEquals("${INVALID", BatchSEEnvironment.parseProp(p4));
Assertions.assertEquals("$INVALID}", BatchSEEnvironment.parseProp(p5));
Assertions.assertEquals("${{INVALID}", BatchSEEnvironment.parseProp(p6));
Assertions.assertEquals("${INVALID}}", BatchSEEnvironment.parseProp(p7));
Assertions.assertEquals("", BatchSEEnvironment.parseProp(p8));
Assertions.assertEquals("${}", BatchSEEnvironment.parseProp(p9));
Assertions.assertNull(BatchSEEnvironment.parseProp(p10));
public void testPropParsingWhenEnvVarIsNull() {
Assertions.assertNull(BatchSEEnvironment.parseProp("${BAR}"));
}

private static Stream<Arguments> provideValuesForTestingPropParsing() {
return Stream.of(Arguments.of("${FOO:bar}", "foo"),
Arguments.of("${FOO}", "foo"),
Arguments.of("${BAR:defaultVal}", "defaultVal"),
Arguments.of("${INVALID", "${INVALID"),
Arguments.of("$INVALID}", "$INVALID}"),
Arguments.of("${{INVALID}", "${{INVALID}"),
Arguments.of("${INVALID}}", "${INVALID}}"),
Arguments.of("${BAR:}", ""),
Arguments.of("${}", "${}"),
Arguments.of("${DB_URL:jdbc:h2:./target/jberet-repo}", "jdbc:h2:./target/jberet-repo"),
Arguments.of("${DB_URL:jdbc:h2:mem:jberet;DB_CLOSE_DELAY=-1}", "jdbc:h2:mem:jberet;DB_CLOSE_DELAY=-1"));
}

private ThreadPoolExecutor verifyThreadPool(final int coreSize,
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,12 @@
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
Expand Down

0 comments on commit ecf9ab5

Please sign in to comment.