Skip to content

Commit 29a2821

Browse files
committed
camel test coverage 100%
1 parent d8a54c5 commit 29a2821

2 files changed

Lines changed: 31 additions & 34 deletions

File tree

jooby-camel/src/main/java/org/jooby/camel/Camel.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
package org.jooby.camel;
2020

2121
import static java.util.Objects.requireNonNull;
22+
import static javaslang.API.$;
23+
import static javaslang.API.Case;
24+
import static javaslang.API.Match;
2225

2326
import java.io.File;
2427
import java.lang.reflect.Method;
@@ -50,6 +53,8 @@
5053
import com.typesafe.config.Config;
5154
import com.typesafe.config.ConfigFactory;
5255

56+
import javaslang.control.Try;
57+
5358
/**
5459
* Camel for Jooby. Exposes a {@link CamelContext}, {@link ProducerTemplate} and
5560
* {@link ConsumerTemplate}.
@@ -272,6 +277,12 @@ public Camel routes(final Class<?> routeClass) {
272277
return this;
273278
}
274279

280+
/**
281+
* Hook to customize a {@link CamelContext}.
282+
*
283+
* @param configurer A configurer callback.
284+
* @return This instance.
285+
*/
275286
public Camel doWith(final Configurer<CamelContext> configurer) {
276287
this.configurer = requireNonNull(configurer, "A configurer is required.");
277288
return this;
@@ -311,8 +322,7 @@ public void configure(final Env env, final Config config, final Binder binder) {
311322
.withoutPath("shutdown")
312323
.withoutPath("threads")
313324
.withoutPath("jmx")
314-
.withoutPath("streamCaching")
315-
);
325+
.withoutPath("streamCaching"));
316326

317327
if (!$camel.getBoolean("jmx")) {
318328
ctx.disableJMX();
@@ -356,7 +366,7 @@ public void configure(final Env env, final Config config, final Binder binder) {
356366
try {
357367
routes(routes, ctx, config);
358368
} catch (Exception ex) {
359-
throw new IllegalStateException("Route builder resulted in error", ex);
369+
throw new IllegalStateException("context.addRoutes(RouteBuilder) resulted in error", ex);
360370
}
361371
}
362372

@@ -426,29 +436,26 @@ private <T> T configure(final T source, final Config config) {
426436
config.entrySet().forEach(o -> {
427437
String key = o.getKey();
428438
String setter = "set" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, key);
429-
Object value = o.getValue().unwrapped();
439+
Object raw = o.getValue().unwrapped();
430440
Optional<Method> result = methods.stream()
431441
.filter(m -> m.getName().equals(setter))
432442
.findFirst();
433443
if (result.isPresent()) {
434444
Method method = result.get();
435445
Class type = method.getParameterTypes()[0];
436-
if (Enum.class.isAssignableFrom(type)) {
437-
value = Enum.valueOf(type, value.toString());
438-
}
439-
if (Long.class.isAssignableFrom(type)) {
440-
value = ((Number) value).longValue();
441-
}
442-
if (File.class.isAssignableFrom(type)) {
443-
value = new File(value.toString());
444-
}
445-
try {
446-
method.invoke(source, value);
447-
} catch (Exception ex) {
448-
throw new IllegalArgumentException("Bad option: <" + value + "> for: " + method, ex);
449-
}
446+
Object value = Match(type).of(
447+
Case(Enum.class::isAssignableFrom, () -> {
448+
Object e = Enum.valueOf(type, raw.toString());
449+
return e;
450+
}),
451+
Case(Long.class::isAssignableFrom, () -> ((Number) raw).longValue()),
452+
Case(File.class::isAssignableFrom, () -> new File(raw.toString())),
453+
Case($(), raw));
454+
Try.of(() -> method.invoke(source, value)).onFailure(ex -> {
455+
throw new IllegalArgumentException("Bad option: <" + raw + "> for: " + method, ex);
456+
});
450457
} else {
451-
log.error("Unknown option camel.{} = {}", key, value);
458+
log.error("Unknown option camel.{} = {}", key, raw);
452459
}
453460
});
454461
return source;

jooby-camel/src/test/java/org/jooby/camel/CamelTest.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static org.easymock.EasyMock.expectLastCall;
55
import static org.easymock.EasyMock.isA;
66
import static org.junit.Assert.assertEquals;
7-
import static org.junit.Assert.assertTrue;
87

98
import java.io.File;
109
import java.io.IOException;
@@ -862,7 +861,7 @@ public void withRoutes() throws Exception {
862861
}
863862

864863
@SuppressWarnings("unchecked")
865-
@Test
864+
@Test(expected = IllegalStateException.class)
866865
public void withRoutesErr() throws Exception {
867866
Config camel = defConfig;
868867
new MockUnit(Env.class, Config.class, Binder.class)
@@ -917,6 +916,7 @@ public void withRoutesErr() throws Exception {
917916
ctx.addComponent("properties", properties);
918917
ctx.disableJMX();
919918
ctx.addRoutes(unit.capture(RouteBuilder.class));
919+
expectLastCall().andThrow(new Exception("intentional error"));
920920

921921
ProducerTemplate producer = unit.mock(ProducerTemplate.class);
922922
expect(ctx.createProducerTemplate()).andReturn(producer);
@@ -964,19 +964,9 @@ public void withRoutesErr() throws Exception {
964964
})
965965
.expect(onStop)
966966
.run(unit -> {
967-
new Camel() {
968-
@Override
969-
public void configure(final Env env, final Config config, final Binder binder) {
970-
super.configure(env, config, binder);
971-
try {
972-
unit.captured(RouteBuilder.class).iterator().next().configure();
973-
} catch (Exception ex) {
974-
assertTrue(ex instanceof IllegalStateException);
975-
}
976-
}
977-
}.routes((router, config) -> {
978-
throw new IllegalStateException("intentional err");
979-
}).configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class));
967+
new Camel()
968+
.routes((router, config) -> {
969+
}).configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class));
980970
});
981971
}
982972

0 commit comments

Comments
 (0)