1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package pl.kernelpanic.dbmonster.test;
48
49 import java.net.URL;
50 import java.sql.Connection;
51 import java.sql.Statement;
52 import java.util.Properties;
53
54 import junit.framework.TestCase;
55
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: MySQLTest.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
68 */
69 public class MySQLTest extends TestCase {
70
71 private static final String TEST_NAME = "mysql";
72
73 public final void testDBMonster() throws Exception {
74 if (!Utils.performTest(TEST_NAME)) {
75 Utils.skipTest(TEST_NAME);
76 return;
77 }
78 String driver = "com.mysql.jdbc.Driver";
79 String url = "jdbc:mysql://127.0.0.1/dbmonster";
80 String user = "dbmonster";
81 String pass = "dbmonster";
82
83 Properties props = new Properties();
84 props.put("dbmonster.jdbc.driver", driver);
85 props.put("dbmonster.jdbc.url", url);
86 props.put("dbmonster.jdbc.username", user);
87 props.put("dbmonster.jdbc.password", pass);
88 props.put("dbmonster.max-tries", "1000");
89
90 ConnectionProvider cp =
91 new DBCPConnectionProvider(driver, url, user, pass);
92
93 Connection conn = cp.getConnection();
94 Statement stmt = conn.createStatement();
95
96 try {
97 stmt.executeUpdate("DROP TABLE DEFAULT_TEST");
98 } catch (Exception e) {
99 }
100 try {
101 stmt.executeUpdate("DROP TABLE USERS");
102 } catch (Exception e) {
103 }
104 try {
105 stmt.executeUpdate("DROP TABLE GROUPS");
106 } catch (Exception e) {
107 }
108 try {
109 stmt.executeUpdate(
110 "CREATE TABLE USERS (id int not null,"
111 + "login varchar(255) not null, id_group int)");
112 stmt.executeUpdate(
113 "CREATE TABLE GROUPS (id int not null,"
114 + "name varchar(255) not null, description text)");
115 stmt.executeUpdate(
116 "CREATE TABLE DEFAULT_TEST (id int8 not null auto_increment primary key,"
117 + "name varchar(255) not null)");
118 stmt.close();
119 conn.close();
120 } catch (Exception e) {
121 System.out.println(e.getMessage());
122 throw e;
123 }
124
125 DBMonster dbm = new DBMonster();
126 dbm.setConnectionProvider(cp);
127 dbm.setProperties(props);
128 String u = "/pl/kernelpanic/dbmonster/test/resources/dbmonster-schema.xml";
129 URL ur = getClass().getResource(u);
130 Schema schema = SchemaUtil.loadSchema(ur, dbm.getLogger());
131 dbm.addSchema(schema);
132 try {
133 dbm.doTheJob();
134 } catch (Exception e) {
135 e.printStackTrace();
136 throw e;
137 }
138 }
139 }