1 package org.objectweb.celtix.bus.configuration.spring; 2 3 import java.beans.PropertyEditor ; 4 import java.lang.reflect.InvocationTargetException ; 5 import java.math.BigInteger ; 6 import java.net.URL ; 7 8 import javax.xml.bind.JAXBException; 9 import javax.xml.namespace.QName ; 10 11 import org.w3c.dom.Element ; 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 pe = new JaxbPropertyEditor(); 29 assertNull(pe.getValue()); 30 assertNull(pe.getAsText()); 31 pe.setValue("abc"); 32 assertEquals("abc", pe.getAsText()); 33 Element element = EasyMock.createMock(Element .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 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 ex) { 53 } 55 pe.setValue("boolean"); 56 pe.setAsText("false"); 57 assertEquals("false", pe.getAsText()); 58 } 59 60 public void testJaxbPropertyEditorGetValue() throws JAXBException { 61 PropertyEditor pe = new JaxbPropertyEditor(); 62 helpTestGetValue(pe, Boolean.TRUE, "address"); 63 } 64 65 public void testJaxbBigIntegerEditorsetAsText() { 66 PropertyEditor pe = new JaxbBigIntegerEditor(); 67 pe.setAsText("12345"); 68 Object o = pe.getValue(); 69 assertTrue(o instanceof BigInteger ); 70 assertEquals(12345, ((BigInteger )o).intValue()); 71 } 72 73 public void testJaxbNumberEditorGetValue() throws JAXBException { 74 Object value = null; 75 PropertyEditor pe = null; 76 77 value = new Byte (Byte.MAX_VALUE); 78 pe = new JaxbNumberEditor(Byte .class); 79 helpTestGetValue(pe, value, "Byte"); 80 81 value = new Short (Short.MAX_VALUE); 82 pe = new JaxbNumberEditor(Short .class); 83 helpTestGetValue(pe, value, "Short"); 84 85 value = new Integer (Integer.MAX_VALUE); 86 pe = new JaxbNumberEditor(Integer .class); 87 helpTestGetValue(pe, value, "Integer"); 88 89 value = new Long (Long.MAX_VALUE); 90 pe = new JaxbNumberEditor(Long .class); 91 helpTestGetValue(pe, value, "Long"); 92 93 value = new Double (Double.MAX_VALUE); 94 pe = new JaxbNumberEditor(Double .class); 95 helpTestGetValue(pe, value, "Double"); 96 97 value = new Float (Float.MAX_VALUE); 98 pe = new JaxbNumberEditor(Float .class); 99 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() throws InvocationTargetException , 112 NoSuchMethodException , IllegalAccessException { 113 URL 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 124 try { 125 bf.getBean("top3"); 126 fail("Expected BeanCreationException not thrown."); 127 } catch (BeanCreationException ex) { 128 } 130 131 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 pe, Object value, String typename) throws JAXBException { 144 Element element = EasyMock.createMock(Element .class); 145 String 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 (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 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 (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 |