First pass at Java MySQL Connector test. This has a dependency on the mysql-connector-java jar file. We'll have to figure out classpath and dependency resolution once we start running these programmatically. The program also only checks the first row, column pair or rows updated of the output to see if it matched expected. This is a pretty big simplification to avoid defining too much configuration.

This commit is contained in:
Timothy Sehn
2020-08-25 15:43:11 -07:00
parent 9d04bc471d
commit 0dbc5b2384
2 changed files with 79 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class MySQLConnectorTest {
public static void main(String[] args) {
Connection conn = null;
String user = args[0];
String port = args[1];
String db = args[2];
try {
String url = "jdbc:mysql://127.0.0.1:" + port + "/" + db;
String password = "";
conn = DriverManager.getConnection(url, user, password);
Statement st = conn.createStatement();
String[] queries = {
"create table test (pk int, value int, primary key(pk))",
"describe test",
"select * from test",
"insert into test (pk, value) values (0,0)",
"select * from test"
};
// Only test the first row, column pair for now
String[] results = {
"0",
"pk",
null,
"1",
"0"
};
for (int i = 0; i < queries.length; i++) {
String query = queries[i];
String expected = results[i];
if ( st.execute(query) ) {
ResultSet rs = st.getResultSet();
if (rs.next()) {
String result = rs.getString(1);
if ( !expected.equals(result) ) {
System.out.println("Query: \n" + query);
System.out.println("Expected:\n" + expected);
System.out.println("Result:\n" + result);
System.exit(1);
}
}
} else {
String result = Integer.toString(st.getUpdateCount());
if ( !expected.equals(result) ) {
System.out.println("Query: \n" + query);
System.out.println("Expected:\n" + expected);
System.out.println("Rows Updated:\n" + result);
System.exit(1);
}
}
}
System.exit(0);
} catch (SQLException ex) {
System.out.println("An error occurred.");
ex.printStackTrace();
System.exit(1);
}
}
}