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;
@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());
}
@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);
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
+}