Drop outdated BeanFactoryLocator / beanRefContext.xml mechanism

Issue: SPR-15154
This commit is contained in:
Juergen Hoeller
2017-01-17 13:58:37 +01:00
parent d96738d613
commit ac6aa53031
30 changed files with 74 additions and 2388 deletions

View File

@@ -1,52 +0,0 @@
/*
* Copyright 2002-2013 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.context.access;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* Unit test for {@link ContextBeanFactoryReference}
*
* @author Colin Sampaleanu
* @author Chris Beams
*/
public class ContextBeanFactoryReferenceTests {
@Test
public void testAllOperations() {
ConfigurableApplicationContext ctx = mock(ConfigurableApplicationContext.class);
ContextBeanFactoryReference bfr = new ContextBeanFactoryReference(ctx);
assertNotNull(bfr.getFactory());
bfr.release();
try {
bfr.getFactory();
}
catch (IllegalStateException e) {
// expected
}
verify(ctx).close();
}
}

View File

@@ -1,173 +0,0 @@
/*
* Copyright 2002-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.context.access;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BootstrapException;
import org.springframework.context.ApplicationContext;
import org.springframework.tests.mock.jndi.SimpleNamingContextBuilder;
import static org.junit.Assert.*;
/**
* @author Colin Sampaleanu
* @author Chris Beams
*/
public class ContextJndiBeanFactoryLocatorTests {
private static final String BEAN_FACTORY_PATH_ENVIRONMENT_KEY = "java:comp/env/ejb/BeanFactoryPath";
private static final Class<?> CLASS = ContextJndiBeanFactoryLocatorTests.class;
private static final String CLASSNAME = CLASS.getSimpleName();
private static final String FQ_PATH = "/org/springframework/context/access/";
private static final String COLLECTIONS_CONTEXT = FQ_PATH + CLASSNAME + "-collections.xml";
private static final String PARENT_CONTEXT = FQ_PATH + CLASSNAME + "-parent.xml";
@Test
public void beanFactoryPathRequiredFromJndiEnvironment() throws Exception {
// Set up initial context but don't bind anything
SimpleNamingContextBuilder.emptyActivatedContextBuilder();
ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
try {
jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY);
fail();
}
catch (BootstrapException ex) {
// Check for helpful JNDI message
assertTrue(ex.getMessage().indexOf(BEAN_FACTORY_PATH_ENVIRONMENT_KEY) != -1);
}
}
@Test
public void beanFactoryPathFromJndiEnvironmentNotFound() throws Exception {
SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
String bogusPath = "RUBBISH/com/xxxx/framework/server/test1.xml";
// Set up initial context
sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, bogusPath);
ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
try {
jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY);
fail();
}
catch (BeansException ex) {
// Check for helpful JNDI message
assertTrue(ex.getMessage().indexOf(bogusPath) != -1);
}
}
@Test
public void beanFactoryPathFromJndiEnvironmentNotValidXml() throws Exception {
SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
String nonXmlPath = "com/xxxx/framework/server/SlsbEndpointBean.class";
// Set up initial context
sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, nonXmlPath);
ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
try {
jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY);
fail();
}
catch (BeansException ex) {
// Check for helpful JNDI message
assertTrue(ex.getMessage().indexOf(nonXmlPath) != -1);
}
}
@Test
public void beanFactoryPathFromJndiEnvironmentWithSingleFile() throws Exception {
SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
// Set up initial context
sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, COLLECTIONS_CONTEXT);
ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
BeanFactory bf = jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY).getFactory();
assertTrue(bf.containsBean("rod"));
assertTrue(bf instanceof ApplicationContext);
}
@Test
public void beanFactoryPathFromJndiEnvironmentWithMultipleFiles() throws Exception {
SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
String path = String.format("%s %s", COLLECTIONS_CONTEXT, PARENT_CONTEXT);
// Set up initial context
sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, path);
ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
BeanFactory bf = jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY).getFactory();
assertTrue(bf.containsBean("rod"));
assertTrue(bf.containsBean("inheritedTestBean"));
}
}
class MapAndSet {
private Object obj;
public MapAndSet(Map<?, ?> map) {
this.obj = map;
}
public MapAndSet(Set<?> set) {
this.obj = set;
}
public Object getObject() {
return obj;
}
}
/**
* Bean that exposes a simple property that can be set
* to a mix of references and individual values.
*/
class MixedCollectionBean {
private Collection<?> jumble;
public void setJumble(Collection<?> jumble) {
this.jumble = jumble;
}
public Collection<?> getJumble() {
return jumble;
}
}

