Add dependency injection configuration to identity accessor
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 24 Jan 2017 21:02:56 +0000 (22:02 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 24 Jan 2017 21:02:56 +0000 (22:02 +0100)
src/main/java/net/pterodactylus/sone/template/IdentityAccessor.java
src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java

index 74c1a77..7766af3 100644 (file)
@@ -19,6 +19,9 @@ package net.pterodactylus.sone.template;
 
 import java.util.Set;
 
 
 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;
 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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
+@Singleton
 public class IdentityAccessor extends ReflectionAccessor {
 
        /** The core. */
 public class IdentityAccessor extends ReflectionAccessor {
 
        /** The core. */
@@ -43,6 +47,7 @@ public class IdentityAccessor extends ReflectionAccessor {
         * @param core
         *            The core
         */
         * @param core
         *            The core
         */
+       @Inject
        public IdentityAccessor(Core core) {
                this.core = core;
        }
        public IdentityAccessor(Core core) {
                this.core = core;
        }
index 33149e0..68d8078 100644 (file)
@@ -2,6 +2,8 @@ package net.pterodactylus.sone.template;
 
 import static java.util.Arrays.asList;
 import static org.hamcrest.MatcherAssert.assertThat;
 
 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;
 
 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 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;
 import org.hamcrest.Matchers;
 import org.junit.Before;
 import org.junit.Test;
@@ -78,4 +84,32 @@ public class IdentityAccessorTest {
                                Matchers.<Object>is(identity.hashCode()));
        }
 
                                Matchers.<Object>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));
+       }
+
 }
 }