Index: src/test/java/org/castor/xml/schema/ComplexTypeTest.java =================================================================== --- src/test/java/org/castor/xml/schema/ComplexTypeTest.java (revision 0) +++ src/test/java/org/castor/xml/schema/ComplexTypeTest.java (revision 0) @@ -0,0 +1,248 @@ +/* + * Copyright 2008 Le Duc Bao + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.castor.xml.schema; + +import org.castor.xml.schema.framework.AbstractSchemaTest; +import org.exolab.castor.xml.schema.ComplexType; +import org.exolab.castor.xml.schema.ElementDecl; +import org.exolab.castor.xml.schema.Group; +import org.exolab.castor.xml.schema.Order; + +/** + * This test covers complex type generation. + * + * @author Le Duc Bao + */ +public class ComplexTypeTest extends AbstractSchemaTest { + + /** + * @param testcase + */ + public ComplexTypeTest(String testcase) { + super(testcase); + } + + /** + * Create simple type + */ + public void testSingleAttribute() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + + Group group = new Group(); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + // compare + TestResult result = doTest("complextype_singleattribute.xsd"); + assertEquals("single attribute test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testSingleNamespace: " + e.getMessage()); + } + } + /** + * sequence multiple attributes + */ + public void testSequenceAttribute() { + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + + Group group = new Group(); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + ElementDecl e2 = new ElementDecl(_schema); + e2.setName("myAttr2"); + group.addElementDecl(e2); + + ElementDecl e3 = new ElementDecl(_schema); + e3.setName("myAttr3"); + group.addElementDecl(e3); + + // compare + TestResult result = doTest("complextype_sequenceattribute.xsd"); + assertEquals("sequence multiple attributes test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testSequenceAttribute: " + e.getMessage()); + } + } + + /** + * un-order attributes + */ + public void testAllOrderAttribute() { + try { + + //TODO it seems the XMLDiff does not detect order of attributes + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + + Group group = new Group(); + group.setOrder(Order.all); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + ElementDecl e2 = new ElementDecl(_schema); + e2.setName("myAttr2"); + group.addElementDecl(e2); + + ElementDecl e3 = new ElementDecl(_schema); + e3.setName("myAttr3"); + group.addElementDecl(e3); + + // compare + TestResult result = doTest("complextype_allorder.xsd"); + assertEquals("all order attributes test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testAllOrderAttribute: " + e.getMessage()); + } + } + + /** + * choice group attributes + */ + public void testChoiceAttribute() { + try { + + //TODO it seems the XMLDiff does not detect order of attributes + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + + Group group = new Group(); + group.setOrder(Order.choice); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + ElementDecl e2 = new ElementDecl(_schema); + e2.setName("myAttr2"); + group.addElementDecl(e2); + + ElementDecl e3 = new ElementDecl(_schema); + e3.setName("myAttr3"); + group.addElementDecl(e3); + + // compare + TestResult result = doTest("complextype_choiceattribute.xsd"); + assertEquals("choice group attributes test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testChoiceAttribute: " + e.getMessage()); + } + } + + /** + * extension generation test + */ + public void testExtension() { + try { + + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + //create base type + ComplexType cBaseType = _schema.createComplexType("baseType"); + _schema.addComplexType(cBaseType); + + Group gBase = new Group(); + cBaseType.addGroup(gBase); + + ElementDecl ebase = new ElementDecl(_schema); + ebase.setName("baseAttr"); + gBase.addElementDecl(ebase); + + //create dependency + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + cType.setBaseType(cBaseType); + + Group group = new Group(); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + ElementDecl e2 = new ElementDecl(_schema); + e2.setName("myAttr2"); + group.addElementDecl(e2); + + ElementDecl e3 = new ElementDecl(_schema); + e3.setName("myAttr3"); + group.addElementDecl(e3); + + // compare + TestResult result = doTest("complextype_attributeorder.xsd"); + assertEquals("create extension test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testExtension: " + e.getMessage()); + } + } + + /** + * extension generation test + */ + public void testCreateElementForComplexType() { + try { + + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + //create dependency + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + + Group group = new Group(); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + ElementDecl element = new ElementDecl(_schema); + element.setName("myElement"); + element.setTypeReference("myType"); + _schema.addElementDecl(element); + + // compare + TestResult result = doTest("complextype_elementforcomplextype.xsd"); + assertEquals("test create element for complexType test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testCreateElementForComplexType: " + e.getMessage()); + } + } +} Index: src/test/java/org/castor/xml/schema/SimpleTypeTest.java =================================================================== --- src/test/java/org/castor/xml/schema/SimpleTypeTest.java (revision 0) +++ src/test/java/org/castor/xml/schema/SimpleTypeTest.java (revision 0) @@ -0,0 +1,166 @@ +/* + * Copyright 2008 Le Duc Bao + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.castor.xml.schema; + +import org.castor.xml.schema.framework.AbstractSchemaTest; +import org.castor.xml.schema.framework.AbstractSchemaTest.TestResult; +import org.exolab.castor.xml.schema.AttributeDecl; +import org.exolab.castor.xml.schema.ComplexType; +import org.exolab.castor.xml.schema.ElementDecl; +import org.exolab.castor.xml.schema.Facet; +import org.exolab.castor.xml.schema.Group; +import org.exolab.castor.xml.schema.SimpleType; + +/** + * This test covers simple type generation. + * @author Le Duc Bao + */ +public class SimpleTypeTest extends AbstractSchemaTest { + + /** + * @param Constructor + */ + public SimpleTypeTest(String testcase) { + super(testcase); + } + + /** + * very simple type + */ + public void testSimpleType() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + SimpleType sType = _schema.createSimpleType("myType", "string", ""); + + _schema.addSimpleType(sType); + + // compare + TestResult result = doTest("simpletype_simple.xsd"); + assertEquals("single attribute test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testSingleNamespace: " + e.getMessage()); + } + } + + /** + * test create attribute, fixed value + */ + public void testAttributeCreation() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + AttributeDecl attr = new AttributeDecl(_schema); + attr.setName("myAttr"); + attr.setSimpleTypeReference("string"); + attr.setFixedValue("#hello"); + attr.setUse(AttributeDecl.USE_OPTIONAL); + _schema.addAttribute(attr); + + // compare + TestResult result = doTest("simpletype_attributecreation.xsd"); + assertEquals("testAttributeCreation test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testAttributeCreation: " + e.getMessage()); + } + } + + /** + * test create attribute + */ + public void testAttributeCreation2() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + AttributeDecl attr = new AttributeDecl(_schema); + attr.setName("myAttr"); + attr.setSimpleTypeReference("string"); + attr.setDefaultValue("hello"); + attr.setUse(AttributeDecl.USE_PROHIBITED); + + _schema.addAttribute(attr); + + // compare + TestResult result = doTest("simpletype_attributecreation2.xsd"); + assertEquals("testAttributeCreation2 test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testAttributeCreation2: " + e.getMessage()); + } + } + + /** + * test create attribute, use required + */ + public void testAttributeCreation3() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + AttributeDecl attr = new AttributeDecl(_schema); + attr.setName("myAttr"); + attr.setSimpleTypeReference("string"); + attr.setDefaultValue("hello"); + attr.setUse(AttributeDecl.USE_REQUIRED); + + _schema.addAttribute(attr); + + // compare + TestResult result = doTest("simpletype_attributecreation3.xsd"); + assertEquals("testAttributeCreation3 test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testAttributeCreation3: " + e.getMessage()); + } + } + //restriction + /** + * test create facet/min-max + */ + public void testMinMax() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + SimpleType sType = _schema.createSimpleType("myType", "int", ""); + + Facet min = new Facet(Facet.MIN_EXCLUSIVE, "0"); + Facet max = new Facet(Facet.MAX_EXCLUSIVE, "100"); + sType.addFacet(min); + sType.addFacet(max); + + _schema.addSimpleType(sType); + + // compare + TestResult result = doTest("simpletype_res_minmax.xsd"); + assertEquals("testMinMax test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testMinMax: " + e.getMessage()); + } + } + //min inclusive, max inclusive + //leng, max length, min length + //whiteSpace preserve, replace, collapse + //enumeration + //union + //pattern + //precision, total digits, fraction digits +} Index: src/test/java/org/castor/xml/schema/NamespaceTest.java =================================================================== --- src/test/java/org/castor/xml/schema/NamespaceTest.java (revision 0) +++ src/test/java/org/castor/xml/schema/NamespaceTest.java (revision 0) @@ -0,0 +1,77 @@ +/* + * Copyright 2008 Le Duc Bao + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.castor.xml.schema; + +import org.castor.xml.schema.framework.AbstractSchemaTest; + +/** + * Namespace tests. + * + * @author Le Duc Bao + */ +public final class NamespaceTest extends AbstractSchemaTest { + public NamespaceTest(final String testcase) { + super(testcase); + } + + /** + * Create a schema with single namespace + */ + public void testSingleNamespace() { + + try { + // create targeted schema + _schema.addNamespace("myprefix", "my.namespace.org"); + + TestResult result = doTest("namespace_singlenamespace.xsd"); + assertEquals("single namespace add failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testSingleNamespace: " + e.getMessage()); + } + } + + /** + * Create a schema with single namespace + */ + public void testDiff() { + + try { + // create targeted schema + _schema.addNamespace("wrong", "my.namespace.org"); + + TestResult result = doTest("namespace_singlenamespace.xsd"); + assertEquals("test diff", TestResult.DIFFERENCE, result); + } catch (Exception e) { + fail("testDiff: " + e.getMessage()); + } + } + /** + * Test for multiple namespaces + */ + public void testMultipleNamespace() { + + try { + // create targeted schema + _schema.addNamespace("myprefix", "my.namespace.org"); + _schema.addNamespace("other", "other.namespace.org"); + + TestResult result = doTest("namespace_multiplenamespace.xsd"); + assertEquals("multiple namespace add failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testSingleNamespace: " + e.getMessage()); + } + } +} Index: src/test/java/org/castor/xml/schema/ComplexTypeTest.java =================================================================== --- src/test/java/org/castor/xml/schema/ComplexTypeTest.java (revision 0) +++ src/test/java/org/castor/xml/schema/ComplexTypeTest.java (revision 0) @@ -0,0 +1,248 @@ +/* + * Copyright 2008 Le Duc Bao + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.castor.xml.schema; + +import org.castor.xml.schema.framework.AbstractSchemaTest; +import org.exolab.castor.xml.schema.ComplexType; +import org.exolab.castor.xml.schema.ElementDecl; +import org.exolab.castor.xml.schema.Group; +import org.exolab.castor.xml.schema.Order; + +/** + * This test covers complex type generation. + * + * @author Le Duc Bao + */ +public class ComplexTypeTest extends AbstractSchemaTest { + + /** + * @param testcase + */ + public ComplexTypeTest(String testcase) { + super(testcase); + } + + /** + * Create simple type + */ + public void testSingleAttribute() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + + Group group = new Group(); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + // compare + TestResult result = doTest("complextype_singleattribute.xsd"); + assertEquals("single attribute test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testSingleNamespace: " + e.getMessage()); + } + } + /** + * sequence multiple attributes + */ + public void testSequenceAttribute() { + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + + Group group = new Group(); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + ElementDecl e2 = new ElementDecl(_schema); + e2.setName("myAttr2"); + group.addElementDecl(e2); + + ElementDecl e3 = new ElementDecl(_schema); + e3.setName("myAttr3"); + group.addElementDecl(e3); + + // compare + TestResult result = doTest("complextype_sequenceattribute.xsd"); + assertEquals("sequence multiple attributes test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testSequenceAttribute: " + e.getMessage()); + } + } + + /** + * un-order attributes + */ + public void testAllOrderAttribute() { + try { + + //TODO it seems the XMLDiff does not detect order of attributes + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + + Group group = new Group(); + group.setOrder(Order.all); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + ElementDecl e2 = new ElementDecl(_schema); + e2.setName("myAttr2"); + group.addElementDecl(e2); + + ElementDecl e3 = new ElementDecl(_schema); + e3.setName("myAttr3"); + group.addElementDecl(e3); + + // compare + TestResult result = doTest("complextype_allorder.xsd"); + assertEquals("all order attributes test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testAllOrderAttribute: " + e.getMessage()); + } + } + + /** + * choice group attributes + */ + public void testChoiceAttribute() { + try { + + //TODO it seems the XMLDiff does not detect order of attributes + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + + Group group = new Group(); + group.setOrder(Order.choice); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + ElementDecl e2 = new ElementDecl(_schema); + e2.setName("myAttr2"); + group.addElementDecl(e2); + + ElementDecl e3 = new ElementDecl(_schema); + e3.setName("myAttr3"); + group.addElementDecl(e3); + + // compare + TestResult result = doTest("complextype_choiceattribute.xsd"); + assertEquals("choice group attributes test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testChoiceAttribute: " + e.getMessage()); + } + } + + /** + * extension generation test + */ + public void testExtension() { + try { + + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + //create base type + ComplexType cBaseType = _schema.createComplexType("baseType"); + _schema.addComplexType(cBaseType); + + Group gBase = new Group(); + cBaseType.addGroup(gBase); + + ElementDecl ebase = new ElementDecl(_schema); + ebase.setName("baseAttr"); + gBase.addElementDecl(ebase); + + //create dependency + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + cType.setBaseType(cBaseType); + + Group group = new Group(); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + ElementDecl e2 = new ElementDecl(_schema); + e2.setName("myAttr2"); + group.addElementDecl(e2); + + ElementDecl e3 = new ElementDecl(_schema); + e3.setName("myAttr3"); + group.addElementDecl(e3); + + // compare + TestResult result = doTest("complextype_attributeorder.xsd"); + assertEquals("create extension test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testExtension: " + e.getMessage()); + } + } + + /** + * extension generation test + */ + public void testCreateElementForComplexType() { + try { + + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + //create dependency + ComplexType cType = _schema.createComplexType("myType"); + _schema.addComplexType(cType); + + Group group = new Group(); + cType.addGroup(group); + + ElementDecl e = new ElementDecl(_schema); + e.setName("myAttr"); + group.addElementDecl(e); + + ElementDecl element = new ElementDecl(_schema); + element.setName("myElement"); + element.setTypeReference("myType"); + _schema.addElementDecl(element); + + // compare + TestResult result = doTest("complextype_elementforcomplextype.xsd"); + assertEquals("test create element for complexType test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testCreateElementForComplexType: " + e.getMessage()); + } + } +} Index: src/test/java/org/castor/xml/schema/framework/AbstractSchemaTest.java =================================================================== --- src/test/java/org/castor/xml/schema/framework/AbstractSchemaTest.java (revision 0) +++ src/test/java/org/castor/xml/schema/framework/AbstractSchemaTest.java (revision 0) @@ -0,0 +1,118 @@ +/* + * Copyright 2008 Le Duc Bao + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.castor.xml.schema.framework; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.Writer; +import java.net.URL; + +import org.castor.xmlctf.xmldiff.XMLDiff; +import org.exolab.castor.xml.schema.Schema; +import org.exolab.castor.xml.schema.writer.SchemaWriter; + +import junit.framework.TestCase; + +/** + * This class aims to set up a test environment and to provide a skeleton for + * testing Schema API. A typical scenarios test is + *
  • Create Schema related to targeted test case
  • + *
  • Generate Schema fragment by calling SchemaWriter
  • + *
  • Load expected schema file
  • + *
  • Compare generated schema vs expected schema
  • + * + * @author Le Duc Bao + */ +public abstract class AbstractSchemaTest extends TestCase { + + /** Test result */ + public enum TestResult { IDENTICAL, DIFFERENCE }; + + /** + * Path of the expected result pattern files. + */ + // private static final String EXPECTED_PATH = ".."; + /** + * Handle targeted schema + */ + protected Schema _schema = null; + + /** + * Constructor for BaseGeneratorTest + * + * @param testcase + * test case + */ + public AbstractSchemaTest(final String testcase) { + super(testcase); + } + + /** + * create a new schema instance + * + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + _schema = new Schema(); + } + + /** + * @see junit.framework.TestCase#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); + _schema = null; + } + + /** + * This function aims to generate schema fragment, load expected schema and + * compare them. + * + * @param expectedFilename + * expected schema filename. + * @return 0, if no differences are found, otherwise a positive number + * indicating the number of differences. + * @see org.castor.xmlctf.xmldiff.XMLDiff#compare() + * @throws Exception + * if any + */ + protected final TestResult doTest(String expected) throws Exception { + // To reuse the existent source code from XMLCTF Framework, all schemas + // will be represented as a XML content. They are inputed to + // org.castor.xmlctf.xmldiff.XMLDiff to get the final result. + + // 1. generate schema and product a xml content, write it in a temporary file + File targetedOutput = File.createTempFile("doTest", "xmlctf"); + Writer writer = new BufferedWriter(new FileWriter(targetedOutput)); + SchemaWriter swriter = new SchemaWriter(writer); + swriter.write(_schema); + + // 2. load expected schema + URL expectedUrl = this.getClass().getResource(expected); + + // 3. compare using org.castor.xmlctf.xmldiff.XMLDiff + XMLDiff diff = new XMLDiff(targetedOutput.getAbsolutePath(), expectedUrl.getFile()); + int result = diff.compare(); + TestResult testResult = result == 0 ? TestResult.IDENTICAL : TestResult.DIFFERENCE; + + // 4. delete temporary file + targetedOutput.delete(); + + return testResult; + } +} Index: src/test/java/org/castor/xml/schema/framework/AbstractSchemaTest.java =================================================================== --- src/test/java/org/castor/xml/schema/framework/AbstractSchemaTest.java (revision 0) +++ src/test/java/org/castor/xml/schema/framework/AbstractSchemaTest.java (revision 0) @@ -0,0 +1,118 @@ +/* + * Copyright 2008 Le Duc Bao + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.castor.xml.schema.framework; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.Writer; +import java.net.URL; + +import org.castor.xmlctf.xmldiff.XMLDiff; +import org.exolab.castor.xml.schema.Schema; +import org.exolab.castor.xml.schema.writer.SchemaWriter; + +import junit.framework.TestCase; + +/** + * This class aims to set up a test environment and to provide a skeleton for + * testing Schema API. A typical scenarios test is + *
  • Create Schema related to targeted test case
  • + *
  • Generate Schema fragment by calling SchemaWriter
  • + *
  • Load expected schema file
  • + *
  • Compare generated schema vs expected schema
  • + * + * @author Le Duc Bao + */ +public abstract class AbstractSchemaTest extends TestCase { + + /** Test result */ + public enum TestResult { IDENTICAL, DIFFERENCE }; + + /** + * Path of the expected result pattern files. + */ + // private static final String EXPECTED_PATH = ".."; + /** + * Handle targeted schema + */ + protected Schema _schema = null; + + /** + * Constructor for BaseGeneratorTest + * + * @param testcase + * test case + */ + public AbstractSchemaTest(final String testcase) { + super(testcase); + } + + /** + * create a new schema instance + * + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + _schema = new Schema(); + } + + /** + * @see junit.framework.TestCase#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); + _schema = null; + } + + /** + * This function aims to generate schema fragment, load expected schema and + * compare them. + * + * @param expectedFilename + * expected schema filename. + * @return 0, if no differences are found, otherwise a positive number + * indicating the number of differences. + * @see org.castor.xmlctf.xmldiff.XMLDiff#compare() + * @throws Exception + * if any + */ + protected final TestResult doTest(String expected) throws Exception { + // To reuse the existent source code from XMLCTF Framework, all schemas + // will be represented as a XML content. They are inputed to + // org.castor.xmlctf.xmldiff.XMLDiff to get the final result. + + // 1. generate schema and product a xml content, write it in a temporary file + File targetedOutput = File.createTempFile("doTest", "xmlctf"); + Writer writer = new BufferedWriter(new FileWriter(targetedOutput)); + SchemaWriter swriter = new SchemaWriter(writer); + swriter.write(_schema); + + // 2. load expected schema + URL expectedUrl = this.getClass().getResource(expected); + + // 3. compare using org.castor.xmlctf.xmldiff.XMLDiff + XMLDiff diff = new XMLDiff(targetedOutput.getAbsolutePath(), expectedUrl.getFile()); + int result = diff.compare(); + TestResult testResult = result == 0 ? TestResult.IDENTICAL : TestResult.DIFFERENCE; + + // 4. delete temporary file + targetedOutput.delete(); + + return testResult; + } +} Index: src/test/java/org/castor/xml/schema/NamespaceTest.java =================================================================== --- src/test/java/org/castor/xml/schema/NamespaceTest.java (revision 0) +++ src/test/java/org/castor/xml/schema/NamespaceTest.java (revision 0) @@ -0,0 +1,77 @@ +/* + * Copyright 2008 Le Duc Bao + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.castor.xml.schema; + +import org.castor.xml.schema.framework.AbstractSchemaTest; + +/** + * Namespace tests. + * + * @author Le Duc Bao + */ +public final class NamespaceTest extends AbstractSchemaTest { + public NamespaceTest(final String testcase) { + super(testcase); + } + + /** + * Create a schema with single namespace + */ + public void testSingleNamespace() { + + try { + // create targeted schema + _schema.addNamespace("myprefix", "my.namespace.org"); + + TestResult result = doTest("namespace_singlenamespace.xsd"); + assertEquals("single namespace add failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testSingleNamespace: " + e.getMessage()); + } + } + + /** + * Create a schema with single namespace + */ + public void testDiff() { + + try { + // create targeted schema + _schema.addNamespace("wrong", "my.namespace.org"); + + TestResult result = doTest("namespace_singlenamespace.xsd"); + assertEquals("test diff", TestResult.DIFFERENCE, result); + } catch (Exception e) { + fail("testDiff: " + e.getMessage()); + } + } + /** + * Test for multiple namespaces + */ + public void testMultipleNamespace() { + + try { + // create targeted schema + _schema.addNamespace("myprefix", "my.namespace.org"); + _schema.addNamespace("other", "other.namespace.org"); + + TestResult result = doTest("namespace_multiplenamespace.xsd"); + assertEquals("multiple namespace add failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testSingleNamespace: " + e.getMessage()); + } + } +} Index: src/test/java/org/castor/xml/schema/SimpleTypeTest.java =================================================================== --- src/test/java/org/castor/xml/schema/SimpleTypeTest.java (revision 0) +++ src/test/java/org/castor/xml/schema/SimpleTypeTest.java (revision 0) @@ -0,0 +1,166 @@ +/* + * Copyright 2008 Le Duc Bao + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.castor.xml.schema; + +import org.castor.xml.schema.framework.AbstractSchemaTest; +import org.castor.xml.schema.framework.AbstractSchemaTest.TestResult; +import org.exolab.castor.xml.schema.AttributeDecl; +import org.exolab.castor.xml.schema.ComplexType; +import org.exolab.castor.xml.schema.ElementDecl; +import org.exolab.castor.xml.schema.Facet; +import org.exolab.castor.xml.schema.Group; +import org.exolab.castor.xml.schema.SimpleType; + +/** + * This test covers simple type generation. + * @author Le Duc Bao + */ +public class SimpleTypeTest extends AbstractSchemaTest { + + /** + * @param Constructor + */ + public SimpleTypeTest(String testcase) { + super(testcase); + } + + /** + * very simple type + */ + public void testSimpleType() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + SimpleType sType = _schema.createSimpleType("myType", "string", ""); + + _schema.addSimpleType(sType); + + // compare + TestResult result = doTest("simpletype_simple.xsd"); + assertEquals("single attribute test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testSingleNamespace: " + e.getMessage()); + } + } + + /** + * test create attribute, fixed value + */ + public void testAttributeCreation() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + AttributeDecl attr = new AttributeDecl(_schema); + attr.setName("myAttr"); + attr.setSimpleTypeReference("string"); + attr.setFixedValue("#hello"); + attr.setUse(AttributeDecl.USE_OPTIONAL); + _schema.addAttribute(attr); + + // compare + TestResult result = doTest("simpletype_attributecreation.xsd"); + assertEquals("testAttributeCreation test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testAttributeCreation: " + e.getMessage()); + } + } + + /** + * test create attribute + */ + public void testAttributeCreation2() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + AttributeDecl attr = new AttributeDecl(_schema); + attr.setName("myAttr"); + attr.setSimpleTypeReference("string"); + attr.setDefaultValue("hello"); + attr.setUse(AttributeDecl.USE_PROHIBITED); + + _schema.addAttribute(attr); + + // compare + TestResult result = doTest("simpletype_attributecreation2.xsd"); + assertEquals("testAttributeCreation2 test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testAttributeCreation2: " + e.getMessage()); + } + } + + /** + * test create attribute, use required + */ + public void testAttributeCreation3() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + AttributeDecl attr = new AttributeDecl(_schema); + attr.setName("myAttr"); + attr.setSimpleTypeReference("string"); + attr.setDefaultValue("hello"); + attr.setUse(AttributeDecl.USE_REQUIRED); + + _schema.addAttribute(attr); + + // compare + TestResult result = doTest("simpletype_attributecreation3.xsd"); + assertEquals("testAttributeCreation3 test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testAttributeCreation3: " + e.getMessage()); + } + } + //restriction + /** + * test create facet/min-max + */ + public void testMinMax() { + + try { + // create targeted schema + _schema.addNamespace("pre", "my.namespace.org"); + + SimpleType sType = _schema.createSimpleType("myType", "int", ""); + + Facet min = new Facet(Facet.MIN_EXCLUSIVE, "0"); + Facet max = new Facet(Facet.MAX_EXCLUSIVE, "100"); + sType.addFacet(min); + sType.addFacet(max); + + _schema.addSimpleType(sType); + + // compare + TestResult result = doTest("simpletype_res_minmax.xsd"); + assertEquals("testMinMax test failed", TestResult.IDENTICAL, result); + } catch (Exception e) { + fail("testMinMax: " + e.getMessage()); + } + } + //min inclusive, max inclusive + //leng, max length, min length + //whiteSpace preserve, replace, collapse + //enumeration + //union + //pattern + //precision, total digits, fraction digits +} Index: src/test/resources/org/castor/xml/schema/complextype_elementforcomplextype.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_elementforcomplextype.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_elementforcomplextype.xsd (revision 0) @@ -0,0 +1,9 @@ + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/simpletype_attributecreation.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/simpletype_attributecreation.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/simpletype_attributecreation.xsd (revision 0) @@ -0,0 +1,4 @@ + + + + Index: src/test/resources/org/castor/xml/schema/namespace_multiplenamespace.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/namespace_multiplenamespace.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/namespace_multiplenamespace.xsd (revision 0) @@ -0,0 +1,2 @@ + + Index: src/test/resources/org/castor/xml/schema/simpletype_simple.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/simpletype_simple.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/simpletype_simple.xsd (revision 0) @@ -0,0 +1,6 @@ + + + + + + Index: src/test/resources/org/castor/xml/schema/complextype_extension.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_extension.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_extension.xsd (revision 0) @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/complextype_sequenceattribute.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_sequenceattribute.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_sequenceattribute.xsd (revision 0) @@ -0,0 +1,10 @@ + + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/simpletype_attributecreation2.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/simpletype_attributecreation2.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/simpletype_attributecreation2.xsd (revision 0) @@ -0,0 +1,4 @@ + + + + Index: src/test/resources/org/castor/xml/schema/simpletype_attributecreation3.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/simpletype_attributecreation3.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/simpletype_attributecreation3.xsd (revision 0) @@ -0,0 +1,4 @@ + + + + Index: src/test/resources/org/castor/xml/schema/complextype_choiceattribute.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_choiceattribute.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_choiceattribute.xsd (revision 0) @@ -0,0 +1,10 @@ + + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/namespace_singlenamespace.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/namespace_singlenamespace.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/namespace_singlenamespace.xsd (revision 0) @@ -0,0 +1,2 @@ + + Index: src/test/resources/org/castor/xml/schema/complextype_allorder.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_allorder.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_allorder.xsd (revision 0) @@ -0,0 +1,10 @@ + + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/simpletype_res_minmax.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/simpletype_res_minmax.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/simpletype_res_minmax.xsd (revision 0) @@ -0,0 +1,9 @@ + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/complextype_singleattribute.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_singleattribute.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_singleattribute.xsd (revision 0) @@ -0,0 +1,8 @@ + + + + + + + + Index: src/test/resources/org/castor/xml/schema/complextype_allorder.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_allorder.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_allorder.xsd (revision 0) @@ -0,0 +1,10 @@ + + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/complextype_choiceattribute.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_choiceattribute.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_choiceattribute.xsd (revision 0) @@ -0,0 +1,10 @@ + + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/complextype_elementforcomplextype.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_elementforcomplextype.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_elementforcomplextype.xsd (revision 0) @@ -0,0 +1,9 @@ + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/complextype_extension.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_extension.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_extension.xsd (revision 0) @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/complextype_sequenceattribute.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_sequenceattribute.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_sequenceattribute.xsd (revision 0) @@ -0,0 +1,10 @@ + + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/complextype_singleattribute.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/complextype_singleattribute.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/complextype_singleattribute.xsd (revision 0) @@ -0,0 +1,8 @@ + + + + + + + + Index: src/test/resources/org/castor/xml/schema/namespace_multiplenamespace.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/namespace_multiplenamespace.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/namespace_multiplenamespace.xsd (revision 0) @@ -0,0 +1,2 @@ + + Index: src/test/resources/org/castor/xml/schema/namespace_singlenamespace.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/namespace_singlenamespace.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/namespace_singlenamespace.xsd (revision 0) @@ -0,0 +1,2 @@ + + Index: src/test/resources/org/castor/xml/schema/simpletype_attributecreation.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/simpletype_attributecreation.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/simpletype_attributecreation.xsd (revision 0) @@ -0,0 +1,4 @@ + + + + Index: src/test/resources/org/castor/xml/schema/simpletype_attributecreation2.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/simpletype_attributecreation2.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/simpletype_attributecreation2.xsd (revision 0) @@ -0,0 +1,4 @@ + + + + Index: src/test/resources/org/castor/xml/schema/simpletype_attributecreation3.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/simpletype_attributecreation3.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/simpletype_attributecreation3.xsd (revision 0) @@ -0,0 +1,4 @@ + + + + Index: src/test/resources/org/castor/xml/schema/simpletype_res_minmax.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/simpletype_res_minmax.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/simpletype_res_minmax.xsd (revision 0) @@ -0,0 +1,9 @@ + + + + + + + + + Index: src/test/resources/org/castor/xml/schema/simpletype_simple.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/simpletype_simple.xsd (revision 0) +++ src/test/resources/org/castor/xml/schema/simpletype_simple.xsd (revision 0) @@ -0,0 +1,6 @@ + + + + + +