1   // 
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   //All rights reserved. 
4   //   
5   //Redistribution and use in source and binary forms, with or without modification,  
6   //are permitted provided that the following conditions are met: 
7   //   
8   //* Redistributions of source code must retain the above copyright notice,  
9   //this list of conditions and the following disclaimer. 
10  //* Redistributions in binary form must reproduce the above copyright notice,  
11  //this list of conditions and the following disclaimer in the documentation  
12  //and/or other materials provided with the distribution. 
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //nor the names of its contributors may be used to endorse or promote products  
15  //derived from this software without specific prior written permission. 
16  // 
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  //POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.utils;
30  
31  import java.io.UnsupportedEncodingException;
32  import java.util.Locale;
33  
34  import junit.framework.TestCase;
35  
36  /**
37   * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
38   *
39   * To change the template for this generated type comment go to
40   * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
41   */
42  public class StringUtilsTest extends TestCase
43  {
44  
45      /***
46       * Constructor for StringUtilsTest.
47       * @param arg0
48       */
49      public StringUtilsTest(String arg0)
50      {
51          super(arg0);
52      }
53  
54      public void testCookieValue()
55          throws Exception
56      {
57          assertEquals(StringUtils.cookieNameSafeString("test"),"test");
58          assertEquals(StringUtils.cookieNameSafeString("te.st"),"te.st");
59          assertEquals(StringUtils.cookieNameSafeString("te=st"),"te.st");
60          assertEquals(StringUtils.cookieNameSafeString("te=st",'='),"te.st");
61          assertNull(StringUtils.cookieNameSafeString(null));
62          
63      }
64      
65      public void testBackslashEscape()
66              throws Exception
67      {
68          assertEquals(StringUtils.backslashEscape("te.st", "."),"te//.st");
69      }
70      
71      public void testUnicodeEscapeExpand()
72                  throws Exception
73      {    
74          assertEquals(StringUtils.escapeNonASCIICharacters("a\u0123bc\u1234"),"a//u0123bc//u1234");
75          assertEquals(StringUtils.expandUnicodeEscapes("a//u1234b"),"a\u1234b");
76          
77          try
78          {
79              StringUtils.expandUnicodeEscapes("a//u12b");
80              fail("should throw the exception");
81          }
82          catch(IllegalArgumentException e)
83          {
84              //ok!
85          }
86          try
87          {
88              StringUtils.expandUnicodeEscapes("a//u12xfe");
89              fail("should throw the exception");
90          }
91          catch(Exception e)
92          {
93              //ok!
94          }
95          try
96          {
97              StringUtils.expandUnicodeEscapes("a//ufd4e");
98              fail("should throw the exception");
99          }
100         catch(Exception e)
101         {
102             //ok!
103         }
104 
105         assertEquals("abc",StringUtils.expandUnicodeEscapes("abc"));
106     }
107     
108     public void testGetLocale()
109     {
110         Locale locale;
111         // Polish
112         locale = StringUtils.getLocale("pl");
113         assertEquals("pl", locale.getLanguage());
114         
115         // Portugal / Brasil
116         locale = StringUtils.getLocale("pt_BR");
117         assertEquals("pt", locale.getLanguage());
118         assertEquals("BR", locale.getCountry());
119         
120         // Spanish / Spain / Traditional collation
121         locale = StringUtils.getLocale("es_ES_Traditional");
122         assertEquals("es", locale.getLanguage());
123         assertEquals("ES", locale.getCountry());
124         assertEquals("Traditional", locale.getVariant());
125         
126         // Spanish / Spain / Traditional collation / Windows
127         locale = StringUtils.getLocale("es_ES_Traditional_Win");
128         assertEquals("es", locale.getLanguage());
129         assertEquals("ES", locale.getCountry());
130         assertEquals("Traditional_Win", locale.getVariant());
131 
132     }
133     
134     public void testGetByteCount()
135         throws Exception
136     {
137         assertEquals(StringUtils.getByteCount("abc","ISO-8859-1"),3);
138         assertEquals(StringUtils.getByteCount("abc","UTF-16"),6);
139         assertEquals(StringUtils.getByteCount("abc","UTF-8"),3);
140         try
141         {
142             assertEquals(StringUtils.getByteCount("abc","UNSUPPORTED"),6);
143             fail("should throw the exception");
144         }
145         catch(UnsupportedEncodingException e)
146         {
147             //ok!
148         }
149         StringBuilder sb = new StringBuilder();
150         for(int i = 0; i < 4000; i++)
151         {
152             sb.append("www.objectledge.org ");
153         }
154         assertEquals(StringUtils.getByteCount(sb.toString(),"UTF-8"),80000);
155     }
156     
157     public void testSubstitute()
158         throws Exception
159     {
160         String[] values = new String[]{"foo","bar"};
161         assertEquals(StringUtils.substitute("abc$1",values),"abcfoo");
162         assertEquals(StringUtils.substitute("abc$2",values),"abcbar");
163         assertEquals(StringUtils.substitute("ab$1c$*",values),"abfoocbar");
164         assertEquals(StringUtils.substitute("ab$$c",values),"ab$c");
165         assertEquals(StringUtils.substitute("a$1b$2c$*",values),"afoobbarc");
166         assertEquals(StringUtils.substitute("a$1b$2c$3",values),"afoobbarc");
167         assertEquals(StringUtils.substitute("a$1b$1c$1",values),"afoobfoocfoo");
168         assertEquals(StringUtils.substitute("a$+",values),"a$+");
169     }
170     
171     public void testIndent()
172         throws Exception
173     {
174         StringBuilder sb = new StringBuilder();
175         StringUtils.indent(sb, 4);
176         assertEquals("    ", sb.toString());        
177     }
178     
179     public void testIsEmpty()
180         throws Exception
181     {
182         String str = null;
183         assertTrue(StringUtils.isEmpty(str));
184         str = "";
185         assertTrue(StringUtils.isEmpty(str));
186         str = "not-empty";
187         assertFalse(StringUtils.isEmpty(str));
188     }
189     
190     public void testShorten()
191     {
192         final String suff = " ...";
193         //           123456789012345678901234567890123456789
194         String p  = "Aaa bbbb, cccc ddd. Yyyy uuuu xxxx.";
195         String s1 = "Aaa bbbb, cccc ddd ...";                // 20 10
196         String s2 = "Aaa bbbb, cccc ...";                    // 15 10
197         String s3 = "Aaa bbbb, ccc ...";                     // 12 10
198         
199         assertEquals(s1, StringUtils.shortenString(p, 10, 20, suff));
200         assertEquals(s2, StringUtils.shortenString(p, 10, 15, suff));
201         assertEquals(s3, StringUtils.shortenString(p, 11, 13, suff));
202     }
203 
204     public void testParseBytesSize()
205     {
206         assertEquals(124L, StringUtils.parseBytesSize("124B"));
207         assertEquals(125L, StringUtils.parseBytesSize("124.55555 B"));
208         assertEquals(1536L, StringUtils.parseBytesSize("1.5kB"));
209         assertEquals(1153434L, StringUtils.parseBytesSize("1.1 MB"));
210         assertEquals(1153434L, StringUtils.parseBytesSize("1.1mb"));
211         assertEquals(1181116006L, StringUtils.parseBytesSize("1.1GB"));
212         assertEquals(1181116006L, StringUtils.parseBytesSize("1.1 gb"));
213 
214         assertEquals(-1L, StringUtils.parseBytesSize("1.sd1 GB"));
215         assertEquals(-1L, StringUtils.parseBytesSize("1.sd1G B"));
216         assertEquals(-1L, StringUtils.parseBytesSize(""));
217         assertEquals(-1L, StringUtils.parseBytesSize(null));
218     }
219 
220 }