package com parallels pba autotests lib database import com google com

  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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.parallels.pba.autotests.lib.database;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import com.parallels.autotests.framework.logger.Logger;
import com.parallels.pba.autotests.general.testNameAppender.PBATestListener;
import com.parallels.pba.autotests.lib.constants.Constants;
import org.apache.commons.io.FileUtils;
import org.apache.derby.jdbc.EmbeddedDriver;
import java.io.*;
import java.net.URL;
import java.sql.*;
/**
* Defines object "Database" and all respective operations.
*/
public class Database implements Constants {
private final static String DATABASE_PATH = "./db";
private final static String SQL_RESOURCE_NAME = "pba-test.sql";
private final static String LAST_MODIFIED_FILE_PATH = "./last-modified.txt";
private static Database testDataStore = new Database();
/**
* Database connection
*/
private Connection connection;
/**
* SQL-statement for database management
*/
private PreparedStatement preparedStatement;
/**
* SQL-query results
*/
private ResultSet resultSet;
/**
* @return the testDataStore
*/
private static Database getTestDataStore() {
if (!testDataStore.isConnected()) {
testDataStore.establishConnection();
}
return testDataStore;
}
public static Result executeQuery(String query) {
if (!Database.getTestDataStore().isConnected()) {
throw new RuntimeException("No connection with test data store.");
}
try {
getTestDataStore().setPreparedStatement(getTestDataStore().getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY));
getTestDataStore().setResultSet(getTestDataStore().getPreparedStatement().executeQuery());
getTestDataStore().getResultSet().first();
} catch (SQLException sqlException) {
throw new RuntimeException(sqlException);
}
return new Result(getTestDataStore().getResultSet());
}
/**
* Executes SQL-query on specified connection return result in result set.
*/
public static Result executeQuery(String query, Integer id) {
executeQuery(query, id, null);
try {
getTestDataStore().getResultSet().first();
} catch (SQLException e) {
throw new RuntimeException(e.getMessage());
}
return new Result(getTestDataStore().getResultSet());
}
public static Result executeQuery(String query, String mark) {
executeQuery(query, null, mark);
return new Result(getTestDataStore().getResultSet());
}
private static void executeQuery(String query, Integer id, String mark) {
if (!Database.getTestDataStore().isConnected()) {
throw new RuntimeException("No connection with test data store.");
}
try {
query += " AND LOWER(test_name) = LOWER(?)";
getTestDataStore().setPreparedStatement(getTestDataStore().getConnection().prepareStatement(
query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY));
if (mark != null) {
getTestDataStore().getPreparedStatement().setString(1, mark);
getTestDataStore().getPreparedStatement().setString(2, PBATestListener.getCurrentTestName());
} else if (id == null) {
getTestDataStore().getPreparedStatement().setInt(1, 1);
getTestDataStore().getPreparedStatement().setString(2, DEFAULT_TEST_NAME);
} else {
getTestDataStore().getPreparedStatement().setInt(1, id);
getTestDataStore().getPreparedStatement().setString(2, PBATestListener.getCurrentTestName());
}
getTestDataStore().setResultSet(getTestDataStore().getPreparedStatement().executeQuery());
} catch (SQLException e) {
throw new RuntimeException(e.getMessage());
}
}
public static Result getObjectById(String tableName, Integer id) {
if (!Database.getTestDataStore().isConnected()) {
throw new RuntimeException("No connection with test data store.");
}
executeQuery(String.format("SELECT * FROM %s WHERE id = ?", tableName), id);
return new Result(getTestDataStore().getResultSet());
}
public void establishConnection() {
if (!isConnected()) {
try {
EmbeddedDriver.class.newInstance();
} catch (Exception e) {
throw new RuntimeException("Can't register Derby JDBC driver. Reason: " + e.getMessage());
}
String currentRequest = "";
try {
Logger.debug("Creating connection to tests database");
if (!isDatabaseResetRequired()) {
connection = DriverManager.getConnection(String.format("jdbc:derby:%s;create=true", DATABASE_PATH));
setConnection(connection);
return;
}
if (new File(DATABASE_PATH).exists()) {
FileUtils.deleteDirectory(new File(DATABASE_PATH));
}
connection = DriverManager.getConnection(String.format("jdbc:derby:%s;create=true", DATABASE_PATH));
setConnection(connection);
InputStream bis = (InputStream) Resources.getResource(SQL_RESOURCE_NAME).getContent();
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader reader = new BufferedReader(isr);
String line;
StringBuilder sb = new StringBuilder();
Statement statement = connection.createStatement();
Logger.debug("Database creation started");
while ((line = reader.readLine()) != null) {
if (!line.isEmpty()) {
sb.append(line);
if (line.trim().endsWith(";")) {
currentRequest = sb.toString().replaceAll("\\\\r\\\\n", "\r\n");
statement.execute(currentRequest.subSequence(0, currentRequest.length() - 1).toString());
sb = new StringBuilder();
}
}
}
Logger.debug("Database successfully created");
statement.close();
} catch (Exception e) {
throw new RuntimeException(String.format("Error while executing SQL query: '%s': '%s'", currentRequest, e.getMessage()));
}
}
}
private boolean isDatabaseResetRequired() {
long sqlSourceLastModified = 0;
try {
URL sqlSourceUrl = Resources.getResource(SQL_RESOURCE_NAME);
sqlSourceLastModified = sqlSourceUrl.openConnection().getLastModified();
if (new File(DATABASE_PATH).exists() && new File(LAST_MODIFIED_FILE_PATH).exists()) {
long lastModified = Long.parseLong(Files.toString(new File(LAST_MODIFIED_FILE_PATH), Charsets.UTF_8));
if (sqlSourceLastModified == lastModified) {
return false;
}
}
} catch (Exception e) {
Logger.debug(String.format("Test database should be recreated since '%s'", e.getMessage()));
}
// writing last modified date into temporary file
try {
Writer writer = new BufferedWriter(new FileWriter(LAST_MODIFIED_FILE_PATH, false));
writer.write(String.valueOf(sqlSourceLastModified));
writer.close();
return true;
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
/**
* Defines if connection to database is established or not.
*
* @return true - connection with Derby database is established, false -
* connection with Derby database is not established.
*/
public Boolean isConnected() {
return getConnection() != null;
}
/**
* @return the connection
*/
public Connection getConnection() {
return connection;
}
/**
* @param connection the connection to set
*/
public void setConnection(Connection connection) {
this.connection = connection;
}
/**
* @return the preparedStatement
*/
public PreparedStatement getPreparedStatement() {
return preparedStatement;
}
/**
* @param preparedStatement the preparedStatement to set
*/
public void setPreparedStatement(PreparedStatement preparedStatement) {
this.preparedStatement = preparedStatement;
}
/**
* @return the resultSet
*/
public ResultSet getResultSet() {
return resultSet;
}
/**
* @param resultSet the resultSet to set
*/
public void setResultSet(ResultSet resultSet) {
this.resultSet = resultSet;
}
}