001 /*****************************************************************************
002 * Copyright (C) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the LICENSE.txt file. *
007 * *
008 * Original code by *
009 *****************************************************************************/
010 package org.picocontainer.behaviors;
011
012 import org.picocontainer.monitors.NullComponentMonitor;
013 import org.picocontainer.lifecycle.NullLifecycleStrategy;
014 import org.picocontainer.Characteristics;
015 import org.picocontainer.ComponentAdapter;
016 import org.picocontainer.annotations.Cache;
017 import org.picocontainer.injectors.SetterInjector;
018 import org.picocontainer.containers.EmptyPicoContainer;
019
020 import java.util.Map;
021 import java.util.HashMap;
022 import java.util.Properties;
023 import java.util.Enumeration;
024
025 import junit.framework.TestCase;
026 import com.thoughtworks.xstream.XStream;
027
028 public class AdaptiveBehaviorFactoryTestCase extends TestCase {
029
030 public void testCachingBehaviorCanBeAddedByCharacteristics() {
031 AdaptiveBehaviorFactory abf = new AdaptiveBehaviorFactory();
032 Properties cc = new Properties();
033 mergeInto(Characteristics.CACHE,cc);
034 ComponentAdapter ca = abf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, HashMap.class);
035 assertTrue(ca instanceof CachingBehavior);
036 Map map = (Map)ca.getComponentInstance(new EmptyPicoContainer());
037 assertNotNull(map);
038 Map map2 = (Map)ca.getComponentInstance(new EmptyPicoContainer());
039 assertSame(map, map2);
040 assertEquals(0, cc.size());
041 }
042
043 public void testCachingBehaviorCanBeAddedByAnnotation() {
044 AdaptiveBehaviorFactory abf = new AdaptiveBehaviorFactory();
045 Properties cc = new Properties();
046 ComponentAdapter ca = abf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, MyHashMap.class);
047 assertTrue(ca instanceof CachingBehavior);
048 Map map = (Map)ca.getComponentInstance(new EmptyPicoContainer());
049 assertNotNull(map);
050 Map map2 = (Map)ca.getComponentInstance(new EmptyPicoContainer());
051 assertSame(map, map2);
052 assertEquals(0, cc.size());
053 }
054
055 @Cache
056 public static class MyHashMap extends HashMap {
057 public MyHashMap() {
058 }
059 }
060
061 public void testImplementationHidingBehaviorCanBeAddedByCharacteristics() {
062 AdaptiveBehaviorFactory abf = new AdaptiveBehaviorFactory();
063 Properties cc = new Properties();
064 mergeInto(Characteristics.HIDE_IMPL,cc);
065 ComponentAdapter ca = abf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, HashMap.class);
066 assertTrue(ca instanceof ImplementationHidingBehavior);
067 Map map = (Map)ca.getComponentInstance(new EmptyPicoContainer());
068 assertNotNull(map);
069 assertTrue(!(map instanceof HashMap));
070
071 assertEquals(0, cc.size());
072
073
074 }
075
076 public void testSetterInjectionCanBeTriggereedMeaningAdaptiveInjectorIsUsed() {
077 AdaptiveBehaviorFactory abf = new AdaptiveBehaviorFactory();
078 Properties cc = new Properties();
079 mergeInto(Characteristics.SDI,cc);
080 ComponentAdapter ca = abf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, HashMap.class);
081 assertTrue(ca instanceof SetterInjector);
082 Map map = (Map)ca.getComponentInstance(new EmptyPicoContainer());
083 assertNotNull(map);
084 assertEquals(0, cc.size());
085
086
087 }
088
089 public void testCachingAndImplHidingAndThreadSafetySetupCorrectly() {
090 AdaptiveBehaviorFactory abf = new AdaptiveBehaviorFactory();
091 Properties cc = new Properties();
092 mergeInto(Characteristics.CACHE,cc);
093 mergeInto(Characteristics.HIDE_IMPL,cc);
094 mergeInto(Characteristics.THREAD_SAFE,cc);
095 ComponentAdapter ca = abf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, HashMap.class);
096 assertTrue(ca instanceof CachingBehavior);
097 Map map = (Map)ca.getComponentInstance(new EmptyPicoContainer());
098 assertNotNull(map);
099 assertTrue(!(map instanceof HashMap));
100
101 XStream xs = new XStream();
102 String foo = xs.toXML(ca);
103
104 int ih = foo.indexOf(ImplementationHidingBehavior.class.getName());
105 int sb = foo.indexOf(SynchronizedBehavior.class.getName());
106
107 // check right nesting order
108 assertTrue(ih>0);
109 assertTrue(sb>0);
110 assertTrue(sb>ih);
111
112 assertEquals(0, cc.size());
113
114
115 }
116
117 public void testCachingAndImplHidingAndThreadSafetySetupCorrectlyForExtraCaching() {
118 CachingBehaviorFactory cbf = new CachingBehaviorFactory();
119 AdaptiveBehaviorFactory abf = new AdaptiveBehaviorFactory();
120 cbf.forThis(abf);
121 Properties cc = new Properties();
122 mergeInto(Characteristics.CACHE,cc);
123 mergeInto(Characteristics.HIDE_IMPL,cc);
124 mergeInto(Characteristics.THREAD_SAFE,cc);
125 ComponentAdapter ca = cbf.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), cc, Map.class, HashMap.class);
126 assertTrue(ca instanceof CachingBehavior);
127 Map map = (Map)ca.getComponentInstance(new EmptyPicoContainer());
128 assertNotNull(map);
129 assertTrue(!(map instanceof HashMap));
130
131 XStream xs = new XStream();
132 String foo = xs.toXML(ca);
133
134 assertTrue(foo.indexOf("<" + CachingBehavior.class.getName() + ">", 0) > -1); // xml does start with CB
135 assertFalse(foo.indexOf("<" + CachingBehavior.class.getName() + ">", 1) > -1); // but only contains it once.
136
137 }
138
139 public void mergeInto(Properties p, Properties into) {
140 Enumeration e = p.propertyNames();
141 while (e.hasMoreElements()) {
142 String s = (String)e.nextElement();
143 into.setProperty(s, p.getProperty(s));
144 }
145
146 }
147
148
149 }