1 10 package org.apache.commons.cli; 11 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 15 import java.io.ByteArrayOutputStream ; 16 import java.io.PrintWriter ; 17 import java.io.StringWriter ; 18 19 25 public class TestHelpFormatter extends TestCase 26 { 27 public static void main( String [] args ) 28 { 29 String [] 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 s ) 39 { 40 super( s ); 41 } 42 43 public void testFindWrapPos() 44 throws Exception 45 { 46 HelpFormatter hf = new HelpFormatter(); 47 48 String text = "This is a test."; 49 assertEquals("wrap position", 7, hf.findWrapPos(text, 8, 0)); 51 assertEquals("wrap position 2", -1, hf.findWrapPos(text, 8, 8)); 53 text = "aaaa aa"; 55 assertEquals("wrap position 3", 4, hf.findWrapPos(text, 3, 0)); 56 } 57 58 public void testPrintWrapped() 59 throws Exception 60 { 61 StringBuffer sb = new StringBuffer (); 62 HelpFormatter hf = new HelpFormatter(); 63 64 String text = "This is a test."; 65 String 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 97 { 98 StringBuffer sb = new StringBuffer (); 99 HelpFormatter hf = new HelpFormatter(); 100 final int leftPad = 1; 101 final int descPad = 3; 102 final String lpad = hf.createPadding(leftPad); 103 final String dpad = hf.createPadding(descPad); 104 Options options = null; 105 String 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 150 { 151 HelpFormatter hf = new HelpFormatter(); 152 Options options = null; 153 String expected = "usage: app [-a]"; 154 ByteArrayOutputStream out = new ByteArrayOutputStream ( ); 155 PrintWriter pw = new PrintWriter ( 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 |