1 package org.objectweb.celtix.configuration.impl; 2 3 import java.net.URL ; 4 import java.util.Collection ; 5 6 import javax.xml.XMLConstants ; 7 import javax.xml.namespace.QName ; 8 9 import org.xml.sax.SAXParseException ; 10 11 import junit.framework.TestCase; 12 13 import org.objectweb.celtix.bus.configuration.AbstractConfigurationImplTest; 14 import org.objectweb.celtix.bus.configuration.spring.ConfigurationProviderImpl; 15 import org.objectweb.celtix.configuration.ConfigurationException; 16 import org.objectweb.celtix.configuration.impl.TypeSchema.TypeSchemaErrorHandler; 17 18 public class TypeSchemaTest extends TestCase { 19 20 private final TypeSchemaHelper tsh = new TypeSchemaHelper(true); 21 22 public void setUp() { 23 TypeSchemaHelper.clearCache(); 24 ConfigurationProviderImpl.clearBeanFactoriesMap(); 25 } 26 27 public void tearDown() { 28 TypeSchemaHelper.clearCache(); 29 ConfigurationProviderImpl.clearBeanFactoriesMap(); 30 } 31 32 public void testConstructor() { 33 34 TypeSchema ts = null; 35 36 ts = tsh.get("http://celtix.objectweb.org/configuration/test/types", 38 null, 39 "org/objectweb/celtix/bus/configuration/resources/test-types.xsd"); 40 assertNotNull(ts); 41 42 TypeSchema ts2 = tsh.get("http://celtix.objectweb.org/configuration/test/types", 43 null, 44 "org/objectweb/celtix/bus/configuration/resources/test-types.xsd"); 45 assertNotNull(ts2); 46 assertTrue(ts == ts2); 47 48 50 try { 51 new TypeSchema("http://celtix.objectweb.org/configuration/test/types", 52 null, 53 "/org/objectweb/celtix/bus/configuration/resources/test-types.xsd", 54 true); 55 } catch (ConfigurationException ex) { 56 assertEquals("SCHEMA_LOCATION_ERROR_EXC", ex.getCode()); 57 } 58 59 61 try { 62 ts = new TypeSchema("http://celtix.objectweb.org/configuration/test/types", 63 null, 64 "file://resources/test-types.xsd", 65 true); 66 } catch (org.objectweb.celtix.configuration.ConfigurationException ex) { 67 assertEquals("SCHEMA_LOCATION_ERROR_EXC", ex.getCode()); 68 } 69 70 URL url = AbstractConfigurationImplTest.class.getResource("resources/test-types.xsd"); 71 72 74 ts = new TypeSchema("http://celtix.objectweb.org/configuration/test/types", 75 null, 76 "file://" + url.getFile(), 77 true); 78 assertNotNull(ts); 79 } 80 81 public void testTypesOnly() { 82 TypeSchema ts = tsh.get("http://celtix.objectweb.org/configuration/test/types-types", 83 null, 84 "org/objectweb/celtix/bus/configuration/resources/test-types-types.xsd"); 85 86 assertEquals(7, ts.getTypes().size()); 87 assertEquals(0, ts.getElements().size()); 88 89 assertTrue(ts.hasType("bool")); 90 assertEquals("boolean", ts.getXMLSchemaBaseType("bool")); 91 92 assertTrue(ts.hasType("int")); 93 assertEquals("integer", ts.getXMLSchemaBaseType("int")); 94 95 assertTrue(ts.hasType("longType")); 96 assertEquals("long", ts.getXMLSchemaBaseType("longType")); 97 98 assertTrue(ts.hasType("longBaseType")); 99 assertEquals("long", ts.getXMLSchemaBaseType("longBaseType")); 100 101 assertTrue(ts.hasType("string")); 102 assertEquals("string", ts.getXMLSchemaBaseType("string")); 103 104 assertTrue(ts.hasType("boolList")); 105 assertNull(ts.getXMLSchemaBaseType("boolList")); 106 107 assertTrue(ts.hasType("addressType")); 108 assertNull(ts.getXMLSchemaBaseType("addressType")); 109 110 assertNotNull(ts.getSchema()); 111 assertNotNull(ts.getValidator()); 112 } 113 114 public void testElementsOnly() { 115 TypeSchema ts = tsh.get("http://celtix.objectweb.org/configuration/test/types-elements", 116 null, 117 "org/objectweb/celtix/bus/configuration/resources/test-types-elements.xsd"); 118 119 assertEquals(0, ts.getTypes().size()); 120 assertEquals(7, ts.getElements().size()); 121 122 String namespace = "http://celtix.objectweb.org/configuration/test/types-types"; 123 assertTrue(ts.hasElement("bool")); 124 assertEquals(new QName (namespace, "bool"), ts.getDeclaredType("bool")); 125 assertTrue(!ts.hasType("bool")); 126 try { 127 ts.getXMLSchemaBaseType("bool"); 128 } catch (ConfigurationException ex) { 129 assertEquals("TYPE_NOT_DEFINED_IN_NAMESPACE_EXC", ex.getCode()); 130 } 131 132 assertTrue(ts.hasElement("int")); 133 assertEquals(new QName (namespace, "int"), ts.getDeclaredType("int")); 134 135 assertTrue(ts.hasElement("long")); 136 assertEquals(new QName (namespace, "longType"), ts.getDeclaredType("long")); 137 138 assertTrue(ts.hasElement("string")); 139 assertEquals(new QName (namespace, "string"), ts.getDeclaredType("string")); 140 141 assertTrue(ts.hasElement("boolList")); 142 assertEquals(new QName (namespace, "boolList"), ts.getDeclaredType("boolList")); 143 144 assertTrue(ts.hasElement("address")); 145 assertEquals(new QName (namespace, "addressType"), ts.getDeclaredType("address")); 146 147 assertTrue(ts.hasElement("floatValue")); 148 assertEquals(new QName (XMLConstants.W3C_XML_SCHEMA_NS_URI, "float"), 149 ts.getDeclaredType("floatValue")); 150 151 assertNotNull(ts.getSchema()); 152 assertNotNull(ts.getValidator()); 153 154 } 155 156 public void testElementsAndTypes() { 157 String namespace = "http://celtix.objectweb.org/configuration/test/types"; 158 TypeSchema ts = tsh.get("http://celtix.objectweb.org/configuration/test/types", 159 null, 160 "org/objectweb/celtix/bus/configuration/resources/test-types.xsd"); 161 assertNotNull(ts); 162 163 assertEquals("org.objectweb.celtix.configuration.test.types", ts.getPackageName()); 164 assertEquals(7, ts.getTypes().size()); 165 assertEquals(7, ts.getElements().size()); 166 167 assertTrue(ts.hasElement("bool")); 168 assertEquals(new QName (namespace, "bool"), ts.getDeclaredType("bool")); 169 assertTrue(ts.hasType("bool")); 170 assertEquals("boolean", ts.getXMLSchemaBaseType("bool")); 171 172 assertTrue(ts.hasElement("int")); 173 assertEquals(new QName (namespace, "int"), ts.getDeclaredType("int")); 174 assertTrue(ts.hasType("int")); 175 assertEquals("integer", ts.getXMLSchemaBaseType("int")); 176 177 assertTrue(ts.hasElement("long")); 178 assertEquals(new QName (namespace, "longType"), ts.getDeclaredType("long")); 179 assertTrue(ts.hasType("longType")); 180 assertEquals("long", ts.getXMLSchemaBaseType("longType")); 181 assertTrue(ts.hasType("longBaseType")); 182 assertEquals("long", ts.getXMLSchemaBaseType("longBaseType")); 183 184 assertTrue(ts.hasElement("string")); 185 assertEquals(new QName (namespace, "string"), ts.getDeclaredType("string")); 186 assertTrue(ts.hasType("string")); 187 assertEquals("string", ts.getXMLSchemaBaseType("string")); 188 189 assertTrue(ts.hasElement("boolList")); 190 assertEquals(new QName (namespace, "boolList"), ts.getDeclaredType("boolList")); 191 assertTrue(ts.hasElement("boolList")); 192 assertNull(ts.getXMLSchemaBaseType("boolList")); 193 194 assertTrue(ts.hasElement("address")); 195 assertEquals(new QName (namespace, "addressType"), ts.getDeclaredType("address")); 196 assertTrue(ts.hasType("addressType")); 197 assertNull(ts.getXMLSchemaBaseType("addressType")); 198 199 assertTrue(ts.hasElement("floatValue")); 200 assertEquals(new QName (XMLConstants.W3C_XML_SCHEMA_NS_URI, "float"), 201 ts.getDeclaredType("floatValue")); 202 assertTrue(!ts.hasType("float")); 203 204 assertNotNull(ts.getSchema()); 205 assertNotNull(ts.getValidator()); 206 } 207 208 public void testAnnotatedPackageName() { 209 TypeSchema ts = new TypeSchema("http://celtix.objectweb.org/configuration/test/custom/pkg", 210 null, "org/objectweb/celtix/bus/configuration/resources/test-types-annotations.xsd", 211 true); 212 assertEquals("org.objectweb.celtix.test.custom", ts.getPackageName()); 213 } 214 215 public void testErrorHandler() { 216 TypeSchemaErrorHandler eh = new TypeSchema.TypeSchemaErrorHandler(); 217 SAXParseException spe = new SAXParseException (null, null, null, 0, 0); 218 219 try { 220 eh.error(spe); 221 fail("Expected SAXParseException not thrown."); 222 } catch (SAXParseException ex) { 223 } 225 226 try { 227 eh.warning(spe); 228 fail("Expected SAXParseException not thrown."); 229 } catch (SAXParseException ex) { 230 } 232 233 try { 234 eh.fatalError(spe); 235 fail("Expected SAXParseException not thrown."); 236 } catch (SAXParseException ex) { 237 } 239 } 240 241 public void testTypeSchemaHelper() { 242 TypeSchema ts = org.easymock.classextension.EasyMock.createMock(TypeSchema.class); 243 String namespaceURI = "http://celtix.objectweb.org/helper/test/types"; 244 assertNull(tsh.get(namespaceURI)); 245 tsh.put(namespaceURI, ts); 246 assertNotNull(tsh.get(namespaceURI)); 247 assertNotNull(tsh.get(namespaceURI, null, "/helper/test/types.xsd")); 248 Collection <TypeSchema> c = tsh.getTypeSchemas(); 249 assertTrue(c.size() > 0); 250 assertTrue(c.contains(ts)); 251 } 252 } 253 | Popular Tags |