Skip to content

Commit 38bbd8c

Browse files
committed
Code cleanup + unit tests
1 parent 48990cd commit 38bbd8c

File tree

16 files changed

+215
-85
lines changed

16 files changed

+215
-85
lines changed

jooby-banner/src/main/java/org/jooby/banner/Banner.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232

33+
import com.google.common.base.CharMatcher;
3334
import com.google.inject.Binder;
3435
import com.google.inject.Key;
3536
import com.google.inject.name.Names;
@@ -96,10 +97,18 @@ public class Banner implements Module {
9697

9798
private final Optional<String> text;
9899

100+
/**
101+
* Creates a new {@link Banner} with the given text.
102+
*
103+
* @param text Texy to display.
104+
*/
99105
public Banner(final String text) {
100106
this.text = Optional.of(text);
101107
}
102108

109+
/**
110+
* Default banner, defined by <code>application.name</code>.
111+
*/
103112
public Banner() {
104113
this.text = Optional.empty();
105114
}
@@ -112,7 +121,8 @@ public void configure(final Env env, final Config conf, final Binder binder) {
112121
String text = this.text.orElse(name);
113122

114123
Provider<String> ascii = () -> Try
115-
.of(() -> trimEnd(convertOneLine(String.format(FONT, font), text)))
124+
.of(() -> CharMatcher.WHITESPACE
125+
.trimTrailingFrom(convertOneLine(String.format(FONT, font), text)))
116126
.getOrElse(text);
117127

118128
binder.bind(Key.get(String.class, Names.named("application.banner"))).toProvider(ascii);
@@ -122,18 +132,15 @@ public void configure(final Env env, final Config conf, final Binder binder) {
122132
});
123133
}
124134

135+
/**
136+
* Set/change default font (speed).
137+
*
138+
* @param font A font's name.
139+
* @return This module.
140+
*/
125141
public Banner font(final String font) {
126142
this.font = requireNonNull(font, "Font is required.");
127143
return this;
128144
}
129-
130-
private String trimEnd(String str) {
131-
int len = str.length();
132-
int st = 0;
133-
char[] val = str.toCharArray();
134-
while ((st < len) && (val[len - 1] <= ' ')) {
135-
len--;
136-
}
137-
return str.substring(st, len);
138-
}
145+
139146
}

jooby-banner/src/test/java/org/jooby/banner/BannerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ public void configure() throws Exception {
4848
});
4949
}
5050

