Skip to content

Commit da48dc1

Browse files
committed
Name changes to builder interface
1 parent c6b1bf0 commit da48dc1

10 files changed

Lines changed: 113 additions & 81 deletions

File tree

README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Gasper is a very simple integration testing JUnit harness for `java -jar` server
66

77
[![WildFly Swarm](https://avatars3.githubusercontent.com/u/11523816?v=3&s=100)](http://wildfly-swarm.io/) [![Spring Boot](https://avatars2.githubusercontent.com/u/317776?v=3&s=100)](http://projects.spring.io/spring-boot/)
88

9-
Gasper provides a simple to use JUnit `TestRule` that can be used to build integration tests with simple apps, like microservices. You can configure Gasper with easy to use builder interface.
9+
Gasper provides a simple to use JUnit `TestRule` that can be used to build integration tests with simple apps, like micro-services. You can configure Gasper with easy to use builder interface.
1010

1111
## Usage
1212

@@ -48,22 +48,31 @@ public void testGetRoot() throws UnirestException {
4848
}
4949
```
5050

51-
### Addtional configuration
51+
### Additional configuration
5252

5353
To configure Gasper use `GasperBuilder` interface, for ex.:
5454

5555
```java
56+
private final int port = 11909;
57+
private final String webContext = "/test";
58+
private final String systemPropertyForPort = "swarm.http.port";
5659
@ClassRule
57-
public static Gasper gasper = Gasper.configurations()
58-
.wildflySwarm()
59-
.usePomFile(Paths.get("target", "it", "wildfly-swarm-tester", "pom.xml"))
60-
.inheritIO()
61-
.maxStartupTime(120)
62-
.maxDeploymentTime(20)
63-
.useContextChecker(MyTestClass::contextChecker)
64-
.withEnvironmentVariable("jdbc.password", DEV_PASSWORD)
65-
.withJavaOption("my.minus-d.option", "true")
66-
.silent()
60+
public static Gasper gasper = Gasper.builder()
61+
.silentGasperMessages()
62+
.usingSystemPropertyForPort(systemPropertyForPort)
63+
.withSystemProperty("swarm.context.path", webContext)
64+
.withSystemProperty(systemPropertyForPort, String.valueOf(port))
65+
.withJVMOptions("-server", "-Xms1G", "-Xmx1G", "-XX:+UseConcMarkSweepGC")
66+
.withMaxStartupTime(100)
67+
.withMaxDeploymentTime(20)
68+
.withEnvironmentVariable("jdbc.password", "S3CreT!1")
69+
.withServerLoggingOnConsole()
70+
.usingPomFile(Paths.get("pom.xml"))
71+
.withArtifactPackaging("jar")
72+
.waitForWebContext(webContext)
73+
.withArtifactClassifier("swarm")
74+
.usingWebContextChecker(GasperBuilderTest::checkContext)
75+
.withPort(port)
6776
.build();
6877
```
6978

src/main/java/pl/wavesoftware/gasper/Gasper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ private List<String> buildCommand() {
122122
}
123123

124124
private void buildJavaOptions(List<String> command) {
125-
command.addAll(settings.getJavaOptions().entrySet().stream()
125+
command.addAll(settings.getJvmOptions());
126+
command.addAll(settings.getSystemProperties().entrySet().stream()
126127
.map(entry -> format("-D%s=%s", entry.getKey(), entry.getValue()))
127128
.collect(Collectors.toList())
128129
);

src/main/java/pl/wavesoftware/gasper/GasperBuilder.java

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package pl.wavesoftware.gasper;
22

3-
import com.google.common.collect.ImmutableMap;
43
import org.slf4j.event.Level;
54
import pl.wavesoftware.eid.utils.EidPreconditions;
65
import pl.wavesoftware.gasper.internal.Executor;
@@ -11,7 +10,10 @@
1110
import java.net.ServerSocket;
1211
import java.nio.file.Path;
1312
import java.nio.file.Paths;
13+
import java.util.ArrayList;
14+
import java.util.Collections;
1415
import java.util.LinkedHashMap;
16+
import java.util.List;
1517
import java.util.Map;
1618
import java.util.function.Function;
1719

@@ -25,9 +27,10 @@ public final class GasperBuilder extends Gasper.RunnerCreator {
2527

2628
private String packaging = MavenResolver.DEFAULT_PACKAGING;
2729
private String classifier = "";
28-
private Map<String, String> javaOptions = new LinkedHashMap<>();
30+
private Map<String, String> systemProperties = new LinkedHashMap<>();
31+
private List<String> jvmOptions = new ArrayList<>();
2932
private Map<String, String> environment = new LinkedHashMap<>();
30-
private String portJavaOption;
33+
private String systemPropertyForPort;
3134
private Integer port;
3235
private boolean inheritIO = false;
3336
private String context = Gasper.DEFAULT_CONTEXT;
@@ -39,12 +42,12 @@ public final class GasperBuilder extends Gasper.RunnerCreator {
3942

4043
protected GasperBuilder() {}
4144

42-
public GasperBuilder withPackaging(String packaging) {
45+
public GasperBuilder withArtifactPackaging(String packaging) {
4346
this.packaging = packaging;
4447
return this;
4548
}
4649

47-
public GasperBuilder withClassifier(String classifier) {
50+
public GasperBuilder withArtifactClassifier(String classifier) {
4851
this.classifier = classifier;
4952
return this;
5053
}
@@ -54,8 +57,13 @@ public GasperBuilder withEnvironmentVariable(String key, String value) {
5457
return this;
5558
}
5659

57-
public GasperBuilder withJavaOption(String key, String value) {
58-
javaOptions.put(key, value);
60+
public GasperBuilder withSystemProperty(String key, String value) {
61+
systemProperties.put(key, value);
62+
return this;
63+
}
64+
65+
public GasperBuilder withJVMOptions(String... options) {
66+
Collections.addAll(jvmOptions, options);
5967
return this;
6068
}
6169

@@ -64,72 +72,72 @@ public GasperBuilder withPort(int port) {
6472
return this;
6573
}
6674

67-
public GasperBuilder usePortJavaOptionFor(String portJavaOption) {
68-
this.portJavaOption = portJavaOption;
75+
public GasperBuilder usingSystemPropertyForPort(String systemPropertyForPort) {
76+
this.systemPropertyForPort = systemPropertyForPort;
6977
return this;
7078
}
7179

72-
public GasperBuilder usePomFile(Path pomfile) {
80+
public GasperBuilder usingPomFile(Path pomfile) {
7381
this.pomfile = pomfile;
7482
return this;
7583
}
7684

77-
public GasperBuilder inheritIO() {
78-
return inheritIO(true);
85+
public GasperBuilder withServerLoggingOnConsole() {
86+
return withServerLoggingOnConsole(true);
7987
}
8088

81-
public GasperBuilder inheritIO(boolean inheritIO) {
89+
public GasperBuilder withServerLoggingOnConsole(boolean inheritIO) {
8290
this.inheritIO = inheritIO;
8391
return this;
8492
}
8593

86-
public Gasper build() {
87-
if (port == null) {
88-
port = findNotBindedPort();
89-
}
90-
if (portJavaOption != null) {
91-
withJavaOption(portJavaOption, port.toString());
92-
}
93-
Settings settings = new Settings(
94-
packaging, classifier, port,
95-
ImmutableMap.copyOf(javaOptions), ImmutableMap.copyOf(environment),
96-
inheritIO, context, contextChecker,
97-
portAvailableMaxTime, deploymentMaxTime,
98-
pomfile, level
99-
);
100-
return create(settings);
101-
}
102-
103-
public GasperBuilder maxStartupTime(int seconds) {
94+
public GasperBuilder withMaxStartupTime(int seconds) {
10495
this.portAvailableMaxTime = seconds;
10596
return this;
10697
}
10798

108-
public GasperBuilder maxDeploymentTime(int seconds) {
99+
public GasperBuilder withMaxDeploymentTime(int seconds) {
109100
this.deploymentMaxTime = seconds;
110101
return this;
111102
}
112103

113-
public GasperBuilder waitForContext(String context) {
104+
public GasperBuilder waitForWebContext(String context) {
114105
this.context = context;
115106
return this;
116107
}
117108

118-
public GasperBuilder useContextChecker(Function<HttpEndpoint, Boolean> contextChecker) {
109+
public GasperBuilder usingWebContextChecker(Function<HttpEndpoint, Boolean> contextChecker) {
119110
this.contextChecker = contextChecker;
120111
return this;
121112
}
122113

123-
public GasperBuilder silent() {
124-
useLogLevel(Level.WARN);
114+
public GasperBuilder silentGasperMessages() {
115+
usingLogLevel(Level.WARN);
125116
return this;
126117
}
127118

128-
public GasperBuilder useLogLevel(Level level) {
119+
public GasperBuilder usingLogLevel(Level level) {
129120
this.level = level;
130121
return this;
131122
}
132123

124+
public Gasper build() {
125+
if (port == null) {
126+
port = findNotBindedPort();
127+
}
128+
if (systemPropertyForPort != null) {
129+
withSystemProperty(systemPropertyForPort, port.toString());
130+
}
131+
Settings settings = new Settings(
132+
packaging, classifier, port,
133+
systemProperties, jvmOptions, environment,
134+
inheritIO, context, contextChecker,
135+
portAvailableMaxTime, deploymentMaxTime,
136+
pomfile, level
137+
);
138+
return create(settings);
139+
}
140+
133141
private static Integer findNotBindedPort() {
134142
return tryToExecute((EidPreconditions.UnsafeSupplier<Integer>) () -> {
135143
try (ServerSocket socket = new ServerSocket(0)) {

src/main/java/pl/wavesoftware/gasper/GasperConfigurations.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@ protected GasperConfigurations() {}
2727
* <p>
2828
* You can use it directly or use {@link GasperBuilder} interface to re-configure it to you needs.
2929
* <p>
30-
* To use it in JUnit execute method {@link GasperBuilder#create()}
30+
* To use it in JUnit execute method {@link GasperBuilder#build()}
3131
* @return pre-configured {@link GasperBuilder} to use with WildFly Swarm.
3232
*/
3333
public GasperBuilder wildflySwarm() {
3434
return Gasper.builder()
35-
.withPackaging("jar")
36-
.withClassifier("swarm")
37-
.usePortJavaOptionFor(GasperConfigurations.WILDFLY_SWARM);
35+
.withArtifactPackaging("jar")
36+
.withArtifactClassifier("swarm")
37+
.usingSystemPropertyForPort(GasperConfigurations.WILDFLY_SWARM);
3838
}
3939

4040
/**
4141
* This method returns pre-configured Gasper builder to use with Spring Boot.
4242
* <p>
4343
* You can use it directly or use {@link GasperBuilder} interface to re-configure it to you needs.
4444
* <p>
45-
* To use it in JUnit execute method {@link GasperBuilder#create()}
45+
* To use it in JUnit execute method {@link GasperBuilder#build()}
4646
* @return pre-configured {@link GasperBuilder} to use with Spring Boot.
4747
*/
4848
public GasperBuilder springBoot() {
4949
return Gasper.builder()
50-
.withPackaging("jar")
51-
.usePortJavaOptionFor(GasperConfigurations.SPRING_BOOT);
50+
.withArtifactPackaging("jar")
51+
.usingSystemPropertyForPort(GasperConfigurations.SPRING_BOOT);
5252
}
5353
}

src/main/java/pl/wavesoftware/gasper/internal/Executor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ private static boolean isPortTaken(int port) {
148148
try (ServerSocket ignored = new ServerSocket(port)) {
149149
return false;
150150
} catch (IOException ex) {
151+
log.trace(format("Port %d taken", port), ex);
151152
return true;
152153
}
153154
}

src/main/java/pl/wavesoftware/gasper/internal/HttpEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class HttpEndpoint {
2525
private final String query;
2626

2727
public String fullAddress() {
28-
String address = format("%s://%s:%d%s",
28+
String address = format("%s://%s:%s%s",
2929
getScheme(),
3030
getDomain(),
3131
getPort(),

src/main/java/pl/wavesoftware/gasper/internal/Settings.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package pl.wavesoftware.gasper.internal;
22

3+
import com.google.common.collect.ImmutableList;
34
import com.google.common.collect.ImmutableMap;
4-
import lombok.AccessLevel;
55
import lombok.Getter;
66
import lombok.RequiredArgsConstructor;
7-
import lombok.Setter;
87
import org.slf4j.event.Level;
98
import pl.wavesoftware.gasper.Gasper;
109

1110
import java.nio.file.Path;
11+
import java.util.List;
1212
import java.util.Map;
1313
import java.util.function.Function;
1414

@@ -23,13 +23,13 @@
2323
* @see Gasper#configurations()
2424
*/
2525
@Getter
26-
@Setter
2726
@RequiredArgsConstructor
2827
public class Settings {
2928
private final String packaging;
3029
private final String classifier;
3130
private final int port;
32-
private final Map<String, String> javaOptions;
31+
private final Map<String, String> systemProperties;
32+
private final List<String> jvmOptions;
3333
private final Map<String, String> environment;
3434
private final boolean inheritIO;
3535
private final String context;
@@ -38,19 +38,26 @@ public class Settings {
3838
private final int deploymentMaxTime;
3939
private final Path pomfile;
4040
private final Level level;
41-
@Setter(AccessLevel.NONE)
4241
private HttpEndpoint endpoint;
4342

4443
/**
45-
* Gets Java options as map
44+
* Retrieves Java <code>-D</code> style options as map
4645
* @return a map for Java options
4746
*/
48-
public Map<String, String> getJavaOptions() {
49-
return ImmutableMap.copyOf(javaOptions);
47+
public Map<String, String> getSystemProperties() {
48+
return ImmutableMap.copyOf(systemProperties);
5049
}
5150

5251
/**
53-
* Gets environment variables as a map
52+
* Retrieves Java VM options as list
53+
* @return a map for Java options
54+
*/
55+
public List<String> getJvmOptions() {
56+
return ImmutableList.copyOf(jvmOptions);
57+
}
58+
59+
/**
60+
* Retrieves environment variables as a map
5461
* @return a map for environment variables
5562
*/
5663
public Map<String, String> getEnvironment() {

src/test/java/pl/wavesoftware/gasper/GasperBuilderTest.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@ public class GasperBuilderTest {
1919
@Test
2020
public void testBuild() throws Exception {
2121
GasperBuilder builder = new GasperBuilder();
22-
Gasper gasper = builder.withJavaOption("swarm.context.path", "/sub")
23-
.maxStartupTime(100)
24-
.maxDeploymentTime(20)
22+
int port = 11909;
23+
String webContext = "/test";
24+
String systemPropertyForPort = "swarm.http.port";
25+
Gasper gasper = builder.silentGasperMessages()
26+
.usingSystemPropertyForPort(systemPropertyForPort)
27+
.withSystemProperty("swarm.context.path", webContext)
28+
.withSystemProperty(systemPropertyForPort, String.valueOf(port))
29+
.withJVMOptions("-server", "-Xms1G", "-Xmx1G", "-XX:+UseConcMarkSweepGC")
30+
.withMaxStartupTime(100)
31+
.withMaxDeploymentTime(20)
2532
.withEnvironmentVariable("jdbc.password", "S3CreT!1")
26-
.inheritIO()
27-
.silent()
28-
.usePomFile(Paths.get("pom.xml"))
29-
.withPackaging("jar")
30-
.waitForContext("/sub")
31-
.withClassifier("swarm")
32-
.useContextChecker(GasperBuilderTest::checkContext)
33-
.withPort(11909)
33+
.withServerLoggingOnConsole()
34+
.usingPomFile(Paths.get("pom.xml"))
35+
.withArtifactPackaging("jar")
36+
.waitForWebContext(webContext)
37+
.withArtifactClassifier("swarm")
38+
.usingWebContextChecker(GasperBuilderTest::checkContext)
39+
.withPort(port)
3440
.build();
3541

3642
assertThat(gasper).isNotNull();

src/test/java/pl/wavesoftware/gasper/GasperForSpringBootIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class GasperForSpringBootIT {
2626
@ClassRule
2727
public static Gasper gasper = Gasper.configurations()
2828
.springBoot()
29-
.usePomFile(SPRING_BOOT_POMFILE)
29+
.usingPomFile(SPRING_BOOT_POMFILE)
3030
.build();
3131

3232
@Test

src/test/java/pl/wavesoftware/gasper/GasperForWildflySwarmIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public class GasperForWildflySwarmIT {
2424
@ClassRule
2525
public static Gasper gasper = Gasper.configurations()
2626
.wildflySwarm()
27-
.usePomFile(WILDFLY_SWARM_POMFILE)
28-
.silent()
27+
.usingPomFile(WILDFLY_SWARM_POMFILE)
28+
.silentGasperMessages()
2929
.build();
3030

3131
@Test

0 commit comments

Comments
 (0)