Skip to content

Commit 47e6a38

Browse files
committed
more unit tests + code coverage
1 parent 47325c0 commit 47e6a38

File tree

4 files changed

+123
-42
lines changed

4 files changed

+123
-42
lines changed

coverage-report/src/test/java/org/jooby/ws/WebSocketPauseResumeFeature.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.ning.http.client.ws.WebSocketTextListener;
2222
import com.ning.http.client.ws.WebSocketUpgradeHandler;
2323

24-
2524
public class WebSocketPauseResumeFeature extends ServerFeature {
2625

2726
static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
@@ -31,26 +30,26 @@ public class WebSocketPauseResumeFeature extends ServerFeature {
3130

3231
ws.pause();
3332
// 2nd ignored
34-
ws.pause();
35-
36-
executor.schedule(() -> {
37-
ws.resume();
38-
// 2nd call ignored
39-
ws.resume();
40-
latch.countDown();
41-
}, 1, TimeUnit.SECONDS);
33+
ws.pause();
4234

43-
ws.onMessage(message -> {
35+
executor.schedule(() -> {
36+
ws.resume();
37+
// 2nd call ignored
38+
ws.resume();
39+
latch.countDown();
40+
}, 1, TimeUnit.SECONDS);
4441

45-
ws.send("=" + message.value(), () -> {
46-
latch.await();
47-
ws.close();
48-
});
42+
ws.onMessage(message -> {
4943

44+
ws.send("=" + message.value(), () -> {
45+
latch.await();
46+
ws.close();
5047
});
5148

5249
});
5350

51+
});
52+
5453
}
5554

5655
private AsyncHttpClient client;
@@ -94,7 +93,8 @@ public void onClose(final WebSocket websocket) {
9493
@Override
9594
public void onError(final Throwable t) {
9695
}
97-
}).build()).get();
96+
}).build())
97+
.get();
9898

