1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.perfidix.output.asciitable;
28
29 import static org.junit.Assert.assertEquals;
30
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.perfidix.ouput.asciitable.NiceTable;
34 import org.perfidix.ouput.asciitable.AbstractTabularComponent.Alignment;
35
36
37
38
39
40
41 public class NiceTableTest {
42
43 private transient NiceTable table;
44 private final static int COLUMNNUMBER = 10;
45 private final static String TESTSTRING = "This is a test";
46
47
48
49
50 @Before
51 public void setUp() {
52 table = new NiceTable(COLUMNNUMBER);
53 }
54
55
56
57
58 @Test
59 public void testCreate() {
60 assertEquals("Test for create", "", table.toString());
61 }
62
63
64
65
66 @Test
67 public void testAddHeaderString() {
68 table.addHeader(TESTSTRING);
69 assertEquals("Test for normal adding", "|= This is a test =============|\n", table.toString());
70 }
71
72
73
74
75
76 @Test
77 public void testAddHeaderStringCharAlignment() {
78 table.addHeader(TESTSTRING, '-', Alignment.Left);
79 assertEquals("Test for left alignement adding", "|- This is a test -------------|\n", table
80 .toString());
81
82 setUp();
83
84 table.addHeader(TESTSTRING, '/', Alignment.Center);
85 assertEquals("Test for center alignment", "|/////// This is a test ///////|\n", table.toString());
86
87 setUp();
88
89 table.addHeader(TESTSTRING, '\\', Alignment.Right);
90 assertEquals("Test for right alignment", "|\\\\\\\\\\\\\\\\\\\\\\\\\\ This is a test \\|\n", table
91 .toString());
92 }
93
94
95
96
97 @Test
98 public void testAddRow() {
99 final String[] data = {
100 "this", "is", "a", "test"
101 };
102 table.addRow(data);
103 assertEquals("Test for | delim", "| this | is | a | test |\n", table.toString());
104 }
105
106
107
108
109 @Test
110 public void testAddLine() {
111 table.addLine('*');
112 assertEquals("Test for adding a line", "|******************************|\n", table.toString());
113 }
114
115
116
117
118 @Test
119 public void testToString() {
120 table.addHeader("This is a header");
121 table.addRow(new String[] {
122 "This", "is", "one", "data"
123 });
124 table.addLine('-');
125 table.addRow(new String[] {
126 "This", "is", "another", "data"
127 });
128 assertEquals(
129 "Test for a complete table",
130 "|= This is a header ===========================|\n| This | is | one | data |\n|----------------------------------------------|\n| This | is | another | data |\n",
131 table.toString());
132 }
133
134
135
136
137 @Test
138 public void testNestedTable() {
139
140 final NiceTable zero = new NiceTable(3);
141 final NiceTable one = new NiceTable(2);
142 final NiceTable two = new NiceTable(2);
143 final NiceTable three = new NiceTable(2);
144 one.addRow(new String[] {
145 "a", "b"
146 });
147 two.addRow(new String[] {
148 "c", "d"
149 });
150 three.addRow(new String[] {
151 "e", "f"
152 });
153 zero.addRow(new String[] {
154 one.toString(), two.toString(), three.toString()
155 });
156
157 final String result = zero.toString().trim();
158 assertEquals("Test for encapsulated tables", "| | a | b | | | c | d | | | e | f | |", result);
159 }
160
161
162
163
164 @Test
165 public void testRowAlignment() {
166 final NiceTable zero = new NiceTable(2);
167 zero.addRow(new String[] {
168 "a\nb\nc", "a\nb"
169 });
170 zero.addRow(new String[] {
171 "d", "d"
172 });
173
174 final String expected = "| a | a |\n" + "| b | b |\n" + "| c | |\n" + "| d | d |\n";
175 assertEquals("Test for row alignment", expected, zero.toString());
176 }
177
178
179
180
181 @Test
182 public void testDynamics() {
183
184 final String[] string = {
185 "a", "b", "c", "d", "e"
186 };
187 final Number[] numbers = {
188 1.222222222, 3.000, 4, 5.0, 4444
189 };
190
191 final NiceTable zero = new NiceTable(string.length);
192 zero.addLine('-');
193 zero.addRow(string);
194 zero.addLine('=');
195 zero.addRow(new String[] {
196 numbers[0].toString(), numbers[1].toString(), numbers[2].toString(), numbers[3].toString(),
197 numbers[4].toString()
198 });
199 zero.addLine('-');
200 final String result = zero.toString();
201 final StringBuilder expected = new StringBuilder();
202 expected.append("|------------------------------------|\n");
203 expected.append("| a | b | c | d | e |\n");
204 expected.append("|====================================|\n");
205 expected.append("| 1.222222222 | 3.0 | 4 | 5.0 | 4444 |\n");
206 expected.append("|------------------------------------|\n");
207 assertEquals("Another test for a complete table", expected.toString(), result);
208
209 }
210 }