| 4 | import java.util.LinkedList; |
| 5 | |
| 6 | public class MySQLTest { |
| 7 | public static class Tests { |
| 8 | private Connection conn; |
| 9 | private Statement st; |
| 10 | private List<Test> tests = new LinkedList<>(); |
| 11 | |
| 12 | public void connect(String ip, int port, String user, String password) { |
| 13 | try { |
| 14 | String url = "jdbc:mysql://" + ip + ":" + port + "/"; |
| 15 | conn = DriverManager.getConnection(url, user, password); |
| 16 | st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
| 17 | } catch (SQLException e) { |
| 18 | throw new RuntimeException(e); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | public void disconnect() { |
| 23 | try { |
| 24 | st.close(); |
| 25 | conn.close(); |
| 26 | } catch (SQLException e) { |
| 27 | throw new RuntimeException(e); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | public void addTest(String query, String[][] expectedResults) { |
| 32 | tests.add(new Test(query, expectedResults)); |
| 33 | } |
| 34 | |
| 35 | public boolean runTests() { |
| 36 | for (Test test : tests) { |
| 37 | if (!test.run()) { |
| 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | public void readTestsFromFile(String filename) { |
| 45 | try (BufferedReader br = new BufferedReader(new FileReader(filename))) { |
| 46 | String line; |
| 47 | while ((line = br.readLine()) != null) { |
| 48 | if (line.trim().isEmpty()) continue; |
| 49 | String query = line; |
| 50 | List<String[]> results = new LinkedList<>(); |
| 51 | while ((line = br.readLine()) != null && !line.trim().isEmpty()) { |
| 52 | results.add(line.split(",")); |
| 53 | } |
| 54 | String[][] expectedResults = results.toArray(new String[0][]); |
| 55 | addTest(query, expectedResults); |
| 56 | } |
| 57 | } catch (IOException e) { |
| 58 | e.printStackTrace(); |
| 59 | System.exit(1); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public class Test { |
nothing calls this directly
no outgoing calls
no test coverage detected