How to paste or pipe input to a Java command line program?
March 10, 2009 11:59 AM Subscribe
I have written a simple little Java program which runs on the command line, displays a text menu, and takes user input. I would like to feed it a very large amount of test input--as though an actual user were sitting there typing in commands all day. I am using Windows, but have access to the basic *nix utilities such as cat. "cat file.txt | java Program" doesn't work, nor does starting the program and using the context menu to paste in several lines of data.
The menu looks like this:
enter a value: e add: a
subtract: s multiply: m
reverse sign: r clear: c
quit: q
->
(only more formatted). After entering an option letter followed by the enter key, it prompts you for a value (value: ). I'd ultimately like to calculate 1000! as a test problem, so input would be like this:
e
1000
m
999
m
998
m
997
...
The menu looks like this:
enter a value: e add: a
subtract: s multiply: m
reverse sign: r clear: c
quit: q
->
(only more formatted). After entering an option letter followed by the enter key, it prompts you for a value (value: ). I'd ultimately like to calculate 1000! as a test problem, so input would be like this:
e
1000
m
999
m
998
m
997
...
Thinking on this a little bit more, have you considered using JUnit to do this? Instead of driving your program end-to-end from the user interface, you can test each individual method, including the user interface (which you will have to harness for non-TTY input as above), and then run the test suite to gain assurance about all components of your program. This also lets you write tests in Java instead of text or bash, which will be nice. You can, for example, test your factorial function on all numbers up to 1000 in a for() loop, rather than having to write them out.
posted by doteatop at 12:09 PM on March 10, 2009
posted by doteatop at 12:09 PM on March 10, 2009
With bash you don't need expect for this, you can just feed it a heredoc.
posted by dhoe at 12:32 PM on March 10, 2009
posted by dhoe at 12:32 PM on March 10, 2009
thirding Expect. A lot of professional QA guys I know use it in their testing scripts.
posted by xbonesgt at 1:46 PM on March 10, 2009
posted by xbonesgt at 1:46 PM on March 10, 2009
How are you currently reading user input from the keyboard? doteatop is right that if you are using System.in then your cat pipeline should work. I don't think expect is going to help you.
posted by Nelson at 3:05 PM on March 10, 2009
posted by Nelson at 3:05 PM on March 10, 2009
Let me be the first to suggest that you might want to refactor to use unit testing internal to your Java app -- use something like JUnit, and write test cases that provide the data. It's something that you could reproduce on any platform, and could automatically run as part of your build process every time you make a change to the app.
posted by delfuego at 5:21 PM on March 10, 2009
posted by delfuego at 5:21 PM on March 10, 2009
Response by poster: For whatever reason it's now working using plain old Windows redirectors like | and <>
Anyway, thanks everyone, Expect looks really interesting for when I have a couple of days to dig into it.>
posted by anaelith at 3:47 AM on March 11, 2009
Anyway, thanks everyone, Expect looks really interesting for when I have a couple of days to dig into it.>
posted by anaelith at 3:47 AM on March 11, 2009
This thread is closed to new comments.
Googling around, an example of someone doing something like this is here. You'll probably need to change your existing code so you can parameterize the input source, but maybe if you are using a kind of InputStream to read the user input, it won't be that hard.
posted by doteatop at 12:04 PM on March 10, 2009