1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.pattern;
19
20 import junit.framework.TestCase;
21
22
23 /***
24 * Tests for FormattingInfo.
25 *
26 * @author Curt Arnold
27 *
28 */
29 public class FormattingInfoTest extends TestCase {
30 /***
31 * Create a new instance.
32 *
33 * @param name test name
34 */
35 public FormattingInfoTest(final String name) {
36 super(name);
37 }
38
39 /***
40 * Check that getDefault does not return null.
41 *
42 */
43 public void testGetDefault() {
44 FormattingInfo field = FormattingInfo.getDefault();
45 assertNotNull(field);
46 assertEquals(0, field.getMinLength());
47 assertEquals(Integer.MAX_VALUE, field.getMaxLength());
48 assertEquals(false, field.isLeftAligned());
49 }
50
51 /***
52 * Check constructor
53 *
54 */
55 public void testConstructor() {
56 FormattingInfo field = new FormattingInfo(true, false, 3, 6);
57 assertNotNull(field);
58 assertEquals(3, field.getMinLength());
59 assertEquals(6, field.getMaxLength());
60 assertEquals(true, field.isLeftAligned());
61 }
62
63 /***
64 * Field exceeds maximum width
65 */
66 public void testTruncate() {
67 StringBuffer buf = new StringBuffer("foobar");
68 FormattingInfo field = new FormattingInfo(true, false, 0, 3);
69 field.format(2, buf);
70 assertEquals("fobar", buf.toString());
71 }
72
73 /***
74 * Field exceeds maximum width, truncate right.
75 */
76 public void testRightTruncate() {
77 StringBuffer buf = new StringBuffer("foobar");
78 FormattingInfo field = new FormattingInfo(true, true, 0, 3);
79 field.format(2, buf);
80 assertEquals("fooba", buf.toString());
81 }
82
83 /***
84 * Add padding to left since field is not minimum width.
85 */
86 public void testPadLeft() {
87 StringBuffer buf = new StringBuffer("foobar");
88 FormattingInfo field = new FormattingInfo(false, false, 5, 10);
89 field.format(2, buf);
90 assertEquals("fo obar", buf.toString());
91 }
92
93 /***
94 * Add padding to right since field is not minimum width.
95 */
96 public void testPadRight() {
97 StringBuffer buf = new StringBuffer("foobar");
98 FormattingInfo field = new FormattingInfo(true, false, 5, 10);
99 field.format(2, buf);
100 assertEquals("foobar ", buf.toString());
101 }
102
103 }