Skip to content

Commit 19e9703

Browse files
committed
Sonar fixes
1 parent 1785276 commit 19e9703

10 files changed

Lines changed: 273 additions & 212 deletions

File tree

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private final int port = 11909;
5757
private final String webContext = "/test";
5858
private final String systemPropertyForPort = "swarm.http.port";
5959
@ClassRule
60-
public static Gasper gasper = Gasper.builder()
60+
public static Gasper gasper = Gasper.configure()
6161
.silentGasperMessages()
6262
.usingSystemPropertyForPort(systemPropertyForPort)
6363
.withSystemProperty("swarm.context.path", webContext)
@@ -89,12 +89,6 @@ public static Gasper gasper = Gasper.builder()
8989
</dependency>
9090
```
9191

92-
### Gradle
93-
94-
```groovy
95-
testCompile 'pl.wavesoftware:gasper:1.0.0'
96-
```
97-
9892
## Requirements
9993

10094
Gasper requires Java 8. Tested on Travis CI.

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

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.junit.runner.Description;
1010
import org.junit.runners.model.Statement;
1111
import pl.wavesoftware.eid.utils.EidPreconditions;
12+
import pl.wavesoftware.gasper.internal.AbstractGasperBuilder;
1213
import pl.wavesoftware.gasper.internal.Executor;
1314
import pl.wavesoftware.gasper.internal.Logger;
1415
import pl.wavesoftware.gasper.internal.Settings;
@@ -37,7 +38,7 @@ public final class Gasper implements TestRule {
3738
public static final int DEFAULT_PORT_AVAILABLE_MAX_SECONDS = 60;
3839
public static final int DEFAULT_DEPLOYMENT_MAX_SECONDS = 30;
3940
public static final String DEFAULT_CONTEXT = "/";
40-
private static final String figlet;
41+
private static final String FIGLET;
4142

4243
private final Settings settings;
4344
private Path artifact;
@@ -46,12 +47,18 @@ public final class Gasper implements TestRule {
4647

4748
static {
4849
InputStream is = Gasper.class.getClassLoader().getResourceAsStream("gasper.txt");
49-
figlet = tryToExecute((EidPreconditions.UnsafeSupplier<String>) () ->
50+
FIGLET = tryToExecute((EidPreconditions.UnsafeSupplier<String>) () ->
5051
CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)), "20160305:201329");
5152
}
5253

53-
public static GasperBuilder builder() {
54-
return new GasperBuilder();
54+
/**
55+
* Creates a builder interface {@link GasperBuilder} that can be used to configure Gasper.
56+
* <p>
57+
* You can also use already created configurations by using method {@link #configurations()} for convenience.
58+
* @return a configure interface for configuration purposes
59+
*/
60+
public static GasperBuilder configure() {
61+
return new GasperBuilderImpl();
5562
}
5663

5764
public static GasperConfigurations configurations() {
@@ -66,50 +73,59 @@ public String getAddress() {
6673
return settings.getEndpoint().fullAddress();
6774
}
6875

69-
protected abstract static class RunnerCreator {
70-
public Gasper create(Settings settings) {
76+
protected interface RunnerCreator {
77+
default Gasper create(Settings settings) {
7178
return new Gasper(settings);
7279
}
7380
}
7481

7582
@Override
7683
public Statement apply(Statement base, Description description) {
77-
GasperStatement afterStmt = new GasperStatement(base, this);
7884
return tryToExecute((EidPreconditions.UnsafeSupplier<Statement>) () -> {
7985
setup();
8086
before();
81-
return afterStmt;
87+
return new GasperStatement(base, this::after);
8288
}, "20160305:004035");
8389
}
8490

91+
private void setup() {
92+
log(FIGLET);
93+
MavenResolver resolver = new MavenResolver(settings.getPomfile());
94+
artifact = resolver.getBuildArtifact(settings.getPackaging(), settings.getClassifier());
95+
File workingDirectory = resolver.getBuildDirectory();
96+
List<String> command = buildCommand();
97+
log("Command to be executed: \"%s\"", command.stream().collect(Collectors.joining(" ")));
98+
executor = new Executor(command, workingDirectory, settings);
99+
}
100+
101+
private void before() throws IOException {
102+
executor.start();
103+
log("All looks ready, running tests...");
104+
}
105+
106+
private void after() {
107+
log("Testing on server completed.");
108+
executor.stop();
109+
}
110+
85111
@RequiredArgsConstructor
86112
private static class GasperStatement extends Statement {
87113
private final Statement base;
88-
private final Gasper gasper;
114+
private final Procedure procedure;
89115

90116
@Override
91117
public void evaluate() throws Throwable {
92118
try {
93119
base.evaluate();
94120
} finally {
95-
gasper.after();
121+
procedure.execute();
96122
}
97123
}
98124
}
99125

100-
private void setup() {
101-
log(figlet);
102-
MavenResolver resolver = new MavenResolver(settings.getPomfile());
103-
artifact = resolver.getBuildArtifact(settings.getPackaging(), settings.getClassifier());
104-
File workingDirectory = resolver.getBuildDirectory();
105-
List<String> command = buildCommand();
106-
log("Command to be executed: \"%s\"", command.stream().collect(Collectors.joining(" ")));
107-
executor = new Executor(command, workingDirectory, settings);
108-
}
109-
110-
private void before() throws IOException {
111-
executor.start();
112-
log("All looks ready, running tests...");
126+
@FunctionalInterface
127+
private interface Procedure {
128+
void execute();
113129
}
114130

115131
private List<String> buildCommand() {
@@ -129,11 +145,6 @@ private void buildJavaOptions(List<String> command) {
129145
);
130146
}
131147

132-
private void after() {
133-
log("Testing on server completed.");
134-
executor.stop();
135-
}
136-
137148
private void log(String frmt, Object... args) {
138149
ensureLogger();
139150
logger.info(format(frmt, args));
@@ -144,4 +155,8 @@ private void ensureLogger() {
144155
logger = new Logger(log, settings);
145156
}
146157
}
158+
159+
private static class GasperBuilderImpl extends AbstractGasperBuilder {
160+
161+
}
147162
}
Lines changed: 35 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,47 @@
11
package pl.wavesoftware.gasper;
22

33
import org.slf4j.event.Level;
4-
import pl.wavesoftware.eid.utils.EidPreconditions;
5-
import pl.wavesoftware.gasper.internal.Executor;
64
import pl.wavesoftware.gasper.internal.HttpEndpoint;
7-
import pl.wavesoftware.gasper.internal.Settings;
8-
import pl.wavesoftware.gasper.internal.maven.MavenResolver;
95

10-
import java.net.ServerSocket;
116
import java.nio.file.Path;
12-
import java.nio.file.Paths;
13-
import java.util.ArrayList;
14-
import java.util.Collections;
15-
import java.util.LinkedHashMap;
16-
import java.util.List;
17-
import java.util.Map;
187
import java.util.function.Function;
198

20-
import static pl.wavesoftware.eid.utils.EidPreconditions.tryToExecute;
21-
229
/**
2310
* @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl>
24-
* @since 2016-03-05
11+
* @since 2016-03-06
2512
*/
26-
public final class GasperBuilder extends Gasper.RunnerCreator {
27-
28-
private String packaging = MavenResolver.DEFAULT_PACKAGING;
29-
private String classifier = "";
30-
private Map<String, String> systemProperties = new LinkedHashMap<>();
31-
private List<String> jvmOptions = new ArrayList<>();
32-
private Map<String, String> environment = new LinkedHashMap<>();
33-
private String systemPropertyForPort;
34-
private Integer port;
35-
private boolean inheritIO = false;
36-
private String context = Gasper.DEFAULT_CONTEXT;
37-
private int portAvailableMaxTime = Gasper.DEFAULT_PORT_AVAILABLE_MAX_SECONDS;
38-
private int deploymentMaxTime = Gasper.DEFAULT_DEPLOYMENT_MAX_SECONDS;
39-
private Function<HttpEndpoint, Boolean> contextChecker = Executor.DEFAULT_CONTEXT_CHECKER;
40-
private Path pomfile = Paths.get(MavenResolver.DEFAULT_POM);
41-
private Level level = Level.INFO;
42-
43-
protected GasperBuilder() {}
44-
45-
public GasperBuilder withArtifactPackaging(String packaging) {
46-
this.packaging = packaging;
47-
return this;
48-
}
49-
50-
public GasperBuilder withArtifactClassifier(String classifier) {
51-
this.classifier = classifier;
52-
return this;
53-
}
54-
55-
public GasperBuilder withEnvironmentVariable(String key, String value) {
56-
environment.put(key, value);
57-
return this;
58-
}
59-
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);
67-
return this;
68-
}
69-
70-
public GasperBuilder withPort(int port) {
71-
this.port = port;
72-
return this;
73-
}
74-
75-
public GasperBuilder usingSystemPropertyForPort(String systemPropertyForPort) {
76-
this.systemPropertyForPort = systemPropertyForPort;
77-
return this;
78-
}
79-
80-
public GasperBuilder usingPomFile(Path pomfile) {
81-
this.pomfile = pomfile;
82-
return this;
83-
}
84-
85-
public GasperBuilder withServerLoggingOnConsole() {
86-
return withServerLoggingOnConsole(true);
87-
}
88-
89-
public GasperBuilder withServerLoggingOnConsole(boolean inheritIO) {
90-
this.inheritIO = inheritIO;
91-
return this;
92-
}
93-
94-
public GasperBuilder withMaxStartupTime(int seconds) {
95-
this.portAvailableMaxTime = seconds;
96-
return this;
97-
}
98-
99-
public GasperBuilder withMaxDeploymentTime(int seconds) {
100-
this.deploymentMaxTime = seconds;
101-
return this;
102-
}
103-
104-
public GasperBuilder waitForWebContext(String context) {
105-
this.context = context;
106-
return this;
107-
}
108-
109-
public GasperBuilder usingWebContextChecker(Function<HttpEndpoint, Boolean> contextChecker) {
110-
this.contextChecker = contextChecker;
111-
return this;
112-
}
113-
114-
public GasperBuilder silentGasperMessages() {
115-
usingLogLevel(Level.WARN);
116-
return this;
117-
}
118-
119-
public GasperBuilder usingLogLevel(Level level) {
120-
this.level = level;
121-
return this;
122-
}
123-
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-
141-
private static Integer findNotBindedPort() {
142-
return tryToExecute((EidPreconditions.UnsafeSupplier<Integer>) () -> {
143-
try (ServerSocket socket = new ServerSocket(0)) {
144-
return socket.getLocalPort();
145-
}
146-
}, "20160305:202934");
147-
148-
}
13+
public interface GasperBuilder extends Gasper.RunnerCreator {
14+
GasperBuilder withArtifactPackaging(String packaging);
15+
16+
GasperBuilder withArtifactClassifier(String classifier);
17+
18+
GasperBuilder withEnvironmentVariable(String key, String value);
19+
20+
GasperBuilder withSystemProperty(String key, String value);
21+
22+
GasperBuilder withJVMOptions(String... options);
23+
24+
GasperBuilder withPort(int port);
25+
26+
GasperBuilder usingSystemPropertyForPort(String systemPropertyForPort);
27+
28+
GasperBuilder usingPomFile(Path pomfile);
29+
30+
GasperBuilder withServerLoggingOnConsole();
31+
32+
GasperBuilder withServerLoggingOnConsole(boolean inheritIO);
33+
34+
GasperBuilder withMaxStartupTime(int seconds);
35+
36+
GasperBuilder withMaxDeploymentTime(int seconds);
37+
38+
GasperBuilder waitForWebContext(String context);
39+
40+
GasperBuilder usingWebContextChecker(Function<HttpEndpoint, Boolean> contextChecker);
41+
42+
GasperBuilder silentGasperMessages();
43+
44+
GasperBuilder usingLogLevel(Level level);
45+
46+
Gasper build();
14947
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This class holds supported and tested configurations for servers.
55
* <p>
66
* Configuration in this class are fairly tested and can serve as a base for configuration.
7-
* You shouldn't try to use this class directly. Use instead {@link Gasper#builderPreconfigured()} for entry point.
7+
* You shouldn't try to use this class directly. Use instead {@link Gasper#configurations()} for entry point.
88
* <p>
99
* Example:
1010
* <pre>
@@ -23,30 +23,30 @@ public final class GasperConfigurations {
2323
protected GasperConfigurations() {}
2424

2525
/**
26-
* This method returns pre-configured Gasper builder to use with WildFly Swarm.
26+
* This method returns pre-configured Gasper configuration to use with WildFly Swarm.
2727
* <p>
2828
* You can use it directly or use {@link GasperBuilder} interface to re-configure it to you needs.
2929
* <p>
3030
* 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() {
34-
return Gasper.builder()
34+
return Gasper.configure()
3535
.withArtifactPackaging("jar")
3636
.withArtifactClassifier("swarm")
3737
.usingSystemPropertyForPort(GasperConfigurations.WILDFLY_SWARM);
3838
}
3939

4040
/**
41-
* This method returns pre-configured Gasper builder to use with Spring Boot.
41+
* This method returns pre-configured Gasper configure 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>
4545
* 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() {
49-
return Gasper.builder()
49+
return Gasper.configure()
5050
.withArtifactPackaging("jar")
5151
.usingSystemPropertyForPort(GasperConfigurations.SPRING_BOOT);
5252
}

0 commit comments

Comments
 (0)