KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > cli > TestHelpFormatter


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  *
8  * $Id: TestHelpFormatter.java,v 1.2 2002/05/17 11:44:32 jstrachan Exp $
9  */

10 package org.apache.commons.cli;
11
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.io.StringWriter JavaDoc;
18
19 /**
20  * Test case for the HelpFormatter class
21  *
22  * @author Slawek Zachcial
23  * @author John Keyes ( john at integralsource.com )
24  **/

25 public class TestHelpFormatter extends TestCase
26 {
27    public static void main( String JavaDoc[] args )
28    {
29       String JavaDoc[] testName = { TestHelpFormatter.class.getName() };
30       junit.textui.TestRunner.main(testName);
31    }
32
33    public static TestSuite suite()
34    {
35       return new TestSuite(TestHelpFormatter.class);
36    }
37
38    public TestHelpFormatter( String JavaDoc s )
39    {
40       super( s );
41    }
42
43    public void testFindWrapPos()
44       throws Exception JavaDoc
45    {
46       HelpFormatter hf = new HelpFormatter();
47
48       String JavaDoc text = "This is a test.";
49       //text width should be max 8; the wrap postition is 7
50
assertEquals("wrap position", 7, hf.findWrapPos(text, 8, 0));
51       //starting from 8 must give -1 - the wrap pos is after end
52
assertEquals("wrap position 2", -1, hf.findWrapPos(text, 8, 8));
53       //if there is no a good position before width to make a wrapping look for the next one
54
text = "aaaa aa";
55       assertEquals("wrap position 3", 4, hf.findWrapPos(text, 3, 0));
56    }
57
58    public void testPrintWrapped()
59       throws Exception JavaDoc
60    {
61       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
62       HelpFormatter hf = new HelpFormatter();
63
64       String JavaDoc text = "This is a test.";
65       String JavaDoc expected;
66
67       expected = "This is a" + hf.defaultNewLine + "test.";
68       hf.renderWrappedText(sb, 12, 0, text);
69       assertEquals("single line text", expected, sb.toString());
70
71       sb.setLength(0);
72       expected = "This is a" + hf.defaultNewLine + " test.";
73       hf.renderWrappedText(sb, 12, 4, text);
74       assertEquals("single line padded text", expected, sb.toString());
75
76       text =
77          "aaaa aaaa aaaa" + hf.defaultNewLine +
78          "aaaaaa" + hf.defaultNewLine +
79          "aaaaa";
80
81       expected = text;
82       sb.setLength(0);
83       hf.renderWrappedText(sb, 16, 0, text);
84       assertEquals("multi line text", expected, sb.toString());
85
86       expected =
87          "aaaa aaaa aaaa" + hf.defaultNewLine +
88          " aaaaaa" + hf.defaultNewLine +
89          " aaaaa";
90       sb.setLength(0);
91       hf.renderWrappedText(sb, 16, 4, text);
92       assertEquals("multi-line padded text", expected, sb.toString());
93    }
94
95    public void testPrintOptions()
96    throws Exception JavaDoc
97    {
98        StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
99        HelpFormatter hf = new HelpFormatter();
100        final int leftPad = 1;
101        final int descPad = 3;
102        final String JavaDoc lpad = hf.createPadding(leftPad);
103        final String JavaDoc dpad = hf.createPadding(descPad);
104        Options options = null;
105        String JavaDoc expected = null;
106
107        options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa");
108        expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa";
109        hf.renderOptions(sb, 60, options, leftPad, descPad);
110        assertEquals("simple non-wrapped option", expected, sb.toString());
111
112        int nextLineTabStop = leftPad+descPad+"-a".length();
113        expected =
114            lpad + "-a" + dpad + "aaaa aaaa aaaa" + hf.defaultNewLine +
115            hf.createPadding(nextLineTabStop) + "aaaa aaaa";
116        sb.setLength(0);
117        hf.renderOptions(sb, nextLineTabStop+17, options, leftPad, descPad);
118        assertEquals("simple wrapped option", expected, sb.toString());
119
120
121        options = new Options().addOption("a", "aaa", false, "dddd dddd dddd dddd");
122        expected = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd";
123        sb.setLength(0);
124        hf.renderOptions(sb, 60, options, leftPad, descPad);
125        assertEquals("long non-wrapped option", expected, sb.toString());
126
127        nextLineTabStop = leftPad+descPad+"-a,--aaa".length();
128        expected =
129            lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine +
130            hf.createPadding(nextLineTabStop) + "dddd dddd";
131        sb.setLength(0);
132        hf.renderOptions(sb, 25, options, leftPad, descPad);
133        assertEquals("long wrapped option", expected, sb.toString());
134
135        options = new Options().
136            addOption("a", "aaa", false, "dddd dddd dddd dddd").
137            addOption("b", false, "feeee eeee eeee eeee");
138        expected =
139            lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine +
140            hf.createPadding(nextLineTabStop) + "dddd dddd" + hf.defaultNewLine +
141            lpad + "-b " + dpad + "feeee eeee" + hf.defaultNewLine +
142            hf.createPadding(nextLineTabStop) + "eeee eeee";
143        sb.setLength(0);
144        hf.renderOptions(sb, 25, options, leftPad, descPad);
145        assertEquals("multiple wrapped options", expected, sb.toString());
146    }
147
148    public void testAutomaticUsage()
149    throws Exception JavaDoc
150    {
151        HelpFormatter hf = new HelpFormatter();
152        Options options = null;
153        String JavaDoc expected = "usage: app [-a]";
154        ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc( );
155        PrintWriter JavaDoc pw = new PrintWriter JavaDoc( out );
156
157        options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa");
158        hf.printUsage( pw, 60, "app", options );
159        pw.flush();
160        assertEquals("simple auto usage", expected, out.toString().trim());
161        out.reset();
162
163        expected = "usage: app [-b] [-a]";
164        options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa")
165        .addOption("b", false, "bbb" );
166        hf.printUsage( pw, 60, "app", options );
167        pw.flush();
168        assertEquals("simple auto usage", expected, out.toString().trim());
169        out.reset();
170    }
171 }
172
Popular Tags