-
Notifications
You must be signed in to change notification settings - Fork 2
/
BreeMojoSqliteTest.java
180 lines (160 loc) · 5.87 KB
/
BreeMojoSqliteTest.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package org.ttzero.plugin.bree.mybatis;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Before;
import org.junit.Test;
import org.ttzero.plugin.bree.mybatis.utils.ConfigUtil;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import static org.ttzero.plugin.bree.mybatis.BreeMojoTest.getOutputTestPath;
import static org.ttzero.plugin.bree.mybatis.BreeMojoTest.getRandomString;
import static org.ttzero.plugin.bree.mybatis.BreeMojoTest.random;
import static org.ttzero.plugin.bree.mybatis.BreeMojoTest.testResourceRoot;
import static org.ttzero.plugin.bree.mybatis.utils.ConfigUtil.getAttr;
public class BreeMojoSqliteTest {
/**
* Returns path of config
*
* @return the config path
*/
public static Path getConfigPath() {
return testResourceRoot().resolve("bree/config/sqlite/config.xml");
}
private static String url;
static {
System.out.println(getConfigPath().toString());
String driver = null;
SAXReader reader = new SAXReader();
try {
Document document = reader.read(Files.newInputStream(
Paths.get(URLDecoder.decode(getConfigPath().toString(), StandardCharsets.UTF_8.name()))));
Element root = document.getRootElement();
Element database = root.element("database");
if (database == null) {
System.out.println("Miss database config.");
System.exit(-1);
}
driver = getAttr(database, "class");
assert driver != null;
@SuppressWarnings({"unchecked", "retype"})
List<Element> properties = database.elements();
assert properties != null && !properties.isEmpty();
for (Element property : properties) {
if ("url".equalsIgnoreCase(getAttr(property, "name"))) {
url = getAttr(property, "value");
assert url != null;
}
}
} catch (DocumentException | IOException e) {
e.printStackTrace();
System.exit(-1);
}
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(-1);
}
}
protected Connection getConnection() throws SQLException {
return DriverManager.getConnection(url);
}
/**
* Install test data
*/
@Before
public void init() {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = getConnection();
String student = "create table if not exists student(id integer primary key, name text, age integer)";
ps = con.prepareStatement(student);
ps.executeUpdate();
ps.close();
ps = con.prepareStatement("select id from student limit 1");
rs = ps.executeQuery();
// No data in database
if (!rs.next()) {
ps.close();
con.setAutoCommit(false);
ps = con.prepareStatement("insert into student(name, age) values (?,?)");
int size = 10_000;
for (int i = 0; i < size; i++) {
ps.setString(1, getRandomString());
ps.setInt(2, random.nextInt(15) + 5);
ps.addBatch();
}
ps.executeBatch();
con.commit();
}
// Create teacher
String teacher = "create table if not exists bl_teacher(id integer primary key, name text, bl integer)";
ps = con.prepareStatement(teacher);
ps.executeUpdate();
ps.close();
ps = con.prepareStatement("select id from bl_teacher limit 1");
rs = ps.executeQuery();
// No data in database
if (!rs.next()) {
ps.close();
con.setAutoCommit(false);
ps = con.prepareStatement("insert into bl_teacher(name, bl) values (?,?)");
int size = 1000;
for (int i = 0; i < size; i++) {
ps.setString(1, getRandomString());
ps.setInt(2, random.nextInt(10) >= 8 ? 1 : 0);
ps.addBatch();
}
ps.executeBatch();
con.commit();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// Add Database
}
@Test public void test() throws IOException, MojoFailureException, MojoExecutionException {
BreeMojo mojo = new BreeMojo(getOutputTestPath().toFile()
, testResourceRoot().resolve("bree/templates/").toFile(), getConfigPath().toFile(), true);
ConfigUtil.setCmd("student,bl_teacher");
mojo.execute();
}
}