From e7498f83e49e43ecfbc5ce9e74deb2b9be3cea42 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 24 Jan 2017 22:02:56 +0100 Subject: [PATCH] Add dependency injection configuration to identity accessor --- .../sone/template/IdentityAccessor.java | 5 ++++ .../sone/template/IdentityAccessorTest.java | 34 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/main/java/net/pterodactylus/sone/template/IdentityAccessor.java b/src/main/java/net/pterodactylus/sone/template/IdentityAccessor.java index 74c1a77..7766af3 100644 --- a/src/main/java/net/pterodactylus/sone/template/IdentityAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/IdentityAccessor.java @@ -19,6 +19,9 @@ package net.pterodactylus.sone.template; import java.util.Set; +import javax.inject.Inject; +import javax.inject.Singleton; + import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.OwnIdentity; @@ -32,6 +35,7 @@ import net.pterodactylus.util.template.TemplateContext; * * @author David ‘Bombe’ Roden */ +@Singleton public class IdentityAccessor extends ReflectionAccessor { /** The core. */ @@ -43,6 +47,7 @@ public class IdentityAccessor extends ReflectionAccessor { * @param core * The core */ + @Inject public IdentityAccessor(Core core) { this.core = core; } diff --git a/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java index 33149e0..68d8078 100644 --- a/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java +++ b/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java @@ -2,6 +2,8 @@ package net.pterodactylus.sone.template; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.sameInstance; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -13,6 +15,10 @@ import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.IdentityManager; import net.pterodactylus.sone.freenet.wot.OwnIdentity; +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; @@ -78,4 +84,32 @@ public class IdentityAccessorTest { Matchers.is(identity.hashCode())); } + @Test + public void accessorCanBeCreatedByGuice() { + Injector injector = Guice.createInjector( + new Module() { + @Override + public void configure(Binder binder) { + binder.bind(Core.class).toInstance(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)); + } + } + ); + IdentityAccessor firstAccessor = injector.getInstance(IdentityAccessor.class); + IdentityAccessor secondAccessor = injector.getInstance(IdentityAccessor.class); + assertThat(firstAccessor, sameInstance(secondAccessor)); + } + } -- 2.7.4