1 /***
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.cli;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.PrintWriter;
21
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 /***
26 * Test case for the HelpFormatter class
27 *
28 * @author Slawek Zachcial
29 * @author John Keyes ( john at integralsource.com )
30 * @author brianegge
31 **/
32 public class HelpFormatterTest extends TestCase
33 {
34
35 private static final String EOL = System.getProperty("line.separator");
36
37 public static void main( String[] args )
38 {
39 String[] testName = { HelpFormatterTest.class.getName() };
40 junit.textui.TestRunner.main(testName);
41 }
42
43 public static TestSuite suite()
44 {
45 return new TestSuite(HelpFormatterTest.class);
46 }
47
48 public HelpFormatterTest( String s )
49 {
50 super( s );
51 }
52
53 public void testFindWrapPos()
54 throws Exception
55 {
56 HelpFormatter hf = new HelpFormatter();
57
58 String text = "This is a test.";
59
60 assertEquals("wrap position", 7, hf.findWrapPos(text, 8, 0));
61
62 assertEquals("wrap position 2", -1, hf.findWrapPos(text, 8, 8));
63
64 text = "aaaa aa";
65 assertEquals("wrap position 3", 4, hf.findWrapPos(text, 3, 0));
66 }
67
68 public void testPrintWrapped()
69 throws Exception
70 {
71 StringBuffer sb = new StringBuffer();
72 HelpFormatter hf = new HelpFormatter();
73
74 String text = "This is a test.";
75 String expected;
76
77 expected = "This is a" + hf.getNewLine() + "test.";
78 hf.renderWrappedText(sb, 12, 0, text);
79 assertEquals("single line text", expected, sb.toString());
80
81 sb.setLength(0);
82 expected = "This is a" + hf.getNewLine() + " test.";
83 hf.renderWrappedText(sb, 12, 4, text);
84 assertEquals("single line padded text", expected, sb.toString());
85
86 text =
87 "aaaa aaaa aaaa" + hf.getNewLine() +
88 "aaaaaa" + hf.getNewLine() +
89 "aaaaa";
90
91 expected = text;
92 sb.setLength(0);
93 hf.renderWrappedText(sb, 16, 0, text);
94 assertEquals("multi line text", expected, sb.toString());
95
96 expected =
97 "aaaa aaaa aaaa" + hf.getNewLine() +
98 " aaaaaa" + hf.getNewLine() +
99 " aaaaa";
100 sb.setLength(0);
101 hf.renderWrappedText(sb, 16, 4, text);
102 assertEquals("multi-line padded text", expected, sb.toString());
103 }
104
105 public void testPrintOptions()
106 throws Exception
107 {
108 StringBuffer sb = new StringBuffer();
109 HelpFormatter hf = new HelpFormatter();
110 final int leftPad = 1;
111 final int descPad = 3;
112 final String lpad = hf.createPadding(leftPad);
113 final String dpad = hf.createPadding(descPad);
114 Options options = null;
115 String expected = null;
116
117 options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa");
118 expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa";
119 hf.renderOptions(sb, 60, options, leftPad, descPad);
120 assertEquals("simple non-wrapped option", expected, sb.toString());
121
122 int nextLineTabStop = leftPad+descPad+"-a".length();
123 expected =
124 lpad + "-a" + dpad + "aaaa aaaa aaaa" + hf.getNewLine() +
125 hf.createPadding(nextLineTabStop) + "aaaa aaaa";
126 sb.setLength(0);
127 hf.renderOptions(sb, nextLineTabStop+17, options, leftPad, descPad);
128 assertEquals("simple wrapped option", expected, sb.toString());
129
130
131 options = new Options().addOption("a", "aaa", false, "dddd dddd dddd dddd");
132 expected = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd";
133 sb.setLength(0);
134 hf.renderOptions(sb, 60, options, leftPad, descPad);
135 assertEquals("long non-wrapped option", expected, sb.toString());
136
137 nextLineTabStop = leftPad+descPad+"-a,--aaa".length();
138 expected =
139 lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.getNewLine() +
140 hf.createPadding(nextLineTabStop) + "dddd dddd";
141 sb.setLength(0);
142 hf.renderOptions(sb, 25, options, leftPad, descPad);
143 assertEquals("long wrapped option", expected, sb.toString());
144
145 options = new Options().
146 addOption("a", "aaa", false, "dddd dddd dddd dddd").
147 addOption("b", false, "feeee eeee eeee eeee");
148 expected =
149 lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.getNewLine() +
150 hf.createPadding(nextLineTabStop) + "dddd dddd" + hf.getNewLine() +
151 lpad + "-b " + dpad + "feeee eeee" + hf.getNewLine() +
152 hf.createPadding(nextLineTabStop) + "eeee eeee";
153 sb.setLength(0);
154 hf.renderOptions(sb, 25, options, leftPad, descPad);
155 assertEquals("multiple wrapped options", expected, sb.toString());
156 }
157
158 public void testAutomaticUsage()
159 throws Exception
160 {
161 HelpFormatter hf = new HelpFormatter();
162 Options options = null;
163 String expected = "usage: app [-a]";
164 ByteArrayOutputStream out = new ByteArrayOutputStream( );
165 PrintWriter pw = new PrintWriter( out );
166
167 options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa");
168 hf.printUsage( pw, 60, "app", options );
169 pw.flush();
170 assertEquals("simple auto usage", expected, out.toString().trim());
171 out.reset();
172
173 expected = "usage: app [-a] [-b]";
174 options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa")
175 .addOption("b", false, "bbb" );
176 hf.printUsage( pw, 60, "app", options );
177 pw.flush();
178 assertEquals("simple auto usage", expected, out.toString().trim());
179 out.reset();
180 }
181
182
183
184 public void testPrintUsage() {
185 Option optionA = new Option("a", "first");
186 Option optionB = new Option("b", "second");
187 Option optionC = new Option("c", "third");
188 Options opts = new Options();
189 opts.addOption(optionA);
190 opts.addOption(optionB);
191 opts.addOption(optionC);
192 HelpFormatter helpFormatter = new HelpFormatter();
193 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
194 PrintWriter printWriter = new PrintWriter(bytesOut);
195 helpFormatter.printUsage(printWriter, 80, "app", opts);
196 printWriter.close();
197 assertEquals("usage: app [-a] [-b] [-c]" + EOL, bytesOut.toString());
198 }
199
200 }