001: package org.objectweb.celtix.bus.configuration.spring;
002:
003: import java.beans.PropertyEditor;
004: import java.io.IOException;
005: import java.lang.reflect.Method;
006: import java.math.BigInteger;
007: import java.net.URL;
008: import java.util.Map;
009:
010: import org.xml.sax.EntityResolver;
011: import org.xml.sax.SAXException;
012:
013: import junit.framework.TestCase;
014:
015: import org.easymock.classextension.EasyMock;
016: import org.objectweb.celtix.bus.configuration.TopConfigurationBuilder;
017: import org.objectweb.celtix.configuration.Configuration;
018: import org.objectweb.celtix.configuration.ConfigurationException;
019: import org.objectweb.celtix.configuration.types.StringListType;
020: import org.springframework.core.io.UrlResource;
021:
022: public class CeltixXmlBeanFactoryTest extends TestCase {
023:
024: public void testEntityResolver() throws SAXException, IOException,
025: NoSuchMethodException {
026: EntityResolver resolver = new CeltixBeansDtdResolver();
027: assertNull(resolver.resolveEntity("-//SPRING//DTD BEAN//EN",
028: "http://www.springframework.org/dtd/spring-beans.dtd"));
029: assertNotNull(resolver
030: .resolveEntity(null,
031: "http://celtix.objectweb.org/configuration/spring/celtix-spring-beans.dtd"));
032:
033: CeltixBeansDtdResolver cer = EasyMock
034: .createMock(CeltixBeansDtdResolver.class,
035: new Method[] { CeltixBeansDtdResolver.class
036: .getDeclaredMethod("getDtdFile",
037: (Class[]) null) });
038:
039: cer.getDtdFile();
040: org.easymock.EasyMock.expectLastCall().andReturn(
041: "celtixx-spring-beans.dtd");
042: EasyMock.replay(cer);
043: try {
044: cer
045: .resolveEntity(null,
046: "http://celtix.objectweb.org/configuration/spring/celtix-spring-beans.dtd");
047: fail("Expected ConfigurationException not thrown.");
048: } catch (ConfigurationException ex) {
049: assertEquals("COULD_NOT_RESOLVE_BEANS_DTD_EXC", ex
050: .getCode());
051: }
052: }
053:
054: public void testConstructor() {
055:
056: URL url = CeltixXmlBeanFactoryTest.class
057: .getResource("resources/top2.xml");
058: UrlResource urlRes = new UrlResource(url);
059: CeltixXmlBeanFactory bf = new CeltixXmlBeanFactory(urlRes);
060:
061: assertNotNull(bf);
062:
063: }
064:
065: public void testCustomEditorRegistration() {
066:
067: URL url = CeltixXmlBeanFactoryTest.class
068: .getResource("resources/top2.xml");
069: UrlResource urlRes = new UrlResource(url);
070: CeltixXmlBeanFactory bf = new CeltixXmlBeanFactory(urlRes);
071: Configuration top = new TopConfigurationBuilder()
072: .build("top22");
073:
074: Map map = bf.getCustomEditors();
075:
076: // no editor for complex types registered yet
077:
078: assertNull(map.get(StringListType.class));
079:
080: // all editors for primitive types registered
081:
082: PropertyEditor pe = null;
083: PropertyEditor pe2 = null;
084:
085: pe = (PropertyEditor) map.get(String.class);
086: assertTrue(pe instanceof JaxbPropertyEditor);
087:
088: pe = (PropertyEditor) map.get(BigInteger.class);
089: assertTrue(pe instanceof JaxbBigIntegerEditor);
090:
091: pe = (PropertyEditor) map.get(Boolean.class);
092: assertTrue(pe instanceof JaxbBooleanEditor);
093: pe2 = (PropertyEditor) map.get(boolean.class);
094: assertTrue(pe == pe2);
095:
096: pe = (PropertyEditor) map.get(Byte.class);
097: assertTrue(pe instanceof JaxbNumberEditor);
098: pe2 = (PropertyEditor) map.get(byte.class);
099: assertTrue(pe == pe2);
100:
101: /*
102: * string
103: * big integer
104: * boolean (2 x)
105: * byte (2 x)
106: * short (2 x)
107: * int (2 x)
108: * long (2 x)
109: * float (2 x)
110: * double (2 x)
111: *
112: */
113:
114: assertEquals(16, map.size());
115:
116: bf.registerCustomEditors(top);
117:
118: assertEquals(17, map.size());
119:
120: pe = (PropertyEditor) map.get(StringListType.class);
121: assertNotNull(pe);
122: assertTrue(pe instanceof JaxbPropertyEditor);
123: assertTrue(pe == map.get(String.class));
124: }
125: }
|