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