Mercurial > hg > Members > nobuyasu > TestJava
changeset 2:b101ee77e78c draft
add testKonoha
author | one |
---|---|
date | Tue, 29 May 2012 00:46:14 +0900 |
parents | 830afc6475b0 |
children | 178aeb91d108 |
files | src/testKonoha/KonohaException.java src/testKonoha/KonohaInvoker.java src/testKonoha/SystemTest.java |
diffstat | 3 files changed, 192 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/testKonoha/KonohaException.java Tue May 29 00:46:14 2012 +0900 @@ -0,0 +1,8 @@ +package testKonoha; + +public class KonohaException extends Exception { + public KonohaException(String str) { + super(str); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/testKonoha/KonohaInvoker.java Tue May 29 00:46:14 2012 +0900 @@ -0,0 +1,86 @@ +package testKonoha; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class KonohaInvoker { + + final static String KONOHA = "/usr/local/bin/konoha"; + + InputStream in, errin; + + void runKonoha(String[] cmd) throws KonohaException { + try { + Runtime run = Runtime.getRuntime(); + Process p = run.exec(cmd); + in = p.getInputStream(); + errin = p.getInputStream(); + + BufferedReader br = new BufferedReader(new InputStreamReader(in)); + BufferedReader ebr = new BufferedReader( + new InputStreamReader(errin)); + + String kout = readMessege(br); + String kerr = readMessege(ebr); + p.waitFor(); + + System.out.println(kout); + System.out.println(kerr); + + if (kerr != null) { + throw new KonohaException(kout); + } + + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + try { + in.close(); + errin.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + void invoke(String filename, String message) throws KonohaException { + String[] cmd = {KONOHA, filename, message}; + runKonoha(cmd); + } + void invoke(String[] messages) throws KonohaException { + int num = messages.length + 1; + String[] cmd = new String[num]; + cmd[0] = KONOHA; + for (int i=0; i < messages.length; i++) { + cmd[i+1] = messages[i]; + } + runKonoha(cmd); + } + + String readMessege(BufferedReader br) throws IOException { + String str = ""; + String line = null; + while ((line = br.readLine()) != null) { + str = str + line + "\n"; + } + return str; + + } + + public static void main(String[] args) { + KonohaInvoker konoha = new KonohaInvoker(); + String filename = "/Users/aotokage/hg/nobuyasu/konoha/test/math.k"; + String message = ""; + try { + konoha.invoke(filename, message); + } catch (KonohaException e) { + System.out.println(e); + } + + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/testKonoha/SystemTest.java Tue May 29 00:46:14 2012 +0900 @@ -0,0 +1,98 @@ +package testKonoha; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class SystemTest { + + private static InputStream in = null; + private static InputStream ein = null; + static BufferedReader ebr = null; + static BufferedReader br = null; + + public static void main(String[] argv) { + +// String[] cmd = {"/usr/local/bin/konoha","/Users/aotokage/testProgram/konoha/math.k"}; +// String[] cmd = {"/usr/local/bin/konoha","math.k"}; + String[] cmd = {"konoha","math.k"}; + Runtime run = Runtime.getRuntime(); + try { + Process p = run.exec(cmd); + in = p.getInputStream(); + ein = p.getErrorStream(); + + + Runnable inputStreamThread = new Runnable() { + public void run() { + String line; + try { + System.out.println("Thread stdRun start"); + br = new BufferedReader(new InputStreamReader(in)); + while ((line = br.readLine()) != null) { + System.out.println(line); + } + System.out.println("Thread stdRun end"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + }; + + Runnable errStreamThread = new Runnable() { + public void run() { + try { + System.out.println("Thread errRun start"); + ebr = new BufferedReader(new InputStreamReader(ein)); + String errLine; + while((errLine = ebr.readLine()) != null) { + System.out.println(errLine); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + }; + + Thread stdRun = new Thread(inputStreamThread); + Thread errRun = new Thread(errStreamThread); + stdRun.start(); + errRun.start(); + + int c = p.waitFor(); + + stdRun.join(); + errRun.join(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + in.close(); + ein.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + public static void printInputStream(InputStream in) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(in)); + try { + for (;;) { + String line = br.readLine(); + if (line == null) break; + System.out.println(line); + } + + } finally { + br.close(); + } + + } + + +}