View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Test class for the NiceTable.
38   * 
39   * @author Sebastian Graf, University of Konstanz
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       * Simple setUp.
49       */
50      @Before
51      public void setUp() {
52          table = new NiceTable(COLUMNNUMBER);
53      }
54  
55      /**
56       * Test method for {@link org.perfidix.ouput.asciitable.NiceTable#NiceTable(int)}.
57       */
58      @Test
59      public void testCreate() {
60          assertEquals("Test for create", "", table.toString());
61      }
62  
63      /**
64       * Test method for {@link org.perfidix.ouput.asciitable.NiceTable#addHeader(java.lang.String)} .
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       * Test method for
74       * {@link org.perfidix.ouput.asciitable.NiceTable#addHeader(String, char, org.perfidix.ouput.asciitable.AbstractTabularComponent.Alignment)}
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       * Test method for {@link org.perfidix.ouput.asciitable.NiceTable#addRow(java.lang.String[])} .
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      * Test method for {@link org.perfidix.ouput.asciitable.NiceTable#addLine(char)}.
108      */
109     @Test
110     public void testAddLine() {
111         table.addLine('*');
112         assertEquals("Test for adding a line", "|******************************|\n", table.toString());
113     }
114 
115     /**
116      * Test method for {@link org.perfidix.ouput.asciitable.NiceTable#toString()}.
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      * Recursive usage of multiple tables
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      * Alignement test if some rows are incomplete
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      * Test dynamic row insertion.
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 }