Project Report: fawkez

Packagesummary org.jcoderz.commons.types.samples

org.jcoderz.commons.types.samples.SampleFixPointTest

LineHitsNoteSource
1  /*
2   * $Id: SampleFixPointTest.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2008, 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.types.samples;
34  
35  import java.math.BigDecimal;
36  
37  import org.jcoderz.commons.ArgumentFractionDigitsViolationException;
38  import org.jcoderz.commons.ArgumentMalformedException;
39  import org.jcoderz.commons.ArgumentMaxValueViolationException;
40  import org.jcoderz.commons.ArgumentMinValueViolationException;
41  
42  import junit.framework.TestCase;
43  
44  /**
45   * Test the FixPoint class generator.
46   *
47   * @author Andreas Mandel
48   */
49100 public class SampleFixPointTest
50      extends TestCase
51  {
52      private static final int INT_TEST_VALUE = -100;
53      private static final long LONG_TEST_VALUE = 40;
54  
55      /**
56       * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#hashCode()}.
57       */
58      public void testHashCode ()
59      {
6075(1)        assertFalse("Different values should have different hashCode.",
61              SampleFixPoint.fromString("1").hashCode()
62              == SampleFixPoint.fromString("2").hashCode());
63100     }
64  
65      /**
66       * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#hashCode()}.
67       */
68      public void testHashCodeSame ()
69      {
70100         assertEquals("Same values should have different hashCode.",
71              SampleFixPoint.fromString("1").hashCode(),
72              SampleFixPoint.fromString("1").hashCode());
73100     }
74      /**
75       * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#intValue()}.
76       */
77      public void testIntValue ()
78      {
79100         assertEquals("Int value should not change.", INT_TEST_VALUE,
80              SampleFixPoint.valueOf(INT_TEST_VALUE).intValue());
81100     }
82  
83      /**
84       * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#longValue()}.
85       */
86      public void testLongValue ()
87      {
88100         assertEquals("Long value should not change.", LONG_TEST_VALUE,
89              SampleFixPoint.valueOf(LONG_TEST_VALUE).longValue());
90100     }
91  
92      /**
93       * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#fromString(java.lang.String)}.
94       */
95      public void testFromString ()
96      {
97100         assertEquals("From string should not change value.", LONG_TEST_VALUE,
98              SampleFixPoint.fromString(
99                  String.valueOf(LONG_TEST_VALUE)).longValue());
100100     }
101  
102      /**
103       * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#fromString(java.lang.String)}.
104       */
105      public void testFromStringDecimal ()
106      {
107100         final BigDecimal test = new BigDecimal("-1.23");
108100         assertEquals("From string should not change value.", test,
109              SampleFixPoint.fromString(String.valueOf(test)).toBigDecimal());
110100     }
111  
112      /**
113       * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#valueOf(java.math.BigDecimal)}.
114       */
115      public void testValueOfBigDecimal ()
116      {
117100         final BigDecimal test = new BigDecimal("-1.23");
118100         assertEquals("From big decimal should not change value.", test,
119              SampleFixPoint.valueOf(test).toBigDecimal());
120100     }
121  
122      /**
123       * Test method for fraction digit overflow.
124       */
125      public void testFractionDigits ()
126      {
127          try
128          {
1290(2)            SampleFixPoint s = SampleFixPoint.fromString("1.234");
1300(3)            fail("Expected exception but got " + s + ".");
131          }
132100         catch (ArgumentFractionDigitsViolationException ex)
133          {
134              // Expected...
1350         }
136100     }
137  
138      /**
139       * Test method for max value overflow.
140       */
141      public void testMaxValue ()
142      {
143          try
144          {
1450(4)            SampleFixPoint s = SampleFixPoint.fromString("12345.67");
1460             fail("Expected exception but got " + s + ".");
147          }
148100         catch (ArgumentMaxValueViolationException ex)
149          {
150              // Expected...
1510         }
152100     }
153  
154      /**
155       * Test method for min value overflow.
156       */
157      public void testMinValue ()
158      {
159          try
160          {
1610(5)            SampleFixPoint s = SampleFixPoint.fromString("-345.67");
1620             fail("Expected exception but got " + s + ".");
163          }
164100         catch (ArgumentMinValueViolationException ex)
165          {
166              // Expected...
1670         }
168100     }
169  
170      /**
171       * Test method for malformed string.
172       */
173      public void testMalformedString ()
174      {
175          try
176          {
1770(6)            SampleFixPoint s = SampleFixPoint.fromString("0xFF12");
1780             fail("Expected exception but got " + s + ".");
179          }
180100         catch (ArgumentMalformedException ex)
181          {
182              // Expected...
1830         }
184100     }
185  
186      /**
187       * Test method for comparison.
188       */
189      public void testComparison ()
190      {
191100         assertEquals("Comparing equal values", 0,
192 (7)            SampleFixPoint.fromString("0.01")
193              .compareTo(SampleFixPoint.fromString("0.01")));
19475         assertTrue("Comparing different values",
195              SampleFixPoint.fromString("0.01")
196                  .compareTo(SampleFixPoint.fromString("0.02")) < 0);
19775         assertTrue("Comparing different values",
198              SampleFixPoint.fromString("0.02")
199                  .compareTo(SampleFixPoint.fromString("0.01")) > 0);
200100     }
201  
202      /**
203       * Test value of static members.
204       */
205      public void testStatics ()
206      {
207100         assertEquals("SampleFixPoint.MAX_VALUE_SCALED",
208 (8)            999, SampleFixPoint.MAX_VALUE_SCALED);
209100         assertEquals("SampleFixPoint.MIN_VALUE_SCALED",
210 (9)            -100, SampleFixPoint.MIN_VALUE_SCALED);
211100         assertEquals("SampleFixPoint.MAX_VALUE",
212              "999.99", SampleFixPoint.MAX_VALUE.toString());
213100         assertEquals("SampleFixPoint.MIN_VALUE",
214              "-100.50", SampleFixPoint.MIN_VALUE.toString());
215100         assertEquals("SampleFixPoint.SCALE",
216 (10)            2, SampleFixPoint.SCALE);
217100         assertEquals("SampleFixPoint.DECIMAL_SCALE",
218 (11)            100, SampleFixPoint.DECIMAL_SCALE);
219100         assertEquals("SampleFixPoint.TOTAL_DIGITS",
220 (12)            5, SampleFixPoint.TOTAL_DIGITS);
221100     }
222  }

Findings in this File

c (1) 60 : 9 Use assertSame(x, y) instead of assertTrue(x==y), or assertNotSame(x,y) vs assertFalse(x==y)
c (2) 129 : 28 Variable 's' should be declared final.
i (3) 130 : 18 The String literal "Expected exception but got " appears 4 times in this file; the first occurrence is on line 130 (test code)
c (4) 145 : 28 Variable 's' should be declared final.
c (5) 161 : 28 Variable 's' should be declared final.
c (6) 177 : 28 Variable 's' should be declared final.
i (7) 192 : 39 The String literal "0.01" appears 4 times in this file; the first occurrence is on line 192 (test code)
c (8) 208 : 13 '999' is a magic number.
c (9) 210 : 13 '-100' is a magic number.
c (10) 216 : 13 '2' is a magic number.
c (11) 218 : 13 '100' is a magic number.
c (12) 220 : 13 '5' is a magic number.