Skip to content

Commit ec2aa62

Browse files
committed
more unit test and coverage
1 parent a52a4f4 commit ec2aa62

File tree

8 files changed

+37
-69
lines changed

8 files changed

+37
-69
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public void stop() throws Exception {
6666
log.debug("no shutdown method found for: {}", dep);
6767
}
6868
} catch (InvocationTargetException ex) {
69-
Throwables.propagate(ex.getCause());
69+
RuntimeException x = Throwables.propagate(ex.getCause());
70+
throw x;
7071
} finally {
7172
dep = null;
7273
}

jooby/src/main/java/org/jooby/Sse.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -875,16 +875,14 @@ private Future<Optional<Object>> send(final Event event) {
875875
List<MediaType> produces = event.type().<List<MediaType>> map(ImmutableList::of)
876876
.orElse(this.produces);
877877
SseRenderer ctx = new SseRenderer(renderers, produces, StandardCharsets.UTF_8, locals);
878-
try {
878+
return Try.of(() -> {
879879
byte[] bytes = ctx.format(event);
880-
return send(event.id(), bytes).future();
881-
} catch (Throwable cause) {
880+
return send(event.id(), bytes);
881+
}).recover(cause -> {
882882
Promise<Optional<Object>> promise = Promise.make(MoreExecutors.newDirectExecutorService());
883883
promise.failure(cause);
884-
return promise.future();
885-
} finally {
886-
ctx.clear();
887-
}
884+
return promise;
885+
}).get().future();
888886
}
889887

890888
}

jooby/src/main/java/org/jooby/internal/SseRenderer.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ public byte[] format(final Sse.Event event) throws Exception {
8383

8484
data = ByteSource.concat(data, NL);
8585

86-
return data.read();
87-
}
88-
89-
public void clear() {
86+
byte[] bytes = data.read();
9087
data = null;
88+
return bytes;
9189
}
9290

9391
@Override

jooby/src/main/java/org/jooby/internal/js/JsJooby.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.InputStreamReader;
2525
import java.io.Reader;
2626
import java.nio.charset.StandardCharsets;
27+
import java.util.function.Consumer;
2728
import java.util.function.Supplier;
2829

2930
import javax.script.ScriptEngine;
@@ -58,12 +59,12 @@ void eval(final InputStream stream) throws Exception {
5859
eval(new InputStreamReader(stream, StandardCharsets.UTF_8));
5960
}
6061

62+
@SuppressWarnings({"rawtypes", "unchecked" })
6163
void eval(final Reader reader) throws Exception {
62-
try {
63-
engine.eval(reader);
64-
} finally {
65-
Closeables.closeQuietly(reader);
66-
}
64+
Consumer closer = x -> Closeables.closeQuietly(reader);
65+
Try.run(() -> engine.eval(reader))
66+
.onFailure(closer)
67+
.onSuccess(closer);
6768
}
6869

6970
}

jooby/src/test/java/org/jooby/CookieDefinitionTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,10 @@ public void cookieSecure() {
107107
assertEquals(true, def.secure(true).secure().get());
108108
assertEquals(true, def.toCookie().secure());
109109
}
110+
111+
@Test
112+
public void toStr() {
113+
Definition def = new Cookie.Definition("name", "q");
114+
assertEquals("name=q;Version=1", def.toString());
115+
}
110116
}

jooby/src/test/java/org/jooby/RequestTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public Locale locale(final BiFunction<List<LanguageRange>, List<Locale>, Locale>
110110

111111
@Override
112112
public List<Locale> locales() {
113-
throw new UnsupportedOperationException();
113+
return Request.super.locales();
114114
}
115115

116116
@Override
@@ -314,4 +314,18 @@ public Route route() {
314314
});
315315
}
316316

317+
@Test
318+
public void locales() throws Exception {
319+
new MockUnit(Route.class)
320+
.run(unit -> {
321+
assertEquals(null, new RequestMock() {
322+
@Override
323+
public List<Locale> locales(
324+
final BiFunction<List<LanguageRange>, List<Locale>, List<Locale>> filter) {
325+
return null;
326+
}
327+
}.locales());
328+
});
329+
}
330+
317331
}

jooby/src/test/java/org/jooby/SseTest.java

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -445,57 +445,6 @@ public void renderFailure() throws Exception {
445445
.build(isA(List.class), isA(List.class), eq(StandardCharsets.UTF_8), isA(Map.class));
446446

447447
expect(renderer.format(isA(Sse.Event.class))).andThrow(new IOException("failure"));
448-
renderer.clear();
449-
})
450-
.run(unit -> {
451-
Sse sse = new Sse() {
452-
453-
@Override
454-
protected void closeInternal() {
455-
}
456-
457-
@Override
458-
protected void fireCloseEvent() {
459-
}
460-
461-
@Override
462-
protected Promise<Optional<Object>> send(final Optional<Object> id, final byte[] data) {
463-
Promise<Optional<Object>> promise = Promise
464-
.make(MoreExecutors.newDirectExecutorService());
465-
promise.failure(new IOException("intentional err"));
466-
return promise;
467-
}
468-
469-
@Override
470-
public Sse keepAlive(final long millis) {
471-
return this;
472-
}
473-
474-
@Override
475-
protected void handshake(final Runnable handler) throws Exception {
476-
}
477-
};
478-
sse.handshake(unit.get(Request.class), unit.get(Runnable.class));
479-
sse.event(data).type(MediaType.all).send().onFailure(cause -> latch.countDown());
480-
latch.await();
481-
});
482-
}
483-
484-
@SuppressWarnings("resource")
485-
@Test(expected = IllegalStateException.class)
486-
public void sendFailure() throws Exception {
487-
CountDownLatch latch = new CountDownLatch(1);
488-
Object data = new Object();
489-
new MockUnit(Request.class, Route.class, Injector.class, Runnable.class)
490-
.expect(handshake)
491-
.expect(unit -> {
492-
SseRenderer renderer = unit.constructor(SseRenderer.class)
493-
.args(List.class, List.class, Charset.class, Map.class)
494-
.build(isA(List.class), isA(List.class), eq(StandardCharsets.UTF_8), isA(Map.class));
495-
496-
expect(renderer.format(isA(Sse.Event.class))).andReturn(new byte[0]);
497-
renderer.clear();
498-
expectLastCall().andThrow(new IllegalStateException("intentional err"));
499448
})
500449
.run(unit -> {
501450
Sse sse = new Sse() {

jooby/src/test/java/org/jooby/StatusTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public class StatusTest {
88
public void badCode() {
99
Status.valueOf(907);
1010
}
11+
1112
}

0 commit comments

Comments
 (0)