KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > configuration > spring > CustomPropertyEditorsTest


1 package org.objectweb.celtix.bus.configuration.spring;
2
3 import java.beans.PropertyEditor JavaDoc;
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5 import java.math.BigInteger JavaDoc;
6 import java.net.URL JavaDoc;
7
8 import javax.xml.bind.JAXBException;
9 import javax.xml.namespace.QName JavaDoc;
10
11 import org.w3c.dom.Element JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import org.easymock.EasyMock;
16 import org.objectweb.celtix.bus.configuration.LeafConfigurationBuilder;
17 import org.objectweb.celtix.bus.configuration.TopConfigurationBuilder;
18 import org.objectweb.celtix.configuration.Configuration;
19 import org.objectweb.celtix.configuration.ConfigurationException;
20 import org.objectweb.celtix.configuration.impl.TypeSchema;
21 import org.objectweb.celtix.configuration.impl.TypeSchemaHelper;
22 import org.springframework.beans.factory.BeanCreationException;
23 import org.springframework.core.io.UrlResource;
24
25 public class CustomPropertyEditorsTest extends TestCase {
26     
27     public void testJaxbPropertyEditorGetAsText() {
28         PropertyEditor JavaDoc pe = new JaxbPropertyEditor();
29         assertNull(pe.getValue());
30         assertNull(pe.getAsText());
31         pe.setValue("abc");
32         assertEquals("abc", pe.getAsText());
33         Element JavaDoc element = EasyMock.createMock(Element JavaDoc.class);
34         element.getTextContent();
35         EasyMock.expectLastCall().andReturn("xyz");
36         EasyMock.replay(element);
37         pe.setValue(element);
38         assertEquals("xyz", pe.getAsText());
39         EasyMock.verify(element);
40     }
41     
42     public void testJaxbPropertyEditorSetAsText() {
43         PropertyEditor JavaDoc pe = new JaxbPropertyEditor();
44         assertNull(pe.getValue());
45         assertNull(pe.getAsText());
46         pe.setAsText("abc");
47         assertEquals("abc", pe.getAsText());
48         pe.setValue(Boolean.TRUE);
49         try {
50             pe.setAsText("false");
51             fail("Expected IllegalArgumentException not thrown.");
52         } catch (IllegalArgumentException JavaDoc ex) {
53             // ignore
54
}
55         pe.setValue("boolean");
56         pe.setAsText("false");
57         assertEquals("false", pe.getAsText());
58     }
59     
60     public void testJaxbPropertyEditorGetValue() throws JAXBException {
61         PropertyEditor JavaDoc pe = new JaxbPropertyEditor();
62         helpTestGetValue(pe, Boolean.TRUE, "address");
63     }
64     
65     public void testJaxbBigIntegerEditorsetAsText() {
66         PropertyEditor JavaDoc pe = new JaxbBigIntegerEditor();
67         pe.setAsText("12345");
68         Object JavaDoc o = pe.getValue();
69         assertTrue(o instanceof BigInteger JavaDoc);
70         assertEquals(12345, ((BigInteger JavaDoc)o).intValue());
71     }
72     
73     public void testJaxbNumberEditorGetValue() throws JAXBException {
74         Object JavaDoc value = null;
75         PropertyEditor JavaDoc pe = null;
76         
77         value = new Byte JavaDoc(Byte.MAX_VALUE);
78         pe = new JaxbNumberEditor(Byte JavaDoc.class);
79         helpTestGetValue(pe, value, "Byte");
80         
81         value = new Short JavaDoc(Short.MAX_VALUE);
82         pe = new JaxbNumberEditor(Short JavaDoc.class);
83         helpTestGetValue(pe, value, "Short");
84         
85         value = new Integer JavaDoc(Integer.MAX_VALUE);
86         pe = new JaxbNumberEditor(Integer JavaDoc.class);
87         helpTestGetValue(pe, value, "Integer");
88         
89         value = new Long JavaDoc(Long.MAX_VALUE);
90         pe = new JaxbNumberEditor(Long JavaDoc.class);
91         helpTestGetValue(pe, value, "Long");
92         
93         value = new Double JavaDoc(Double.MAX_VALUE);
94         pe = new JaxbNumberEditor(Double JavaDoc.class);
95         helpTestGetValue(pe, value, "Double");
96         
97         value = new Float JavaDoc(Float.MAX_VALUE);
98         pe = new JaxbNumberEditor(Float JavaDoc.class);
99         helpTestGetValue(pe, value, "Float");
100     }
101     
102     public void testJaxbBooleanEditor() throws JAXBException {
103         Object JavaDoc value = null;
104         PropertyEditor JavaDoc pe = null;
105         
106         value = Boolean.TRUE;
107         pe = new JaxbBooleanEditor();
108         helpTestGetValue(pe, value, "Boolean");
109     }
110     
111     public void testPropertyEditorConversionFailure() throws InvocationTargetException JavaDoc,
112         NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc {
113         URL JavaDoc url = CustomPropertyEditorsTest.class.getResource("resources/top3.xml");
114         UrlResource urlRes = new UrlResource(url);
115         CeltixXmlBeanFactory bf = new CeltixXmlBeanFactory(urlRes);
116         Configuration top = new TopConfigurationBuilder().build("top3");
117         bf.registerCustomEditors(top);
118         Configuration leaf = new LeafConfigurationBuilder().build(top, "leaf");
119         bf.registerCustomEditors(leaf);
120       
121         // the first form results in a BeanCreationException (caused by a
122
// PropertyAccessExceptionsException)
123

124         try {
125             bf.getBean("top3");
126             fail("Expected BeanCreationException not thrown.");
127         } catch (BeanCreationException ex) {
128             // ignore
129
}
130         
131         // the second form (preferrable because it performs schema validation)
132
// results in a BeanCreationException (caused by a JAXBException)
133

134         try {
135             bf.getBean("top4");
136             fail("Expected BeanCreationException not thrown.");
137         } catch (BeanCreationException ex) {
138             ConfigurationException cause = (ConfigurationException)ex.getCause();
139             assertEquals("JAXB_PROPERTY_EDITOR_EXC", cause.getCode());
140         }
141     }
142     
143     private void helpTestGetValue(PropertyEditor JavaDoc pe, Object JavaDoc value, String JavaDoc typename) throws JAXBException {
144         Element JavaDoc element = EasyMock.createMock(Element JavaDoc.class);
145         String JavaDoc testURI = "http://celtix.objectweb.org/configuration/test/types";
146         element.getNamespaceURI();
147         EasyMock.expectLastCall().andReturn(testURI);
148         element.getLocalName();
149         EasyMock.expectLastCall().andReturn(typename);
150         TypeSchema ts = org.easymock.classextension.EasyMock.createMock(TypeSchema.class);
151         TypeSchemaHelper tsh = new TypeSchemaHelper(true);
152         tsh.put(testURI, ts);
153         ts.unmarshal(new QName JavaDoc(testURI, typename), element);
154         EasyMock.expectLastCall().andReturn(value);
155         EasyMock.replay(element);
156         org.easymock.classextension.EasyMock.replay(ts);
157        
158         pe.setValue(element);
159         Object JavaDoc o = pe.getValue();
160         assertTrue(o == value);
161         
162         EasyMock.reset(element);
163         org.easymock.classextension.EasyMock.reset(ts);
164         
165         element.getNamespaceURI();
166         EasyMock.expectLastCall().andReturn(testURI);
167         element.getLocalName();
168         EasyMock.expectLastCall().andReturn(typename);
169         ts.unmarshal(new QName JavaDoc(testURI, typename), element);
170         EasyMock.expectLastCall().andThrow(new JAXBException("test"));
171         EasyMock.replay(element);
172         org.easymock.classextension.EasyMock.replay(ts);
173         try {
174             pe.getValue();
175             fail("Expected ConfigurationException not thrown.");
176         } catch (ConfigurationException ex) {
177             assertEquals("JAXB_PROPERTY_EDITOR_EXC", ex.getCode());
178         }
179     }
180     
181        
182 }
183
Popular Tags