1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import org.mule.tck.AbstractMuleTestCase;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18
19 public class StringMessageUtilsTestCase extends AbstractMuleTestCase
20 {
21
22 public void testToString() throws Exception
23 {
24 Object test = "Oscar";
25 Object result = StringMessageUtils.toString(test);
26 assertEquals("Oscar", result);
27
28 test = getClass();
29 result = StringMessageUtils.toString(test);
30 assertEquals(getClass().getName(), result);
31
32 test = new TestObject("Ernie");
33 result = StringMessageUtils.toString(test);
34 assertEquals(test.toString(), result);
35
36 test = new AnotherTestObject("Bert");
37 result = StringMessageUtils.toString(test);
38 assertEquals("Bert", result);
39
40 test = new String[]{"foo", "bar"};
41 result = StringMessageUtils.toString(test);
42 assertEquals("{foo,bar}", result);
43
44 test = new byte[]{1, 2};
45 result = StringMessageUtils.toString(test);
46 assertEquals("{1,2}", result);
47
48
49 test = new byte[StringMessageUtils.MAX_ELEMENTS + 100];
50 for (int i = 0; i < ((byte[]) test).length; i++)
51 {
52 ((byte[]) test)[i] = (byte) i;
53 }
54
55
56 result = StringMessageUtils.toString(test);
57 assertTrue(((String) result).endsWith("[..]}"));
58 assertEquals(StringMessageUtils.MAX_ELEMENTS - 1, StringUtils.countMatches((String) result, ","));
59
60 test = new long[]{5068875495743534L, 457635546759674L};
61 result = StringMessageUtils.toString(test);
62 assertEquals("{5068875495743534,457635546759674}", result);
63
64 test = new double[] {1.1, 2.02};
65 result = StringMessageUtils.toString(test);
66 assertEquals("{1.1,2.02}", result);
67
68
69 test = new ArrayList(100);
70 for (int i = 0; i < 100; i++)
71 {
72 ((Collection) test).add(new Integer(i));
73 }
74
75
76 result = StringMessageUtils.toString(test);
77 assertTrue(((String) result).endsWith("[..]]"));
78 assertEquals(StringMessageUtils.MAX_ELEMENTS - 1, StringUtils.countMatches((String) result, ","));
79
80 }
81
82 public void testFormattedString() throws Exception
83 {
84 String result;
85 String msg1 = "There is not substitution here";
86
87 result = StringMessageUtils.getFormattedMessage(msg1, null);
88 assertEquals(msg1, result);
89
90 result = StringMessageUtils.getFormattedMessage(msg1, new Object[]{});
91 assertEquals(msg1, result);
92
93 String msg2 = "There should be a variable {0}, {1} and {2}";
94 result = StringMessageUtils.getFormattedMessage(msg2, new Object[]{"here", "there", "everywhere"});
95 assertEquals("There should be a variable here, there and everywhere", result);
96 }
97
98 public void testBoilerPlateSingleLine()
99 {
100 String plate = StringMessageUtils.getBoilerPlate("Single message.", '*', 12);
101 assertEquals(SystemUtils.LINE_SEPARATOR + "************" + SystemUtils.LINE_SEPARATOR
102 + "* Single *" + SystemUtils.LINE_SEPARATOR + "* message. *"
103 + SystemUtils.LINE_SEPARATOR + "************", plate);
104 }
105
106 public void testBoilerPlate() throws Exception
107 {
108 List msgs = new ArrayList();
109 msgs.add("This");
110 msgs.add("is a");
111 msgs.add("Boiler Plate");
112
113 String plate = StringMessageUtils.getBoilerPlate(msgs, '*', 12);
114 assertEquals(SystemUtils.LINE_SEPARATOR + "************" + SystemUtils.LINE_SEPARATOR
115 + "* This *" + SystemUtils.LINE_SEPARATOR + "* is a *"
116 + SystemUtils.LINE_SEPARATOR + "* Boiler *" + SystemUtils.LINE_SEPARATOR
117 + "* Plate *" + SystemUtils.LINE_SEPARATOR + "************", plate);
118
119 }
120
121 public void testBoilerPlate2() throws Exception
122 {
123 List msgs = new ArrayList();
124 msgs.add("This");
125 msgs.add("is a");
126 msgs.add("Boiler Plate Message that should get wrapped to the next line if it is working properly");
127
128 String plate = StringMessageUtils.getBoilerPlate(msgs, '*', 12);
129 assertEquals(SystemUtils.LINE_SEPARATOR + "************" + SystemUtils.LINE_SEPARATOR
130 + "* This *" + SystemUtils.LINE_SEPARATOR + "* is a *"
131 + SystemUtils.LINE_SEPARATOR + "* Boiler *" + SystemUtils.LINE_SEPARATOR
132 + "* Plate *" + SystemUtils.LINE_SEPARATOR + "* Message *"
133 + SystemUtils.LINE_SEPARATOR + "* that *" + SystemUtils.LINE_SEPARATOR
134 + "* should *" + SystemUtils.LINE_SEPARATOR + "* get *"
135 + SystemUtils.LINE_SEPARATOR + "* wrapped *" + SystemUtils.LINE_SEPARATOR
136 + "* to the *" + SystemUtils.LINE_SEPARATOR + "* next *"
137 + SystemUtils.LINE_SEPARATOR + "* line if *" + SystemUtils.LINE_SEPARATOR
138 + "* it is *" + SystemUtils.LINE_SEPARATOR + "* working *"
139 + SystemUtils.LINE_SEPARATOR + "* properly *" + SystemUtils.LINE_SEPARATOR
140 + "************", plate);
141 }
142
143 public void testTruncate()
144 {
145 String msg = "this is a test message for truncating";
146 String result = StringMessageUtils.truncate(msg, 100, true);
147 assertEquals(msg, result);
148
149 result = StringMessageUtils.truncate(msg, 10, false);
150 assertEquals("this is a ...", result);
151
152 result = StringMessageUtils.truncate(msg, 10, true);
153 assertEquals("this is a ...[10 of 37]", result);
154 }
155
156 private class TestObject
157 {
158 private String name;
159
160 public TestObject(String name)
161 {
162 this.name = name;
163 }
164
165 public String getName()
166 {
167 return name;
168 }
169 }
170
171 private class AnotherTestObject extends TestObject
172 {
173 public AnotherTestObject(String name)
174 {
175 super(name);
176 }
177
178 public String toString()
179 {
180 return getName();
181 }
182 }
183
184 }