View File

@@ -1,97 +0,0 @@
/*
* Copyright 2002-2012 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.context.access;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.beans.factory.access.SingletonBeanFactoryLocatorTests;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils;
import static org.junit.Assert.*;
/**
* @author Colin Sampaleanu
* @author Juergen Hoeller
* @author Chris Beams
*/
public class ContextSingletonBeanFactoryLocatorTests extends SingletonBeanFactoryLocatorTests {
private static final Class<?> CLASS = ContextSingletonBeanFactoryLocatorTests.class;
private static final String CONTEXT = CLASS.getSimpleName() + "-context.xml";
@Test
public void testBaseBeanFactoryDefs() {
// Just test the base BeanFactory/AppContext defs we are going to work
// with in other tests.
new XmlBeanDefinitionReader(new DefaultListableBeanFactory()).loadBeanDefinitions(new ClassPathResource(
"/org/springframework/beans/factory/access/beans1.xml"));
new XmlBeanDefinitionReader(new DefaultListableBeanFactory()).loadBeanDefinitions(new ClassPathResource(
"/org/springframework/beans/factory/access/beans2.xml"));
}
@Override
@Test
public void testBasicFunctionality() {
ContextSingletonBeanFactoryLocator facLoc = new ContextSingletonBeanFactoryLocator(
"classpath*:" + ClassUtils.addResourcePathToPackagePath(CLASS, CONTEXT));
basicFunctionalityTest(facLoc);
BeanFactoryReference bfr = facLoc.useBeanFactory("a.qualified.name.of.some.sort");
BeanFactory fac = bfr.getFactory();
assertTrue(fac instanceof ApplicationContext);
assertEquals("a.qualified.name.of.some.sort", ((ApplicationContext) fac).getId());
assertTrue(((ApplicationContext) fac).getDisplayName().contains("a.qualified.name.of.some.sort"));
BeanFactoryReference bfr2 = facLoc.useBeanFactory("another.qualified.name");
BeanFactory fac2 = bfr2.getFactory();
assertEquals("another.qualified.name", ((ApplicationContext) fac2).getId());
assertTrue(((ApplicationContext) fac2).getDisplayName().contains("another.qualified.name"));
assertTrue(fac2 instanceof ApplicationContext);
}
/**
* This test can run multiple times, but due to static keyed lookup of the locators,
* 2nd and subsequent calls will actually get back same locator instance. This is not
* really an issue, since the contained bean factories will still be loaded and released.
*/
@Override
@Test
public void testGetInstance() {
// Try with and without 'classpath*:' prefix, and with 'classpath:' prefix.
BeanFactoryLocator facLoc = ContextSingletonBeanFactoryLocator.getInstance(
ClassUtils.addResourcePathToPackagePath(CLASS, CONTEXT));
getInstanceTest1(facLoc);
facLoc = ContextSingletonBeanFactoryLocator.getInstance(
"classpath*:" + ClassUtils.addResourcePathToPackagePath(CLASS, CONTEXT));
getInstanceTest2(facLoc);
// This will actually get another locator instance, as the key is the resource name.
facLoc = ContextSingletonBeanFactoryLocator.getInstance(
"classpath:" + ClassUtils.addResourcePathToPackagePath(CLASS, CONTEXT));
getInstanceTest3(facLoc);
}
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright 2002-2012 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.context.access;
import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.beans.factory.access.BeanFactoryLocator;
/**
* @author Colin Sampaleanu
*/
public class DefaultLocatorFactoryTests {
/*
* Class to test for BeanFactoryLocator getInstance()
*/
@Test
public void getInstance() {
BeanFactoryLocator bf = DefaultLocatorFactory.getInstance();
BeanFactoryLocator bf2 = DefaultLocatorFactory.getInstance();
assertTrue(bf.equals(bf2));
}
/*
* Class to test for BeanFactoryLocator getInstance(String)
*/
@Test
public void getInstanceString() {
BeanFactoryLocator bf = DefaultLocatorFactory.getInstance("my-bean-refs.xml");
BeanFactoryLocator bf2 = DefaultLocatorFactory.getInstance("my-bean-refs.xml");
assertTrue(bf.equals(bf2));
}
}