Skip to content

Commit 2455787

Browse files
committed
Flyway integration tests fixed #1034
1 parent 0553eae commit 2455787

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

modules/coverage-report/src/test/java/org/jooby/issues/Issue623.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package org.jooby.issues;
22

3-
import java.util.Arrays;
4-
3+
import com.typesafe.config.ConfigFactory;
4+
import com.typesafe.config.ConfigValueFactory;
55
import org.flywaydb.core.Flyway;
66
import org.jooby.flyway.Flywaydb;
77
import org.jooby.test.ServerFeature;
88
import org.junit.Test;
99

10-
import com.typesafe.config.ConfigFactory;
11-
import com.typesafe.config.ConfigValueFactory;
10+
import java.util.Arrays;
1211

1312
public class Issue623 extends ServerFeature {
1413

modules/jooby-flyway/src/main/java/org/jooby/flyway/Flywaydb.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@
208208
import com.google.inject.name.Names;
209209
import com.typesafe.config.Config;
210210
import com.typesafe.config.ConfigFactory;
211+
import static com.typesafe.config.ConfigFactory.empty;
212+
import com.typesafe.config.ConfigValueType;
211213
import static java.util.Objects.requireNonNull;
212214
import org.flywaydb.core.Flyway;
213215
import org.jooby.Env;
@@ -356,8 +358,8 @@ public Flywaydb() {
356358

357359
@Override
358360
public void configure(final Env env, final Config conf, final Binder binder) {
359-
Config $base = conf.getConfig("flyway").withoutPath(name);
360-
Config $flyway = Try.apply(() -> conf.getConfig("flyway." + name).withFallback($base))
361+
Config $base = flyway(conf.getConfig("flyway"));
362+
Config $flyway = Try.apply(() -> conf.getConfig(name).withFallback($base))
361363
.orElse($base);
362364

363365
Flyway flyway = new Flyway();
@@ -377,6 +379,14 @@ public void configure(final Env env, final Config conf, final Binder binder) {
377379
cmds.forEach(cmd -> cmd.run(flyway));
378380
}
379381

382+
private Config flyway(Config conf) {
383+
Config flyway = conf.root().entrySet().stream()
384+
.filter(it -> it.getValue().valueType() != ConfigValueType.OBJECT)
385+
.reduce(empty(), (seed, entry) -> seed.withValue(entry.getKey(), entry.getValue()),
386+
Config::withFallback);
387+
return flyway;
388+
}
389+
380390
@Override
381391
public Config config() {
382392
return ConfigFactory.parseResources(getClass(), "flyway.conf");

modules/jooby-servlet/src/main/java/org/jooby/servlet/ServletServletResponse.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@
206206
import com.google.common.collect.ImmutableList;
207207
import com.google.common.io.ByteStreams;
208208
import static java.util.Objects.requireNonNull;
209-
import org.jooby.funzy.Try;
210209
import org.jooby.spi.NativeResponse;
211210

212211
import javax.servlet.AsyncContext;
@@ -282,22 +281,18 @@ public void send(final ByteBuffer buffer) throws Exception {
282281

283282
@Override
284283
public void send(final FileChannel file) throws Exception {
285-
Try.of(file).run(src -> {
286-
WritableByteChannel dest = Channels.newChannel(rsp.getOutputStream());
287-
src.transferTo(0, src.size(), dest);
288-
dest.close();
289-
committed = true;
290-
}).throwException();
284+
send(file, 0, file.size());
291285
}
292286

293287
@Override
294-
public void send(final FileChannel channel, final long position, final long count) {
295-
Try.of(channel).run(src -> {
288+
public void send(final FileChannel channel, final long position, final long count)
289+
throws Exception {
290+
try (FileChannel src = channel) {
296291
WritableByteChannel dest = Channels.newChannel(rsp.getOutputStream());
297292
src.transferTo(position, count, dest);
298293
dest.close();
299294
committed = true;
300-
}).throwException();
295+
}
301296
}
302297

303298
@Override

modules/jooby-servlet/src/test/java/org/jooby/servlet/ServletServletResponseTest.java

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package org.jooby.servlet;
22

3+
import com.google.common.io.ByteStreams;
34
import static org.easymock.EasyMock.expect;
5+
import org.jooby.funzy.Throwing;
6+
import org.jooby.test.MockUnit;
47
import static org.junit.Assert.assertEquals;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.powermock.core.classloader.annotations.PrepareForTest;
11+
import org.powermock.modules.junit4.PowerMockRunner;
512

13+
import javax.servlet.ServletOutputStream;
14+
import javax.servlet.http.HttpServletRequest;
15+
import javax.servlet.http.HttpServletResponse;
616
import java.io.InputStream;
717
import java.nio.ByteBuffer;
818
import java.nio.channels.Channels;
@@ -12,21 +22,9 @@
1222
import java.util.Collections;
1323
import java.util.Optional;
1424

15-
import javax.servlet.ServletOutputStream;
16-
import javax.servlet.http.HttpServletRequest;
17-
import javax.servlet.http.HttpServletResponse;
18-
19-
import org.jooby.test.MockUnit;
20-
import org.junit.Test;
21-
import org.junit.runner.RunWith;
22-
import org.powermock.core.classloader.annotations.PrepareForTest;
23-
import org.powermock.modules.junit4.PowerMockRunner;
24-
25-
import com.google.common.io.ByteStreams;
26-
2725
@RunWith(PowerMockRunner.class)
2826
@PrepareForTest({ServletServletResponse.class, Channels.class, ByteStreams.class,
29-
FileChannel.class })
27+
FileChannel.class, Throwing.class, Throwing.Runnable.class})
3028
public class ServletServletResponseTest {
3129

3230
@Test
@@ -178,7 +176,7 @@ public void sendByteBuffer() throws Exception {
178176
public void sendFileChannel() throws Exception {
179177
new MockUnit(HttpServletRequest.class, HttpServletResponse.class, ServletOutputStream.class)
180178
.expect(unit -> {
181-
FileChannel channel = unit.powerMock(FileChannel.class);
179+
FileChannel channel = unit.partialMock(FileChannel.class, "transferTo", "close");
182180
unit.registerMock(FileChannel.class, channel);
183181
})
184182
.expect(unit -> {
@@ -192,7 +190,6 @@ public void sendFileChannel() throws Exception {
192190
expect(Channels.newChannel(output)).andReturn(channel);
193191

194192
expect(fchannel.transferTo(0L, 10L, channel)).andReturn(1L);
195-
196193
fchannel.close();
197194
channel.close();
198195

@@ -209,23 +206,23 @@ public void sendFileChannel() throws Exception {
209206
public void sendInputStream() throws Exception {
210207
new MockUnit(HttpServletRequest.class, HttpServletResponse.class, InputStream.class,
211208
ServletOutputStream.class)
212-
.expect(unit -> {
213-
InputStream in = unit.get(InputStream.class);
214-
ServletOutputStream output = unit.get(ServletOutputStream.class);
209+
.expect(unit -> {
210+
InputStream in = unit.get(InputStream.class);
211+
ServletOutputStream output = unit.get(ServletOutputStream.class);
215212

216-
unit.mockStatic(ByteStreams.class);
217-
expect(ByteStreams.copy(in, output)).andReturn(0L);
213+
unit.mockStatic(ByteStreams.class);
214+
expect(ByteStreams.copy(in, output)).andReturn(0L);
218215

219-
output.close();
220-
in.close();
216+
output.close();
217+
in.close();
221218

222-
HttpServletResponse rsp = unit.get(HttpServletResponse.class);
223-
expect(rsp.getOutputStream()).andReturn(output);
224-
})
225-
.run(unit -> {
226-
new ServletServletResponse(unit.get(HttpServletRequest.class),
227-
unit.get(HttpServletResponse.class)).send(unit.get(InputStream.class));
228-
});
219+
HttpServletResponse rsp = unit.get(HttpServletResponse.class);
220+
expect(rsp.getOutputStream()).andReturn(output);
221+
})
222+
.run(unit -> {
223+
new ServletServletResponse(unit.get(HttpServletRequest.class),
224+
unit.get(HttpServletResponse.class)).send(unit.get(InputStream.class));
225+
});
229226
}
230227

231228
}

0 commit comments

Comments
 (0)