Mercurial > hg > monetdb-java
comparison tests/OnClientTester.java @ 571:8a4e6c82815a onclient
Fix problems with FileTransferHandler and add tests
author | Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com> |
---|---|
date | Mon, 04 Oct 2021 12:35:15 +0200 (2021-10-04) |
parents | ce2b616ed22e |
children | 3370027aeb7f |
comparison
equal
deleted
inserted
replaced
570:721b7b109ecc | 571:8a4e6c82815a |
---|---|
7 */ | 7 */ |
8 | 8 |
9 import org.monetdb.jdbc.MonetConnection; | 9 import org.monetdb.jdbc.MonetConnection; |
10 import org.monetdb.jdbc.MonetConnection.UploadHandler; | 10 import org.monetdb.jdbc.MonetConnection.UploadHandler; |
11 import org.monetdb.jdbc.MonetConnection.DownloadHandler; | 11 import org.monetdb.jdbc.MonetConnection.DownloadHandler; |
12 import org.monetdb.util.FileTransferHandler; | |
12 | 13 |
13 import java.io.*; | 14 import java.io.*; |
14 import java.nio.charset.StandardCharsets; | 15 import java.nio.charset.StandardCharsets; |
16 import java.nio.file.Files; | |
17 import java.nio.file.Path; | |
15 import java.sql.SQLException; | 18 import java.sql.SQLException; |
19 import java.util.List; | |
20 | |
21 import static java.nio.file.StandardOpenOption.CREATE_NEW; | |
16 | 22 |
17 public final class OnClientTester extends TestRunner { | 23 public final class OnClientTester extends TestRunner { |
18 | 24 |
19 public OnClientTester(String jdbcUrl, int verbosity, boolean watchDogEnabled) { | 25 public OnClientTester(String jdbcUrl, int verbosity, boolean watchDogEnabled) { |
20 super(jdbcUrl, verbosity, watchDogEnabled); | 26 super(jdbcUrl, verbosity, watchDogEnabled); |
247 conn.setDownloadHandler(handler); | 253 conn.setDownloadHandler(handler); |
248 update("INSERT INTO foo SELECT value as i, 'number' || value AS t FROM sys.generate_series(0, 100)", 100); | 254 update("INSERT INTO foo SELECT value as i, 'number' || value AS t FROM sys.generate_series(0, 100)", 100); |
249 expectError("COPY (SELECT * FROM sys.generate_series(0,200)) INTO 'banana' ON CLIENT", "download refused"); | 255 expectError("COPY (SELECT * FROM sys.generate_series(0,200)) INTO 'banana' ON CLIENT", "download refused"); |
250 // Exception closes the connection | 256 // Exception closes the connection |
251 assertEq("connection is closed", conn.isClosed(), true); | 257 assertEq("connection is closed", conn.isClosed(), true); |
258 } | |
259 | |
260 public void test_FileTransferHandlerUpload() throws IOException, SQLException, Failure { | |
261 prepare(); | |
262 Path d = getTmpDir(currentTestName); | |
263 Path f = d.resolve("data.txt"); | |
264 OutputStream s = Files.newOutputStream(f, CREATE_NEW); | |
265 PrintStream ps = new PrintStream(s, false, "UTF-8"); | |
266 ps.println("1|one"); | |
267 ps.println("2|two"); | |
268 ps.println("3|three"); | |
269 ps.close(); | |
270 conn.setUploadHandler(new FileTransferHandler(d, true)); | |
271 update("COPY INTO foo FROM 'data.txt' ON CLIENT", 3); | |
272 queryInt("SELECT SUM(i) FROM foo", 6); | |
273 } | |
274 | |
275 public void test_FileTransferHandlerUploadRefused() throws IOException, SQLException, Failure { | |
276 prepare(); | |
277 Path d = getTmpDir(currentTestName); | |
278 Path f = d.resolve("data.txt"); | |
279 OutputStream s = Files.newOutputStream(f, CREATE_NEW); | |
280 PrintStream ps = new PrintStream(s, false, "UTF-8"); | |
281 ps.println("1|one"); | |
282 ps.println("2|two"); | |
283 ps.println("3|three"); | |
284 ps.close(); | |
285 | |
286 Path d2 = getTmpDir(currentTestName + "2"); | |
287 conn.setUploadHandler(new FileTransferHandler(d2, false)); | |
288 String quoted = f.toAbsolutePath().toString().replaceAll("'", "''"); | |
289 expectError("COPY INTO foo FROM R'"+ quoted + "' ON CLIENT", "not in upload directory"); | |
290 // connection is still alive | |
291 queryInt("SELECT SUM(i) FROM foo", 0); | |
292 } | |
293 | |
294 public void test_FileTransferHandlerDownload() throws SQLException, Failure, IOException { | |
295 prepare(); | |
296 update("INSERT INTO foo VALUES (42, 'forty-two')", 1); | |
297 Path d = getTmpDir(currentTestName); | |
298 conn.setDownloadHandler(new FileTransferHandler(d, false)); | |
299 update("COPY SELECT * FROM foo INTO 'data.txt' ON CLIENT", -1); | |
300 List<String> lines = Files.readAllLines(d.resolve("data.txt")); | |
301 assertEq("lines written", lines.size(), 1); | |
302 assertEq("line content", lines.get(0), "42|\"forty-two\""); | |
303 } | |
304 | |
305 public void test_FileTransferHandlerDownloadRefused() throws SQLException, Failure, IOException { | |
306 prepare(); | |
307 update("INSERT INTO foo VALUES (42, 'forty-two')", 1); | |
308 Path d = getTmpDir(currentTestName); | |
309 Path d2 = getTmpDir(currentTestName + "2"); | |
310 conn.setDownloadHandler(new FileTransferHandler(d2, false)); | |
311 String quoted = d.resolve("data.txt").toAbsolutePath().toString().replaceAll("'", "''"); | |
312 expectError("COPY SELECT * FROM foo INTO R'" + quoted + "' ON CLIENT", "not in download directory"); | |
313 | |
252 } | 314 } |
253 | 315 |
254 static class MyUploadHandler implements UploadHandler { | 316 static class MyUploadHandler implements UploadHandler { |
255 private final long rows; | 317 private final long rows; |
256 private final long errorAt; | 318 private final long errorAt; |