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