KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > BeanPropertyComponentAdapterFactoryTestCase


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by *
9  *****************************************************************************/

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 JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import javax.swing.*;
26
27 /**
28  * @author Aslak Hellesøy
29  * @author Mirko Novakovic
30  * @version $Revision: 2316 $
31  */

32 public class BeanPropertyComponentAdapterFactoryTestCase extends AbstractComponentAdapterFactoryTestCase {
33
34     public static class Foo {
35         public String JavaDoc message;
36
37         public void setMessage(String JavaDoc message) {
38             this.message = message;
39         }
40     }
41
42     public static class Failing {
43         public void setMessage(String JavaDoc message) {
44             throw new ArrayIndexOutOfBoundsException JavaDoc();
45         }
46     }
47
48     /**
49      * Class that contains all types of Java primitives, to test if they are
50      * set correctly.
51      *
52      * @author Mirko Novakovic
53      */

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 JavaDoc file_;
64         public URL JavaDoc url_;
65         public Class JavaDoc class_;
66         public String JavaDoc string_;
67
68         public void setClass_(Class JavaDoc class_) {
69             this.class_ = class_;
70         }
71
72         public void setString_(String JavaDoc 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 JavaDoc file_) {
109             this.file_ = file_;
110         }
111
112         public void setUrl_(URL JavaDoc 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 JavaDoc properties = new HashMap JavaDoc();
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 JavaDoc impl) {
171         BeanPropertyComponentAdapterFactory factory = (BeanPropertyComponentAdapterFactory) createComponentAdapterFactory();
172
173         Map JavaDoc properties = new HashMap JavaDoc();
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 JavaDoc {
182         BeanPropertyComponentAdapterFactory factory = (BeanPropertyComponentAdapterFactory) createComponentAdapterFactory();
183         Map JavaDoc properties = new HashMap JavaDoc();
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 JavaDoc("/foo/bar"), primitives.file_);
210         assertEquals(new URL JavaDoc("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 JavaDoc properties = new HashMap JavaDoc();
219
220         // the second b is the key of the B implementation
221
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 JavaDoc properties = new HashMap JavaDoc();
235       properties.put("lenient", Boolean.FALSE);
236       properties.put("2DigitYearStart", new Date JavaDoc(0));
237
238       BeanPropertyComponentAdapter adapter = (BeanPropertyComponentAdapter)factory.createComponentAdapter(SimpleDateFormat JavaDoc.class,SimpleDateFormat JavaDoc.class,null);
239       adapter.setProperties(properties);
240       picoContainer.registerComponent(adapter);
241
242
243       SimpleDateFormat JavaDoc dateFormat = (SimpleDateFormat JavaDoc)picoContainer.getComponentInstance(SimpleDateFormat JavaDoc.class);
244       assertNotNull(dateFormat);
245       assertEquals(false, dateFormat.isLenient());
246       assertEquals(new Date JavaDoc(0), dateFormat.get2DigitYearStart());
247     }
248
249
250     /**
251      * todo Is this test duplicated elsewhere? --MR
252      */

253     public void testSetBeanPropertiesWithWrongNumberOfParametersThrowsPicoInitializationException() {
254         Object JavaDoc testBean = new Object JavaDoc() {
255             public void setMultiValues(String JavaDoc val1, String JavaDoc Val2) {
256                 throw new IllegalStateException JavaDoc("Setter should never have been called");
257             }
258
259             public void setSomeString(String JavaDoc val1) {
260                 throw new IllegalStateException JavaDoc("Setter should never have been called");
261             }
262         };
263
264