1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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, 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, 0, 3);
69 field.format(2, buf);
70 assertEquals("fobar", buf.toString());
71 }
72
73 /**
74 * Add padding to left since field is not minimum width.
75 */
76 public void testPadLeft() {
77 StringBuffer buf = new StringBuffer("foobar");
78 FormattingInfo field = new FormattingInfo(false, 5, 10);
79 field.format(2, buf);
80 assertEquals("fo obar", buf.toString());
81 }
82
83 /**
84 * Add padding to right since field is not minimum width.
85 */
86 public void testPadRight() {
87 StringBuffer buf = new StringBuffer("foobar");
88 FormattingInfo field = new FormattingInfo(true, 5, 10);
89 field.format(2, buf);
90 assertEquals("foobar ", buf.toString());
91 }
92
93 }