1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
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
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
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
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
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
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
97 assertTrue(ex.getMessage().indexOf(nonXmlPath) != -1);
98 }
99 }
100
101 public void testBeanFactoryPathFromJndiEnvironmentWithSingleFile() throws Exception {
102 SimpleNamingContextBuilder sncb = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
103
104
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
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
151
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 }