3

I want to do some tests on my P2P system by using some input like: "join 8". 8 is the Node number. For my system, the command "join 8" is read from stdin, but I don't want to type it hundred times for hundred tests, so I write a test function to randomly generate node numbers and then call the "join" command by itself. So I want JAVA to write commands instead of my own input to stdin. How can I do that? The goal is: When I input "test join 3", the code should randomly generate 3 node numbers between 1-255 and call join commands for them. My code doesn't really work now:

if (command[0].equals("test")) {
            //test join
            if (command[1].equals("join")) {
                int nodenum = Integer.parseInt(command[2]);
                Random rand = new Random();
                Set<Integer> generated = new LinkedHashSet<Integer>();
                while (generated.size() < nodenum) {
                    Integer next = rand.nextInt(255) + 1;
                    generated.add(next);
                    ProcessBuilder builder = new ProcessBuilder("java", "Test");
                    Process process = builder.start();
                    //stdIn=new BufferedReader(new InputStreamReader("join"));
                    OutputStream stdin = process.getOutputStream();
                    InputStream stdout = process.getInputStream();


                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
                    writer.write("join "+next);
                    writer.flush();
                    writer.close();

                }


            }

        }
leoliu1994
  • 31
  • 1
  • 2
  • 3
    You cannot _write_ to stdin by definition... – fge Apr 13 '15 at 21:28
  • 1
    A better method would be to rewrite your code so that the input is handled in a separate class, which you can substitute for a dummy class that can be used for automatic testing. – Sentry Apr 13 '15 at 22:32

2 Answers2

9

Java System class offer you a method to set the input stream of System.in.

The method is called setIn and allow you to re-assign the standard input.


Edit

Here an example of how you can do it :

InputStream fakeIn = new ByteArrayInputStream(dataYourWantToPassAsBytesArray);
System.setIn(fakeIn);
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
  • 4
    I am a bit puzzled by the sentence `writer to it as you would normally do on any inputstream`. As far as I know, `InputStream` is only meant for reading, not for writing. Although I get the idea on how you could achieve the desired effect. – Guillaume Polet Apr 13 '15 at 21:28
  • @GuillaumePolet Good comment, that's not really what I meant I guess I typed too fast. Anyway, edited. – Jean-François Savard Apr 13 '15 at 21:36
0

It looks like you want to look at https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html.

It lets you create the data the inputStream will produce.

e.g.

public void doTestStreamStuff(){
    byte[] testData = ....
    InputStream is = new ByteArrayInputStream(testData);
    while(doStuff){
        int i = is.read();
        ...
    }
}
BevynQ
  • 8,089
  • 4
  • 25
  • 37