Repository: jooby-project/jooby
Pull request #1210 (2018-07-13 20:30:03)

Implemented require(String, TypeLiteral) according to #1209.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/17980678/badge)](https://coveralls.io/builds/17980678)

Coverage increased (+3.0e-05%) to 99.576% when pulling **62b3fec63a47d35c605711ae0668d402ac327748 on DerDackel:issue1209** into **f7eb2d5e3bc35aa1640ff64b49e16ceefa2999c6 on jooby-project:master**.


Patch:
diff --git a/jooby/src/main/java/org/jooby/Registry.java b/jooby/src/main/java/org/jooby/Registry.java
index cd123bf0b..4f8f87f84 100644
--- a/jooby/src/main/java/org/jooby/Registry.java
+++ b/jooby/src/main/java/org/jooby/Registry.java
@@ -268,4 +268,17 @@ public interface Registry {
   @Nonnull
   <T> T require(Key<T> key);
 
+  /**
+   * Request a service of a given type by a given name.
+   *
+   * @param name A service name
+   * @param type A service type.
+   * @param <T> Service type.
+   * @return A ready to use object
+   */
+  @Nonnull
+  default <T> T require(final String name, final TypeLiteral<T> type) {
+    return require(Key.get(type, Names.named(name)));
+  }
+
 }


Test patch:
diff --git a/jooby/src/test/java/org/jooby/JoobyTest.java b/jooby/src/test/java/org/jooby/JoobyTest.java
index 5bd2bd188..8c4900fd5 100644
--- a/jooby/src/test/java/org/jooby/JoobyTest.java
+++ b/jooby/src/test/java/org/jooby/JoobyTest.java
@@ -823,6 +823,50 @@ public class JoobyTest {
         }, boot);
   }
 
+  @Test
+  public void requireByNameAndTypeLiteralShouldWork() throws Exception {
+
+    Object someVerySpecificObject = new Object();
+
+    new MockUnit(Binder.class)
+            .expect(guice)
+            .expect(internalOnStart(false))
+            .expect(shutdown)
+            .expect(config)
+            .expect(env)
+            .expect(classInfo)
+            .expect(ssl)
+            .expect(charset)
+            .expect(locale)
+            .expect(zoneId)
+            .expect(timeZone)
+            .expect(dateTimeFormatter)
+            .expect(numberFormat)
+            .expect(decimalFormat)
+            .expect(renderers)
+            .expect(session)
+            .expect(routes)
+            .expect(routeHandler)
+            .expect(params)
+            .expect(requestScope)
+            .expect(webSockets)
+            .expect(tmpdir)
+            .expect(err)
+            .expect(unit -> {
+              Injector injector = unit.get(Injector.class);
+              expect(injector.getInstance(Key.get(Object.class, Names.named("foo")))).andReturn(someVerySpecificObject);
+            })
+            .run(unit -> {
+
+              Jooby jooby = new Jooby();
+
+              jooby.start();
+              Object actual = jooby.require("foo", TypeLiteral.get(Object.class));
+              assertEquals(actual, someVerySpecificObject);
+
+            }, boot);
+  }
+
   private Block internalOnStart(final boolean b) {
     return unit -> {
       Config conf = unit.get(Config.class);
diff --git a/modules/coverage-report/src/test/java/org/jooby/RegistryRequireTest.java b/modules/coverage-report/src/test/java/org/jooby/RegistryRequireTest.java
new file mode 100644
index 000000000..815b3a164
--- /dev/null
+++ b/modules/coverage-report/src/test/java/org/jooby/RegistryRequireTest.java
@@ -0,0 +1,68 @@
+package org.jooby;
+
+import com.google.inject.Binder;
+import com.google.inject.ConfigurationException;
+import com.google.inject.Key;
+import com.google.inject.TypeLiteral;
+import com.google.inject.name.Names;
+import com.typesafe.config.Config;
+import org.jooby.test.ServerFeature;
+import org.junit.Test;
+
+public class RegistryRequireTest extends ServerFeature {
+
+    class Plumbus {
+
+        private final String message;
+
+        public Plumbus(String message) {
+            this.message = message;
+        }
+        @Override
+        public String toString() {
+            return message;
+        }
+    }
+
+    class BagOfHolding implements Jooby.Module {
+
+        private final Plumbus thing;
+        private final String name;
+
+        BagOfHolding(String name, Plumbus thing) {
+            this.name = name;
+            this.thing = thing;
+        }
+
+        @Override
+        public void configure(Env env, Config conf, Binder binder) {
+            binder.bind(Key.get(TypeLiteral.get(Plumbus.class), Names.named(name))).toInstance(thing);
+        }
+    }
+
+    {
+        use(new BagOfHolding("RicksPlumbus", new Plumbus("Property of Rick Sanchez!")));
+
+        get("/gimme", (req, rsp) -> {
+            try {
+                Plumbus myPlumbus = require(req.param("thing").value(), TypeLiteral.get(Plumbus.class));
+                rsp.send(myPlumbus.toString());
+            } catch (ConfigurationException e) {
+                rsp.status(Status.NOT_FOUND);
+                rsp.send("No Rick, no plumbus!");
+            }
+
+
+        });
+    }
+
+    @Test
+    public void plumbussesShouldBeDependencyInjected() throws Exception {
+        request().get("/gimme?thing=RicksPlumbus").expect("Property of Rick Sanchez!");
+    }
+
+    @Test
+    public void mortysShouldNotReceiveRicksPlumbus() throws Exception {
+        request().get("/gimme?thing=MortysPlumbus").expect(Status.NOT_FOUND.value()).expect("No Rick, no plumbus!");
+    }
+}