root/trunk/test/java/org/jcoderz/commons/util/StringUtilTest.java

Revision 1268, 10.0 kB (checked in by amandel, 3 years ago)

New method trimLengthLeft to cut of the left part of a string if the string is to long. There are several cases where the right part is more significant than the left eg when dealing with package names etc.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.util;
34
35import java.io.UnsupportedEncodingException;
36import junit.framework.TestCase;
37import org.jcoderz.commons.ArgumentMalformedException;
38
39/**
40 * Tests the StringUtil class.
41 *
42 * @author Andreas Mandel
43 */
44public class StringUtilTest
45      extends TestCase
46{
47   private static final String FOO_STRING = "foo";
48   private static final String NULL_SHOULD_PRODUCE_NULL
49         = "Null should produce null.";
50   private static final int MIN_LENGTH = 10;
51   private static final int MAX_LENGTH = 20;
52
53   /** testAsciiBytesToString with null argument. */
54   public void testAsciiToStringNull ()
55   {
56      assertEquals(NULL_SHOULD_PRODUCE_NULL, null,
57            StringUtil.asciiToString(null));
58      assertEquals(NULL_SHOULD_PRODUCE_NULL, null,
59            StringUtil.asciiToString(null, 0, 0));
60   }
61
62   /** testAsciiBytesToString with valid argument. */
63   public void testAsciiToString ()
64   {
65      final String testString = "ABCDE";
66      assertEquals("Teststring unexpected result.", testString,
67            StringUtil.asciiToString(testString.getBytes()));
68      assertNull("null byte[] should return a null string",
69            StringUtil.asciiToString(null));
70      assertEquals("Teststring unexpected result.", testString,
71            StringUtil.asciiToString(
72                  testString.getBytes(), 0, testString.length()));
73      assertNull("null byte[] should return a null string",
74            StringUtil.asciiToString(null, 0, 0));
75   }
76
77   /**
78    * Tests the {@link StringUtil#asciiToString(byte[])}
79    * with non ASCII characters.
80    */
81   public void testAsciiToStringWithNonAsciiChars ()
82   {
83      final String testString = "\u00c4\u00d6\u00dc";
84      try
85      {
86         assertEquals("String should contain '?' only.",
87               "???", StringUtil.asciiToString(
88               testString.getBytes(Constants.ENCODING_ASCII)));
89      }
90      catch (UnsupportedEncodingException e)
91      {
92         fail("Ups, ASCII encoding not supported?" + e);
93      }
94   }
95
96   /** testAsciiBytesToString with null argument. */
97   public void testToStringNull ()
98   {
99      assertEquals(NULL_SHOULD_PRODUCE_NULL, null,
100            StringUtil.toString(null));
101      assertEquals(NULL_SHOULD_PRODUCE_NULL, null,
102            StringUtil.toString(null, 0 , 0));
103   }
104
105   /** test bytesToString with valid argument. */
106   public void testToString ()
107   {
108      final String testString = "ABCDE";
109      assertEquals("Teststring unexpected result.", testString,
110            StringUtil.toString(testString.getBytes()));
111   }
112
113   /** test umlauteBytesToString with valid argument. */
114   public void testToStringUmlaute ()
115   {
116      final String testString = "ABCDE\u00c4\u00d6\u00dc\u00e4\u00f6\u00fc"
117            + "\u00df\u00e1\u00b5";
118      assertEquals("Teststring with umlauts unexpected result.", testString,
119            StringUtil.toString(StringUtil.toBytes(testString)));
120   }
121
122   /**
123    * Tests the method {@link StringUtil#isAscii(char)}.
124    */
125   public void testIsAscii ()
126   {
127      // positive tests
128      assertTrue("test with valid ASCII char",
129            StringUtil.isAscii('A'));
130      assertTrue("test with valid ASCII string",
131            StringUtil.isAscii("The brown fox."));
132      // negative tests
133      assertFalse("test with invalid ASCII char",
134            StringUtil.isAscii('\u00dc'));
135      assertFalse("test with invalid ASCII string",
136            StringUtil.isAscii("The brown \u00e4fox."));
137   }
138
139   /**
140    * Tests the method {@link StringUtil#isNullOrEmpty(String)}.
141    */
142   public void testIsNullOrEmpty ()
143   {
144      assertTrue("null string should be true", StringUtil.isNullOrEmpty(null));
145      assertTrue("empty string should be true", StringUtil.isNullOrEmpty(""));
146      assertFalse("any string should be false",
147            StringUtil.isNullOrEmpty(FOO_STRING));
148   }
149
150   /**
151    * Tests the method {@link StringUtil#isEmptyOrNull(String)}.
152    */
153   public void testIsEmptyOrNull ()
154   {
155      assertTrue("null string should be true", StringUtil.isEmptyOrNull(null));
156      assertTrue("empty string should be true", StringUtil.isEmptyOrNull(""));
157      assertFalse("any string should be false",
158            StringUtil.isEmptyOrNull(FOO_STRING));
159   }
160
161   /**
162    * Tests the method {@link StringUtil#isNullOrBlank(String)}.
163    */
164   public void testIsNullOrBlank ()
165   {
166      assertTrue("null string should be true", StringUtil.isNullOrBlank(null));
167      assertTrue("empty string should be true", StringUtil.isNullOrBlank(""));
168      assertTrue("tab string should be true", StringUtil.isNullOrBlank("\t"));
169      assertTrue("whitespace string should be true", StringUtil.isNullOrBlank(" "));
170      assertFalse("any string should be false",
171            StringUtil.isNullOrBlank(FOO_STRING));
172      assertFalse("any string should be false",
173          StringUtil.isNullOrBlank(" " + FOO_STRING + " "));
174      assertFalse("'x ' string should be false",
175          StringUtil.isNullOrBlank("x "));
176      assertFalse("' x' string should be false",
177          StringUtil.isNullOrBlank(" x"));
178   }
179
180   /**
181    * Tests the method {@link StringUtil#equals(String, String)}.
182    */
183   public void testEquals ()
184   {
185      // equals == true
186      assertTrue("two null string references should be equal",
187            StringUtil.equals(null, null));
188      assertTrue("same string reference should be equals",
189            StringUtil.equals(FOO_STRING, FOO_STRING));
190
191      // equals == false
192      assertFalse("string reference equals null reference should be false",
193            StringUtil.equals(null, FOO_STRING));
194      assertFalse("string reference equals null reference should be false",
195            StringUtil.equals(FOO_STRING, null));
196      assertFalse("different string reference should not be equals",
197            StringUtil.equals(FOO_STRING, "bar"));
198   }
199
200   /**
201    * Tests the method {@link StringUtil#fitToLength(String, int, int)}.
202    * @throws Exception in case of an unexpected error.
203    */
204   public void testFitToLength ()
205         throws Exception
206   {
207      final String smallStr = "xxx";
208      final String mediumStr = "xxxxxxxxxxxxxxx";
209      //                        123456789012345
210      final String bigStr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
211      //                     1234567890123456789012345678901234567890
212      //                              1         2         3         4
213
214      assertEquals("Result should be " + MIN_LENGTH + " chars long", MIN_LENGTH,
215            StringUtil.fitToLength(smallStr, MIN_LENGTH, MAX_LENGTH).length());
216      assertEquals("Result should be " + MAX_LENGTH + " chars long", MAX_LENGTH,
217            StringUtil.fitToLength(bigStr, MIN_LENGTH, MAX_LENGTH).length());
218      assertEquals("String that fits between MIN_LENGTH and MAX_LENGTH "
219            + "should be returned unmodified", mediumStr,
220            StringUtil.fitToLength(mediumStr, MIN_LENGTH, MAX_LENGTH));
221
222      // boundary tests
223      StringUtil.fitToLength(smallStr, MIN_LENGTH, MIN_LENGTH);
224      try
225      {
226         StringUtil.fitToLength(smallStr, MAX_LENGTH, MIN_LENGTH);
227         fail("Should throw exception if minLength is bigger than maxLength");
228      }
229      catch (ArgumentMalformedException x)
230      {
231         // expected
232      }
233      try
234      {
235         StringUtil.fitToLength(null, MIN_LENGTH, MAX_LENGTH);
236         fail("Should throw exception if string argument is null");
237      }
238      catch (ArgumentMalformedException x)
239      {
240         // expected
241      }
242   }
243
244   /**
245    * Tests the method {@link StringUtil#trimLengthLeft(String, int)}.
246    */
247   public void testTrimLeft ()
248   {
249      final String trimmed = StringUtil.trimLengthLeft("12345", 1);
250      assertEquals("Unexpected string length.",
251            1, trimmed.length());
252      assertEquals("Unexpected result from trimLength", "5", trimmed);
253   }
254
255   /**
256    * Tests the method {@link StringUtil#padLeft(String, char, int)}.
257    */
258   public void testPadLeft ()
259   {
260      final String paddedString = StringUtil.padLeft("", '0', MAX_LENGTH);
261      assertEquals("Modified string length should be " + MAX_LENGTH,
262            MAX_LENGTH, paddedString.length());
263      assertTrue("Modified string should contain only zeros, but was "
264            + paddedString, paddedString.matches("[0]{" + MAX_LENGTH + "}"));
265   }
266
267   /**
268    * Tests the method {@link StringUtil#contains(String, String)}.
269    */
270   public void testContains ()
271   {
272      assertTrue("'aaa' is contained in 'bbbaaaa'.",
273              StringUtil.contains("bbbbaaaa", "aaa"));
274      assertFalse("'ccc' is not contained in 'bbbaaaa'.",
275          StringUtil.contains("bbbbaaaa", "ccc"));
276   }
277
278}
Note: See TracBrowser for help on using the browser.