Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.StringUtilTest

LineHitsNoteSource
1  /*
2   * $Id: StringUtilTest.java 1268 2009-01-21 15:03:56Z amandel $
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   */
33  package org.jcoderz.commons.util;
34  
35  import java.io.UnsupportedEncodingException;
36  import junit.framework.TestCase;
37  import org.jcoderz.commons.ArgumentMalformedException;
38  
39  /**
40   * Tests the StringUtil class.
41   *
42   * @author Andreas Mandel
43   */
44100 public 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     {
56100       assertEquals(NULL_SHOULD_PRODUCE_NULL, null,
57              StringUtil.asciiToString(null));
58100       assertEquals(NULL_SHOULD_PRODUCE_NULL, null,
59              StringUtil.asciiToString(null, 0, 0));
60100    }
61  
62     /** testAsciiBytesToString with valid argument. */
63     public void testAsciiToString ()
64     {
65100       final String testString = "ABCDE";
66100       assertEquals("Teststring unexpected result.", testString,
67              StringUtil.asciiToString(testString.getBytes()));
68100       assertNull("null byte[] should return a null string",
69              StringUtil.asciiToString(null));
70100       assertEquals("Teststring unexpected result.", testString,
71              StringUtil.asciiToString(
72                    testString.getBytes(), 0, testString.length()));
73100       assertNull("null byte[] should return a null string",
74              StringUtil.asciiToString(null, 0, 0));
75100    }
76  
77     /**
78      * Tests the {@link StringUtil#asciiToString(byte[])}
79      * with non ASCII characters.
80      */
81     public void testAsciiToStringWithNonAsciiChars ()
82     {
83100       final String testString = "\u00c4\u00d6\u00dc";
84        try
85        {
86100          assertEquals("String should contain '?' only.",
87                 "???", StringUtil.asciiToString(
88                 testString.getBytes(Constants.ENCODING_ASCII)));
89        }
900       catch (UnsupportedEncodingException e)
91        {
920          fail("Ups, ASCII encoding not supported?" + e);
93100       }
94100    }
95  
96     /** testAsciiBytesToString with null argument. */
97     public void testToStringNull ()
98     {
99100       assertEquals(NULL_SHOULD_PRODUCE_NULL, null,
100              StringUtil.toString(null));
101100       assertEquals(NULL_SHOULD_PRODUCE_NULL, null,
102              StringUtil.toString(null, 0 , 0));
103100    }
104  
105     /** test bytesToString with valid argument. */
106     public void testToString ()
107     {
108100       final String testString = "ABCDE";
109100       assertEquals("Teststring unexpected result.", testString,
110              StringUtil.toString(testString.getBytes()));
111100    }
112  
113     /** test umlauteBytesToString with valid argument. */
114     public void testToStringUmlaute ()
115     {
116100       final String testString = "ABCDE\u00c4\u00d6\u00dc\u00e4\u00f6\u00fc"
117              + "\u00df\u00e1\u00b5";
118100       assertEquals("Teststring with umlauts unexpected result.", testString,
119              StringUtil.toString(StringUtil.toBytes(testString)));
120100    }
121  
122     /**
123      * Tests the method {@link StringUtil#isAscii(char)}.
124      */
125     public void testIsAscii ()
126     {
127        // positive tests
128100       assertTrue("test with valid ASCII char",
129              StringUtil.isAscii('A'));
130100       assertTrue("test with valid ASCII string",
131              StringUtil.isAscii("The brown fox."));
132        // negative tests
133100       assertFalse("test with invalid ASCII char",
134              StringUtil.isAscii('\u00dc'));
135100       assertFalse("test with invalid ASCII string",
136              StringUtil.isAscii("The brown \u00e4fox."));
137100    }
138  
139     /**
140      * Tests the method {@link StringUtil#isNullOrEmpty(String)}.
141      */
142     public void testIsNullOrEmpty ()
143     {
144100       assertTrue("null string should be true", StringUtil.isNullOrEmpty(null));
145100       assertTrue("empty string should be true", StringUtil.isNullOrEmpty(""));
146100(1)      assertFalse("any string should be false",
147              StringUtil.isNullOrEmpty(FOO_STRING));
148100    }
149  
150     /**
151      * Tests the method {@link StringUtil#isEmptyOrNull(String)}.
152      */
153     public void testIsEmptyOrNull ()
154     {
155100       assertTrue("null string should be true", StringUtil.isEmptyOrNull(null));
156100       assertTrue("empty string should be true", StringUtil.isEmptyOrNull(""));
157100       assertFalse("any string should be false",
158              StringUtil.isEmptyOrNull(FOO_STRING));
159100    }
160  
161     /**
162      * Tests the method {@link StringUtil#isNullOrBlank(String)}.
163      */
164     public void testIsNullOrBlank ()
165     {
166100       assertTrue("null string should be true", StringUtil.isNullOrBlank(null));
167100       assertTrue("empty string should be true", StringUtil.isNullOrBlank(""));
168100       assertTrue("tab string should be true", StringUtil.isNullOrBlank("\t"));
169100(2)      assertTrue("whitespace string should be true", StringUtil.isNullOrBlank(" "));
170100       assertFalse("any string should be false",
171              StringUtil.isNullOrBlank(FOO_STRING));
172100       assertFalse("any string should be false",
173            StringUtil.isNullOrBlank(" " + FOO_STRING + " "));
174100       assertFalse("'x ' string should be false",
175            StringUtil.isNullOrBlank("x "));
176100       assertFalse("' x' string should be false",
177            StringUtil.isNullOrBlank(" x"));
178100    }
179  
180     /**
181      * Tests the method {@link StringUtil#equals(String, String)}.
182      */
183     public void testEquals ()
184     {
185        // equals == true
186100       assertTrue("two null string references should be equal",
187              StringUtil.equals(null, null));
188100       assertTrue("same string reference should be equals",
189              StringUtil.equals(FOO_STRING, FOO_STRING));
190  
191        // equals == false
192100       assertFalse("string reference equals null reference should be false",
193              StringUtil.equals(null, FOO_STRING));
194100       assertFalse("string reference equals null reference should be false",
195              StringUtil.equals(FOO_STRING, null));
196100       assertFalse("different string reference should not be equals",
197              StringUtil.equals(FOO_STRING, "bar"));
198100    }
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     {
2070       final String smallStr = "xxx";
2080       final String mediumStr = "xxxxxxxxxxxxxxx";
209        // 123456789012345
2100       final String bigStr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
211        // 1234567890123456789012345678901234567890
212        // 1 2 3 4
213  
2140       assertEquals("Result should be " + MIN_LENGTH + " chars long", MIN_LENGTH,
215              StringUtil.fitToLength(smallStr, MIN_LENGTH, MAX_LENGTH).length());
2160       assertEquals("Result should be " + MAX_LENGTH + " chars long", MAX_LENGTH,
217              StringUtil.fitToLength(bigStr, MIN_LENGTH, MAX_LENGTH).length());
2180       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
2230       StringUtil.fitToLength(smallStr, MIN_LENGTH, MIN_LENGTH);
224        try
225        {
2260          StringUtil.fitToLength(smallStr, MAX_LENGTH, MIN_LENGTH);
2270          fail("Should throw exception if minLength is bigger than maxLength");
228        }
229100       catch (ArgumentMalformedException x)
230        {
231           // expected
2320       }
233        try
234        {
2350          StringUtil.fitToLength(null, MIN_LENGTH, MAX_LENGTH);
2360          fail("Should throw exception if string argument is null");
237        }
238100       catch (ArgumentMalformedException x)
239        {
240           // expected
2410       }
242100    }
243  
244     /**
245      * Tests the method {@link StringUtil#trimLengthLeft(String, int)}.
246      */
247     public void testTrimLeft ()
248     {
249100       final String trimmed = StringUtil.trimLengthLeft("12345", 1);
250100       assertEquals("Unexpected string length.",
251              1, trimmed.length());
252100       assertEquals("Unexpected result from trimLength", "5", trimmed);
253100    }
254  
255     /**
256      * Tests the method {@link StringUtil#padLeft(String, char, int)}.
257      */
258     public void testPadLeft ()
259     {
260100       final String paddedString = StringUtil.padLeft("", '0', MAX_LENGTH);
261100       assertEquals("Modified string length should be " + MAX_LENGTH,
262              MAX_LENGTH, paddedString.length());
263100       assertTrue("Modified string should contain only zeros, but was "
264              + paddedString, paddedString.matches("[0]{" + MAX_LENGTH + "}"));
265100    }
266  
267     /**
268      * Tests the method {@link StringUtil#contains(String, String)}.
269      */
270     public void testContains ()
271     {
272100       assertTrue("'aaa' is contained in 'bbbaaaa'.",
273                StringUtil.contains("bbbbaaaa", "aaa"));
274100       assertFalse("'ccc' is not contained in 'bbbaaaa'.",
275            StringUtil.contains("bbbbaaaa", "ccc"));
276100    }
277  
278  }

Findings in this File

f (3) Use assertEquals(x, y) instead of assertTrue(x.equals(y)) Ok.
f (4) Use assertEquals(x, y) instead of assertTrue(x.equals(y)) Ok.
i (1) 146 : 19 The String literal "any string should be false" appears 4 times in this file; the first occurrence is on line 146 (test code)
c (2) 169 : 0 Line is longer than 80 characters.