📄 servicelocatorfactorybeantests.java
字号:
}
public void testServiceMappings() {
StaticApplicationContext ctx = new StaticApplicationContext();
ctx.registerPrototype("testService1", TestService.class, new MutablePropertyValues());
ctx.registerPrototype("testService2", ExtendedTestService.class, new MutablePropertyValues());
MutablePropertyValues mpv = new MutablePropertyValues();
mpv.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class);
mpv.addPropertyValue("serviceMappings", "=testService1\n1=testService1\n2=testService2");
ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
ctx.refresh();
TestServiceLocator3 factory = (TestServiceLocator3) ctx.getBean("factory");
TestService testBean1 = factory.getTestService();
TestService testBean2 = factory.getTestService("testService1");
TestService testBean3 = factory.getTestService(1);
TestService testBean4 = factory.getTestService(2);
assertNotSame(testBean1, testBean2);
assertNotSame(testBean1, testBean3);
assertNotSame(testBean1, testBean4);
assertNotSame(testBean2, testBean3);
assertNotSame(testBean2, testBean4);
assertNotSame(testBean3, testBean4);
assertFalse(testBean1 instanceof ExtendedTestService);
assertFalse(testBean2 instanceof ExtendedTestService);
assertFalse(testBean3 instanceof ExtendedTestService);
assertTrue(testBean4 instanceof ExtendedTestService);
}
public void testNoServiceLocatorInterfaceSupplied() throws Exception {
new AssertThrows(IllegalArgumentException.class, "No serviceLocator interface supplied") {
public void test() throws Exception {
new ServiceLocatorFactoryBean().afterPropertiesSet();
}
}.runTest();
}
public void testWhenServiceLocatorInterfaceIsNotAnInterfaceType() throws Exception {
new AssertThrows(IllegalArgumentException.class, "Bad (non-interface-type) serviceLocator interface supplied") {
public void test() throws Exception {
ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean();
factory.setServiceLocatorInterface(getClass());
factory.afterPropertiesSet();
}
}.runTest();
}
public void testWhenServiceLocatorExceptionClassToExceptionTypeWithOnlyNoArgCtor() throws Exception {
new AssertThrows(IllegalArgumentException.class, "Bad (invalid-Exception-type) serviceLocatorException class supplied") {
public void test() throws Exception {
ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean();
factory.setServiceLocatorExceptionClass(ExceptionClassWithOnlyZeroArgCtor.class);
}
}.runTest();
}
public void testWhenServiceLocatorExceptionClassIsNotAnExceptionSubclass() throws Exception {
new AssertThrows(IllegalArgumentException.class, "Bad (non-Exception-type) serviceLocatorException class supplied") {
public void test() throws Exception {
ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean();
factory.setServiceLocatorExceptionClass(getClass());
}
}.runTest();
}
public void testWhenServiceLocatorMethodCalledWithTooManyParameters() throws Exception {
ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean();
factory.setServiceLocatorInterface(ServiceLocatorInterfaceWithExtraNonCompliantMethod.class);
factory.afterPropertiesSet();
final ServiceLocatorInterfaceWithExtraNonCompliantMethod locator = (ServiceLocatorInterfaceWithExtraNonCompliantMethod) factory.getObject();
new AssertThrows(UnsupportedOperationException.class, "Bad method (too many args, doesn't obey class contract)") {
public void test() throws Exception {
locator.getTestService("not", "allowed");
}
}.runTest();
}
public void testRequiresListableBeanFactoryAndChokesOnAnythingElse() throws Exception {
MockControl mockBeanFactory = MockControl.createControl(BeanFactory.class);
final BeanFactory beanFactory = (BeanFactory) mockBeanFactory.getMock();
mockBeanFactory.replay();
new AssertThrows(FatalBeanException.class) {
public void test() throws Exception {
ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean();
factory.setBeanFactory(beanFactory);
}
}.runTest();
mockBeanFactory.verify();
}
public static class TestService {
}
public static class ExtendedTestService extends TestService {
}
public static class TestService2 {
}
public static interface TestServiceLocator {
TestService getTestService();
}
public static interface TestServiceLocator2 {
TestService getTestService(String id) throws CustomServiceLocatorException2;
}
public static interface TestServiceLocator3 {
TestService getTestService();
TestService getTestService(String id);
TestService getTestService(int id);
TestService someFactoryMethod();
}
public static interface TestService2Locator {
TestService2 getTestService() throws CustomServiceLocatorException3;
}
public static interface ServiceLocatorInterfaceWithExtraNonCompliantMethod {
TestService2 getTestService();
TestService2 getTestService(String serviceName, String defaultNotAllowedParameter);
}
public static class CustomServiceLocatorException1 extends NestedRuntimeException {
public CustomServiceLocatorException1(String message, Throwable cause) {
super(message, cause);
}
}
public static class CustomServiceLocatorException2 extends NestedCheckedException {
public CustomServiceLocatorException2(Throwable cause) {
super("", cause);
}
}
public static class CustomServiceLocatorException3 extends NestedCheckedException {
public CustomServiceLocatorException3(String message) {
super(message);
}
}
public static class ExceptionClassWithOnlyZeroArgCtor extends Exception {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -