/*
 * a simple test for kaffe.lang.UNIXProcess.
 * This will run on Unixoids only.
 *
 * We spawn a copy of the kaffe JVM which executes this program, sends
 * a message through stdout back to its parent, which is compared.
 *
 * If the output is "readline returning null", that likely means the exec
 * failed.  Check execve/execvp/your path, look in jthread.c
 */
import java.io.*;

public class ProcessTest {

    // note that this will only work if we have execvp()
    public static String kaffe_exe = "Kaffe";
    public static final String childmsg = "Hi, mom";

    public static void main(String args[])
    {
	if (args.length > 0) {
	    // child
	    System.out.println(args[0]);
	} else {

	    // parent
	    try {
		Process process = Runtime.getRuntime().exec(
		    new String [] {
			kaffe_exe, "ProcessTest", childmsg
		    });
		InputStream is = process.getInputStream();
		if (is == null)
			throw new Exception("no input stream");

		InputStreamReader isr = new InputStreamReader(is);
		LineNumberReader r = new LineNumberReader(isr);

		String s = r.readLine();
		if (s == null)
			throw new Exception("readline returning null");
		if (!s.equals(childmsg))
		    throw new Exception("child talked back " + s);
		process.waitFor();
		System.out.println("ProcessTest: Success.");

	    } catch (Throwable th) {
		System.out.println("Caught exception: " +th.getMessage());
		th.printStackTrace(System.out);
	    }
	}
    }
}
