View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
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   */
16  
17  package org.springframework.context.access;
18  
19  import java.util.Collection;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import junit.framework.TestCase;
24  
25  import org.springframework.beans.BeansException;
26  import org.springframework.beans.factory.BeanFactory;
27  import org.springframework.beans.factory.access.BootstrapException;
28  import org.springframework.context.ApplicationContext;
29  import org.springframework.tests.mock.jndi.SimpleNamingContextBuilder;
30  
31  /**
32   * @author Colin Sampaleanu
33   * @author Chris Beams
34   */
35  public final class ContextJndiBeanFactoryLocatorTests extends TestCase {
36  
37  	private static final String BEAN_FACTORY_PATH_ENVIRONMENT_KEY = "java:comp/env/ejb/BeanFactoryPath";
38  
39  	private static final Class<?> CLASS = ContextJndiBeanFactoryLocatorTests.class;
40  	private static final String CLASSNAME = CLASS.getSimpleName();
41  
42  	private static final String FQ_PATH = "/org/springframework/context/access/";
43  
44  	private static final String COLLECTIONS_CONTEXT = FQ_PATH + CLASSNAME + "-collections.xml";
45  	private static final String PARENT_CONTEXT = FQ_PATH + CLASSNAME + "-parent.xml";
46  
47  
48  	public void testBeanFactoryPathRequiredFromJndiEnvironment() throws Exception {
49  		// Set up initial context but don't bind anything
50  		SimpleNamingContextBuilder.emptyActivatedContextBuilder();
51  
52  		ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
53  		try {
54  			jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY);
55  			fail();
56  		}
57  		catch (BootstrapException ex) {
58  			// Check for helpful JNDI message
59  			assertTrue(ex.getMessage().indexOf(BEAN_FACTORY_PATH_ENVIRONMENT_KEY) != -1);
60  		}
61  	}
62  
63  	public void testBeanFactoryPathFromJndiEnvironmentNotFound() throws Exception  {
64  		SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
65  
66  		String bogusPath = "RUBBISH/com/xxxx/framework/server/test1.xml";
67  
68  		// Set up initial context
69  		sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, bogusPath);
70  
71  		ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
72  		try {
73  			jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY);
74  			fail();
75  		}
76  		catch (BeansException ex) {
77  			// Check for helpful JNDI message
78  			assertTrue(ex.getMessage().indexOf(bogusPath) != -1);
79  		}
80  	}
81  
82  	public void testBeanFactoryPathFromJndiEnvironmentNotValidXml() throws Exception {
83  		SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
84  
85  		String nonXmlPath = "com/xxxx/framework/server/SlsbEndpointBean.class";
86  
87  		// Set up initial context
88  		sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, nonXmlPath);
89  
90  		ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
91  		try {
92  			jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY);
93  			fail();
94  		}
95  		catch (BeansException ex) {
96  			// Check for helpful JNDI message
97  			assertTrue(ex.getMessage().indexOf(nonXmlPath) != -1);
98  		}
99  	}
100 
101 	public void testBeanFactoryPathFromJndiEnvironmentWithSingleFile() throws Exception {
102 		SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
103 
104 		// Set up initial context
105 		sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, COLLECTIONS_CONTEXT);
106 
107 		ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
108 		BeanFactory bf = jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY).getFactory();
109 		assertTrue(bf.containsBean("rod"));
110 		assertTrue(bf instanceof ApplicationContext);
111 	}
112 
113 	public void testBeanFactoryPathFromJndiEnvironmentWithMultipleFiles() throws Exception {
114 		SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
115 
116 		String path = String.format("%s %s", COLLECTIONS_CONTEXT, PARENT_CONTEXT);
117 
118 		// Set up initial context
119 		sncb.bind(BEAN_FACTORY_PATH_ENVIRONMENT_KEY, path);
120 
121 		ContextJndiBeanFactoryLocator jbfl = new ContextJndiBeanFactoryLocator();
122 		BeanFactory bf = jbfl.useBeanFactory(BEAN_FACTORY_PATH_ENVIRONMENT_KEY).getFactory();
123 		assertTrue(bf.containsBean("rod"));
124 		assertTrue(bf.containsBean("inheritedTestBean"));
125 	}
126 
127 }
128 
129 
130 class MapAndSet {
131 
132 	private Object obj;
133 
134 	public MapAndSet(Map<?, ?> map) {
135 		this.obj = map;
136 	}
137 
138 	public MapAndSet(Set<?> set) {
139 		this.obj = set;
140 	}
141 
142 	public Object getObject() {
143 		return obj;
144 	}
145 }
146 
147 
148 
149 /**
150  * Bean that exposes a simple property that can be set
151  * to a mix of references and individual values.
152  */
153 class MixedCollectionBean {
154 
155 	private Collection<?> jumble;
156 
157 
158 	public void setJumble(Collection<?> jumble) {
159 		this.jumble = jumble;
160 	}
161 
162 	public Collection<?> getJumble() {
163 		return jumble;
164 	}
165 
166 }