summaryrefslogtreecommitdiffstats
path: root/args4j/args4j/test/org/kohsuke/args4j/Args4JTestBase.java
blob: 61ef0fd8798a5999bd21c3b9ba4deea55fd8a4e8 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package org.kohsuke.args4j;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Locale;

import junit.framework.TestCase;


/**
 * Base class for Args4J Tests.
 * It instantiates the test object, the CmdLineParser for
 * that test object and provides a String array for passing
 * to the parser.
 *
 * @author Jan Materne
 */
public abstract class Args4JTestBase<T> extends TestCase {

    CmdLineParser parser;
    String[] args;
    T testObject;
    
    private Locale defaultLocale;

    /**
     * Specifies which concrete object to return as test object.
     * @return the test object
     */
    public abstract T getTestObject();
    
    /**
     * Setter for args in a vararg manner.
     * @param args
     */
    public void setArgs(String... args) {
    	this.args = args;
    }
    
    /**
     * Returns the Locale to use for the tests.
     * Defaults to use the US-Locale but should be overwritten by classes
     * which tests I18N.          
     */         
    public Locale getLocale() {
        return Locale.US;
    }

    /**
     * Initializes the testObject and the parser for that object.
     * @see junit.framework.TestCase#setUp()
     */
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        defaultLocale = Locale.getDefault();
        Locale.setDefault(getLocale());
        testObject = getTestObject();
        parser = createParser();
    }

    protected CmdLineParser createParser() {
        return new CmdLineParser(testObject);
    }

    /**
     * Restores the environment, namely the default Locale.
     */         
    @Override
    protected void tearDown() throws Exception {
        Locale.setDefault(defaultLocale);
    }

    /**
     * Checks the number of lines of the parsers usage message.
     * @param expectedLength
     * @see TestCase#assertEquals(String, int, int)
     * @see Args4JTestBase#getUsageMessage()
     */
    public void assertUsageLength(int expectedLength) {
        assertEquals("Wrong amount of lines in usage message", expectedLength, getUsageMessage().length);
    }
    
    /**
     * Asserts that a given text is part of the usage message.
     * @param message Error message if the text is not found.
     * @param containingText Text to search for.
     */
    public void assertUsageContains(String message, String containingText) {
    	boolean contains = false;
    	for (String line : getUsageMessage()) {
    		if (line.contains(containingText)) {
    			contains = true;
    			break;
    		}
    	}
    	if (!contains) {
    		throw new AssertionError(message);
    	}
    }

    /**
     * Extracts the usage message from the parser as String array.
     * @return the usage message
     * @see CmdLineParser#printUsage(OutputStream)
     */
    public String[] getUsageMessage() {
        Stream2String s2s = new Stream2String();
        parser.printUsage(s2s);
        return s2s.getString().split(System.getProperty("line.separator"));
    }

    /**
     * Utility class for capturing an OutputStream into a String.
     * @author Jan Materne
     */
    private class Stream2String extends OutputStream {
        private StringBuffer sb = new StringBuffer();

        @Override
        public void write(int b) throws IOException {
            sb.append((char)b);
        }

        public String getString() {
            return sb.toString();
        }
    }

}