|
| 1 | +package org.apache.commons.cli2.commandline; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.PrintWriter; |
| 6 | +import java.io.StringReader; |
| 7 | +import java.io.StringWriter; |
| 8 | + |
| 9 | +import org.apache.commons.cli2.CommandLine; |
| 10 | +import org.apache.commons.cli2.Group; |
| 11 | +import org.apache.commons.cli2.OptionException; |
| 12 | +import org.apache.commons.cli2.builder.DefaultOptionBuilder; |
| 13 | +import org.apache.commons.cli2.builder.GroupBuilder; |
| 14 | +import org.apache.commons.cli2.option.DefaultOption; |
| 15 | +import org.apache.commons.cli2.util.HelpFormatter; |
| 16 | + |
| 17 | +import junit.framework.TestCase; |
| 18 | + |
| 19 | +public class ParserTest extends TestCase { |
| 20 | + |
| 21 | + private Parser parser; |
| 22 | + private DefaultOption verboseOption; |
| 23 | + private DefaultOption helpOption; |
| 24 | + private Group options; |
| 25 | + private HelpFormatter helpFormatter; |
| 26 | + private StringWriter out; |
| 27 | + private BufferedReader in; |
| 28 | + |
| 29 | + public void setUp() { |
| 30 | + parser = new Parser(); |
| 31 | + |
| 32 | + final GroupBuilder gBuilder = new GroupBuilder(); |
| 33 | + final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder(); |
| 34 | + |
| 35 | + helpOption = oBuilder.withLongName("help").withShortName("h").create(); |
| 36 | + verboseOption = oBuilder.withLongName("verbose").withShortName("v").create(); |
| 37 | + options = gBuilder.withOption(helpOption).withOption(verboseOption).create(); |
| 38 | + parser.setGroup(options); |
| 39 | + |
| 40 | + helpFormatter = new HelpFormatter(); |
| 41 | + out = new StringWriter(); |
| 42 | + helpFormatter.setPrintWriter(new PrintWriter(out)); |
| 43 | + parser.setHelpFormatter(helpFormatter); |
| 44 | + } |
| 45 | + |
| 46 | + public void testParse_Successful() throws OptionException { |
| 47 | + final CommandLine cl = parser.parse(new String[]{"-hv"}); |
| 48 | + |
| 49 | + assertTrue(cl.hasOption(helpOption)); |
| 50 | + assertTrue(cl.hasOption(verboseOption)); |
| 51 | + |
| 52 | + assertEquals("--help --verbose",cl.toString()); |
| 53 | + |
| 54 | + final WriteableCommandLineImpl wcli = (WriteableCommandLineImpl)cl; |
| 55 | + assertEquals("[--help, --verbose]",wcli.getNormalised().toString()); |
| 56 | + } |
| 57 | + |
| 58 | + public void testParse_WithUnexpectedOption() { |
| 59 | + try { |
| 60 | + parser.parse(new String[]{"--unexpected"}); |
| 61 | + fail("OptionException"); |
| 62 | + } |
| 63 | + catch(OptionException e) { |
| 64 | + assertEquals(options,e.getOption()); |
| 65 | + assertEquals("Unexpected --unexpected while processing --help|--verbose",e.getMessage()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public void testParseAndHelp_Successful() throws IOException { |
| 70 | + final CommandLine cl = parser.parseAndHelp(new String[]{"-v"}); |
| 71 | + |
| 72 | + assertTrue(cl.hasOption(verboseOption)); |
| 73 | + assertEquals("",out.getBuffer().toString()); |
| 74 | + } |
| 75 | + |
| 76 | + public void testParseAndHelp_ByHelpOption() throws IOException { |
| 77 | + parser.setHelpOption(helpOption); |
| 78 | + |
| 79 | + assertNull(parser.parseAndHelp(new String[]{"-hv"})); |
| 80 | + |
| 81 | + inReader(); |
| 82 | + assertInReaderUsage(); |
| 83 | + assertInReaderEOF(); |
| 84 | + } |
| 85 | + |
| 86 | + public void testParseAndHelp_ByHelpTrigger() throws IOException { |
| 87 | + parser.setHelpTrigger("--help"); |
| 88 | + |
| 89 | + assertNull(parser.parseAndHelp(new String[]{"-hv"})); |
| 90 | + |
| 91 | + inReader(); |
| 92 | + assertInReaderUsage(); |
| 93 | + assertInReaderEOF(); |
| 94 | + } |
| 95 | + |
| 96 | + public void testParseAndHelp_WithUnexpectedOption() throws IOException { |
| 97 | + assertNull(parser.parseAndHelp(new String[]{"--unexpected"})); |
| 98 | + |
| 99 | + inReader(); |
| 100 | + assertInReaderLine("Unexpected --unexpected while processing --help|--verbose"); |
| 101 | + assertInReaderUsage(); |
| 102 | + assertInReaderEOF(); |
| 103 | + } |
| 104 | + |
| 105 | + private void assertInReaderUsage() throws IOException { |
| 106 | + assertInReaderLine("Usage:"); |
| 107 | + assertInReaderLine("[--help --verbose]"); |
| 108 | + assertInReaderLine("--help|--verbose"); |
| 109 | + assertInReaderLine("--help (-h)"); |
| 110 | + assertInReaderLine("--verbose (-v)"); |
| 111 | + } |
| 112 | + |
| 113 | + private void assertInReaderLine(final String string) throws IOException { |
| 114 | + assertEquals(string,in.readLine().trim()); |
| 115 | + } |
| 116 | + |
| 117 | + private void assertInReaderEOF() throws IOException { |
| 118 | + assertNull(in.readLine()); |
| 119 | + } |
| 120 | + |
| 121 | + private void inReader() { |
| 122 | + in = new BufferedReader(new StringReader(out.getBuffer().toString())); |
| 123 | + } |
| 124 | +} |
0 commit comments