1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli.example;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21
22 import java.io.IOException;
23 import java.io.StringReader;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.apache.commons.cli.help.TableDefinition;
30 import org.apache.commons.cli.help.TextStyle;
31 import org.apache.commons.io.IOUtils;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38 class XhtmlHelpAppendableTest {
39
40 private StringBuilder sb;
41 private XhtmlHelpAppendable underTest;
42
43 @BeforeEach
44 public void beforeEach() {
45 sb = new StringBuilder();
46 underTest = new XhtmlHelpAppendable(sb);
47 }
48
49 @Test
50 void testAppendHeaderTest() throws IOException {
51 underTest.appendHeader(1, "Hello World");
52 assertEquals(String.format("<h1>Hello World</h1>%n"), sb.toString());
53 sb.setLength(0);
54 underTest.appendHeader(2, "Hello World");
55 assertEquals(String.format("<h2>Hello World</h2>%n"), sb.toString());
56 sb.setLength(0);
57 assertThrows(IllegalArgumentException.class, () -> underTest.appendHeader(0, "Hello World"));
58 }
59
60 @Test
61 void testAppendListTest() throws IOException {
62 final String[] entries = { "one", "two", "three" };
63 underTest.appendList(true, Arrays.asList(entries));
64 assertEquals(String.format("<ol>%n <li>one</li>%n <li>two</li>%n <li>three</li>%n</ol>%n"), sb.toString());
65 sb.setLength(0);
66 underTest.appendList(false, Arrays.asList(entries));
67 assertEquals(String.format("<ul>%n <li>one</li>%n <li>two</li>%n <li>three</li>%n</ul>%n"), sb.toString());
68 }
69
70 @Test
71 void testAppendParagraphFormatTest() throws IOException {
72 underTest.appendParagraphFormat("Hello %s World %,d", "Joe", 309);
73 assertEquals(String.format("<p>Hello Joe World 309</p>%n"), sb.toString());
74 }
75
76 @Test
77 void testAppendParagraphTest() throws IOException {
78 underTest.appendParagraph("Hello World");
79 assertEquals(String.format("<p>Hello World</p>%n"), sb.toString());
80 }
81
82 @Test
83 void testAppendTableTest() throws IOException {
84 final List<TextStyle> styles = Arrays.asList(TextStyle.DEFAULT, TextStyle.DEFAULT, TextStyle.DEFAULT);
85 final String[] headers = { "one", "two", "three" };
86
87 final List<List<String>> rows = Arrays.asList(
88 Arrays.asList(new String[]{"uno", "dos", "tres"}),
89 Arrays.asList(new String[]{"aon", "dhá", "trí"}),
90 Arrays.asList(new String[]{"واحد", "اثنين", "ثلاثة"})
91 );
92
93 List<String> expected = new ArrayList<>();
94 expected.add("<table class='commons_cli_table'>");
95 expected.add(" <caption>The caption</caption>");
96 expected.add(" <tr>");
97 expected.add(" <th>one</th>");
98 expected.add(" <th>two</th>");
99 expected.add(" <th>three</th>");
100 expected.add(" </tr>");
101 expected.add(" <tr>");
102 expected.add(" <td>uno</td>");
103 expected.add(" <td>dos</td>");
104 expected.add(" <td>tres</td>");
105 expected.add(" </tr>");
106 expected.add(" <tr>");
107 expected.add(" <td>aon</td>");
108 expected.add(" <td>dhá</td>");
109 expected.add(" <td>trí</td>");
110 expected.add(" </tr>");
111 expected.add(" <tr>");
112 expected.add(" <td>واحد</td>");
113 expected.add(" <td>اثنين</td>");
114 expected.add(" <td>ثلاثة</td>");
115 expected.add(" </tr>");
116 expected.add("</table>");
117 TableDefinition table = TableDefinition.from("The caption", styles, Arrays.asList(headers), rows);
118 underTest.appendTable(table);
119 List<String> actual = IOUtils.readLines(new StringReader(sb.toString()));
120 assertEquals(expected, actual, "full table failed");
121 table = TableDefinition.from(null, styles, Arrays.asList(headers), rows);
122 expected.remove(1);
123 sb.setLength(0);
124 underTest.appendTable(table);
125 actual = IOUtils.readLines(new StringReader(sb.toString()));
126 assertEquals(expected, actual);
127 table = TableDefinition.from(null, styles, Arrays.asList(headers), Collections.emptyList());
128 expected = new ArrayList<>();
129 expected.add("<table class='commons_cli_table'>");
130 expected.add(" <tr>");
131 expected.add(" <th>one</th>");
132 expected.add(" <th>two</th>");
133 expected.add(" <th>three</th>");
134 expected.add(" </tr>");
135 expected.add("</table>");
136 sb.setLength(0);
137 underTest.appendTable(table);
138 actual = IOUtils.readLines(new StringReader(sb.toString()));
139 assertEquals(expected, actual, "no rows test failed");
140 }
141
142 @Test
143 void testAppendTitleTest() throws IOException {
144 underTest.appendTitle("Hello World");
145 assertEquals(String.format("<span class='commons_cli_title'>Hello World</span>%n"), sb.toString());
146 }
147 }