Skip to content

Commit 393c1a6

Browse files
DerDackeljknack
authored andcommitted
Implemented require(String, TypeLiteral) according to jooby-project#1209. (jooby-project#1210)
1 parent f7eb2d5 commit 393c1a6

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

jooby/src/main/java/org/jooby/Registry.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,17 @@ default <T> T require(final TypeLiteral<T> type) {
268268
@Nonnull
269269
<T> T require(Key<T> key);
270270

271+
/**
272+
* Request a service of a given type by a given name.
273+
*
274+
* @param name A service name
275+
* @param type A service type.
276+
* @param <T> Service type.
277+
* @return A ready to use object
278+
*/
279+
@Nonnull
280+
default <T> T require(final String name, final TypeLiteral<T> type) {
281+
return require(Key.get(type, Names.named(name)));
282+
}
283+
271284
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,50 @@ public void withInternalOnStart() throws Exception {
823823
}, boot);
824824
}
825825

826+
@Test
827+
public void requireByNameAndTypeLiteralShouldWork() throws Exception {
828+
829+
Object someVerySpecificObject = new Object();
830+
831+
new MockUnit(Binder.class)
832+
.expect(guice)
833+
.expect(internalOnStart(false))
834+
.expect(shutdown)
835+
.expect(config)
836+
.expect(env)
837+
.expect(classInfo)
838+
.expect(ssl)
839+
.expect(charset)
840+
.expect(locale)
841+
.expect(zoneId)
842+
.expect(timeZone)
843+
.expect(dateTimeFormatter)
844+
.expect(numberFormat)
845+
.expect(decimalFormat)
846+
.expect(renderers)
847+
.expect(session)
848+
.expect(routes)
849+
.expect(routeHandler)
850+
.expect(params)
851+
.expect(requestScope)
852+
.expect(webSockets)
853+
.expect(tmpdir)
854+
.expect(err)
855+
.expect(unit -> {
856+
Injector injector = unit.get(Injector.class);
857+
expect(injector.getInstance(Key.get(Object.class, Names.named("foo")))).andReturn(someVerySpecificObject);
858+
})
859+
.run(unit -> {
860+
861+
Jooby jooby = new Jooby();
862+
863+
jooby.start();
864+
Object actual = jooby.require("foo", TypeLiteral.get(Object.class));
865+
assertEquals(actual, someVerySpecificObject);
866+
867+
}, boot);
868+
}
869+
826870
private Block internalOnStart(final boolean b) {
827871
return unit -> {
828872
Config conf = unit.get(Config.class);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.jooby;
2+
3+
import com.google.inject.Binder;
4+
import com.google.inject.ConfigurationException;
5+
import com.google.inject.Key;
6+
import com.google.inject.TypeLiteral;
7+
import com.google.inject.name.Names;
8+
import com.typesafe.config.Config;
9+
import org.jooby.test.ServerFeature;
10+
import org.junit.Test;
11+
12+
public class RegistryRequireTest extends ServerFeature {
13+
14+
class Plumbus {
15+
16+
private final String message;
17+
18+
public Plumbus(String message) {
19+
this.message = message;
20+
}
21+
@Override
22+
public String toString() {
23+
return message;
24+
}
25+
}
26+
27+
class BagOfHolding implements Jooby.Module {
28+
29+
private final Plumbus thing;
30+
private final String name;
31+
32+
BagOfHolding(String name, Plumbus thing) {
33+
this.name = name;
34+
this.thing = thing;
35+
}
36+
37+
@Override
38+
public void configure(Env env, Config conf, Binder binder) {
39+
binder.bind(Key.get(TypeLiteral.get(Plumbus.class), Names.named(name))).toInstance(thing);
40+
}
41+
}
42+
43+
{
44+
use(new BagOfHolding("RicksPlumbus", new Plumbus("Property of Rick Sanchez!")));
45+
46+
get("/gimme", (req, rsp) -> {
47+
try {
48+
Plumbus myPlumbus = require(req.param("thing").value(), TypeLiteral.get(Plumbus.class));
49+
rsp.send(myPlumbus.toString());
50+
} catch (ConfigurationException e) {
51+
rsp.status(Status.NOT_FOUND);
52+
rsp.send("No Rick, no plumbus!");
53+
}
54+
55+
56+
});
57+
}
58+
59+
@Test
60+
public void plumbussesShouldBeDependencyInjected() throws Exception {
61+
request().get("/gimme?thing=RicksPlumbus").expect("Property of Rick Sanchez!");
62+
}
63+
64+
@Test
65+
public void mortysShouldNotReceiveRicksPlumbus() throws Exception {
66+
request().get("/gimme?thing=MortysPlumbus").expect(Status.NOT_FOUND.value()).expect("No Rick, no plumbus!");
67+
}
68+
}

0 commit comments

Comments
 (0)