Skip to content

Commit a482ceb

Browse files
committed
more unit test and code coverage
1 parent 18e1d84 commit a482ceb

File tree

6 files changed

+319
-31
lines changed

6 files changed

+319
-31
lines changed

jooby-aws/src/main/java/org/jooby/internal/aws/AwsGenericManaged.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
*/
1919
package org.jooby.internal.aws;
2020

21+
import static javaslang.API.$;
22+
import static javaslang.API.Case;
23+
import static javaslang.API.Match;
24+
import static javaslang.Predicates.instanceOf;
25+
2126
import java.lang.reflect.InvocationTargetException;
2227
import java.lang.reflect.Method;
2328
import java.lang.reflect.Modifier;
@@ -30,8 +35,6 @@
3035
import org.slf4j.Logger;
3136
import org.slf4j.LoggerFactory;
3237

33-
import com.google.common.base.Throwables;
34-
3538
@SuppressWarnings("rawtypes")
3639
public class AwsGenericManaged implements Provider, Managed {
3740

@@ -66,8 +69,9 @@ public void stop() throws Exception {
6669
log.debug("no shutdown method found for: {}", dep);
6770
}
6871
} catch (InvocationTargetException ex) {
69-
RuntimeException x = Throwables.propagate(ex.getCause());
70-
throw x;
72+
throw Match(ex.getTargetException()).of(
73+
Case(instanceOf(Exception.class), x -> x),
74+
Case($(), x -> new IllegalStateException("shutdown result in error", x)));
7175
} finally {
7276
dep = null;
7377
}

jooby-aws/src/test/java/org/jooby/internal/aws/AwsGenericManagedTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ public void nostop() throws Exception {
7575
});
7676
}
7777

78+
@Test
79+
@SuppressWarnings("unused")
80+
public void shouldIgnorePrivateStop() throws Exception {
81+
new MockUnit()
82+
.run(unit -> {
83+
AwsGenericManaged aws = new AwsGenericManaged(new Object() {
84+
private void shutdown() {
85+
throw new UnsupportedOperationException();
86+
}
87+
});
88+
aws.stop();
89+
});
90+
}
91+
7892
@Test
7993
public void shutdownOverloaded() throws Exception {
8094
new MockUnit(ShutdownOverloaded.class)
@@ -96,4 +110,18 @@ public void stopErr() throws Exception {
96110
});
97111
}
98112

113+
@Test(expected = IllegalStateException.class)
114+
@SuppressWarnings("unused")
115+
public void stopNoRuntimeErr() throws Exception {
116+
new MockUnit()
117+
.run(unit -> {
118+
AwsGenericManaged aws = new AwsGenericManaged(new Object() {
119+
public void shutdown() throws Throwable {
120+
throw new Throwable();
121+
}
122+
});
123+
aws.stop();
124+
});
125+
}
126+
99127
}

jooby/src/main/java/org/jooby/internal/handlers/HeadHandler.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,32 @@
3434

