spring.lookup/src/test/java/org/apidesign/spring/LookupBasedApplicationContextTest.java
We need to use AbstractLookup because of a bug in Lookups.fixed when dealling with lookup by ID
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
30 * If you wish your version of this file to be governed by only the CDDL
31 * or only the GPL Version 2, indicate your decision by adding
32 * "[Contributor] elects to include this software in this distribution
33 * under the [CDDL or GPL Version 2] license." If you do not indicate a
34 * single choice of license, a recipient has the option to distribute
35 * your version of this file under either the CDDL, the GPL Version 2 or
36 * to extend the choice of license to its licensees as provided above.
37 * However, if you add GPL Version 2 code and therefore, elected the GPL
38 * Version 2 license, then the option applies only if the new code is
39 * made subject to such option by the copyright holder.
42 package org.apidesign.spring;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.LinkedList;
47 import java.util.List;
49 import junit.framework.TestCase;
50 import org.openide.util.Lookup;
51 import org.openide.util.Lookup.Template;
52 import org.openide.util.lookup.AbstractLookup;
53 import org.openide.util.lookup.AbstractLookup.Content;
54 import org.openide.util.lookup.AbstractLookup.Pair;
55 import org.openide.util.lookup.InstanceContent;
56 import org.openide.util.lookup.Lookups;
57 import org.springframework.beans.factory.BeanDefinitionStoreException;
58 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.context.ApplicationContext;
61 import org.springframework.context.support.ClassPathXmlApplicationContext;
65 * @author Andrei Badea
67 public class LookupBasedApplicationContextTest extends TestCase {
69 // TODO test others ApplicationContext methods.
71 public LookupBasedApplicationContextTest(String testName) {
75 public void testBeanFactoryMethods() {
76 Content content = new Content();
77 ArrayList array = new ArrayList();
78 LinkedList linked = new LinkedList();
79 content.addPair(new PairImpl<Object>("array", array));
80 content.addPair(new PairImpl<Object>("linked", linked));
81 content.addPair(new PairImpl<Object>(null, new HashMap()));
82 Lookup lookup = new AbstractLookup(content);
83 ApplicationContext ctx = LookupBasedApplicationContext.create("mycontext", lookup, this.getClass().getClassLoader());
84 assertEquals("mycontext", ctx.getId());
86 assertSame(array, ctx.getBean("array"));
88 assertSame(array, ctx.getBean("array", ArrayList.class));
89 // Order should be preserved.
90 assertSame(array, ctx.getBean("array", List.class));
91 assertNull(ctx.getBean("array", LinkedList.class));
93 assertSame(array, ctx.getBean("array", new Object[0]));
95 ctx.getBean("array", new Object[] { null });
97 } catch (BeanDefinitionStoreException e) {}
99 ctx.getBean("foo", new Object[0]);
101 } catch (NoSuchBeanDefinitionException e) {}
103 assertTrue(ctx.containsBean("array"));
104 assertFalse(ctx.containsBean("foo"));
106 assertTrue(ctx.isSingleton("array"));
108 ctx.isSingleton("foo");
109 } catch (NoSuchBeanDefinitionException e) {}
111 assertFalse(ctx.isPrototype("array"));
113 ctx.isPrototype("foo");
114 } catch (NoSuchBeanDefinitionException e) {}
116 assertTrue(ctx.isTypeMatch("array", ArrayList.class));
117 assertTrue(ctx.isTypeMatch("array", List.class));
118 assertFalse(ctx.isTypeMatch("linked", ArrayList.class));
120 ctx.isTypeMatch("foo", Object.class);
121 } catch (NoSuchBeanDefinitionException e) {}
123 assertSame(ArrayList.class, ctx.getType("array"));
125 assertTrue(ctx.containsBeanDefinition("array"));
126 assertFalse(ctx.containsBeanDefinition("foo"));
128 assertEquals(3, ctx.getBeanDefinitionCount());
130 String[] names = ctx.getBeanDefinitionNames();
131 assertEquals(2, names.length);
132 assertEquals("array", names[0]);
133 assertEquals("linked", names[1]);
135 names = ctx.getBeanNamesForType(List.class);
136 assertEquals(2, names.length);
137 assertEquals("array", names[0]);
138 assertEquals("linked", names[1]);
140 Map beans = ctx.getBeansOfType(List.class);
141 assertEquals(2, beans.size());
142 assertSame(array, beans.get("array"));
143 assertSame(linked, beans.get("linked"));
145 assertTrue(ctx.containsLocalBean("array"));
146 assertFalse(ctx.containsLocalBean("foo"));
149 public void testDynamicChanges() {
150 final Content content = new Content();
151 Lookup lookup = new AbstractLookup(content) {
153 protected void beforeLookup(Template<?> template) {
154 if (template.getType().equals(Long.class)) {
157 "number", Long.valueOf(1000L)
164 ApplicationContext ctx = LookupBasedApplicationContext.create("mycontext", lookup, this.getClass().getClassLoader());
165 assertEquals(Long.valueOf(1000L), ctx.getBean("number", Long.class));
168 public void testCooperationWithAutowire() throws Exception {
169 InstanceContent ic = new InstanceContent();
170 Lookup base = new AbstractLookup(ic);
171 ic.add(Long.valueOf(1000L));
172 ApplicationContext baseCtx = SpringAndLookup.create(base, "base");
173 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
174 new String[] {"cooperation-with-autowire.xml"},
175 LookupBasedApplicationContextTest.class,
178 Object obj = context.getBean("calc");
179 assertNotNull("Object found", obj);
180 assertEquals("it is the holder", LongHolder.class, obj.getClass());
181 LongHolder lh = (LongHolder)obj;
182 assertEquals("thousands it its value", 1000L, lh.lng.longValue());
185 private static final class PairImpl<T> extends Pair<T> {
187 private final String id;
188 private final T instance;
190 public PairImpl(String id, T instance) {
192 this.instance = instance;
196 protected boolean instanceOf(Class<?> c) {
197 return c.isInstance(instance);
201 protected boolean creatorOf(Object obj) {
202 return obj == instance;
206 public T getInstance() {
211 @SuppressWarnings("unchecked")
212 public Class<? extends T> getType() {
213 return (Class<? extends T>)instance.getClass();
217 public String getId() {
222 public String getDisplayName() {
227 public static final class LongHolder {
228 public final Long lng;
231 public LongHolder(Long l) {