001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.schema.compile;
020:
021: import junit.framework.TestCase;
022: import org.apache.axis2.schema.CompilerOptions;
023: import org.apache.axis2.schema.SchemaCompiler;
024: import org.apache.ws.commons.schema.XmlSchema;
025: import org.apache.ws.commons.schema.XmlSchemaCollection;
026: import org.w3c.dom.Document;
027:
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030: import java.io.File;
031:
032: /* Maven is a little dumb when it comes to executing test cases. It tries to run everything
033: that ends with 'test' even if the class is abstract or non junit!!!!!
034: Rather than putting an explicit exclude, this is a cheaper way out
035: */
036: public abstract class AbstractSchemaCompilerTester extends TestCase {
037:
038: //this should be an xsd name in the test-resource directory
039: protected String fileName = "";
040: protected XmlSchema currentSchema;
041: protected File outputFolder = null;
042:
043: private static String TEMP_OUT_FOLDER = "temp_compile";
044:
045: protected void setUp() throws Exception {
046: //load the current Schema through a file
047: //first read the file into a DOM
048: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
049: .newInstance();
050: documentBuilderFactory.setNamespaceAware(true);
051:
052: DocumentBuilder builder = documentBuilderFactory
053: .newDocumentBuilder();
054: Document doc = builder.parse(new File(System.getProperty(
055: "basedir", ".")
056: + "/" + fileName));
057:
058: //now read it to a schema
059: XmlSchemaCollection schemaCol = getSchemaReader();
060: currentSchema = schemaCol.read(doc, null);
061:
062: outputFolder = new File(TEMP_OUT_FOLDER);
063: if (outputFolder.exists()) {
064: if (outputFolder.isFile()) {
065: outputFolder.delete();
066: outputFolder.mkdirs();
067: }
068: } else {
069: outputFolder.mkdirs();
070: }
071: }
072:
073: protected XmlSchemaCollection getSchemaReader() {
074: return new XmlSchemaCollection();
075: }
076:
077: public void testSchema() throws Exception {
078: CompilerOptions compilerOptions = new CompilerOptions();
079: compilerOptions.setOutputLocation(outputFolder);
080: compilerOptions.setWrapClasses(false);
081: compilerOptions.setWriteOutput(true);
082: SchemaCompiler compiler = new SchemaCompiler(compilerOptions);
083: compiler.compile(currentSchema);
084:
085: }
086:
087: protected void tearDown() throws Exception {
088: deleteDir(outputFolder);
089: }
090:
091: /**
092: * Deletes all files and subdirectories under dir.
093: * Returns true if all deletions were successful.
094: * If a deletion fails, the method stops attempting to delete and returns false.
095: */
096: private boolean deleteDir(File dir) {
097: if (dir.isDirectory()) {
098: String[] children = dir.list();
099: for (int i = 0; i < children.length; i++) {
100: boolean success = deleteDir(new File(dir, children[i]));
101: if (!success) {
102: return false;
103: }
104: }
105: }
106:
107: // The directory is now empty so delete it
108: return dir.delete();
109: }
110: }
|