1   /* Version 1.0 based on Apache Software License 1.1
2    *
3    * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4    * 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   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   *
17   * 3. The end-user documentation included with the redistribution, if any,
18   *    must include the following acknowledgment:
19   *
20   *    "This product includes software developed by DBMonster developers
21   *    (http://dbmonster.kernelpanic.pl/)."
22   *
23   *  Alternately, this acknowledgment may appear in the software itself,
24   *  if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The name "DBMonster" must not be used to endorse or promote products
27   *    derived from this software without prior written permission. For
28   *    written permission, please contact piotr.maj@kernelpanic.pl.
29   *
30   * 5. Products derived from this software may not be called "DBMonster",
31   *    nor may "DBMonster" appear in their name, without prior written
32   *    permission of Piotr Maj.
33   *
34   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44   * POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package pl.kernelpanic.dbmonster.test;
48  
49  import java.util.Random;
50  
51  import junit.framework.TestCase;
52  
53  import pl.kernelpanic.dbmonster.DBMonster;
54  import pl.kernelpanic.dbmonster.DBMonsterContext;
55  import pl.kernelpanic.dbmonster.generator.NumberGenerator;
56  
57  /***
58   * Test NumberGenerator.
59   *
60   * @author Piotr Maj <piotr.maj@kernelpanic.pl>
61   *
62   * @version $Id: NumberGeneratorTest.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
63   */
64  public class NumberGeneratorTest extends TestCase {
65  
66      DBMonsterContext context = new DBMonsterContext();
67      NumberGenerator g = new NumberGenerator();
68      
69      protected void setUp() throws Exception {
70          context.setProperty(DBMonster.RANDOM_KEY, new Random());
71          g.initialize(context);
72      }
73  
74      public final void testShort() throws Exception {
75          g.setMaxValue("" + Short.MAX_VALUE);
76          g.setMinValue("" + Short.MIN_VALUE);
77          g.setReturnedType("short");
78          for (int i = 0; i < 10000; i++) {
79              Short value = (Short) g.generate();
80              assertNotNull(value);
81          }
82      }
83  
84      public final void testInteger() throws Exception {
85          g.setMaxValue("" + Integer.MAX_VALUE);
86          g.setMinValue("" + Integer.MIN_VALUE);
87          g.setReturnedType("integer");
88          for (int i = 0; i < 1000; i++) {
89              Integer value = (Integer) g.generate();
90              assertNotNull(value);
91          }
92      }
93  
94      public final void testLong() throws Exception {
95          g.setMaxValue("" + Long.MAX_VALUE);
96          g.setMinValue("" + Long.MIN_VALUE);
97          g.setReturnedType("long");
98          for (int i = 0; i < 1000; i++) {
99              Long value = (Long) g.generate();
100             assertNotNull(value);
101         }
102     }
103 
104     public final void testFloat() throws Exception {
105         g.setMaxValue("" + Float.MAX_VALUE);
106         g.setMinValue("" + Float.MIN_VALUE);
107         g.setReturnedType("float");
108         for (int i = 0; i < 1000; i++) {
109             Float value = (Float) g.generate();
110             assertNotNull(value);
111         }
112     }
113 
114     public final void testDouble() throws Exception {
115         g.setMaxValue("" + Double.MAX_VALUE);
116         g.setMinValue("" + Double.MIN_VALUE);
117         g.setReturnedType("double");
118         for (int i = 0; i < 1000; i++) {
119             Double value = (Double) g.generate();
120             assertNotNull(value);
121         }
122     }
123 
124     public final void testSame() throws Exception {
125         g.setMinValue("" + Integer.MIN_VALUE);
126         g.setMaxValue("" + Integer.MIN_VALUE);
127         g.setReturnedType("integer");
128         for (int i = 0; i < 1000; i++) {
129             Integer value = (Integer) g.generate();
130             assertEquals(value, new Integer(Integer.MIN_VALUE));
131         }
132     }
133 
134     public final void testNulls() throws Exception {
135         g.setNulls(100);
136         for (int i = 0; i < 1000; i++) {
137             Object rs = g.generate();
138             assertNull(rs);
139         }
140     }
141 }