1 10 package org.picocontainer.defaults; 11 12 import org.picocontainer.ComponentAdapter; 13 import org.picocontainer.PicoInitializationException; 14 import org.picocontainer.tck.AbstractComponentAdapterFactoryTestCase; 15 import org.picocontainer.testmodel.SimpleTouchable; 16 import org.picocontainer.testmodel.Touchable; 17 18 import java.io.File ; 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 import java.text.SimpleDateFormat ; 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import javax.swing.*; 26 27 32 public class BeanPropertyComponentAdapterFactoryTestCase extends AbstractComponentAdapterFactoryTestCase { 33 34 public static class Foo { 35 public String message; 36 37 public void setMessage(String message) { 38 this.message = message; 39 } 40 } 41 42 public static class Failing { 43 public void setMessage(String message) { 44 throw new ArrayIndexOutOfBoundsException (); 45 } 46 } 47 48 54 public static class Primitives { 55 public byte byte_; 56 public short short_; 57 public int int_; 58 public long long_; 59 public float float_; 60 public double double_; 61 public boolean boolean_; 62 public char char_; 63 public File file_; 64 public URL url_; 65 public Class class_; 66 public String string_; 67 68 public void setClass_(Class class_) { 69 this.class_ = class_; 70 } 71 72 public void setString_(String string_) { 73 this.string_ = string_; 74 } 75 76 public void setBoolean_(boolean boolean_) { 77 this.boolean_ = boolean_; 78 } 79 80 public void setByte_(byte byte_) { 81 this.byte_ = byte_; 82 } 83 84 public void setChar_(char char_) { 85 this.char_ = char_; 86 } 87 88 public void setDouble_(double double_) { 89 this.double_ = double_; 90 } 91 92 public void setFloat_(float float_) { 93 this.float_ = float_; 94 } 95 96 public void setInt_(int int_) { 97 this.int_ = int_; 98 } 99 100 public void setLong_(long long_) { 101 this.long_ = long_; 102 } 103 104 public void setShort_(short short_) { 105 this.short_ = short_; 106 } 107 108 public void setFile_(File file_) { 109 this.file_ = file_; 110 } 111 112 public void setUrl_(URL url_) { 113 this.url_ = url_; 114 } 115 } 116 117 public static class A { 118 private B b; 119 120 public void setB(B b) { 121 this.b = b; 122 } 123 } 124 125 public static class B { 126 } 127 128 public void testSetProperties() { 129 ComponentAdapter adapter = createAdapterCallingSetMessage(Foo.class); 130 Foo foo = (Foo) adapter.getComponentInstance(null); 131 assertNotNull(foo); 132 assertEquals("hello", foo.message); 133 } 134 135 public void testFailingSetter() { 136 ComponentAdapter adapter = createAdapterCallingSetMessage(Failing.class); 137 try { 138 adapter.getComponentInstance(null); 139 fail(); 140 } catch (PicoInitializationException e) { 141 } 142 } 143 144 protected ComponentAdapterFactory createComponentAdapterFactory() { 145 return new BeanPropertyComponentAdapterFactory(new DefaultComponentAdapterFactory()); 146 } 147 148 public void testPropertiesSetAfterAdapterCreationShouldBeTakenIntoAccount() { 149 BeanPropertyComponentAdapterFactory factory = (BeanPropertyComponentAdapterFactory) createComponentAdapterFactory(); 150 151 BeanPropertyComponentAdapter adapter = (BeanPropertyComponentAdapter) factory.createComponentAdapter("foo", Foo.class, null); 152 153 Map properties = new HashMap (); 154 properties.put("message", "hello"); 155 adapter.setProperties(properties); 156 157 Foo foo = (Foo) adapter.getComponentInstance(null); 158 159 assertEquals("hello", foo.message); 160 } 161 162 163 public void testDelegateIsAccessible() { 164 DecoratingComponentAdapter componentAdapter = 165 (DecoratingComponentAdapter) createComponentAdapterFactory().createComponentAdapter(Touchable.class, SimpleTouchable.class, null); 166 167 assertNotNull(componentAdapter.getDelegate()); 168 } 169 170 private ComponentAdapter createAdapterCallingSetMessage(Class impl) { 171 BeanPropertyComponentAdapterFactory factory = (BeanPropertyComponentAdapterFactory) createComponentAdapterFactory(); 172 173 Map properties = new HashMap (); 174 properties.put("message", "hello"); 175 176 BeanPropertyComponentAdapter adapter = (BeanPropertyComponentAdapter) factory.createComponentAdapter(impl, impl, null); 177 adapter.setProperties(properties); 178 return adapter; 179 } 180 181 public void testAllJavaPrimitiveAttributesShouldBeSetByTheAdapter() throws MalformedURLException { 182 BeanPropertyComponentAdapterFactory factory = (BeanPropertyComponentAdapterFactory) createComponentAdapterFactory(); 183 Map properties = new HashMap (); 184 properties.put("byte_", "1"); 185 properties.put("short_", "2"); 186 properties.put("int_", "3"); 187 properties.put("long_", "4"); 188 properties.put("float_", "5.0"); 189 properties.put("double_", "6.0"); 190 properties.put("char_", "a"); 191 properties.put("boolean_", "true"); 192 properties.put("file_", "/foo/bar"); 193 properties.put("url_", "http://www.picocontainer.org/"); 194 properties.put("string_", "g string"); 195 properties.put("class_", "javax.swing.JLabel"); 196 BeanPropertyComponentAdapter adapter = (BeanPropertyComponentAdapter) factory.createComponentAdapter(Primitives.class, Primitives.class, null); 197 adapter.setProperties(properties); 198 Primitives primitives = (Primitives) adapter.getComponentInstance(null); 199 200 assertNotNull(primitives); 201 assertEquals(1, primitives.byte_); 202 assertEquals(2, primitives.short_); 203 assertEquals(3, primitives.int_); 204 assertEquals(4, primitives.long_); 205 assertEquals(5.0, primitives.float_, 0.1); 206 assertEquals(6.0, primitives.double_, 0.1); 207 assertEquals('a', primitives.char_); 208 assertEquals(true, primitives.boolean_); 209 assertEquals(new File ("/foo/bar"), primitives.file_); 210 assertEquals(new URL ("http://www.picocontainer.org/"), primitives.url_); 211 assertEquals("g string", primitives.string_); 212 assertEquals(JLabel.class, primitives.class_); 213 } 214 215 public void testSetDependenComponentWillBeSetByTheAdapter() { 216 picoContainer.registerComponentImplementation("b", B.class); 217 BeanPropertyComponentAdapterFactory factory = (BeanPropertyComponentAdapterFactory) createComponentAdapterFactory(); 218 Map properties = new HashMap (); 219 220 properties.put("b", "b"); 222 BeanPropertyComponentAdapter adapter = (BeanPropertyComponentAdapter) factory.createComponentAdapter(A.class, A.class, null); 223 adapter.setProperties(properties); 224 picoContainer.registerComponent(adapter); 225 A a = (A) picoContainer.getComponentInstance(A.class); 226 227 assertNotNull(a); 228 assertNotNull(a.b); 229 } 230 231 public void testSetBeanPropertiesWithValueObjects() { 232 BeanPropertyComponentAdapterFactory factory = (BeanPropertyComponentAdapterFactory) createComponentAdapterFactory(); 233 234 Map properties = new HashMap (); 235 properties.put("lenient", Boolean.FALSE); 236 properties.put("2DigitYearStart", new Date (0)); 237 238 BeanPropertyComponentAdapter adapter = (BeanPropertyComponentAdapter)factory.createComponentAdapter(SimpleDateFormat .class,SimpleDateFormat .class,null); 239 adapter.setProperties(properties); 240 picoContainer.registerComponent(adapter); 241 242 243 SimpleDateFormat dateFormat = (SimpleDateFormat )picoContainer.getComponentInstance(SimpleDateFormat .class); 244 assertNotNull(dateFormat); 245 assertEquals(false, dateFormat.isLenient()); 246 assertEquals(new Date (0), dateFormat.get2DigitYearStart()); 247 } 248 249 250 253 public void testSetBeanPropertiesWithWrongNumberOfParametersThrowsPicoInitializationException() { 254 Object testBean = new Object () { 255 public void setMultiValues(String val1, String Val2) { 256 throw new IllegalStateException ("Setter should never have been called"); 257 } 258 259 public void setSomeString(String val1) { 260 throw new IllegalStateException ("Setter should never have been called"); 261 } 262 }; 263 264
|