PageRenderTime 34ms CodeModel.GetById 26ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/scanner/sonatype/src/test/java/de/devsurf/injection/guice/scanner/sonatype/tests/autobind/filter/PackageFilterTests.java

https://github.com/manzke/guice-automatic-injection
Java | 145 lines | 107 code | 22 blank | 16 comment | 1 complexity | e55b18ea96df7b1b34fae065d2fbd624 MD5 | raw file
  1/**
  2 * Copyright (C) 2010 Daniel Manzke <daniel.manzke@googlemail.com>
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *         http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16package de.devsurf.injection.guice.scanner.sonatype.tests.autobind.filter;
 17
 18import static org.junit.Assert.assertNotNull;
 19import static org.junit.Assert.assertTrue;
 20import static org.junit.Assert.fail;
 21
 22import org.junit.Test;
 23
 24import com.google.inject.Binder;
 25import com.google.inject.ConfigurationException;
 26import com.google.inject.Guice;
 27import com.google.inject.Injector;
 28import com.google.inject.multibindings.Multibinder;
 29
 30import de.devsurf.injection.guice.annotations.Bind;
 31import de.devsurf.injection.guice.annotations.features.AutoBindingFeature;
 32import de.devsurf.injection.guice.scanner.ClasspathScanner;
 33import de.devsurf.injection.guice.scanner.PackageFilter;
 34import de.devsurf.injection.guice.scanner.StartupModule;
 35import de.devsurf.injection.guice.scanner.features.ScannerFeature;
 36import de.devsurf.injection.guice.scanner.sonatype.SonatypeScanner;
 37
 38public class PackageFilterTests {
 39	@Test
 40	public void createDynamicModule() {
 41		Injector injector = Guice.createInjector(new TestStartupModule(SonatypeScanner.class,
 42			PackageFilter.create(PackageFilterTests.class)));
 43		assertNotNull(injector);
 44	}
 45
 46	@Test
 47	public void testWithWrongPackage() {
 48		Injector injector = Guice.createInjector(new TestStartupModule(SonatypeScanner.class,
 49			PackageFilter.create("java")));
 50		assertNotNull(injector);
 51
 52		TestInterface testInstance;
 53		try {
 54			testInstance = injector.getInstance(TestInterface.class);
 55			fail("The Scanner scanned the wrong package, so no Implementation should be bound to this Interface. Instance null? "
 56					+ (testInstance == null));
 57		} catch (ConfigurationException e) {
 58			// ok
 59		}
 60	}
 61
 62	@Test
 63	public void createTestInterface() {
 64		Injector injector = Guice.createInjector(new TestStartupModule(SonatypeScanner.class,
 65			PackageFilter.create(PackageFilterTests.class)));
 66		assertNotNull(injector);
 67
 68		TestInterface testInstance = injector.getInstance(TestInterface.class);
 69		assertNotNull(testInstance);
 70		assertTrue(testInstance.sayHello().equals(TestInterfaceImplementation.TEST));
 71		assertTrue(testInstance instanceof TestInterfaceImplementation);
 72		assertTrue(testInstance instanceof SecondTestInterface);
 73	}
 74
 75	@Test
 76	public void createSecondTestInterface() {
 77		Injector injector = Guice.createInjector(new TestStartupModule(SonatypeScanner.class,
 78			PackageFilter.create(PackageFilterTests.class)));
 79		assertNotNull(injector);
 80
 81		SecondTestInterface sameInstance = injector.getInstance(SecondTestInterface.class);
 82		assertNotNull(sameInstance);
 83		assertTrue(sameInstance.fireEvent().equals(TestInterfaceImplementation.EVENT));
 84		assertTrue(sameInstance instanceof TestInterfaceImplementation);
 85		assertTrue(sameInstance instanceof TestInterface);
 86	}
 87
 88	@Test
 89	public void createAllInterfaces() {
 90		Injector injector = Guice.createInjector(new TestStartupModule(SonatypeScanner.class,
 91			PackageFilter.create(PackageFilterTests.class)));
 92		assertNotNull(injector);
 93
 94		TestInterface testInstance = injector.getInstance(TestInterface.class);
 95		assertNotNull(testInstance);
 96		assertTrue(testInstance.sayHello().equals(TestInterfaceImplementation.TEST));
 97		assertTrue(testInstance instanceof TestInterfaceImplementation);
 98		assertTrue(testInstance instanceof SecondTestInterface);
 99
100		SecondTestInterface sameInstance = injector.getInstance(SecondTestInterface.class);
101		assertNotNull(sameInstance);
102		assertTrue(sameInstance.fireEvent().equals(TestInterfaceImplementation.EVENT));
103		assertTrue(sameInstance instanceof TestInterfaceImplementation);
104		assertTrue(testInstance instanceof TestInterface);
105	}
106
107	public static interface TestInterface {
108		String sayHello();
109	}
110
111	public static interface SecondTestInterface {
112		String fireEvent();
113	}
114
115	@Bind
116	public static class TestInterfaceImplementation implements TestInterface, SecondTestInterface {
117		public static final String TEST = "test";
118		public static final String EVENT = "event";
119
120		@Override
121		public String sayHello() {
122			return TEST;
123		}
124
125		@Override
126		public String fireEvent() {
127			return EVENT;
128		}
129	}
130
131	public static class TestStartupModule extends StartupModule {
132		public TestStartupModule(Class<? extends ClasspathScanner> scanner, PackageFilter... packages) {
133			super(scanner, packages);
134		}
135
136		@Override
137		protected Multibinder<ScannerFeature> bindFeatures(Binder binder) {
138			Multibinder<ScannerFeature> listeners = Multibinder.newSetBinder(binder,
139				ScannerFeature.class);
140			listeners.addBinding().to(AutoBindingFeature.class);
141
142			return listeners;
143		}
144	}
145}