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,63 @@ +/* + * 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; + +/** + * Complex type tests + * + * @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 + int result = doTest("complextype_singleattribute.xsd"); + assertEquals("single attribute test failed", NO_DIFFERENCE, result); + } catch (Exception e) { + fail("testSingleNamespace: " + e.getMessage()); + } + } + +} 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,45 @@ +/* + * 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"); + + int result = doTest("namespace_1.xsd"); + assertEquals("single namespace add failed", NO_DIFFERENCE, result); + } catch (Exception e) { + fail("testSingleNamespace: " + 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,116 @@ +/* + * 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 { + + /** + * Path of the expected result pattern files. + */ + // private static final String EXPECTED_PATH = ".."; + /** + * Handle targeted schema + */ + protected Schema _schema = null; + + protected static final int NO_DIFFERENCE = 0; + + /** + * 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 int 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(); + + // 4. delete temporary file + targetedOutput.delete(); + + return result; + } +} Index: src/test/resources/org/castor/xml/schema/namespace_1.xsd =================================================================== --- src/test/resources/org/castor/xml/schema/namespace_1.xsd (Revision 0) +++ src/test/resources/org/castor/xml/schema/namespace_1.xsd (Revision 0) @@ -0,0 +1,2 @@ + + 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 @@ + + + + + + + + \ Kein Zeilenvorschub am Ende der Datei