9999
if (latch.await(1L, TimeUnit.SECONDS)) {
100100
assertEquals(Sets.newHashSet("=hey!"), messages);

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import com.google.inject.Injector;
4141
import com.google.inject.Key;
4242

43+
import javaslang.control.Try;
44+
4345
public class WebSocketImpl implements WebSocket {
4446

4547
@SuppressWarnings({"rawtypes" })
@@ -143,34 +145,26 @@ public void connect(final Injector injector, final Request req, final NativeWebS
143145
/**
144146
* Bind callbacks
145147
*/
146-
ws.onBinaryMessage(buffer -> {
147-
try {
148-
messageCallback.invoke(new WsBinaryMessage(buffer));
149-
} catch (Throwable ex) {
150-
handleErr(ex);
151-
}
152-
});
153-
ws.onTextMessage(message -> {
154-
try {
155-
messageCallback.invoke(
148+
ws.onBinaryMessage(buffer -> Try
149+
.run(() -> messageCallback.invoke(new WsBinaryMessage(buffer)))
150+
.onFailure(this::handleErr));
151+
152+
ws.onTextMessage(message -> Try
153+
.run(() -> messageCallback.invoke(
156154
new MutantImpl(injector.getInstance(ParserExecutor.class),
157-
new StrParamReferenceImpl("body", "message", ImmutableList.of(message))));
158-
} catch (Throwable ex) {
159-
handleErr(ex);
160-
}
161-
});
162-
ws.onCloseMessage((code, reason) -> {
163-
try {
164-
if (closeCallback != null) {
165-
closeCallback.invoke(reason.map(r -> WebSocket.CloseStatus.of(code, r)).orElse(
166-
WebSocket.CloseStatus.of(code)));
167-
closeCallback = null;
168-
}
169-
} catch (Throwable ex) {
170-
handleErr(ex);
171-
}
172-
});
173-
ws.onErrorMessage(cause -> handleErr(cause));
155+
new StrParamReferenceImpl("body", "message", ImmutableList.of(message)))))
156+
.onFailure(this::handleErr));
157+
158+
ws.onCloseMessage((code, reason) -> Try
159+
.run(() -> {
160+
if (closeCallback != null) {
161+
closeCallback.invoke(reason.map(r -> WebSocket.CloseStatus.of(code, r)).orElse(
162+
WebSocket.CloseStatus.of(code)));
163+
closeCallback = null;
164+
}
165+
}).onFailure(this::handleErr));
166+
167+
ws.onErrorMessage(this::handleErr);
174168

175169
// connect now
176170
try {

jooby/src/test/java/org/jooby/internal/BodyReferenceImplTest.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.jooby.internal;
22

33
import static org.easymock.EasyMock.expect;
4+
import static org.easymock.EasyMock.expectLastCall;
45
import static org.junit.Assert.assertArrayEquals;
56
import static org.junit.Assert.assertEquals;
67

78
import java.io.ByteArrayOutputStream;
89
import java.io.File;
910
import java.io.FileOutputStream;
11+
import java.io.IOException;
1012
import java.io.InputStream;
1113
import java.io.OutputStream;
1214
import java.nio.charset.StandardCharsets;
@@ -56,6 +58,77 @@ public void fromBytes() throws Exception {
5658
});
5759
}
5860

61+
@Test(expected = IOException.class)
62+
public void inErr() throws Exception {
63+
long len = 1;
64+
long bsize = 2;
65+
byte[] bytes = "bytes".getBytes();
66+
new MockUnit(File.class, InputStream.class)
67+
.expect(baos(bytes))
68+
.expect(unit -> {
69+
InputStream in = unit.get(InputStream.class);
70+
71+
expect(in.read(unit.capture(byte[].class))).andThrow(new IOException());
72+
73+
OutputStream out = unit.get(ByteArrayOutputStream.class);
74+
75+
in.close();
76+
out.close();
77+
})
78+
.run(unit -> {
79+
new BodyReferenceImpl(len, StandardCharsets.UTF_8, unit.get(File.class),
80+
unit.get(InputStream.class), bsize);
81+
});
82+
}
83+
84+
@Test(expected = IOException.class)
85+
public void outErr() throws Exception {
86+
long len = 1;
87+
long bsize = 2;
88+
byte[] bytes = "bytes".getBytes();
89+
new MockUnit(File.class, InputStream.class)
90+
.expect(baos(bytes))
91+
.expect(unit -> {
92+
InputStream in = unit.get(InputStream.class);
93+
in.close();
94+
95+
OutputStream out = unit.get(ByteArrayOutputStream.class);
96+
out.close();
97+
expectLastCall().andThrow(new IOException());
98+
99+
unit.mockStatic(ByteStreams.class);
100+
expect(ByteStreams.copy(in, out)).andReturn(1L);
101+
})
102+
.run(unit -> {
103+
new BodyReferenceImpl(len, StandardCharsets.UTF_8, unit.get(File.class),
104+
unit.get(InputStream.class), bsize);
105+
});
106+
}
107+
108+
@Test(expected = IOException.class)
109+
public void inErrOnClose() throws Exception {
110+
long len = 1;
111+
long bsize = 2;
112+
byte[] bytes = "bytes".getBytes();
113+
new MockUnit(File.class, InputStream.class)
114+
.expect(baos(bytes))
115+
.expect(unit -> {
116+
InputStream in = unit.get(InputStream.class);
117+
in.close();
118+
expectLastCall().andThrow(new IOException());
119+
120+
OutputStream out = unit.get(ByteArrayOutputStream.class);
121+
out.close();
122+
123+
unit.mockStatic(ByteStreams.class);
124+
expect(ByteStreams.copy(in, out)).andReturn(1L);
125+
})
126+
.run(unit -> {
127+
new BodyReferenceImpl(len, StandardCharsets.UTF_8, unit.get(File.class),
128+
unit.get(InputStream.class), bsize);
129+
});
130+
}
131+
59132
@Test
60133
public void fromFile() throws Exception {
61134
long len = 1;

jooby/src/test/java/org/jooby/internal/reqparam/ParserExecutorTest.java

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

51+
@Test
52+
public void ctxStr() throws Exception {
53+
new MockUnit(Injector.class, Upload.class)
54+
.run(unit -> {
55+
Set<Parser> parsers = Sets
56+
.newHashSet((Parser) (type, ctx) -> ctx.toString());
57+
Object converted = new ParserExecutor(unit.get(Injector.class), parsers,
58+
new StatusCodeProvider(ConfigFactory.empty()))
59+
.convert(TypeLiteral.get(Upload.class),
60+
new UploadParamReferenceImpl("x", Lists.newArrayList()));
61+
assertEquals(parsers.toString(), converted);
62+
});
63+
}
64+
5165
}

0 commit comments

Comments
 (0)