comparison tests/OnClientTester.java @ 524:71644dc873fe onclient

Manage verbosity
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Thu, 26 Aug 2021 12:36:07 +0200 (2021-08-26)
parents 3ff972d266cb
children 70ff796c42f7
comparison
equal deleted inserted replaced
523:3ff972d266cb 524:71644dc873fe
6 import java.lang.reflect.InvocationTargetException; 6 import java.lang.reflect.InvocationTargetException;
7 import java.lang.reflect.Method; 7 import java.lang.reflect.Method;
8 import java.sql.*; 8 import java.sql.*;
9 9
10 public final class OnClientTester { 10 public final class OnClientTester {
11 public static final int VERBOSITY_NONE = 0;
12 public static final int VERBOSITY_ON = 1;
13 public static final int VERBOSITY_SHOW_ALL = 2;
11 private String jdbcUrl; 14 private String jdbcUrl;
12 boolean verbose = false; 15 int verbosity = VERBOSITY_NONE;
13 int testCount = 0; 16 int testCount = 0;
14 int failureCount = 0; 17 int failureCount = 0;
15 private MonetConnection conn; 18 private MonetConnection conn;
16 private PrintWriter out; 19 private PrintWriter out;
17 private Statement stmt; 20 private Statement stmt;
18 private StringWriter outBuffer; 21 private StringWriter outBuffer;
19 22
20 public static void main(String[] args) throws SQLException, NoSuchMethodException { 23 public static void main(String[] args) throws SQLException, NoSuchMethodException {
21 String jdbcUrl; 24 String jdbcUrl = null;
22 String specificTest = null; 25 String specificTest = null;
23 switch (args.length) { 26 int verbosity = 0;
24 case 2: 27
25 specificTest = args[1]; 28 for (String arg: args) {
26 /* fallthrough */ 29 if (arg.equals("-v"))
27 case 1: 30 verbosity++;
28 jdbcUrl = args[0]; 31 else if (arg.equals("-vv"))
29 break; 32 verbosity += 2;
30 default: 33 else if (jdbcUrl == null)
31 throw new IllegalArgumentException("Usage: OnClientTester JDBC_URL [TESTNAME]"); 34 jdbcUrl = arg;
32 } 35 else if (specificTest == null)
33 36 specificTest = arg;
34 OnClientTester tester = new OnClientTester(jdbcUrl); 37 else {
38 System.err.println("Unexpected argument " + arg);
39 System.exit(2);
40 }
41 }
42
43 OnClientTester tester = new OnClientTester(jdbcUrl, verbosity);
35 if (specificTest != null) 44 if (specificTest != null)
36 tester.runTest(specificTest); 45 tester.runTest(specificTest);
37 else 46 else
38 tester.runTests(); 47 tester.runTests();
39 48
40 System.out.println(); 49 if (tester.verbosity >= VERBOSITY_ON || tester.failureCount > 0) {
41 System.out.println("Ran " + tester.testCount + " tests, " + tester.failureCount + " failed"); 50 System.out.println();
51 System.out.println("Ran " + tester.testCount + " tests, " + tester.failureCount + " failed");
52 }
42 if (tester.failureCount > 0) { 53 if (tester.failureCount > 0) {
43 System.exit(1); 54 System.exit(1);
44 } 55 }
45 } 56 }
46 57
47 public OnClientTester(String jdbcUrl) { 58 public OnClientTester(String jdbcUrl, int verbosity) {
48 this.jdbcUrl = jdbcUrl; 59 this.jdbcUrl = jdbcUrl;
60 this.verbosity = verbosity;
49 } 61 }
50 62
51 private void runTests() throws SQLException, NoSuchMethodException { 63 private void runTests() throws SQLException, NoSuchMethodException {
52 for (Method method: this.getClass().getDeclaredMethods()) { 64 for (Method method: this.getClass().getDeclaredMethods()) {
53 String methodName = method.getName(); 65 String methodName = method.getName();
69 out = new PrintWriter(outBuffer); 81 out = new PrintWriter(outBuffer);
70 82
71 Connection genericConnection = DriverManager.getConnection(jdbcUrl); 83 Connection genericConnection = DriverManager.getConnection(jdbcUrl);
72 conn = genericConnection.unwrap(MonetConnection.class); 84 conn = genericConnection.unwrap(MonetConnection.class);
73 stmt = conn.createStatement(); 85 stmt = conn.createStatement();
74
75 System.out.println();
76 86
77 boolean failed = false; 87 boolean failed = false;
78 try { 88 try {
79 try { 89 try {
80 method.invoke(this); 90 method.invoke(this);
87 } else { 97 } else {
88 throw e; 98 throw e;
89 } 99 }
90 } 100 }
91 101
92 System.out.println("Test " + testName + " succeeded"); 102 if (verbosity > VERBOSITY_ON)
93 if (verbose) 103 System.out.println();
104 if (verbosity >= VERBOSITY_ON)
105 System.out.println("Test " + testName + " succeeded");
106 if (verbosity >= VERBOSITY_SHOW_ALL)
94 dumpOutput(testName); 107 dumpOutput(testName);
95 } catch (Failure e) { 108 } catch (Failure e) {
96 failed = true; 109 failed = true;
110 System.out.println();
97 System.out.println("Test " + testName + " failed"); 111 System.out.println("Test " + testName + " failed");
98 dumpOutput(testName); 112 dumpOutput(testName);
99 } catch (Exception e) { 113 } catch (Exception e) {
100 failed = true; 114 failed = true;
115 System.out.println();
101 System.out.println("Test " + testName + " failed:"); 116 System.out.println("Test " + testName + " failed:");
102 e.printStackTrace(); 117 e.printStackTrace(System.out);
103 dumpOutput(testName); 118 dumpOutput(testName);
104 // Show the inner bits of the exception again, they may have scrolled off screen 119 // Show the inner bits of the exception again, they may have scrolled off screen
105 Throwable t = e; 120 Throwable t = e;
106 while (t.getCause() != null) { 121 while (t.getCause() != null) {
107 t = t.getCause(); 122 t = t.getCause();
108 } 123 }
109 System.out.println();
110 System.out.println("Innermost cause was " + t); 124 System.out.println("Innermost cause was " + t);
111 if (t.getStackTrace().length > 0) { 125 if (t.getStackTrace().length > 0) {
112 System.out.println(" at " + t.getStackTrace()[0]); 126 System.out.println(" at " + t.getStackTrace()[0]);
113 } 127 }
114 } finally { 128 } finally {
115 testCount++; 129 testCount++;
116 if (failed) 130 if (failed)
117 failureCount++; 131 failureCount++;
132 if (failed && verbosity == VERBOSITY_ON) {
133 // next test case will not print separator
134 System.out.println();
135 }
118 stmt.close(); 136 stmt.close();
119 conn.close(); 137 conn.close();
120 } 138 }
121 } 139 }
122 140
123 private void dumpOutput(String testName) { 141 private void dumpOutput(String testName) {
124 String output = outBuffer.getBuffer().toString(); 142 String output = outBuffer.getBuffer().toString();
125 if (output.isEmpty()) { 143 if (output.isEmpty()) {
126 System.out.println("(Test did not produce any output)"); 144 System.out.println("(Test did not produce any output)");
127 } else { 145 } else {
128 System.out.println("------ Accumulated output for " + testName + ":"); 146 System.out.println("------ Accumulated output for test " + testName + ":");
129 boolean terminated = output.endsWith(System.lineSeparator()); 147 boolean terminated = output.endsWith(System.lineSeparator());
130 if (terminated) { 148 if (terminated) {
131 System.out.print(output); 149 System.out.print(output);
132 } else { 150 } else {
133 System.out.println(output); 151 System.out.println(output);
139 private void fail(String message) throws Failure { 157 private void fail(String message) throws Failure {
140 out.println("FAILURE: " + message); 158 out.println("FAILURE: " + message);
141 throw new Failure(message); 159 throw new Failure(message);
142 } 160 }
143 161
162 private void checked(String quantity, Object actual) {
163 out.println(" CHECKED: " + "<" + quantity + "> is " + actual + " as expected");
164 }
165
144 private void assertEq(String quantity, Object expected, Object actual) throws Failure { 166 private void assertEq(String quantity, Object expected, Object actual) throws Failure {
145 if (expected.equals(actual)) 167 if (expected.equals(actual)) {
146 return; 168 checked(quantity, actual);
147 fail("Expected '" + quantity + "' to be " + expected + ", got " + actual); 169 } else {
170 fail("Expected <" + quantity + "' to be " + expected + "> got " + actual);
171 }
148 } 172 }
149 173
150 protected boolean execute(String query) throws SQLException { 174 protected boolean execute(String query) throws SQLException {
151 out.println("EXEC " + query); 175 out.println("EXECUTE: " + query);
152 boolean result; 176 boolean result;
153 result = stmt.execute(query); 177 result = stmt.execute(query);
154 if (result) { 178 if (result) {
155 out.println(" OK"); 179 out.println(" OK");
156 } else { 180 } else {
191 if (rs.next()) { 215 if (rs.next()) {
192 String message = "Result set has more than one row"; 216 String message = "Result set has more than one row";
193 fail(message); 217 fail(message);
194 } 218 }
195 rs.close(); 219 rs.close();
220 checked("row count", 1);
196 assertEq("query result", expected, result); 221 assertEq("query result", expected, result);
197 } 222 }
198 223
199 protected void prepare() throws SQLException { 224 protected void prepare() throws SQLException {
200 execute("DROP TABLE IF EXISTS foo"); 225 execute("DROP TABLE IF EXISTS foo");
393 prepare(); 418 prepare();
394 MyDownloadHandler handler = new MyDownloadHandler("download refused"); 419 MyDownloadHandler handler = new MyDownloadHandler("download refused");
395 conn.setDownloadHandler(handler); 420 conn.setDownloadHandler(handler);
396 update("INSERT INTO foo SELECT value as i, 'number' || value AS t FROM sys.generate_series(0, 100)", 100); 421 update("INSERT INTO foo SELECT value as i, 'number' || value AS t FROM sys.generate_series(0, 100)", 100);
397 expectError("COPY (SELECT * FROM foo) INTO 'banana' ON CLIENT", "download refused"); 422 expectError("COPY (SELECT * FROM foo) INTO 'banana' ON CLIENT", "download refused");
398 // check if the connection still works 423 queryInt("SELECT 42 -- check if the connection still works", 42);
399 queryInt("SELECT 42", 42);
400 } 424 }
401 425
402 public void test_LargeUpload() throws SQLException, Failure { 426 public void test_LargeUpload() throws SQLException, Failure {
403 prepare(); 427 prepare();
404 int n = 4_000_000; 428 int n = 4_000_000;