/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.yoko.tools;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.security.Permission;
import org.apache.yoko.tools.common.ToolTestBase;
import org.apache.yoko.tools.utils.TestUtils;
public class IDLToWSDLTest extends ToolTestBase {
private static StringBuffer usageBuf;
private static int noError;
private static int error = -1;
ByteArrayOutputStream bout;
PrintStream newOut;
private File output;
public void setUp() {
super.setUp();
try {
TestUtils utils = new TestUtils(IDLToWSDL.TOOL_NAME, IDLToWSDL.class
.getResourceAsStream("/toolspecs/idl2wsdl.xml"));
usageBuf = new StringBuffer(utils.getUsage());
bout = new ByteArrayOutputStream();
newOut = new PrintStream(bout);
//System.setOut(newOut);
//System.setErr(newOut);
} catch (Exception e) {
// complete
}
try {
URL url = IDLToWSDLTest.class.getResource(".");
output = new File(url.getFile());
output = new File(output, "/resources");
if (!output.exists()) {
output.mkdir();
}
} catch (Exception e) {
// complete
}
}
public void tearDown() {
output.deleteOnExit();
output = null;
}
private int execute(String[] args) {
SecurityManager oldManager = System.getSecurityManager();
try {
SecurityManager newManager = new SecurityManager() {
public void checkPermission(Permission perm) {
if ("exitVM".equals(perm.getName())) {
throw new SecurityException("Exit Not Allowed");
}
}
};
System.setSecurityManager(newManager);
IDLToWSDL.main(args);
} catch (Throwable t) {
return error;
} finally {
System.setSecurityManager(oldManager);
}
return noError;
}
private void checkStrings(byte orig[], byte generated[]) throws Exception {
BufferedReader origReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(orig)));
BufferedReader genReader =
new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(generated)));
String sorig = origReader.readLine();
String sgen = genReader.readLine();
while (sorig != null && sgen != null) {
if (!sorig.equals(sgen)) {
//assertEquals(sorig, sgen);
//sorig = origReader.readLine();
sgen = genReader.readLine();
} else {
assertEquals(sorig, sgen);
sorig = null;
sgen = null;
break;
}
}
}
public void testIDLToWSDL() throws Exception {
String[] cmdArgs = {getClass().getResource("/idl/HelloWorld.idl").toString()};
int exc = execute(cmdArgs);
//assertEquals("IDLToWSDL Failed", noError, exc);
}
public void testNoArgs() throws Exception {
String[] cmdArgs = {};
int exc = execute(cmdArgs);
assertEquals("IDLToWSDL Failed", error, exc);
StringBuffer strBuf = new StringBuffer();
strBuf.append("Missing argument: idl\n\n");
strBuf.append(usageBuf.toString());
checkStrings(strBuf.toString().getBytes(), bout.toByteArray());
}
public void testDetailOutput() throws Exception {
String[] args = new String[] {"-?"};
IDLToWSDL.main(args);
assertNotNull(getStdOut());
}
public void testVersionOutput() throws Exception {
String[] args = new String[] {"-v"};
IDLToWSDL.main(args);
assertNotNull(getStdOut());
}
public void testHelpOutput() throws Exception {
String[] args = new String[] {"-help"};
IDLToWSDL.main(args);
assertNotNull(getStdOut());
}
}