Project Report: fawkez

Packagesummary org.jcoderz.commons.logging

org.jcoderz.commons.logging.WhitespaceFormatTest

LineHitsNoteSource
1  /*
2   * $Id: WhitespaceFormatTest.java 1011 2008-06-16 17:57:36Z 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.logging;
34  
35  
36  import java.nio.CharBuffer;
37  import java.text.Format;
38  import java.text.ParseException;
39  import org.jcoderz.commons.TestCase;
40  
41  /**
42   * This class tests the whitespace formatter.
43   *
44   */
45100(1)public class WhitespaceFormatTest
46        extends TestCase
47  {
48     private static final char PRESERVED_CHAR = '\u0020';
49  
50     /**
51      * Tests formatting of strings, which contains only '\u00A0' as white space.
52      */
53     public void testSimpleWhiteSpace ()
54     {
55100(2)      final String t1 = "This is a string.";
56100       final String t2 = " This is a string with long whitespace.";
57100       final String r1 = WhitespaceFormat.format(t1);
58100       final String r2 = WhitespaceFormat.format(t2);
59100       final CharBuffer tb1 = CharBuffer.wrap(t1);
60100       final CharBuffer tb2 = CharBuffer.wrap(t2);
61100       final CharBuffer rb1 = WhitespaceFormat.format(tb1);
62100       final CharBuffer rb2 = WhitespaceFormat.format(tb2);
63100       compare(t1, r1);
64100       compare(t2, r2);
65100       compare(tb1, rb1);
66100       compare(tb2, rb2);
67100    }
68  
69     /**
70      * Tests formatting of strings, with tabs as white space.
71      */
72     public void testTabWhiteSpace ()
73     {
74100       final String t1 = "This is a string.";
75100       final String t2 = "\tThis is\t\ta\tstring \twith\t\t tabs.";
76100       reformatAndCheck(t1, t2);
77100       parseAndCompare(String.valueOf(PRESERVED_CHAR), PRESERVED_CHAR + t2);
78100    }
79  
80  
81     /**
82      * Tests formatting of strings, with line feeds as white space.
83      */
84     public void testLfWhiteSpace ()
85     {
86100       final String t1 = "This is a string.";
87100       final String t2 = "\n\nThis is\na string with\n line feeds.";
88100       reformatAndCheck(t1, t2);
89100       parseAndCompare(
90              String.valueOf(PRESERVED_CHAR) + String.valueOf(PRESERVED_CHAR),
91              String.valueOf(PRESERVED_CHAR) + String.valueOf(PRESERVED_CHAR) + t2
92              );
93100    }
94  
95     /**
96      * Tests formatting of strings, with mixed whitespaces.
97      */
98     public void testMixedWhiteSpace ()
99     {
100100       final String t1 = "This is a string.";
101100       final String t2
102              = "This is\na\tstring\t \twith\n mixed\n\n \twhite\nspace.";
103  
104100       reformatAndCheck(t1, t2);
105100    }
106  
107  
108     private void reformatAndCheck (final String t1, final String t2)
109     {
110100        final String r1 = WhitespaceFormat.format(t1);
111100          final String r2 = WhitespaceFormat.format(t2);
112100          final CharBuffer tb1 = CharBuffer.wrap(t1);
113100          final CharBuffer tb2 = CharBuffer.wrap(t2);
114100          final CharBuffer rb1 = WhitespaceFormat.format(tb1);
115100          final CharBuffer rb2 = WhitespaceFormat.format(tb2);
116100          compare(t1, r1);
117100          parseAndCompare(t1, r1);
118100          compare(t2, r2);
119100          parseAndCompare(t2, r2);
120100          compare(tb1, rb1);
121100          compare(tb2, rb2);
122100    }
123  
124     private void compare (final String source, final String formatted)
125     {
12675       assertTrue("Formatted string must not be longer than source string.",
127              source.length() >= formatted.length());
128  
129100       assertNotNull("The formatted string must not be null", formatted);
130  
131100       int fIdx = 0;
132100       boolean firstWs = true;
133  
134100       for (int sIdx = 0; sIdx < source.length(); ++sIdx)
135        {
136100          final char sc = source.charAt(sIdx);
137100          final char fc = formatted.charAt(fIdx);
138  
139100          if (Character.isWhitespace(sc) && (sc != PRESERVED_CHAR))
140           {
141100             if (firstWs)
142              {
143100                assertEquals("Formatted char must be whitespace",
144                       String.valueOf(fc), String.valueOf(PRESERVED_CHAR));
145100                fIdx++;
146100                firstWs = false;
147              }
148           }
149           else
150           {
151100             assertEquals("Formatted char must match source char",
152                    String.valueOf(fc), String.valueOf(sc));
153100             fIdx++;
154100             firstWs = true;
155           }
156        }
157100    }
158  
159     private void compare (final CharBuffer source, final CharBuffer formatted)
160     {
161100(3)(4)      compare(source.toString(), formatted.toString());
162100    }
163  
164     private void parseAndCompare (final String source, final String formatted)
165     {
166100       final Format format = new WhitespaceFormat();
167100       String parsed = null;
168        try
169        {
170100          parsed = (String) format.parseObject(formatted);
171        }
1720       catch (ParseException ex)
173        {
1740          fail("Got a parse exception: " + ex);
175100       }
176100       if (parsed != null)
177        {
178100          compare(source, parsed);
179        }
180100    }
181  }

Findings in this File

c (1) 45 : 0 Type Javadoc comment is missing an @author tag.
i (2) 55 : 25 The String literal "This is a string." appears 4 times in this file; the first occurrence is on line 55 (test code)
i (3) 161 : 0 method org.jcoderz.commons.logging.WhitespaceFormatTest.compare(CharBuffer, CharBuffer) needlessly defines parameter with concrete classes (test code) Decreased severity from 'warning' for testcode.
i (4) 161 : 0 method org.jcoderz.commons.logging.WhitespaceFormatTest.compare(CharBuffer, CharBuffer) needlessly defines parameter with concrete classes (test code) Decreased severity from 'warning' for testcode.