51+
@Test
52+
public void trimEnd() throws Exception {
53+
String banner = "banner";
54+
new MockUnit(Env.class, Config.class, Binder.class, Logger.class)
55+
.expect(conf("app", "1.0.0"))
56+
.expect(log("app"))
57+
.expect(banner())
58+
.expect(onStart)
59+
.run(unit -> {
60+
new Banner(banner + " ")
61+
.configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class));
62+
});
63+
}
64+
5165
@Test
5266
public void print() throws Exception {
5367
String banner = "banner";

jooby-servlet/src/main/java/org/jooby/servlet/ServletContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class ServletContainer implements Server {
3232

3333
public static final Server NOOP = new ServletContainer();
3434

35-
private ServletContainer() {
35+
ServletContainer() {
3636
}
3737

3838
@Override
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.jooby.servlet;
2+
3+
import org.junit.Test;
4+
5+
public class ServletContainerTest {
6+
7+
@Test
8+
public void start() throws Exception {
9+
ServletContainer.NOOP.start();
10+
}
11+
12+
@Test
13+
public void stop() throws Exception {
14+
ServletContainer.NOOP.stop();
15+
}
16+
17+
@Test
18+
public void join() throws Exception {
19+
ServletContainer.NOOP.join();
20+
}
21+
}

jooby/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
<includes>
6868
<include>org/jooby/test/*</include>
6969
</includes>
70+
<excludes>
71+
<exclude>**/*Test.*</exclude>
72+
</excludes>
7073
</configuration>
7174
</execution>
7275
</executions>

jooby/src/main/java/org/jooby/test/JoobyRule.java

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,99 @@
2020

2121
import static java.util.Objects.requireNonNull;
2222

23-
import org.jooby.Env;
2423
import org.jooby.Jooby;
2524
import org.junit.rules.ExternalResource;
2625

27-
import com.google.inject.Binder;
28-
import com.typesafe.config.Config;
29-
import com.typesafe.config.ConfigFactory;
30-
import com.typesafe.config.ConfigValueFactory;
31-
26+
/**
27+
* <p>
28+
* Junit rule to run integration tests. You can choose between @ClassRule or @Rule. The next example
29+
* uses ClassRule:
30+
*
31+
* <pre>
32+
* import org.jooby.test.JoobyRule;
33+
*
34+
* public class MyIntegrationTest {
35+
*
36+
* &#64;ClassRule
37+
* private static JoobyRule bootstrap = new JoobyRule(new MyApp());
38+
*
39+
* }
40+
* </pre>
41+
*
42+
* <p>
43+
* Here one and only one instance will be created, which means the application start before the
44+
* first test and stop after the last test. Application state is shared between tests.
45+
* </p>
46+
* <p>
47+
* While with Rule a new application is created per test. If you have N test, then the application
48+
* will start/stop N times:
49+
* </p>
50+
*
51+
* <pre>
52+
* import org.jooby.test.JoobyRule;
53+
*
54+
* public class MyIntegrationTest {
55+
*
56+
* &#64;Rule
57+
* private static JoobyRule bootstrap = new JoobyRule(new MyApp());
58+
*
59+
* }
60+
* </pre>
61+
*
62+
* <p>
63+
* You are free to choice the HTTP client of your choice, like Fluent Apache HTTP client, REST
64+
* Assured, etc..
65+
* </p>
66+
* <p>
67+
* Here is a full example with REST Assured:
68+
* </p>
69+
*
70+
* <pre>{@code
71+
* import org.jooby.Jooby;
72+
*
73+
* public class MyApp extends Jooby {
74+
*
75+
* {
76+
* get("/", () -> "I'm real");
77+
* }
78+
*
79+
* }
80+
*
81+
* import org.jooby.test.JoobyRyle;
82+
*
83+
* public class MyIntegrationTest {
84+
*
85+
* &#64;ClassRule
86+
* static JoobyRule bootstrap = new JoobyRule(new MyApp());
87+
*
88+
* &#64;Test
89+
* public void integrationTestJustWorks() {
90+
* get("/")
91+
* .then()
92+
* .assertThat()
93+
* .body(equalTo("I'm real"));
94+
* }
95+
* }
96+
* }</pre>
97+
*
98+
* @author edgar
99+
*/
32100
public class JoobyRule extends ExternalResource {
33101

34-
private static class NoJoin implements Jooby.Module {
35-
36-
@Override
37-
public void configure(final Env env, final Config config, final Binder binder) {
38-
}
39-
40-
@Override
41-
public Config config() {
42-
return ConfigFactory.empty("test-config")
43-
.withValue("server.join", ConfigValueFactory.fromAnyRef(false));
44-
}
45-
}
46-
47102
private Jooby app;
48103

104+
/**
105+
* Creates a new {@link JoobyRule} to run integration tests.
106+
*
107+
* @param app Application to test.
108+
*/
49109
public JoobyRule(final Jooby app) {
50110
this.app = requireNonNull(app, "App required.");
51-
52-
app.use(new NoJoin());
53111
}
54112

55113
@Override
56114
protected void before() throws Throwable {
57-
app.start();
115+
app.start("server.join=false");
58116
}
59117

60118
@Override

jooby/src/test/java/org/jooby/test/AppRule.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

jooby/src/test/java/org/jooby/test/Client.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242

4343
import com.google.common.base.Throwables;
4444

45+
/**
46+
* Utility test class integration test. Internal use only.
47+
*
48+
* @author edgar
49+
*/
4550
public class Client extends ExternalResource {
4651

4752
public interface Callback {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.jooby.test;
2+
3+
import org.jooby.Jooby;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.powermock.core.classloader.annotations.PrepareForTest;
7+
import org.powermock.modules.junit4.PowerMockRunner;
8+
9+
@RunWith(PowerMockRunner.class)
10+
@PrepareForTest({JoobyRule.class, Jooby.class })
11+
public class JoobyRuleTest {
12+
13+
@Test
14+
public void before() throws Exception {
15+
new MockUnit(Jooby.class)
16+
.expect(unit -> {
17+
Jooby app = unit.get(Jooby.class);
18+
app.start("server.join=false");
19+
})
20+
.run(unit -> {
21+
new JoobyRule(unit.get(Jooby.class)).before();
22+
});
23+
}
24+
25+
@Test
26+
public void after() throws Exception {
27+
new MockUnit(Jooby.class)
28+
.expect(unit -> {
29+
Jooby app = unit.get(Jooby.class);
30+
app.stop();
31+
})
32+
.run(unit -> {
33+
new JoobyRule(unit.get(Jooby.class)).after();
34+
});
35+
}
36+
}

jooby/src/test/java/org/jooby/test/JoobyRunner.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
import com.typesafe.config.ConfigFactory;
3737
import com.typesafe.config.ConfigValueFactory;
3838

39+
/**
40+
* JUnit4 block runner for Jooby. Internal use only.
41+
*
42+
* @author edgar
43+
*
44+
*/
3945
public class JoobyRunner extends BlockJUnit4ClassRunner {
4046

4147
private Jooby app;

0 commit comments

Comments
 (0)