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.sql.Date;
50  import java.sql.Time;
51  import java.sql.Timestamp;
52  import java.util.Random;
53  
54  import junit.framework.TestCase;
55  
56  import pl.kernelpanic.dbmonster.DBMonster;
57  import pl.kernelpanic.dbmonster.DBMonsterContext;
58  import pl.kernelpanic.dbmonster.generator.DateTimeGenerator;
59  
60  /***
61   * Test NumberGenerator.
62   *
63   * @author Piotr Maj <piotr.maj@kernelpanic.pl>
64   *
65   * @version $Id: DateTimeGeneratorTest.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
66   */
67  public class DateTimeGeneratorTest extends TestCase {
68  
69      DBMonsterContext context = new DBMonsterContext();
70      DateTimeGenerator g = new DateTimeGenerator();
71      
72      protected void setUp() throws Exception {
73          context.setProperty(DBMonster.RANDOM_KEY, new Random());
74          g.initialize(context);
75      }
76  
77      public final void testDate() throws Exception {
78          try {
79              g.setStartDate("1980-01-01");
80              g.setEndDate("2003-01-02");
81              g.setReturnedType("date");
82              for (int i = 0; i < 1000; i++) {
83                  Date value = (Date) g.generate();
84                  assertNotNull(value);
85              }
86          } catch (Exception e) {
87              System.out.println(e.getMessage());
88          }
89      }
90  
91      public final void testTimestamp() throws Exception {
92          try {
93              g.setStartDate("1980-01-01");
94              g.setEndDate("2003-01-02");
95              g.setReturnedType("timestamp");
96              for (int i = 0; i < 1000; i++) {
97                  Timestamp value = (Timestamp) g.generate();
98                  assertNotNull(value);
99              }
100         } catch (Exception e) {
101             System.out.println(e.getMessage());
102         }
103     }
104 
105     public final void testTime() throws Exception {
106         try {
107             g.setStartDate("1980-01-01 00:00:00.000");
108             g.setEndDate("1980-01-01 00:15:00.000");
109             g.setReturnedType("time");
110             for (int i = 0; i < 1000; i++) {
111                 Time value = (Time) g.generate();
112                 assertNotNull(value);
113             }
114         } catch (Exception e) {
115             System.out.println(e.getMessage());
116         }
117     }
118 
119     public final void testNulls() throws Exception {
120         g.setNulls(100);
121         for (int i = 0; i < 1000; i++) {
122             Object rs = g.generate();
123             assertNull(rs);
124         }
125     }
126 }