001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.commons.cli;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.PrintWriter;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: /**
025: * Test case for the HelpFormatter class
026: *
027: * @author Slawek Zachcial
028: * @author John Keyes ( john at integralsource.com )
029: * @author brianegge
030: **/
031: public class HelpFormatterTest extends TestCase {
032:
033: private static final String EOL = System
034: .getProperty("line.separator");
035:
036: public static void main(String[] args) {
037: String[] testName = { HelpFormatterTest.class.getName() };
038: junit.textui.TestRunner.main(testName);
039: }
040:
041: public static TestSuite suite() {
042: return new TestSuite(HelpFormatterTest.class);
043: }
044:
045: public HelpFormatterTest(String s) {
046: super (s);
047: }
048:
049: public void testFindWrapPos() throws Exception {
050: HelpFormatter hf = new HelpFormatter();
051:
052: String text = "This is a test.";
053: //text width should be max 8; the wrap postition is 7
054: assertEquals("wrap position", 7, hf.findWrapPos(text, 8, 0));
055: //starting from 8 must give -1 - the wrap pos is after end
056: assertEquals("wrap position 2", -1, hf.findWrapPos(text, 8, 8));
057: //if there is no a good position before width to make a wrapping look for the next one
058: text = "aaaa aa";
059: assertEquals("wrap position 3", 4, hf.findWrapPos(text, 3, 0));
060: }
061:
062: public void testPrintWrapped() throws Exception {
063: StringBuffer sb = new StringBuffer();
064: HelpFormatter hf = new HelpFormatter();
065:
066: String text = "This is a test.";
067: String expected;
068:
069: expected = "This is a" + hf.getNewLine() + "test.";
070: hf.renderWrappedText(sb, 12, 0, text);
071: assertEquals("single line text", expected, sb.toString());
072:
073: sb.setLength(0);
074: expected = "This is a" + hf.getNewLine() + " test.";
075: hf.renderWrappedText(sb, 12, 4, text);
076: assertEquals("single line padded text", expected, sb.toString());
077:
078: text = "aaaa aaaa aaaa" + hf.getNewLine() + "aaaaaa"
079: + hf.getNewLine() + "aaaaa";
080:
081: expected = text;
082: sb.setLength(0);
083: hf.renderWrappedText(sb, 16, 0, text);
084: assertEquals("multi line text", expected, sb.toString());
085:
086: expected = "aaaa aaaa aaaa" + hf.getNewLine() + " aaaaaa"
087: + hf.getNewLine() + " aaaaa";
088: sb.setLength(0);
089: hf.renderWrappedText(sb, 16, 4, text);
090: assertEquals("multi-line padded text", expected, sb.toString());
091: }
092:
093: public void testPrintOptions() throws Exception {
094: StringBuffer sb = new StringBuffer();
095: HelpFormatter hf = new HelpFormatter();
096: final int leftPad = 1;
097: final int descPad = 3;
098: final String lpad = hf.createPadding(leftPad);
099: final String dpad = hf.createPadding(descPad);
100: Options options = null;
101: String expected = null;
102:
103: options = new Options().addOption("a", false,
104: "aaaa aaaa aaaa aaaa aaaa");
105: expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa";
106: hf.renderOptions(sb, 60, options, leftPad, descPad);
107: assertEquals("simple non-wrapped option", expected, sb
108: .toString());
109:
110: int nextLineTabStop = leftPad + descPad + "-a".length();
111: expected = lpad + "-a" + dpad + "aaaa aaaa aaaa"
112: + hf.getNewLine() + hf.createPadding(nextLineTabStop)
113: + "aaaa aaaa";
114: sb.setLength(0);
115: hf.renderOptions(sb, nextLineTabStop + 17, options, leftPad,
116: descPad);
117: assertEquals("simple wrapped option", expected, sb.toString());
118:
119: options = new Options().addOption("a", "aaa", false,
120: "dddd dddd dddd dddd");
121: expected = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd";
122: sb.setLength(0);
123: hf.renderOptions(sb, 60, options, leftPad, descPad);
124: assertEquals("long non-wrapped option", expected, sb.toString());
125:
126: nextLineTabStop = leftPad + descPad + "-a,--aaa".length();
127: expected = lpad + "-a,--aaa" + dpad + "dddd dddd"
128: + hf.getNewLine() + hf.createPadding(nextLineTabStop)
129: + "dddd dddd";
130: sb.setLength(0);
131: hf.renderOptions(sb, 25, options, leftPad, descPad);
132: assertEquals("long wrapped option", expected, sb.toString());
133:
134: options = new Options().addOption("a", "aaa", false,
135: "dddd dddd dddd dddd").addOption("b", false,
136: "feeee eeee eeee eeee");
137: expected = lpad + "-a,--aaa" + dpad + "dddd dddd"
138: + hf.getNewLine() + hf.createPadding(nextLineTabStop)
139: + "dddd dddd" + hf.getNewLine() + lpad + "-b "
140: + dpad + "feeee eeee" + hf.getNewLine()
141: + hf.createPadding(nextLineTabStop) + "eeee eeee";
142: sb.setLength(0);
143: hf.renderOptions(sb, 25, options, leftPad, descPad);
144: assertEquals("multiple wrapped options", expected, sb
145: .toString());
146: }
147:
148: public void testAutomaticUsage() throws Exception {
149: HelpFormatter hf = new HelpFormatter();
150: Options options = null;
151: String expected = "usage: app [-a]";
152: ByteArrayOutputStream out = new ByteArrayOutputStream();
153: PrintWriter pw = new PrintWriter(out);
154:
155: options = new Options().addOption("a", false,
156: "aaaa aaaa aaaa aaaa aaaa");
157: hf.printUsage(pw, 60, "app", options);
158: pw.flush();
159: assertEquals("simple auto usage", expected, out.toString()
160: .trim());
161: out.reset();
162:
163: expected = "usage: app [-a] [-b]";
164: options = new Options().addOption("a", false,
165: "aaaa aaaa aaaa aaaa aaaa")
166: .addOption("b", false, "bbb");
167: hf.printUsage(pw, 60, "app", options);
168: pw.flush();
169: assertEquals("simple auto usage", expected, out.toString()
170: .trim());
171: out.reset();
172: }
173:
174: // This test ensures the options are properly sorted
175: // See https://issues.apache.org/jira/browse/CLI-131
176: public void testPrintUsage() {
177: Option optionA = new Option("a", "first");
178: Option optionB = new Option("b", "second");
179: Option optionC = new Option("c", "third");
180: Options opts = new Options();
181: opts.addOption(optionA);
182: opts.addOption(optionB);
183: opts.addOption(optionC);
184: HelpFormatter helpFormatter = new HelpFormatter();
185: ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
186: PrintWriter printWriter = new PrintWriter(bytesOut);
187: helpFormatter.printUsage(printWriter, 80, "app", opts);
188: printWriter.close();
189: assertEquals("usage: app [-a] [-b] [-c]" + EOL, bytesOut
190: .toString());
191: }
192:
193: }
|