hardy
hardy

Reputation: 49

how can we simulate Ctrl+C to stop the console app from waiting for user input in java

I am trying to test a command line app that waits for the user input after every step. I am able to test the app using System Rules provided by Stefan Birkner. Currently, I provide inputs from the beginning to the end which works like a charm and I can assert the final output from system log.

However, I want to test for the negative cases before the end of the app for which I give invalid inputs in the beginning to evaluate the error message. When invalid inputs are given, the console prints an error message and keeps waiting for the user to provide a valid input. How do I send Ctrl+C using as shown below:

systemInMock.provideLines(Ctrl+C);

systemInMock.provideLines accepts only strings. Is there a way to send Ctrl+C signal?

An example of my junit test is shown below:

@Test
public void testInValidMarker() throws Exception{
    systemInMock.provideLines("abc","def","1");
    Main.main(new String[]{});
    assertTrue(systemOutRule.getLog().contains("Invalid marker, try again"));
}

Appreciate your help!

Upvotes: 3

Views: 1827

Answers (3)

Ishnark
Ishnark

Reputation: 681

If I'm not mistaken, when you do ctrl+c, it doesn't actually get written to console. If that's true, then in no case will your program ever be given ctrl+c, so provideLines will never be in a position where it is given ctrl+c.

For proof, open up cmd and type in a program with program arguments (in my case, I use ant). If you type ant and then ctrl+c, the cursor is moved to a new line.

There are two ways you can control termination behavior:

  1. You can use a shutdown hook (found from this previously asked question ). Doing this will allow you to handle what happens (potentially with issues).
  2. Or you could create your own termination argument like -q or q, which would trigger an action to end the program (maybe a System.exit(1)). This way you can mock that input.

Upvotes: 2

slim
slim

Reputation: 41271

In UNIX/Linux, when you type CTRL-C, your shell intercepts it and sends the process a SIGINT signal -- see: How does Ctrl-C terminate a child process?

Therefore the System Rules project doesn't have anything to help you -- in this situation the process doesn't receive any character input.

By default, the whole JVM shuts down when it receives SIGINT. This is obviously bad news for a running test.

The SO question Signal handling using "TERM" -- may be of use.

A side effect of Java's portability is that for some OS features, it either abstracts things away until they're unrecognisable, or doesn't expose them at all.

I suspect what you're asking for can't be achieved.

If you're allowed to change the requirements slightly, you could ask the user to close with CTRL-D -- this closes stdin with EOF.

Although it's quite the overkill, you could launch a whole new JVM running your program, using ProcessBuilder. You might imagine you'd get an API to send arbitrary signals to that process. But for portability reasons, all you can do is process.destroy(), which sends SIGTERM.

Upvotes: 0

Bill K
Bill K

Reputation: 62789

Tried this as a comment, but it didn't read right. It's not exactly an "Answer" though.

So Java is really bad at console input. It reads an entire line at once and you can't do anything about it--there is no way to trap special characters or even see any of the input before the user hits return. Also I think a ctrl-d will close your input session--(Add that test to your use case if you don't use any other suggestion here because it can put you into a state you didn't expect!)

Three suggestions:

The simplest: If you can use a GUI and aren't really looking for an ongoing input/response REPL the simplest answer is usually to use JOptionPane to throw up a quick dialog. It's a one-line solution to get some user input, but not so good for an ongoing command-driven system.

If you can't use swing (If you are running headless) then you may have little choice, but you can use the JLine library. That will give you a lot more flexibility. This is how Groovysh does it's REPL. It will let you see each character as it is typed and do things like completion where a user might type part of a file name and hit tab and you put the rest in for him.

If you don't want to use JLine but want a REPL feel there is also a more complex GUI solution--create a swing console window. A trivial solution would just be a text input box to allow typing and a text area to display results, but there are certainly libraries out there with more complete console solutions.

The point here is that using Java standard input alone is just not a good solution for anything beyond a trivial/personal script--and even then I avoid it. Perhaps not the answer you asked for, but maybe it's the one you need :)

Upvotes: -1

Related Questions