3535
public class HeadHandler implements Route.Filter {
3636

37-
private Set<Definition> routeDefs;
37+
private Set<Definition> routes;
3838

3939
@Inject
40-
public HeadHandler(final Set<Route.Definition> routeDefs) {
41-
this.routeDefs = requireNonNull(routeDefs, "Route definitions are required.");
40+
public HeadHandler(final Set<Route.Definition> routes) {
41+
this.routes = requireNonNull(routes, "Routes are required.");
4242
}
4343

4444
@Override
4545
public void handle(final Request req, final Response rsp, final Route.Chain chain)
4646
throws Throwable {
4747

4848
String path = req.path();
49-
for (Route.Definition routeDef : routeDefs) {
50-
Optional<Route> route = routeDef
51-
.matches("GET", path, MediaType.all, MediaType.ALL);
52-
if (route.isPresent() && !route.get().pattern().contains("*")) {
53-
// route found
54-
rsp.length(0);
55-
((RouteImpl) route.get()).handle(req, rsp, chain);
56-
return;
49+
for (Route.Definition router : routes) {
50+
// ignore glob route
51+
if (!router.glob()) {
52+
Optional<Route> ifRoute = router
53+
.matches(Route.GET, path, MediaType.all, MediaType.ALL);
54+
if (ifRoute.isPresent()) {
55+
// route found
56+
rsp.length(0);
57+
((RouteImpl) ifRoute.get()).handle(req, rsp, chain);
58+
return;
59+
}
5760
}
5861
}
5962
// not handled, just call next
6063
chain.next(req, rsp);
6164
}
62-
6365
}

jooby/src/main/java/org/jooby/internal/handlers/OptionsHandler.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static java.util.Objects.requireNonNull;
2222

2323
import java.util.LinkedHashSet;
24-
import java.util.Optional;
2524
import java.util.Set;
2625

2726
import org.jooby.MediaType;
@@ -36,31 +35,32 @@
3635

3736
public class OptionsHandler implements Route.Handler {
3837

39-
private Set<Definition> routeDefs;
38+
private static final String SEP = ", ";
39+
40+
private static final String ALLOW = "Allow";
41+
42+
private Set<Definition> routes;
4043

4144
@Inject
42-
public OptionsHandler(final Set<Route.Definition> routeDefs) {
43-
this.routeDefs = requireNonNull(routeDefs, "Route definitions are required.");
45+
public OptionsHandler(final Set<Route.Definition> routes) {
46+
this.routes = requireNonNull(routes, "Routes are required.");
4447
}
4548

4649
@Override
4750
public void handle(final Request req, final Response rsp) throws Exception {
48-
if (!rsp.header("Allow").toOptional(String.class).isPresent()) {
51+
if (!rsp.header(ALLOW).isSet()) {
4952
Set<String> allow = new LinkedHashSet<>();
5053
Set<String> methods = new LinkedHashSet<>(Route.METHODS);
5154
String path = req.path();
5255
methods.remove(req.method());
53-
for (String alt : methods) {
54-
for (Route.Definition routeDef : routeDefs) {
55-
Optional<Route> route = routeDef.matches(alt, path, MediaType.all, MediaType.ALL);
56-
if (route.isPresent()) {
57-
allow.add(route.get().method());
58-
}
59-
}
56+
for (String method : methods) {
57+
routes.stream()
58+
.filter(route -> route.matches(method, path, MediaType.all, MediaType.ALL).isPresent())
59+
.forEach(route -> allow.add(route.method()));
6060
}
61-
rsp.header("Allow", Joiner.on(", ").join(allow));
62-
rsp.length(0);
63-
rsp.status(Status.OK);
61+
rsp.header(ALLOW, Joiner.on(SEP).join(allow))
62+
.length(0)
63+
.status(Status.OK);
6464
}
6565
}
6666

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.jooby.internal.handlers;
2+
3+
import static org.easymock.EasyMock.expect;
4+
5+
import java.util.Optional;
6+
import java.util.Set;
7+
8+
import org.jooby.MediaType;
9+
import org.jooby.Request;
10+
import org.jooby.Response;
11+
import org.jooby.Route;
12+
import org.jooby.Route.Chain;
13+
import org.jooby.Route.Definition;
14+
import org.jooby.internal.RouteImpl;
15+
import org.jooby.test.MockUnit;
16+
import org.jooby.test.MockUnit.Block;
17+
import org.junit.Test;
18+
19+
import com.google.common.collect.Sets;
20+
21+
public class HeadHandlerTest {
22+
23+
private Block path = unit -> {
24+
Request req = unit.get(Request.class);
25+
expect(req.path()).andReturn("/");
26+
};
27+
28+
private Block len = unit -> {
29+
Response rsp = unit.get(Response.class);
30+
expect(rsp.length(0)).andReturn(rsp);
31+
};
32+
33+
private Block next = unit -> {
34+
Chain chain = unit.get(Route.Chain.class);
35+
chain.next(unit.get(Request.class), unit.get(Response.class));
36+
};
37+
38+
@Test
39+
public void handle() throws Exception {
40+
new MockUnit(Request.class, Response.class, Route.Chain.class, Route.Definition.class)
41+
.expect(path)
42+
.expect(unit -> {
43+
Route.Definition routeDef = unit.get(Route.Definition.class);
44+
expect(routeDef.glob()).andReturn(false);
45+
46+
RouteImpl route = unit.mock(RouteImpl.class);
47+
route.handle(unit.get(Request.class), unit.get(Response.class),
48+
unit.get(Route.Chain.class));
49+
50+
Optional<Route> ifRoute = Optional.of(route);
51+
52+
expect(routeDef.matches(Route.GET, "/", MediaType.all, MediaType.ALL)).andReturn(ifRoute);
53+
})
54+
.expect(len)
55+
.run(unit -> {
56+
Set<Definition> routes = Sets.newHashSet(unit.get(Route.Definition.class));
57+
new HeadHandler(routes)
58+
.handle(unit.get(Request.class), unit.get(Response.class),
59+
unit.get(Route.Chain.class));
60+
});
61+
}
62+
63+
@Test
64+
public void noRoute() throws Exception {
65+
new MockUnit(Request.class, Response.class, Route.Chain.class, Route.Definition.class)
66+
.expect(path)
67+
.expect(unit -> {
68+
Route.Definition routeDef = unit.get(Route.Definition.class);
69+
expect(routeDef.glob()).andReturn(false);
70+
71+
Optional<Route> ifRoute = Optional.empty();
72+
73+
expect(routeDef.matches(Route.GET, "/", MediaType.all, MediaType.ALL)).andReturn(ifRoute);
74+
})
75+
.expect(next)
76+
.run(unit -> {
77+
Set<Definition> routes = Sets.newHashSet(unit.get(Route.Definition.class));
78+
new HeadHandler(routes)
79+
.handle(unit.get(Request.class), unit.get(Response.class),
80+
unit.get(Route.Chain.class));
81+
});
82+
}
83+
84+
@Test
85+
public void ignoreGlob() throws Exception {
86+
new MockUnit(Request.class, Response.class, Route.Chain.class, Route.Definition.class)
87+
.expect(path)
88+
.expect(unit -> {
89+
Route.Definition routeDef = unit.get(Route.Definition.class);
90+
expect(routeDef.glob()).andReturn(true);
91+
})
92+
.expect(next)
93+
.run(unit -> {
94+
Set<Definition> routes = Sets.newHashSet(unit.get(Route.Definition.class));
95+
new HeadHandler(routes)
96+
.handle(unit.get(Request.class), unit.get(Response.class),
97+
unit.get(Route.Chain.class));
98+
});
99+
}
100+
101+
@Test
102+
public void noroutes() throws Exception {
103+
new MockUnit(Request.class, Response.class, Route.Chain.class, Route.Definition.class)
104+
.expect(path)
105+
.expect(next)
106+
.run(unit -> {
107+
Set<Definition> routes = Sets.newHashSet();
108+
new HeadHandler(routes)
109+
.handle(unit.get(Request.class), unit.get(Response.class),
110+
unit.get(Route.Chain.class));
111+
});
112+
}
113+
114+
}

0 commit comments

Comments
 (0)