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.net.URL;
50  import java.sql.Connection;
51  import java.sql.ResultSet;
52  import java.sql.Statement;
53  import java.util.Properties;
54  
55  import junit.framework.TestCase;
56  import pl.kernelpanic.dbmonster.DBMonster;
57  import pl.kernelpanic.dbmonster.connection.ConnectionProvider;
58  import pl.kernelpanic.dbmonster.connection.DBCPConnectionProvider;
59  import pl.kernelpanic.dbmonster.schema.Schema;
60  import pl.kernelpanic.dbmonster.schema.SchemaUtil;
61  
62  /***
63   * DBMonster test.
64   *
65   * @author Piotr Maj <piotr.maj@kernelpanic.pl>
66   *
67   * @version $Id: DBMonsterTest.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
68   */
69  public class DBMonsterTest extends TestCase {
70  
71      public final void testDBMonster() throws Exception {
72          String driver = "org.hsqldb.jdbcDriver";
73          String url    = "jdbc:hsqldb:.";
74          String user   = "sa";
75          String pass   = "";
76          
77          Properties props = new Properties();
78          props.put("dbmonster.jdbc.driver", driver);
79          props.put("dbmonster.jdbc.url", url);
80          props.put("dbmonster.jdbc.username", user);
81          props.put("dbmonster.jdbc.password", pass);
82          props.put("dbmonster.max-tries", "1000");
83          
84          ConnectionProvider cp = 
85              new DBCPConnectionProvider(driver, url, user, pass);
86  
87          Connection conn = cp.getConnection();
88          Statement stmt = conn.createStatement();
89          try {
90              stmt.executeUpdate(
91                  "CREATE TABLE users (id int not null,"
92                  + "login varchar(255) not null, id_group int)");
93  
94              stmt.executeUpdate(
95                  "CREATE TABLE groups (id int not null,"
96                  + "name varchar(255) not null, description varchar)");
97              stmt.executeUpdate(
98                  "CREATE TABLE DEFAULT_TEST (id identity,"
99                  + "name varchar(255) not null)");
100             stmt.executeUpdate(
101                 "CREATE TABLE CONSTANT_TEST (f_i int, "
102                 + " f_s varchar, f_t timestamp, f_f float)");
103             stmt.close();
104             conn.commit();
105             conn.close();
106         } catch (Exception e) {
107             System.out.println(e.getMessage());
108             //throw e;
109         }
110 
111         DBMonster dbm = new DBMonster();
112         dbm.setConnectionProvider(cp);
113         dbm.setProperties(props);
114         String u = "/pl/kernelpanic/dbmonster/test/resources/dbmonster-schema.xml";
115         URL ur = getClass().getResource(u);
116         Schema schema = SchemaUtil.loadSchema(ur, dbm.getLogger());
117         dbm.addSchema(schema);
118         try {
119             dbm.doTheJob();
120             conn = cp.getConnection();
121             stmt = conn.createStatement();
122             ResultSet rs = stmt.executeQuery("SELECT * FROM CONSTANT_TEST");
123             while (rs.next()) {
124                 if (10 != rs.getInt("f_i")) {
125                     throw new Exception("Constant generator failed.");
126                 }
127                 if (!"blah".equals(rs.getString("f_s"))) {
128                     throw new Exception("Constant generator failed.");
129                 }
130                 if (1.5 != rs.getFloat("f_f")) {
131                     throw new Exception("Constant generator failed.");
132                 }
133             }
134         } catch (Exception e) {
135             e.printStackTrace();
136             throw e;
137         }
138     }
139 }