Added an option to deduplicate bindings in favor of Spring
User can set Environment property spring.guice.dedup=true.
This commit is contained in:
committed by
Dave Syer
parent
e94c94837f
commit
a098f7854b
36
src/test/java/org/springframework/guice/AdhocTestSuite.java
Normal file
36
src/test/java/org/springframework/guice/AdhocTestSuite.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.guice;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
import org.springframework.guice.annotation.EnableGuiceModulesTests;
|
||||
|
||||
/**
|
||||
* A test suite for probing weird ordering problems in the tests.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({ BindingDeduplicationTests.class, EnableGuiceModulesTests.class })
|
||||
@Ignore
|
||||
public class AdhocTestSuite {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.springframework.guice;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.CreationException;
|
||||
import com.google.inject.Module;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.guice.BindingDeduplicationTests.SomeDependency;
|
||||
import org.springframework.guice.annotation.EnableGuiceModules;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class BindingDeduplicationTests {
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
System.clearProperty("spring.guice.dedup");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyNoDuplicateBindingErrorWhenDedupeEnabled() {
|
||||
System.setProperty("spring.guice.dedup", "true");
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
BindingDeduplicationTestsConfig.class);
|
||||
SomeDependency someDependency = context.getBean(SomeDependency.class);
|
||||
assertNotNull(someDependency);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test(expected = CreationException.class)
|
||||
public void verifyDuplicateBindingErrorWhenDedupeNotEnabled() {
|
||||
System.setProperty("spring.guice.dedup", "false");
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
BindingDeduplicationTestsConfig.class);
|
||||
context.close();
|
||||
}
|
||||
|
||||
public static class SomeDependency {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableGuiceModules
|
||||
@Configuration
|
||||
class BindingDeduplicationTestsConfig {
|
||||
|
||||
@Bean
|
||||
public SomeDependency stringBean() {
|
||||
return new SomeDependency();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Module module() {
|
||||
return new AbstractModule() {
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(SomeDependency.class).asEagerSingleton();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,9 @@
|
||||
package org.springframework.guice;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.guice.ElementVisitorTests.DuplicateBean;
|
||||
import org.springframework.guice.ElementVisitorTests.ElementVisitorTestGuiceBean;
|
||||
import org.springframework.guice.ElementVisitorTests.ElementVisitorTestSpringBean;
|
||||
import org.springframework.guice.annotation.EnableGuiceModules;
|
||||
import org.springframework.guice.annotation.InjectorFactory;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
@@ -26,48 +12,68 @@ import com.google.inject.Stage;
|
||||
import com.google.inject.spi.Element;
|
||||
import com.google.inject.spi.Elements;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.guice.ElementVisitorTests.DuplicateBean;
|
||||
import org.springframework.guice.ElementVisitorTests.ElementVisitorTestGuiceBean;
|
||||
import org.springframework.guice.ElementVisitorTests.ElementVisitorTestSpringBean;
|
||||
import org.springframework.guice.annotation.EnableGuiceModules;
|
||||
import org.springframework.guice.annotation.InjectorFactory;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ElementVisitorTests {
|
||||
|
||||
private static AnnotationConfigApplicationContext context;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
System.setProperty("spring.guice.dedup", "true");
|
||||
context = new AnnotationConfigApplicationContext(ElementVisitorTestConfig.class);
|
||||
}
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
if(context != null) {
|
||||
System.clearProperty("spring.guice.dedup");
|
||||
if (context != null) {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifySpringModuleDoesNotBreakWhenUsingElementVisitors() {
|
||||
ElementVisitorTestSpringBean testSpringBean = context.getBean(ElementVisitorTestSpringBean.class);
|
||||
ElementVisitorTestSpringBean testSpringBean = context
|
||||
.getBean(ElementVisitorTestSpringBean.class);
|
||||
assertEquals("spring created", testSpringBean.toString());
|
||||
ElementVisitorTestGuiceBean testGuiceBean = context.getBean(ElementVisitorTestGuiceBean.class);
|
||||
ElementVisitorTestGuiceBean testGuiceBean = context
|
||||
.getBean(ElementVisitorTestGuiceBean.class);
|
||||
assertEquals("spring created", testGuiceBean.toString());
|
||||
}
|
||||
|
||||
|
||||
public static class ElementVisitorTestSpringBean {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "default";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ElementVisitorTestGuiceBean {
|
||||
@Inject
|
||||
ElementVisitorTestSpringBean springBean;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return springBean.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static class DuplicateBean {}
|
||||
public static class DuplicateBean {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableGuiceModules
|
||||
@@ -76,14 +82,14 @@ class ElementVisitorTestConfig {
|
||||
|
||||
@Bean
|
||||
public ElementVisitorTestSpringBean testBean() {
|
||||
return new ElementVisitorTestSpringBean(){
|
||||
return new ElementVisitorTestSpringBean() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "spring created";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public Module module() {
|
||||
return new AbstractModule() {
|
||||
@@ -94,23 +100,24 @@ class ElementVisitorTestConfig {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public InjectorFactory injectorFactory() {
|
||||
return new InjectorFactory() {
|
||||
return new InjectorFactory() {
|
||||
@Override
|
||||
public Injector createInjector(List<Module> modules) {
|
||||
List<Element> elements = Elements.getElements(Stage.TOOL, modules);
|
||||
return Guice.createInjector(Stage.PRODUCTION,Elements.getModule(elements));
|
||||
return Guice.createInjector(Stage.PRODUCTION,
|
||||
Elements.getModule(elements));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public DuplicateBean dupeBean1() {
|
||||
return new DuplicateBean();
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public DuplicateBean dupeBean2() {
|
||||
return new DuplicateBean();
|
||||
|
||||
Reference in New Issue
Block a user