spring.lookup/src/test/java/org/apidesign/spring/LookupBasedApplicationContextTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 03 Feb 2010 00:26:11 +0100
changeset 973 5653a70ebb56
parent 578 94aba25fa5a2
permissions -rw-r--r--
We need to use AbstractLookup because of a bug in Lookups.fixed when dealling with lookup by ID
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
     5  *
     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]"
    23  *
    24  * Contributor(s):
    25  *
    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.
    29  *
    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.
    40  */
    41 
    42 package org.apidesign.spring;
    43 
    44 import java.util.ArrayList;
    45 import java.util.HashMap;
    46 import java.util.LinkedList;
    47 import java.util.List;
    48 import java.util.Map;
    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;
    62 
    63 /**
    64  *
    65  * @author Andrei Badea
    66  */
    67 public class LookupBasedApplicationContextTest extends TestCase {
    68 
    69     // TODO test others ApplicationContext methods.
    70 
    71     public LookupBasedApplicationContextTest(String testName) {
    72         super(testName);
    73     }
    74 
    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());
    85 
    86         assertSame(array, ctx.getBean("array"));
    87 
    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));
    92 
    93         assertSame(array, ctx.getBean("array", new Object[0]));
    94         try {
    95             ctx.getBean("array", new Object[] { null });
    96             fail();
    97         } catch (BeanDefinitionStoreException e) {}
    98         try {
    99             ctx.getBean("foo", new Object[0]);
   100             fail();
   101         } catch (NoSuchBeanDefinitionException e) {}
   102 
   103         assertTrue(ctx.containsBean("array"));
   104         assertFalse(ctx.containsBean("foo"));
   105 
   106         assertTrue(ctx.isSingleton("array"));
   107         try {
   108             ctx.isSingleton("foo");
   109         } catch (NoSuchBeanDefinitionException e) {}
   110 
   111         assertFalse(ctx.isPrototype("array"));
   112         try {
   113             ctx.isPrototype("foo");
   114         } catch (NoSuchBeanDefinitionException e) {}
   115 
   116         assertTrue(ctx.isTypeMatch("array", ArrayList.class));
   117         assertTrue(ctx.isTypeMatch("array", List.class));
   118         assertFalse(ctx.isTypeMatch("linked", ArrayList.class));
   119         try {
   120             ctx.isTypeMatch("foo", Object.class);
   121         } catch (NoSuchBeanDefinitionException e) {}
   122 
   123         assertSame(ArrayList.class, ctx.getType("array"));
   124 
   125         assertTrue(ctx.containsBeanDefinition("array"));
   126         assertFalse(ctx.containsBeanDefinition("foo"));
   127 
   128         assertEquals(3, ctx.getBeanDefinitionCount());
   129 
   130         String[] names = ctx.getBeanDefinitionNames();
   131         assertEquals(2, names.length);
   132         assertEquals("array", names[0]);
   133         assertEquals("linked", names[1]);
   134 
   135         names = ctx.getBeanNamesForType(List.class);
   136         assertEquals(2, names.length);
   137         assertEquals("array", names[0]);
   138         assertEquals("linked", names[1]);
   139 
   140         Map beans = ctx.getBeansOfType(List.class);
   141         assertEquals(2, beans.size());
   142         assertSame(array, beans.get("array"));
   143         assertSame(linked, beans.get("linked"));
   144 
   145         assertTrue(ctx.containsLocalBean("array"));
   146         assertFalse(ctx.containsLocalBean("foo"));
   147     }
   148 
   149     public void testDynamicChanges() {
   150         final Content content = new Content();
   151         Lookup lookup = new AbstractLookup(content) {
   152             @Override
   153             protected void beforeLookup(Template<?> template) {
   154                 if (template.getType().equals(Long.class)) {
   155                     content.addPair(
   156                         new PairImpl<Long>(
   157                             "number", Long.valueOf(1000L)
   158                         )
   159                     );
   160                 }
   161             }
   162         };
   163 
   164         ApplicationContext ctx = LookupBasedApplicationContext.create("mycontext", lookup, this.getClass().getClassLoader());
   165         assertEquals(Long.valueOf(1000L), ctx.getBean("number", Long.class));
   166     }
   167 
   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,
   176             baseCtx
   177         );
   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());
   183     }
   184 
   185     private static final class PairImpl<T> extends Pair<T> {
   186 
   187         private final String id;
   188         private final T instance;
   189 
   190         public PairImpl(String id, T instance) {
   191             this.id = id;
   192             this.instance = instance;
   193         }
   194 
   195         @Override
   196         protected boolean instanceOf(Class<?> c) {
   197             return c.isInstance(instance);
   198         }
   199 
   200         @Override
   201         protected boolean creatorOf(Object obj) {
   202             return obj == instance;
   203         }
   204 
   205         @Override
   206         public T getInstance() {
   207             return instance;
   208         }
   209 
   210         @Override
   211         @SuppressWarnings("unchecked")
   212         public Class<? extends T> getType() {
   213             return (Class<? extends T>)instance.getClass();
   214         }
   215 
   216         @Override
   217         public String getId() {
   218             return id;
   219         }
   220 
   221         @Override
   222         public String getDisplayName() {
   223             return getId();
   224         }
   225     }
   226 
   227     public static final class LongHolder {
   228         public final Long lng;
   229 
   230         @Autowired
   231         public LongHolder(Long l) {
   232             this.lng = l;
   233         }
   234     }
   235 }