public interface CmdlineContext
CmdlineContext
provides and abstraction for testing command line interfaces. Class
DefaultCmdlineContext
implements default functionality and is injected as a service by this compilation unit.
Tests should replace the injected service or inject the test time
Please note that System.exit(int)
is not delegated by this interface since execution of finally
blocks would depend on the actual implementation.Modifier and Type | Interface and Description |
---|---|
static class |
CmdlineContext.ExitWithNonZeroStatus
Expected exception when
exit(int) invoked with non-zero status. |
static class |
CmdlineContext.ExitWithZeroStatus
Expected exception when
exit(int) invoked with zero status. |
Modifier and Type | Method and Description |
---|---|
PrintStream |
err()
Error stream, usually mapped to
System.err . |
void |
exit(int status)
Terminate execution with given status code.
|
InputStream |
in()
Input stream, usually mapped to
System.in . |
boolean |
isFileExists(String name)
Check if a file exists.
|
InputStream |
openFileInput(String name)
Open a file for reading.
|
OutputStream |
openFileOutput(String name)
Open a file for writing.
|
InputStream |
openUrlStream(String spec)
Open a connection to an URL.
|
PrintStream |
out()
Output stream, usually mapped to
System.out . |
SeekableFileOutput |
seekableFileOutput(String name)
Open a seekable file for writing.
|
PrintStream out()
System.out
.PrintStream err()
System.err
.InputStream in()
System.in
.boolean isFileExists(String name)
File.exists()
of an instance created by File.File(java.lang.String)
.name
- File name to checktrue
when given file existsInputStream openUrlStream(String spec) throws IOException
URL.openStream()
of an instance created by URL.URL(java.lang.String)
.spec
- The String
to parse as an URL.IOException
- propagatedInputStream openFileInput(String name) throws FileNotFoundException
FileInputStream.FileInputStream(java.lang.String)
.name
- File name to openFileNotFoundException
OutputStream openFileOutput(String name) throws FileNotFoundException
FileOutputStream.FileOutputStream(java.lang.String)
.name
- File name to openFileNotFoundException
SeekableFileOutput seekableFileOutput(String name) throws FileNotFoundException
name
- File name to openFileNotFoundException
void exit(int status) throws CmdlineContext.ExitWithZeroStatus, CmdlineContext.ExitWithNonZeroStatus
System.exit(int)
. Test time implementations might decide to throw
CmdlineContext.ExitWithZeroStatus
or CmdlineContext.ExitWithNonZeroStatus
instead of delegating to System.exit(int)
.
These RuntimeException
s are helps for test implementations, regular application code must not catch them.status
- Status code; by convention nonzero status code indicates abnormal terminationCmdlineContext.ExitWithZeroStatus
- Test time implementation might throw this instead of delegating to
System.exit(int)
Note that normal application code must not catch this.CmdlineContext.ExitWithNonZeroStatus
- Test time implementation might throw this instead of delegating to
System.exit(int)
Note that normal application code must not catch this.