From: David ‘Bombe’ Roden Date: Wed, 25 Jan 2017 18:04:42 +0000 (+0100) Subject: Add some guice binding helpers for Java X-Git-Tag: 0.9.7^2~314 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=a1b3db7a8bb0c03d291371e0d15b5681f7ec0083;p=Sone.git Add some guice binding helpers for Java --- diff --git a/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java index 68d8078..6744bd8 100644 --- a/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java +++ b/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java @@ -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); diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt index 5eca8fa..a5df14e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt @@ -8,3 +8,17 @@ fun KClass.isInstance(instance: T) = Module { it.bind(this.java).to fun KClass.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider { instance } } fun KClass.isProvidedBy(provider: com.google.inject.Provider) = Module { it.bind(this.java).toProvider(provider) } fun KClass.isProvidedBy(provider: KClass>) = Module { it.bind(this.java).toProvider(provider.java) } + +fun supply(javaClass: Class): Source = object : Source { + 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) = Module { it.bind(javaClass).toProvider(provider) } + override fun byProvider(provider: Class>) = Module { it.bind(javaClass).toProvider(provider) } +} + +interface Source { + fun fromInstance(instance: T): Module + fun byInstance(instance: T): Module + fun byProvider(provider: com.google.inject.Provider): Module + fun byProvider(provider: Class>): Module +}