File: HelpFormatterTest.java

package info (click to toggle)
jython 2.7.2%2Brepack1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 62,676 kB
  • sloc: python: 640,908; java: 306,458; xml: 1,984; sh: 522; ansic: 126; makefile: 76
file content (82 lines) | stat: -rw-r--r-- 2,685 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.apache.commons.cli;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

import junit.framework.TestCase;

public class HelpFormatterTest extends TestCase {

    private Options _options;

    protected void setUp() {
        _options = new Options();
        Option aOption = new Option("a", "Aa", false, "option A");
        Option bOption = new Option("b", "Bb", false, "option B");
        OptionGroup group1 = new OptionGroup();
        group1.addOption(aOption);
        group1.addOption(bOption);
        _options.addOptionGroup(group1);
    }

    /**
     * the setUp above used to print [-a | -b] [-a] [-b]
     */
    public void testOptionGroupDuplication() {
        String help = unifyNewLines(getFormattedHelp());
        String expectedHelp = unifyNewLines(new String("usage: syntax [-a | -b]\n-a,--Aa option A\n-b,--Bb option B\n"));
        assertEquals("expected usage to be '" + expectedHelp + "' instead of '" + help + "'",
                     expectedHelp,
                     help);
    }

    /**
     * Options following an option group used to be non blank separated: [-b | -a][-o] instead of
     * [-b | -a] [-o]
     */
    public void testOptionGroupSubsequentOptions() {
        _options.addOption(new Option("o", "Option O"));
        String help = getFormattedHelp();
        assertTrue(help.indexOf("][") < 0);
        assertTrue(help.indexOf("[-a | -b] [-o]") >= 0);
    }

    //
    // private methods
    //
    private String getFormattedHelp() {
        HelpFormatter formatter = new HelpFormatter();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter pw = new PrintWriter(baos);
        formatter.printHelp(pw, 60, "syntax", null, _options, 0, 1, null, true);
        pw.flush();
        String usage = baos.toString();
        return usage;
    }

    /**
     * replace the windows specific \r\n line endings with java like \n line endings
     * 
     * @param in
     *            The string to be transformed
     * @return The string with unified line endings
     */
    private String unifyNewLines(String in) {
        char[] inChars = in.toCharArray();
        StringBuilder b = new StringBuilder(inChars.length);
        for (int i = 0; i < inChars.length; i++) {
            char current = inChars[i];
            if (current == '\r') {
                if (i < inChars.length) {
                    char next = inChars[i + 1];
                    if (next == '\n') {
                        i++;
                        current = next;
                    }
                }
            }
            b.append(current);
        }
        return b.toString();
    }
}