comparison tests/OnClientTester.java @ 580:13134a44dfc8 onclient

Test FileTransferHandler with multiple encodings
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Thu, 14 Oct 2021 16:26:36 +0200 (2021-10-14)
parents 687034945b3f
children 5aef0ea654b1
comparison
equal deleted inserted replaced
579:72f4437de9be 580:13134a44dfc8
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 import org.monetdb.util.FileTransferHandler;
13 13
14 import java.io.*; 14 import java.io.*;
15 import java.nio.charset.Charset;
15 import java.nio.charset.StandardCharsets; 16 import java.nio.charset.StandardCharsets;
16 import java.nio.file.Files; 17 import java.nio.file.Files;
17 import java.nio.file.Path; 18 import java.nio.file.Path;
18 import java.sql.SQLException; 19 import java.sql.SQLException;
19 import java.util.List; 20 import java.util.List;
353 expectError("COPY (SELECT * FROM sys.generate_series(0,200)) INTO 'banana' ON CLIENT", "download refused"); 354 expectError("COPY (SELECT * FROM sys.generate_series(0,200)) INTO 'banana' ON CLIENT", "download refused");
354 // Exception closes the connection 355 // Exception closes the connection
355 assertEq("connection is closed", conn.isClosed(), true); 356 assertEq("connection is closed", conn.isClosed(), true);
356 } 357 }
357 358
358 public void test_FileTransferHandlerUpload() throws IOException, SQLException, Failure { 359 public void test_FileTransferHandlerUploadUtf8() throws IOException, SQLException, Failure {
360 testFileTransferHandlerUpload("UTF-8");
361 }
362
363 public void test_FileTransferHandlerUploadLatin1() throws IOException, SQLException, Failure {
364 testFileTransferHandlerUpload("latin1");
365 }
366
367 public void testFileTransferHandlerUpload(String encoding) throws IOException, SQLException, Failure {
368 prepare();
369 Path d = getTmpDir(currentTestName);
370 Path f = d.resolve("data.txt");
371 OutputStream s = Files.newOutputStream(f, CREATE_NEW);
372 PrintStream ps = new PrintStream(s, false, encoding);
373 ps.println("1|one");
374 ps.println("2|twø");
375 ps.println("3|three");
376 ps.close();
377 conn.setUploadHandler(new FileTransferHandler(d, Charset.forName(encoding)));
378 update("COPY INTO foo FROM 'data.txt' ON CLIENT");
379 assertQueryInt("SELECT SUM(i) FROM foo", 6);
380 assertQueryString("SELECT t FROM foo WHERE i = 2", "twø");
381 }
382
383 public void test_FileTransferHandlerUploadRefused() throws IOException, SQLException, Failure {
359 prepare(); 384 prepare();
360 Path d = getTmpDir(currentTestName); 385 Path d = getTmpDir(currentTestName);
361 Path f = d.resolve("data.txt"); 386 Path f = d.resolve("data.txt");
362 OutputStream s = Files.newOutputStream(f, CREATE_NEW); 387 OutputStream s = Files.newOutputStream(f, CREATE_NEW);
363 PrintStream ps = new PrintStream(s, false, "UTF-8"); 388 PrintStream ps = new PrintStream(s, false, "UTF-8");
364 ps.println("1|one"); 389 ps.println("1|one");
365 ps.println("2|two"); 390 ps.println("2|two");
366 ps.println("3|three"); 391 ps.println("3|three");
367 ps.close(); 392 ps.close();
368 conn.setUploadHandler(new FileTransferHandler(d, StandardCharsets.UTF_8));
369 update("COPY INTO foo FROM 'data.txt' ON CLIENT");
370 assertQueryInt("SELECT SUM(i) FROM foo", 6);
371 }
372
373 public void test_FileTransferHandlerUploadRefused() throws IOException, SQLException, Failure {
374 prepare();
375 Path d = getTmpDir(currentTestName);
376 Path f = d.resolve("data.txt");
377 OutputStream s = Files.newOutputStream(f, CREATE_NEW);
378 PrintStream ps = new PrintStream(s, false, "UTF-8");
379 ps.println("1|one");
380 ps.println("2|two");
381 ps.println("3|three");
382 ps.close();
383 393
384 Path d2 = getTmpDir(currentTestName + "2"); 394 Path d2 = getTmpDir(currentTestName + "2");
385 conn.setUploadHandler(new FileTransferHandler(d2, StandardCharsets.UTF_8)); 395 conn.setUploadHandler(new FileTransferHandler(d2, StandardCharsets.UTF_8));
386 String quoted = f.toAbsolutePath().toString().replaceAll("'", "''"); 396 String quoted = f.toAbsolutePath().toString().replaceAll("'", "''");
387 expectError("COPY INTO foo FROM R'"+ quoted + "' ON CLIENT", "not in upload directory"); 397 expectError("COPY INTO foo FROM R'"+ quoted + "' ON CLIENT", "not in upload directory");
388 // connection is still alive 398 // connection is still alive
389 assertQueryInt("SELECT SUM(i) FROM foo", 0); 399 assertQueryInt("SELECT SUM(i) FROM foo", 0);
390 } 400 }
391 401
392 public void test_FileTransferHandlerDownload() throws SQLException, Failure, IOException { 402 public void test_FileTransferHandlerDownloadUtf8() throws SQLException, Failure, IOException {
393 prepare(); 403 testFileTransferHandlerDownload(StandardCharsets.UTF_8);
394 update("INSERT INTO foo VALUES (42, 'forty-two')"); 404 }
405
406 public void test_FileTransferHandlerDownloadLatin1() throws SQLException, Failure, IOException {
407 Charset latin1 = Charset.forName("latin1");
408 testFileTransferHandlerDownload(latin1);
409 }
410
411 public void testFileTransferHandlerDownload(Charset encoding) throws SQLException, Failure, IOException {
412 prepare();
413 update("INSERT INTO foo VALUES (42, 'forty-twø')");
395 Path d = getTmpDir(currentTestName); 414 Path d = getTmpDir(currentTestName);
396 conn.setDownloadHandler(new FileTransferHandler(d, StandardCharsets.UTF_8)); 415 conn.setDownloadHandler(new FileTransferHandler(d, encoding));
397 update("COPY SELECT * FROM foo INTO 'data.txt' ON CLIENT"); 416 update("COPY SELECT * FROM foo INTO 'data.txt' ON CLIENT");
398 List<String> lines = Files.readAllLines(d.resolve("data.txt")); 417 List<String> lines = Files.readAllLines(d.resolve("data.txt"), encoding);
399 assertEq("lines written", lines.size(), 1); 418 assertEq("lines written", lines.size(), 1);
400 assertEq("line content", lines.get(0), "42|\"forty-two\""); 419 assertEq("line content", lines.get(0), "42|\"forty-twø\"");
401 // connection is still alive 420 // connection is still alive
402 assertQueryInt("SELECT SUM(i) FROM foo", 42); 421 assertQueryInt("SELECT SUM(i) FROM foo", 42);
403 } 422 }
404 423
405 public void test_FileTransferHandlerDownloadRefused() throws SQLException, Failure, IOException { 424 public void test_FileTransferHandlerDownloadRefused() throws SQLException, Failure, IOException {