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:
020: package org.apache.axis2.xmlbeans;
021:
022: import junit.framework.TestCase;
023: import org.apache.axis2.util.CommandLineOption;
024: import org.apache.axis2.util.CommandLineOptionConstants;
025: import org.apache.axis2.util.CommandLineOptionParser;
026: import org.apache.axis2.wsdl.codegen.CodeGenerationEngine;
027: import org.apache.axis2.wsdl.codegen.CodeGenerationException;
028: import org.apache.tools.ant.Project;
029: import org.apache.tools.ant.Target;
030: import org.apache.tools.ant.taskdefs.Javac;
031: import org.apache.tools.ant.types.Path;
032:
033: import java.io.BufferedReader;
034: import java.io.File;
035: import java.io.FileReader;
036: import java.util.HashMap;
037: import java.util.Map;
038:
039: public abstract class WSDL2JavaSuccessTestBase extends TestCase {
040:
041: public static final String OUTPUT_LOCATION_BASE = System
042: .getProperty("basedir", ".")
043: + "/out_put_classes";
044: public static final String OUTPUT_LOCATION_PREFIX = "/test";
045: protected static int folderCount = 0;
046: public static final String WSDL_BASE_DIR = System.getProperty(
047: "basedir", ".")
048: + "/test-resources/";
049: public static final String CLASSES_DIR = System.getProperty(
050: "basedir", ".")
051: + "/target/classes/";
052: private String[] moduleNames = { "xml", "common", "core" };
053: private static final String MODULE_PATH_PREFIX = "../modules/";
054: private static final String COMPILE_TARGET_NAME = "compile";
055:
056: protected String wsdlFileName;
057:
058: /**
059: * Make the root output directory
060: *
061: * @throws Exception
062: */
063: protected void setUp() throws Exception {
064: File outputFile = new File(OUTPUT_LOCATION_BASE);
065: if (outputFile.exists() && outputFile.isDirectory()) {
066: deleteDir(outputFile);
067: outputFile.mkdir();
068: } else {
069: outputFile.mkdir();
070: }
071: }
072:
073: /**
074: * Remove the root output directory
075: *
076: * @throws Exception
077: */
078: protected void tearDown() throws Exception {
079: File outputFile = new File(OUTPUT_LOCATION_BASE);
080: if (outputFile.exists() && outputFile.isDirectory()) {
081: deleteDir(outputFile);
082: }
083: }
084:
085: /**
086: * Deletes all files and subdirectories under dir. Returns true if all deletions were
087: * successful. If a deletion fails, the method stops attempting to delete and returns false.
088: */
089: private boolean deleteDir(File dir) {
090: if (dir.isDirectory()) {
091: String[] children = dir.list();
092: for (int i = 0; i < children.length; i++) {
093: boolean success = deleteDir(new File(dir, children[i]));
094: if (!success) {
095: return false;
096: }
097: }
098: }
099:
100: // The directory is now empty so delete it
101: return dir.delete();
102: }
103:
104: public void testWSDLFile() {
105: try {
106: generateAndCompile(wsdlFileName, OUTPUT_LOCATION_BASE
107: + OUTPUT_LOCATION_PREFIX + folderCount++);
108: } catch (CodeGenerationException e) {
109: e.printStackTrace();
110: fail("Exception while code generation test! "
111: + wsdlFileName + e.getMessage());
112: }
113:
114: }
115:
116: // /**
117: // * Test for the sales rank and price!
118: // */
119: // public void testCodeGenerationSalesRankNPrice(){
120: // try {
121: // generateAndCompile("SalesRankNPrice.wsdl", OUTPUT_LOCATION_BASE+OUTPUT_LOCATION_PREFIX+folderCount++);
122: // } catch (CodeGenerationException e) {
123: // fail("Exception while code generation test!"+ e.getMessage());
124: // }
125: // }
126:
127: // /**
128: // * Test for the dime doc
129: // */
130: // public void testCodeGenerationDimeDoc(){
131: //
132: // try {
133: // generateAndCompile("dime-doc.wsdl", OUTPUT_LOCATION_BASE+OUTPUT_LOCATION_PREFIX+folderCount++);
134: // } catch (CodeGenerationException e) {
135: // fail("Exception while code generation test!"+ e.getMessage());
136: // }
137: // }
138:
139: /**
140: * Test for the modified ping wsdl. it will be the test for the detached schema with only an import
141: * statement
142: */
143: // public void testCodeGenerationPingModified(){
144: //
145: // try {
146: // generateAndCompile("ping-modified.wsdl", OUTPUT_LOCATION_BASE+OUTPUT_LOCATION_PREFIX+folderCount++);
147: // } catch (CodeGenerationException e) {
148: // fail("Exception while code generation test!"+ e.getMessage());
149: // }
150: // }
151:
152: /**
153: * @param wsdlName
154: * @param outputLocation
155: * @throws CodeGenerationException
156: */
157: protected void generateAndCompile(String wsdlName,
158: String outputLocation) throws CodeGenerationException {
159: codeGenerate(WSDL_BASE_DIR + wsdlName, outputLocation);
160: compile(outputLocation);
161: }
162:
163: /**
164: * @param wsdlFile
165: * @param outputLocation
166: * @throws CodeGenerationException
167: */
168: private void codeGenerate(String wsdlFile, String outputLocation)
169: throws CodeGenerationException {
170: //create the option map
171: Map optionMap = fillOptionMap(wsdlFile, outputLocation);
172: CommandLineOptionParser parser = new CommandLineOptionParser(
173: optionMap);
174: new CodeGenerationEngine(parser).generate();
175: }
176:
177: /** @param outputLocation */
178: private void compile(String outputLocation) {
179: String cp = null;
180: try {
181: BufferedReader br = new BufferedReader(new FileReader(
182: System.getProperty("basedir", ".")
183: + "/target/cp.txt"));
184: cp = br.readLine();
185: } catch (Exception e) {
186: // Don't care
187: }
188: if (cp == null) {
189: cp = "";
190: }
191:
192: //using the ant javac task for compilation
193: Javac javaCompiler = new Javac();
194: Project codeGenProject = new Project();
195: Target compileTarget = new Target();
196:
197: compileTarget.setName(COMPILE_TARGET_NAME);
198: compileTarget.addTask(javaCompiler);
199: codeGenProject.addTarget(compileTarget);
200: codeGenProject.setSystemProperties();
201: javaCompiler.setProject(codeGenProject);
202: javaCompiler.setIncludejavaruntime(true);
203: javaCompiler.setIncludeantruntime(true);
204:
205: /*
206: This harmless looking setFork is actually very important. unless the compiler is
207: forked it wont work!
208: */
209: javaCompiler.setFork(true);
210:
211: //Create classpath - The generated output directories also become part of the classpath
212: //reason for this is that some codegenerators(XMLBeans) produce compiled classes as part of
213: //generated artifacts
214: File outputLocationFile = new File(outputLocation);
215: Path classPath = new Path(codeGenProject, outputLocation);
216: classPath.addExisting(classPath.concatSystemClasspath(), false);
217: for (int i = 0; i < moduleNames.length; i++) {
218: classPath.add(new Path(codeGenProject, MODULE_PATH_PREFIX
219: + moduleNames[i] + CLASSES_DIR));
220: }
221:
222: classPath.add(new Path(codeGenProject, cp));
223:
224: javaCompiler.setClasspath(classPath);
225:
226: //set sourcePath - The generated output directories also become part of the sourcepath
227: Path sourcePath = new Path(codeGenProject, outputLocation);
228: sourcePath.setLocation(outputLocationFile);
229: javaCompiler.setSrcdir(sourcePath);
230:
231: //output the classes into the output dir as well
232: javaCompiler.setDestdir(outputLocationFile);
233: javaCompiler.setVerbose(true);
234: javaCompiler.execute();
235:
236: }
237:
238: /**
239: *
240: */
241: private Map fillOptionMap(String wsdlFileName, String outputLocation) {
242: Map optionMap = new HashMap();
243: optionMap
244: .put(
245: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_LOCATION_URI_OPTION,
246: new CommandLineOption(
247: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_LOCATION_URI_OPTION,
248: new String[] { wsdlFileName }));
249:
250: //use default sync option - No option is given
251: //use default async option - No option is given
252: //use default language option - No option is given
253: //output location - code_gen_output
254:
255: optionMap
256: .put(
257: CommandLineOptionConstants.WSDL2JavaConstants.OUTPUT_LOCATION_OPTION,
258: new CommandLineOption(
259: CommandLineOptionConstants.WSDL2JavaConstants.OUTPUT_LOCATION_OPTION,
260: new String[] { outputLocation }));
261: //server side option is on
262: optionMap
263: .put(
264: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_CODE_OPTION,
265: new CommandLineOption(
266: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_CODE_OPTION,
267: new String[0]));
268: // descriptor option is on
269: optionMap
270: .put(
271: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_SERVICE_DESCRIPTION_OPTION,
272: new CommandLineOption(
273: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_SERVICE_DESCRIPTION_OPTION,
274: new String[0]));
275: // db is xmlbeans option is on
276: optionMap
277: .put(
278: CommandLineOptionConstants.WSDL2JavaConstants.DATA_BINDING_TYPE_OPTION,
279: new CommandLineOption(
280: CommandLineOptionConstants.WSDL2JavaConstants.DATA_BINDING_TYPE_OPTION,
281: new String[] { TestConstants.Databinding.XML_BEANS }));
282:
283: optionMap
284: .put(
285: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_ALL_OPTION,
286: new CommandLineOption(
287: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_ALL_OPTION,
288: new String[0]));
289:
290: //todo Make this work
291: //test case option is on
292: // optionMap.put(
293: // CommandLineOptionConstants.GENERATE_TEST_CASE_OPTION,
294: // new CommandLineOption(
295: // CommandLineOptionConstants.GENERATE_TEST_CASE_OPTION,
296: // new String[0]));
297: //databinding is default
298:
299: return optionMap;
300: }
301:
302: }
|