Author: ebourg Date: Mon Nov 10 02:42:35 2008 New Revision: 712642 URL: http://svn.apache.org/viewvc?rev=712642&view=rev Log: Moved the tests related to the HelpFormatter in BugsTest into HelpFormatterTest
Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BugsTest.java commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/HelpFormatterTest.java Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BugsTest.java URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BugsTest.java?rev=712642&r1=712641&r2=712642&view=diff ============================================================================== --- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BugsTest.java (original) +++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/BugsTest.java Mon Nov 10 02:42:35 2008 @@ -22,7 +22,6 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.util.Iterator; -import java.util.List; import java.util.Properties; import junit.framework.TestCase; @@ -365,75 +364,6 @@ CommandLine line = parser.parse( options, args ); assertEquals( "Two Words", line.getOptionValue( "m" ) ); } - - public void test27635() - { - Option help = new Option("h", "help", false, "print this message"); - Option version = new Option("v", "version", false, "print version information"); - Option newRun = new Option("n", "new", false, "Create NLT cache entries only for new items"); - Option trackerRun = new Option("t", "tracker", false, "Create NLT cache entries only for tracker items"); - - Option timeLimit = OptionBuilder.withLongOpt("limit") - .hasArg() - .withValueSeparator() - .withDescription("Set time limit for execution, in mintues") - .create("l"); - - Option age = OptionBuilder.withLongOpt("age") - .hasArg() - .withValueSeparator() - .withDescription("Age (in days) of cache item before being recomputed") - .create("a"); - - Option server = OptionBuilder.withLongOpt("server") - .hasArg() - .withValueSeparator() - .withDescription("The NLT server address") - .create("s"); - - Option numResults = OptionBuilder.withLongOpt("results") - .hasArg() - .withValueSeparator() - .withDescription("Number of results per item") - .create("r"); - - Option configFile = OptionBuilder.withLongOpt("config") - .hasArg() - .withValueSeparator() - .withDescription("Use the specified configuration file") - .create(); - - Options mOptions = new Options(); - mOptions.addOption(help); - mOptions.addOption(version); - mOptions.addOption(newRun); - mOptions.addOption(trackerRun); - mOptions.addOption(timeLimit); - mOptions.addOption(age); - mOptions.addOption(server); - mOptions.addOption(numResults); - mOptions.addOption(configFile); - - HelpFormatter formatter = new HelpFormatter(); - final String EOL = System.getProperty("line.separator"); - StringWriter out = new StringWriter(); - formatter.printHelp(new PrintWriter(out),80,"commandline","header",mOptions,2,2,"footer",true); - assertEquals( - "usage: commandline [-a <arg>] [--config <arg>] [-h] [-l <arg>] [-n] [-r <arg>]" + EOL + - " [-s <arg>] [-t] [-v]" + EOL + - "header"+EOL+ - " -a,--age <arg> Age (in days) of cache item before being recomputed"+EOL+ - " --config <arg> Use the specified configuration file"+EOL+ - " -h,--help print this message"+EOL+ - " -l,--limit <arg> Set time limit for execution, in mintues"+EOL+ - " -n,--new Create NLT cache entries only for new items"+EOL+ - " -r,--results <arg> Number of results per item"+EOL+ - " -s,--server <arg> The NLT server address"+EOL+ - " -t,--tracker Create NLT cache entries only for tracker items"+EOL+ - " -v,--version print version information"+EOL+ - "footer"+EOL - ,out.toString()); - } public void test31148() throws ParseException { @@ -452,42 +382,5 @@ assertTrue(cl.hasOption('o')); assertEquals("ovalue",cl.getOptionValue('o')); } - - public void test21215() - { - Options options = new Options(); - HelpFormatter formatter = new HelpFormatter(); - String SEP = System.getProperty("line.separator"); - String header = SEP+"Header"; - String footer = "Footer"; - StringWriter out = new StringWriter(); - formatter.printHelp(new PrintWriter(out),80, "foobar", header, options, 2, 2, footer, true); - assertEquals( - "usage: foobar"+SEP+ - ""+SEP+ - "Header"+SEP+ - ""+SEP+ - "Footer"+SEP - ,out.toString()); - } - - public void test19383() - { - Options options = new Options(); - options.addOption(new Option("a","aaa",false,"aaaaaaa")); - options.addOption(new Option(null,"bbb",false,"bbbbbbb")); - options.addOption(new Option("c",null,false,"ccccccc")); - - HelpFormatter formatter = new HelpFormatter(); - String SEP = System.getProperty("line.separator"); - StringWriter out = new StringWriter(); - formatter.printHelp(new PrintWriter(out),80, "foobar", "", options, 2, 2, "", true); - assertEquals( - "usage: foobar [-a] [--bbb] [-c]"+SEP+ - " -a,--aaa aaaaaaa"+SEP+ - " --bbb bbbbbbb"+SEP+ - " -c ccccccc"+SEP - ,out.toString()); - } } Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/HelpFormatterTest.java URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/HelpFormatterTest.java?rev=712642&r1=712641&r2=712642&view=diff ============================================================================== --- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/HelpFormatterTest.java (original) +++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/HelpFormatterTest.java Mon Nov 10 02:42:35 2008 @@ -30,130 +30,118 @@ * @author Slawek Zachcial * @author John Keyes ( john at integralsource.com ) * @author brianegge - **/ + */ public class HelpFormatterTest extends TestCase { - private static final String EOL = System.getProperty("line.separator"); + private static final String EOL = System.getProperty("line.separator"); - public static void main( String[] args ) - { - String[] testName = { HelpFormatterTest.class.getName() }; - junit.textui.TestRunner.main(testName); - } - - public void testFindWrapPos() throws Exception - { - HelpFormatter hf = new HelpFormatter(); - - String text = "This is a test."; - //text width should be max 8; the wrap postition is 7 - assertEquals("wrap position", 7, hf.findWrapPos(text, 8, 0)); - //starting from 8 must give -1 - the wrap pos is after end - assertEquals("wrap position 2", -1, hf.findWrapPos(text, 8, 8)); - //if there is no a good position before width to make a wrapping look for the next one - text = "aaaa aa"; - assertEquals("wrap position 3", 4, hf.findWrapPos(text, 3, 0)); - } - - public void testPrintWrapped() throws Exception - { - StringBuffer sb = new StringBuffer(); - HelpFormatter hf = new HelpFormatter(); - - String text = "This is a test."; - String expected; - - expected = "This is a" + hf.getNewLine() + "test."; - hf.renderWrappedText(sb, 12, 0, text); - assertEquals("single line text", expected, sb.toString()); - - sb.setLength(0); - expected = "This is a" + hf.getNewLine() + " test."; - hf.renderWrappedText(sb, 12, 4, text); - assertEquals("single line padded text", expected, sb.toString()); - - text = " -p,--period <PERIOD> PERIOD is time duration of form " + - "DATE[-DATE] where DATE has form YYYY[MM[DD]]"; - - sb.setLength(0); - expected = " -p,--period <PERIOD> PERIOD is time duration of" + - hf.getNewLine() + - " form DATE[-DATE] where DATE" + - hf.getNewLine() + - " has form YYYY[MM[DD]]"; - hf.renderWrappedText(sb, 53, 24, text); - assertEquals("single line padded text 2", expected, sb.toString()); - - text = - "aaaa aaaa aaaa" + hf.getNewLine() + - "aaaaaa" + hf.getNewLine() + - "aaaaa"; - - expected = text; - sb.setLength(0); - hf.renderWrappedText(sb, 16, 0, text); - assertEquals("multi line text", expected, sb.toString()); - - expected = - "aaaa aaaa aaaa" + hf.getNewLine() + - " aaaaaa" + hf.getNewLine() + - " aaaaa"; - sb.setLength(0); - hf.renderWrappedText(sb, 16, 4, text); - assertEquals("multi-line padded text", expected, sb.toString()); - } - - public void testPrintOptions() throws Exception - { - StringBuffer sb = new StringBuffer(); - HelpFormatter hf = new HelpFormatter(); - final int leftPad = 1; - final int descPad = 3; - final String lpad = hf.createPadding(leftPad); - final String dpad = hf.createPadding(descPad); - Options options = null; - String expected = null; - - options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa"); - expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa"; - hf.renderOptions(sb, 60, options, leftPad, descPad); - assertEquals("simple non-wrapped option", expected, sb.toString()); - - int nextLineTabStop = leftPad+descPad+"-a".length(); - expected = - lpad + "-a" + dpad + "aaaa aaaa aaaa" + hf.getNewLine() + - hf.createPadding(nextLineTabStop) + "aaaa aaaa"; - sb.setLength(0); - hf.renderOptions(sb, nextLineTabStop+17, options, leftPad, descPad); - assertEquals("simple wrapped option", expected, sb.toString()); - - - options = new Options().addOption("a", "aaa", false, "dddd dddd dddd dddd"); - expected = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd"; - sb.setLength(0); - hf.renderOptions(sb, 60, options, leftPad, descPad); - assertEquals("long non-wrapped option", expected, sb.toString()); - - nextLineTabStop = leftPad+descPad+"-a,--aaa".length(); - expected = - lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.getNewLine() + - hf.createPadding(nextLineTabStop) + "dddd dddd"; - sb.setLength(0); - hf.renderOptions(sb, 25, options, leftPad, descPad); - assertEquals("long wrapped option", expected, sb.toString()); - - options = new Options(). - addOption("a", "aaa", false, "dddd dddd dddd dddd"). - addOption("b", false, "feeee eeee eeee eeee"); - expected = - lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.getNewLine() + - hf.createPadding(nextLineTabStop) + "dddd dddd" + hf.getNewLine() + - lpad + "-b " + dpad + "feeee eeee" + hf.getNewLine() + - hf.createPadding(nextLineTabStop) + "eeee eeee"; - sb.setLength(0); - hf.renderOptions(sb, 25, options, leftPad, descPad); - assertEquals("multiple wrapped options", expected, sb.toString()); - } + public void testFindWrapPos() throws Exception + { + HelpFormatter hf = new HelpFormatter(); + + String text = "This is a test."; + //text width should be max 8; the wrap postition is 7 + assertEquals("wrap position", 7, hf.findWrapPos(text, 8, 0)); + //starting from 8 must give -1 - the wrap pos is after end + assertEquals("wrap position 2", -1, hf.findWrapPos(text, 8, 8)); + //if there is no a good position before width to make a wrapping look for the next one + text = "aaaa aa"; + assertEquals("wrap position 3", 4, hf.findWrapPos(text, 3, 0)); + } + + public void testPrintWrapped() throws Exception + { + StringBuffer sb = new StringBuffer(); + HelpFormatter hf = new HelpFormatter(); + + String text = "This is a test."; + + String expected = "This is a" + hf.getNewLine() + "test."; + hf.renderWrappedText(sb, 12, 0, text); + assertEquals("single line text", expected, sb.toString()); + + sb.setLength(0); + expected = "This is a" + hf.getNewLine() + " test."; + hf.renderWrappedText(sb, 12, 4, text); + assertEquals("single line padded text", expected, sb.toString()); + + text = " -p,--period <PERIOD> PERIOD is time duration of form " + + "DATE[-DATE] where DATE has form YYYY[MM[DD]]"; + + sb.setLength(0); + expected = " -p,--period <PERIOD> PERIOD is time duration of" + + hf.getNewLine() + + " form DATE[-DATE] where DATE" + + hf.getNewLine() + + " has form YYYY[MM[DD]]"; + hf.renderWrappedText(sb, 53, 24, text); + assertEquals("single line padded text 2", expected, sb.toString()); + + text = "aaaa aaaa aaaa" + hf.getNewLine() + + "aaaaaa" + hf.getNewLine() + + "aaaaa"; + + expected = text; + sb.setLength(0); + hf.renderWrappedText(sb, 16, 0, text); + assertEquals("multi line text", expected, sb.toString()); + + expected = "aaaa aaaa aaaa" + hf.getNewLine() + + " aaaaaa" + hf.getNewLine() + + " aaaaa"; + sb.setLength(0); + hf.renderWrappedText(sb, 16, 4, text); + assertEquals("multi-line padded text", expected, sb.toString()); + } + + public void testPrintOptions() throws Exception + { + StringBuffer sb = new StringBuffer(); + HelpFormatter hf = new HelpFormatter(); + final int leftPad = 1; + final int descPad = 3; + final String lpad = hf.createPadding(leftPad); + final String dpad = hf.createPadding(descPad); + Options options = null; + String expected = null; + + options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa"); + expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa"; + hf.renderOptions(sb, 60, options, leftPad, descPad); + assertEquals("simple non-wrapped option", expected, sb.toString()); + + int nextLineTabStop = leftPad + descPad + "-a".length(); + expected = lpad + "-a" + dpad + "aaaa aaaa aaaa" + hf.getNewLine() + + hf.createPadding(nextLineTabStop) + "aaaa aaaa"; + sb.setLength(0); + hf.renderOptions(sb, nextLineTabStop + 17, options, leftPad, descPad); + assertEquals("simple wrapped option", expected, sb.toString()); + + + options = new Options().addOption("a", "aaa", false, "dddd dddd dddd dddd"); + expected = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd"; + sb.setLength(0); + hf.renderOptions(sb, 60, options, leftPad, descPad); + assertEquals("long non-wrapped option", expected, sb.toString()); + + nextLineTabStop = leftPad + descPad + "-a,--aaa".length(); + expected = lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.getNewLine() + + hf.createPadding(nextLineTabStop) + "dddd dddd"; + sb.setLength(0); + hf.renderOptions(sb, 25, options, leftPad, descPad); + assertEquals("long wrapped option", expected, sb.toString()); + + options = new Options(). + addOption("a", "aaa", false, "dddd dddd dddd dddd"). + addOption("b", false, "feeee eeee eeee eeee"); + expected = lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.getNewLine() + + hf.createPadding(nextLineTabStop) + "dddd dddd" + hf.getNewLine() + + lpad + "-b " + dpad + "feeee eeee" + hf.getNewLine() + + hf.createPadding(nextLineTabStop) + "eeee eeee"; + sb.setLength(0); + hf.renderOptions(sb, 25, options, leftPad, descPad); + assertEquals("multiple wrapped options", expected, sb.toString()); + } public void testPrintHelpWithEmptySyntax() { @@ -179,29 +167,28 @@ } } - - public void testAutomaticUsage() throws Exception - { - HelpFormatter hf = new HelpFormatter(); - Options options = null; - String expected = "usage: app [-a]"; - ByteArrayOutputStream out = new ByteArrayOutputStream( ); - PrintWriter pw = new PrintWriter( out ); - - options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa"); - hf.printUsage( pw, 60, "app", options ); - pw.flush(); - assertEquals("simple auto usage", expected, out.toString().trim()); - out.reset(); - - expected = "usage: app [-a] [-b]"; - options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa") - .addOption("b", false, "bbb" ); - hf.printUsage( pw, 60, "app", options ); - pw.flush(); - assertEquals("simple auto usage", expected, out.toString().trim()); - out.reset(); - } + public void testAutomaticUsage() throws Exception + { + HelpFormatter hf = new HelpFormatter(); + Options options = null; + String expected = "usage: app [-a]"; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + PrintWriter pw = new PrintWriter(out); + + options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa"); + hf.printUsage(pw, 60, "app", options); + pw.flush(); + assertEquals("simple auto usage", expected, out.toString().trim()); + out.reset(); + + expected = "usage: app [-a] [-b]"; + options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa") + .addOption("b", false, "bbb"); + hf.printUsage(pw, 60, "app", options); + pw.flush(); + assertEquals("simple auto usage", expected, out.toString().trim()); + out.reset(); + } // This test ensures the options are properly sorted // See https://issues.apache.org/jira/browse/CLI-131 @@ -355,5 +342,112 @@ formatter.setWidth(80); assertEquals("width", 80, formatter.getWidth()); } + + public void testHeaderStartingWithLineSeparator() + { + // related to Bugzilla #21215 + Options options = new Options(); + HelpFormatter formatter = new HelpFormatter(); + String header = EOL + "Header"; + String footer = "Footer"; + StringWriter out = new StringWriter(); + formatter.printHelp(new PrintWriter(out), 80, "foobar", header, options, 2, 2, footer, true); + assertEquals( + "usage: foobar" + EOL + + "" + EOL + + "Header" + EOL + + "" + EOL + + "Footer" + EOL + , out.toString()); + } + public void testOptionWithoutShortFormat() + { + // related to Bugzilla #19383 (CLI-67) + Options options = new Options(); + options.addOption(new Option("a", "aaa", false, "aaaaaaa")); + options.addOption(new Option(null, "bbb", false, "bbbbbbb")); + options.addOption(new Option("c", null, false, "ccccccc")); + + HelpFormatter formatter = new HelpFormatter(); + StringWriter out = new StringWriter(); + formatter.printHelp(new PrintWriter(out), 80, "foobar", "", options, 2, 2, "", true); + assertEquals( + "usage: foobar [-a] [--bbb] [-c]" + EOL + + " -a,--aaa aaaaaaa" + EOL + + " --bbb bbbbbbb" + EOL + + " -c ccccccc" + EOL + , out.toString()); + } + + + public void testOptionWithoutShortFormat2() + { + // related to Bugzilla #27635 (CLI-26) + Option help = new Option("h", "help", false, "print this message"); + Option version = new Option("v", "version", false, "print version information"); + Option newRun = new Option("n", "new", false, "Create NLT cache entries only for new items"); + Option trackerRun = new Option("t", "tracker", false, "Create NLT cache entries only for tracker items"); + + Option timeLimit = OptionBuilder.withLongOpt("limit") + .hasArg() + .withValueSeparator() + .withDescription("Set time limit for execution, in mintues") + .create("l"); + + Option age = OptionBuilder.withLongOpt("age") + .hasArg() + .withValueSeparator() + .withDescription("Age (in days) of cache item before being recomputed") + .create("a"); + + Option server = OptionBuilder.withLongOpt("server") + .hasArg() + .withValueSeparator() + .withDescription("The NLT server address") + .create("s"); + + Option numResults = OptionBuilder.withLongOpt("results") + .hasArg() + .withValueSeparator() + .withDescription("Number of results per item") + .create("r"); + + Option configFile = OptionBuilder.withLongOpt("config") + .hasArg() + .withValueSeparator() + .withDescription("Use the specified configuration file") + .create(); + + Options mOptions = new Options(); + mOptions.addOption(help); + mOptions.addOption(version); + mOptions.addOption(newRun); + mOptions.addOption(trackerRun); + mOptions.addOption(timeLimit); + mOptions.addOption(age); + mOptions.addOption(server); + mOptions.addOption(numResults); + mOptions.addOption(configFile); + + HelpFormatter formatter = new HelpFormatter(); + final String EOL = System.getProperty("line.separator"); + StringWriter out = new StringWriter(); + formatter.printHelp(new PrintWriter(out),80,"commandline","header",mOptions,2,2,"footer",true); + assertEquals( + "usage: commandline [-a <arg>] [--config <arg>] [-h] [-l <arg>] [-n] [-r <arg>]" + EOL + + " [-s <arg>] [-t] [-v]" + EOL + + "header"+EOL+ + " -a,--age <arg> Age (in days) of cache item before being recomputed"+EOL+ + " --config <arg> Use the specified configuration file"+EOL+ + " -h,--help print this message"+EOL+ + " -l,--limit <arg> Set time limit for execution, in mintues"+EOL+ + " -n,--new Create NLT cache entries only for new items"+EOL+ + " -r,--results <arg> Number of results per item"+EOL+ + " -s,--server <arg> The NLT server address"+EOL+ + " -t,--tracker Create NLT cache entries only for tracker items"+EOL+ + " -v,--version print version information"+EOL+ + "footer"+EOL + ,out.toString()); + } }