Skip to content

Commit 93b2921

Browse files
committed
add a conf method to Jooby fix jooby-project#428
1 parent 12b7bf1 commit 93b2921

File tree

2 files changed

+108
-11
lines changed

2 files changed

+108
-11
lines changed

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3297,6 +3297,30 @@ public Jooby use(final Jooby.Module module) {
32973297
return this;
32983298
}
32993299

3300+
/**
3301+
* Set/specify a custom .conf file, useful when you don't want a <code>application.conf</code>
3302+
* file.
3303+
*
3304+
* @param path Classpath location.
3305+
* @return This jooby instance.
3306+
*/
3307+
public Jooby conf(final String path) {
3308+
use(ConfigFactory.parseResources(path));
3309+
return this;
3310+
}
3311+
3312+
/**
3313+
* Set/specify a custom .conf file, useful when you don't want a <code>application.conf</code>
3314+
* file.
3315+
*
3316+
* @param path File system location.
3317+
* @return This jooby instance.
3318+
*/
3319+
public Jooby conf(final File path) {
3320+
use(ConfigFactory.parseFile(path));
3321+
return this;
3322+
}
3323+
33003324
/**
33013325
* Set the application configuration object. You must call this method when the default file
33023326
* name: <code>application.conf</code> doesn't work for you or when you need/want to register two
@@ -3307,7 +3331,7 @@ public Jooby use(final Jooby.Module module) {
33073331
* @see Config
33083332
*/
33093333
public Jooby use(final Config config) {
3310-
this.srcconf = requireNonNull(config, "A config is required.");
3334+
this.srcconf = requireNonNull(config, "Config required.");
33113335
return this;
33123336
}
33133337

jooby/src/test/java/org/jooby/JoobyTest.java

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,17 +2704,16 @@ public void useConfig() throws Exception {
27042704
.expect(webSockets)
27052705
.expect(tmpdir)
27062706
.expect(err)
2707-
.expect(
2708-
unit -> {
2709-
AnnotatedBindingBuilder<List<Integer>> listAnnotatedBinding = unit
2710-
.mock(AnnotatedBindingBuilder.class);
2711-
listAnnotatedBinding.toInstance(Arrays.asList(1, 2, 3));
2707+
.expect(unit -> {
2708+
AnnotatedBindingBuilder<List<Integer>> listAnnotatedBinding = unit
2709+
.mock(AnnotatedBindingBuilder.class);
2710+
listAnnotatedBinding.toInstance(Arrays.asList(1, 2, 3));
27122711

2713-
Binder binder = unit.get(Binder.class);
2714-
Key<List<Integer>> key = (Key<List<Integer>>) Key.get(Types.listOf(Integer.class),
2715-
Names.named("list"));
2716-
expect(binder.bind(key)).andReturn(listAnnotatedBinding);
2717-
})
2712+
Binder binder = unit.get(Binder.class);
2713+
Key<List<Integer>> key = (Key<List<Integer>>) Key.get(Types.listOf(Integer.class),
2714+
Names.named("list"));
2715+
expect(binder.bind(key)).andReturn(listAnnotatedBinding);
2716+
})
27182717
.run(unit -> {
27192718

27202719
Jooby jooby = new Jooby();
@@ -2726,6 +2725,80 @@ public void useConfig() throws Exception {
27262725
}, boot);
27272726
}
27282727

2728+
@Test
2729+
public void customConf() throws Exception {
2730+
2731+
new MockUnit(Binder.class)
2732+
.expect(guice)
2733+
.expect(shutdown)
2734+
.expect(config)
2735+
.expect(env)
2736+
.expect(classInfo)
2737+
.expect(ssl)
2738+
.expect(charset)
2739+
.expect(locale)
2740+
.expect(zoneId)
2741+
.expect(timeZone)
2742+
.expect(dateTimeFormatter)
2743+
.expect(numberFormat)
2744+
.expect(decimalFormat)
2745+
.expect(renderers)
2746+
.expect(session)
2747+
.expect(routes)
2748+
.expect(routeHandler)
2749+
.expect(params)
2750+
.expect(requestScope)
2751+
.expect(webSockets)
2752+
.expect(tmpdir)
2753+
.expect(err)
2754+
.run(unit -> {
2755+
2756+
Jooby jooby = new Jooby();
2757+
2758+
jooby.conf("JoobyTest.conf");
2759+
2760+
jooby.start();
2761+
2762+
}, boot);
2763+
}
2764+
2765+
@Test
2766+
public void customConfFile() throws Exception {
2767+
2768+
new MockUnit(Binder.class)
2769+
.expect(guice)
2770+
.expect(shutdown)
2771+
.expect(config)
2772+
.expect(env)
2773+
.expect(classInfo)
2774+
.expect(ssl)
2775+
.expect(charset)
2776+
.expect(locale)
2777+
.expect(zoneId)
2778+
.expect(timeZone)
2779+
.expect(dateTimeFormatter)
2780+
.expect(numberFormat)
2781+
.expect(decimalFormat)
2782+
.expect(renderers)
2783+
.expect(session)
2784+
.expect(routes)
2785+
.expect(routeHandler)
2786+
.expect(params)
2787+
.expect(requestScope)
2788+
.expect(webSockets)
2789+
.expect(tmpdir)
2790+
.expect(err)
2791+
.run(unit -> {
2792+
2793+
Jooby jooby = new Jooby();
2794+
2795+
jooby.conf(new File("JoobyTest.conf"));
2796+
2797+
jooby.start();
2798+
2799+
}, boot);
2800+
}
2801+
27292802
@Test
27302803
public void useMissingConfig() throws Exception {
27312804

0 commit comments

Comments
 (0)