Skip to content
Permalink
Browse files
Add failing FVD test
  • Loading branch information
ngmr committed Aug 3, 2015
1 parent 4fb8a47 commit ffe65d6a2a1836c61a8deb9c4c7675610a23c154
Showing 19 changed files with 1,130 additions and 48 deletions.
@@ -37,23 +37,23 @@
* It also sets the java.endorsed.dirs property and launches the client process.
*
* Currently, the client waits for the server to create a file containing an IOR. This
* is used to delay the client until the server has started. the setWaitFile(File) method
* is used to delay the client until the server has started. the setWaitFile(File) method
* can be used to set the name of this file, which varies in the ORB tests.
*/
public class AbstractOrbTestBase extends TestCase {
protected ProcessManager processManager;
protected JavaProcess server, client;
protected File waitForFile;
int waitForFileTimeout = 10000;

public AbstractOrbTestBase() {
super();
}

public AbstractOrbTestBase(String name) {
super(name);
}

protected void setUp() throws Exception {
super.setUp();
processManager = new ProcessManager(Registry.REGISTRY_PORT);
@@ -84,15 +84,15 @@ public void tearDown() throws Exception {
getWaitForFile().delete();
}
}

protected void runServerClientTest(Class<?> serverClass, Class<?> clientClass, String...commonArgs) throws Exception {
runServerClientTest(serverClass.getName(), commonArgs, clientClass.getName(), commonArgs);
}

protected void runServerClientTest(String serverClass, String clientClass) throws Exception {
runServerClientTest(serverClass, new String[0], clientClass, new String[0]);
}
protected void runServerClientTest(String serverClass, String[] serverArgs,
protected void runServerClientTest(String serverClass, String[] serverArgs,
String clientClass, String[] clientArgs) throws Exception {
server.launch();
Future<Void> serverFuture = server.invokeMainAsync(serverClass, serverArgs);
@@ -108,34 +108,38 @@ protected void runServerClientTest(String serverClass, String[] serverArgs,
}
server.exit(0);
}

public void setWaitForFile(String file) {
this.waitForFile = new File(file);
}

public File getWaitForFile() {
public final File getWaitForFile() {
return waitForFile;
}

protected void waitForFile() {
File file = getWaitForFile();
if(file != null) {
waitFor(file, waitForFileTimeout);
}
}

public static void waitFor(File file, int timeout) throws Error {
long timeBefore = System.currentTimeMillis();
if(getWaitForFile() != null) {
while(true) {
do {
try {
if(getWaitForFile().exists()) {
if(file.exists()) {
break;
}
Thread.sleep(50);
if(System.currentTimeMillis() > timeBefore + waitForFileTimeout) {
fail("The file " + getWaitForFile() +
" was not created within " + waitForFileTimeout + "ms");
if(System.currentTimeMillis() > timeBefore + timeout) {
fail("The file " + file + " was not created within " + timeout + "ms");
}
}
catch(InterruptedException e) {
throw new Error(e);
}
getWaitForFile().deleteOnExit();
}
}
}
} while(true);
file.deleteOnExit();
}
}
@@ -0,0 +1,35 @@
/**
*
* 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.
*/

/**
* @version $Rev: 491396 $ $Date: 2006-12-30 22:06:13 -0800 (Sat, 30 Dec 2006) $
*/
package org.apache.yoko;

import static test.fvd.Marshalling.VERSION1;
import static test.fvd.Marshalling.VERSION2;
import test.fvd.MissingFieldsClient;
import test.fvd.MissingFieldsServer;

public class FVDTest extends AbstractOrbTestBase {
public void testMissingFields() throws Exception {
final String refFile = "missingfields.ref";
setWaitForFile(refFile);
runServerClientTest(MissingFieldsServer.class, MissingFieldsClient.class, refFile, VERSION1.name(), VERSION2.name());
}
}
@@ -18,11 +18,19 @@
package test.common;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.rmi.Remote;

import javax.rmi.PortableRemoteObject;

import org.omg.CORBA.ORB;
import org.omg.CORBA.portable.IDLEntity;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextPackage.InvalidName;
@@ -43,14 +51,20 @@ public static org.omg.CORBA.TypeCode getOrigType(org.omg.CORBA.TypeCode tc) {

protected static void writeRef(ORB orb, PrintWriter out, org.omg.CORBA.Object obj,
NamingContextExt context, NameComponent[] name) throws InvalidName {
writeRef(orb, out, obj, context.to_string(name));
}

private static void writeRef(ORB orb, PrintWriter out, org.omg.CORBA.Object obj, String name) {
out.println("ref:");
String ref = orb.object_to_string(obj);
out.println(ref);
String nameString = context.to_string(name);
out.println(nameString);
out.println(orb.object_to_string(obj));
out.println(name);
}

protected static void writeRef(ORB orb, PrintWriter out, org.omg.CORBA.Object obj) {
writeRef(orb, out, obj, "");
}

protected static void readRef(BufferedReader reader, String[] refStrings) throws IOException {
protected static String[] readRef(BufferedReader reader, String[] refStrings) throws IOException {
String line = reader.readLine();
if (line == null) {
throw new RuntimeException("Unknown Server error");
@@ -65,8 +79,35 @@ protected static void readRef(BufferedReader reader, String[] refStrings) throws
throw new RuntimeException(sw.toString());
}
}
refStrings[0] = reader.readLine(); // IOR
refStrings[1] = reader.readLine(); // name
System.out.println(refStrings[1] + " = "+ refStrings[0]);
refStrings[0] = reader.readLine();
refStrings[1] = reader.readLine();
return refStrings;
}

private static org.omg.CORBA.Object readGenericStub(ORB orb, BufferedReader reader) throws IOException {
return orb.string_to_object(readRef(reader, new String[2])[0]);
}

@SuppressWarnings("unchecked")
protected static<T extends Remote> T readRmiStub(ORB orb, BufferedReader reader, Class<T> type) throws ClassCastException, IOException {
return (T)PortableRemoteObject.narrow(readGenericStub(orb, reader), type);
}

@SuppressWarnings("unchecked")
protected static<T extends IDLEntity> T readIdlStub(ORB orb, BufferedReader reader, Class<T> type) throws ClassCastException, IOException {
return (T)PortableRemoteObject.narrow(readGenericStub(orb, reader), type);
}

protected static BufferedReader openFileReader(final String refFile) throws FileNotFoundException {
return new BufferedReader(new FileReader(refFile)) {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
Files.delete(Paths.get(refFile));
}
}
};
}
}
@@ -0,0 +1,7 @@
package test.fvd;

import java.io.Serializable;

public interface Abstract extends Serializable {

}
@@ -0,0 +1,87 @@
package test.fvd;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLClassLoader;
import java.util.HashSet;
import java.util.Set;

public final class ApeClassLoader extends URLClassLoader {
private final Set<String> skipClassNames = new HashSet<>();

public ApeClassLoader() {
super(((URLClassLoader)ApeClassLoader.class.getClassLoader()).getURLs(),
ApeClassLoader.class.getClassLoader().getParent());
}

public ApeClassLoader doNotLoad(Class<?>... types) {
for (Class<?> type : types)
skipClassNames.add(type.getName());
return this;
}

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
if (skipClassNames.contains(name))
throw new ClassNotFoundException(name);
return super.findClass(name);
}

public boolean apeMain(String...args) {
if (alreadyAped()) {
System.out.println("invoked from an already aped main() in target loader " + this.getClass().getClassLoader());
return false;
}
System.out.println("aping call to main() from class loader " + this.getClass().getClassLoader());
invokeApedMain(args);
return true;
}

private static boolean alreadyAped() {
String expectedLoader = ApeClassLoader.class.getName();
String actualLoader = ApeClassLoader.class.getClassLoader().getClass().getName();
return expectedLoader.equals(actualLoader);
}

private void invokeApedMain(String...args) {
final String className = getCallerClassName();
final ClassLoader oldTCCL = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this);
try {
Class<?> targetClass = Class.forName(className, true, this);
Method m = targetClass.getMethod("main", String[].class);
// m.invoke(null, (Object[])args); // BAD
m.invoke(null, (Object)args); // GOOD
} catch (ClassNotFoundException e) {
throw new Error("Failed to load mirrored class " + className, e);
} catch (NoSuchMethodException | SecurityException e) {
throw new Error("Failed to get method main(String[]) for mirrored class " + className, e);
} catch (IllegalAccessException | IllegalArgumentException e) {
throw new Error("Failed to invoke method main(String[]) for mirrored class" + className, e);
} catch (InvocationTargetException e) {
rethrow(e.getTargetException());
throw new AssertionError("This code should be unreachable");
} finally {
Thread.currentThread().setContextClassLoader(oldTCCL);
}
}

private static void rethrow(Throwable t) throws RuntimeException {
ApeClassLoader.<RuntimeException>useTypeErasureMadnessToThrowAnyCheckedException(t);
}

@SuppressWarnings("unchecked")
private static <T extends Throwable> void useTypeErasureMadnessToThrowAnyCheckedException(Throwable t) throws T {
throw (T)t;
}

private static String getCallerClassName() {
StackTraceElement[] frames = new Throwable().getStackTrace();
int i = 0;
// find this class in the stack
while (!!!frames[i].getClassName().equals(ApeClassLoader.class.getName())) i++;
// find the next class down in the stack
while (frames[i].getClassName().equals(ApeClassLoader.class.getName())) i++;
return frames[i].getClassName();
}
}
@@ -0,0 +1,5 @@
package test.fvd;

public interface Bounceable extends Abstract, Value {
Bounceable validateAndReplace();
}

0 comments on commit ffe65d6

Please sign in to comment.