Add some guice binding helpers for Java
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 25 Jan 2017 18:04:42 +0000 (19:04 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 25 Jan 2017 18:04:42 +0000 (19:04 +0100)
src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java
src/test/kotlin/net/pterodactylus/sone/test/Guice.kt

index 68d8078..6744bd8 100644 (file)
@@ -14,11 +14,10 @@ import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.IdentityManager;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+import net.pterodactylus.sone.test.GuiceKt;
 
-import com.google.inject.Binder;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.Module;
 import org.hamcrest.Matchers;
 import org.junit.Before;
 import org.junit.Test;
@@ -87,12 +86,7 @@ public class IdentityAccessorTest {
        @Test
        public void accessorCanBeCreatedByGuice() {
                Injector injector = Guice.createInjector(
-                               new Module() {
-                                       @Override
-                                       public void configure(Binder binder) {
-                                               binder.bind(Core.class).toInstance(mock(Core.class));
-                                       }
-                               }
+                               GuiceKt.supply(Core.class).byInstance(mock(Core.class))
                );
                assertThat(injector.getInstance(IdentityAccessor.class), notNullValue());
        }
@@ -100,12 +94,7 @@ public class IdentityAccessorTest {
        @Test
        public void accessorIsCreatedAsSingleton() {
                Injector injector = Guice.createInjector(
-                               new Module() {
-                                       @Override
-                                       public void configure(Binder binder) {
-                                               binder.bind(Core.class).toInstance(mock(Core.class));
-                                       }
-                               }
+                               GuiceKt.supply(Core.class).byInstance(mock(Core.class))
                );
                IdentityAccessor firstAccessor = injector.getInstance(IdentityAccessor.class);
                IdentityAccessor secondAccessor = injector.getInstance(IdentityAccessor.class);
index 5eca8fa..a5df14e 100644 (file)
@@ -8,3 +8,17 @@ fun <T : Any> KClass<T>.isInstance(instance: T) = Module { it.bind(this.java).to
 fun <T : Any> KClass<T>.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider { instance } }
 fun <T : Any> KClass<T>.isProvidedBy(provider: com.google.inject.Provider<T>) = Module { it.bind(this.java).toProvider(provider) }
 fun <T : Any> KClass<T>.isProvidedBy(provider: KClass<out Provider<T>>) = Module { it.bind(this.java).toProvider(provider.java) }
+
+fun <T : Any> supply(javaClass: Class<T>): Source<T> = object : Source<T> {
+       override fun fromInstance(instance: T) = Module { it.bind(javaClass).toInstance(instance) }
+       override fun byInstance(instance: T) = Module { it.bind(javaClass).toProvider { instance } }
+       override fun byProvider(provider: com.google.inject.Provider<T>) = Module { it.bind(javaClass).toProvider(provider) }
+       override fun byProvider(provider: Class<Provider<T>>) = Module { it.bind(javaClass).toProvider(provider) }
+}
+
+interface Source<T : Any> {
+       fun fromInstance(instance: T): Module
+       fun byInstance(instance: T): Module
+       fun byProvider(provider: com.google.inject.Provider<T>): Module
+       fun byProvider(provider: Class<Provider<T>>): Module
+}