001: package org.objectweb.celtix.bus.configuration.spring;
002:
003: import java.beans.PropertyEditor;
004: import java.lang.reflect.InvocationTargetException;
005: import java.math.BigInteger;
006: import java.net.URL;
007:
008: import javax.xml.bind.JAXBException;
009: import javax.xml.namespace.QName;
010:
011: import org.w3c.dom.Element;
012:
013: import junit.framework.TestCase;
014:
015: import org.easymock.EasyMock;
016: import org.objectweb.celtix.bus.configuration.LeafConfigurationBuilder;
017: import org.objectweb.celtix.bus.configuration.TopConfigurationBuilder;
018: import org.objectweb.celtix.configuration.Configuration;
019: import org.objectweb.celtix.configuration.ConfigurationException;
020: import org.objectweb.celtix.configuration.impl.TypeSchema;
021: import org.objectweb.celtix.configuration.impl.TypeSchemaHelper;
022: import org.springframework.beans.factory.BeanCreationException;
023: import org.springframework.core.io.UrlResource;
024:
025: public class CustomPropertyEditorsTest extends TestCase {
026:
027: public void testJaxbPropertyEditorGetAsText() {
028: PropertyEditor pe = new JaxbPropertyEditor();
029: assertNull(pe.getValue());
030: assertNull(pe.getAsText());
031: pe.setValue("abc");
032: assertEquals("abc", pe.getAsText());
033: Element element = EasyMock.createMock(Element.class);
034: element.getTextContent();
035: EasyMock.expectLastCall().andReturn("xyz");
036: EasyMock.replay(element);
037: pe.setValue(element);
038: assertEquals("xyz", pe.getAsText());
039: EasyMock.verify(element);
040: }
041:
042: public void testJaxbPropertyEditorSetAsText() {
043: PropertyEditor pe = new JaxbPropertyEditor();
044: assertNull(pe.getValue());
045: assertNull(pe.getAsText());
046: pe.setAsText("abc");
047: assertEquals("abc", pe.getAsText());
048: pe.setValue(Boolean.TRUE);
049: try {
050: pe.setAsText("false");
051: fail("Expected IllegalArgumentException not thrown.");
052: } catch (IllegalArgumentException ex) {
053: // ignore
054: }
055: pe.setValue("boolean");
056: pe.setAsText("false");
057: assertEquals("false", pe.getAsText());
058: }
059:
060: public void testJaxbPropertyEditorGetValue() throws JAXBException {
061: PropertyEditor pe = new JaxbPropertyEditor();
062: helpTestGetValue(pe, Boolean.TRUE, "address");
063: }
064:
065: public void testJaxbBigIntegerEditorsetAsText() {
066: PropertyEditor pe = new JaxbBigIntegerEditor();
067: pe.setAsText("12345");
068: Object o = pe.getValue();
069: assertTrue(o instanceof BigInteger);
070: assertEquals(12345, ((BigInteger) o).intValue());
071: }
072:
073: public void testJaxbNumberEditorGetValue() throws JAXBException {
074: Object value = null;
075: PropertyEditor pe = null;
076:
077: value = new Byte(Byte.MAX_VALUE);
078: pe = new JaxbNumberEditor(Byte.class);
079: helpTestGetValue(pe, value, "Byte");
080:
081: value = new Short(Short.MAX_VALUE);
082: pe = new JaxbNumberEditor(Short.class);
083: helpTestGetValue(pe, value, "Short");
084:
085: value = new Integer(Integer.MAX_VALUE);
086: pe = new JaxbNumberEditor(Integer.class);
087: helpTestGetValue(pe, value, "Integer");
088:
089: value = new Long(Long.MAX_VALUE);
090: pe = new JaxbNumberEditor(Long.class);
091: helpTestGetValue(pe, value, "Long");
092:
093: value = new Double(Double.MAX_VALUE);
094: pe = new JaxbNumberEditor(Double.class);
095: helpTestGetValue(pe, value, "Double");
096:
097: value = new Float(Float.MAX_VALUE);
098: pe = new JaxbNumberEditor(Float.class);
099: helpTestGetValue(pe, value, "Float");
100: }
101:
102: public void testJaxbBooleanEditor() throws JAXBException {
103: Object value = null;
104: PropertyEditor pe = null;
105:
106: value = Boolean.TRUE;
107: pe = new JaxbBooleanEditor();
108: helpTestGetValue(pe, value, "Boolean");
109: }
110:
111: public void testPropertyEditorConversionFailure()
112: throws InvocationTargetException, NoSuchMethodException,
113: IllegalAccessException {
114: URL url = CustomPropertyEditorsTest.class
115: .getResource("resources/top3.xml");
116: UrlResource urlRes = new UrlResource(url);
117: CeltixXmlBeanFactory bf = new CeltixXmlBeanFactory(urlRes);
118: Configuration top = new TopConfigurationBuilder().build("top3");
119: bf.registerCustomEditors(top);
120: Configuration leaf = new LeafConfigurationBuilder().build(top,
121: "leaf");
122: bf.registerCustomEditors(leaf);
123:
124: // the first form results in a BeanCreationException (caused by a
125: // PropertyAccessExceptionsException)
126:
127: try {
128: bf.getBean("top3");
129: fail("Expected BeanCreationException not thrown.");
130: } catch (BeanCreationException ex) {
131: // ignore
132: }
133:
134: // the second form (preferrable because it performs schema validation)
135: // results in a BeanCreationException (caused by a JAXBException)
136:
137: try {
138: bf.getBean("top4");
139: fail("Expected BeanCreationException not thrown.");
140: } catch (BeanCreationException ex) {
141: ConfigurationException cause = (ConfigurationException) ex
142: .getCause();
143: assertEquals("JAXB_PROPERTY_EDITOR_EXC", cause.getCode());
144: }
145: }
146:
147: private void helpTestGetValue(PropertyEditor pe, Object value,
148: String typename) throws JAXBException {
149: Element element = EasyMock.createMock(Element.class);
150: String testURI = "http://celtix.objectweb.org/configuration/test/types";
151: element.getNamespaceURI();
152: EasyMock.expectLastCall().andReturn(testURI);
153: element.getLocalName();
154: EasyMock.expectLastCall().andReturn(typename);
155: TypeSchema ts = org.easymock.classextension.EasyMock
156: .createMock(TypeSchema.class);
157: TypeSchemaHelper tsh = new TypeSchemaHelper(true);
158: tsh.put(testURI, ts);
159: ts.unmarshal(new QName(testURI, typename), element);
160: EasyMock.expectLastCall().andReturn(value);
161: EasyMock.replay(element);
162: org.easymock.classextension.EasyMock.replay(ts);
163:
164: pe.setValue(element);
165: Object o = pe.getValue();
166: assertTrue(o == value);
167:
168: EasyMock.reset(element);
169: org.easymock.classextension.EasyMock.reset(ts);
170:
171: element.getNamespaceURI();
172: EasyMock.expectLastCall().andReturn(testURI);
173: element.getLocalName();
174: EasyMock.expectLastCall().andReturn(typename);
175: ts.unmarshal(new QName(testURI, typename), element);
176: EasyMock.expectLastCall().andThrow(new JAXBException("test"));
177: EasyMock.replay(element);
178: org.easymock.classextension.EasyMock.replay(ts);
179: try {
180: pe.getValue();
181: fail("Expected ConfigurationException not thrown.");
182: } catch (ConfigurationException ex) {
183: assertEquals("JAXB_PROPERTY_EDITOR_EXC", ex.getCode());
184: }
185: }
186:
187: }
|