Mercurial > hg > monetdb-java
comparison tests/OnClientTester.java @ 604:8e5dbe2ebe7d
Test uploading compressed files in FileTransferHandler
author | Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com> |
---|---|
date | Thu, 09 Dec 2021 16:53:27 +0100 (2021-12-09) |
parents | 662e8de9b002 |
children | 69b0bcf5f62d |
comparison
equal
deleted
inserted
replaced
603:7a6b003d65d6 | 604:8e5dbe2ebe7d |
---|---|
26 import java.sql.ResultSetMetaData; | 26 import java.sql.ResultSetMetaData; |
27 import java.sql.Statement; | 27 import java.sql.Statement; |
28 import java.sql.SQLException; | 28 import java.sql.SQLException; |
29 import java.util.ArrayList; | 29 import java.util.ArrayList; |
30 import java.util.List; | 30 import java.util.List; |
31 import java.util.zip.GZIPOutputStream; | |
31 | 32 |
32 import static java.nio.file.StandardOpenOption.CREATE_NEW; | 33 import static java.nio.file.StandardOpenOption.CREATE_NEW; |
33 | 34 |
34 | 35 |
35 /** | 36 /** |
166 test_FileTransferHandlerDownloadUtf8(); | 167 test_FileTransferHandlerDownloadUtf8(); |
167 if (isSelected("FileTransferHandlerDownloadLatin1")) | 168 if (isSelected("FileTransferHandlerDownloadLatin1")) |
168 test_FileTransferHandlerDownloadLatin1(); | 169 test_FileTransferHandlerDownloadLatin1(); |
169 if (isSelected("FileTransferHandlerDownloadNull")) | 170 if (isSelected("FileTransferHandlerDownloadNull")) |
170 test_FileTransferHandlerDownloadNull(); | 171 test_FileTransferHandlerDownloadNull(); |
172 if (isSelected("test_FileTransferHandlerUploadNotCompressed")) | |
173 test_FileTransferHandlerUploadNotCompressed(); | |
174 if (isSelected("test_FileTransferHandlerUploadNotCompressedSkip")) | |
175 test_FileTransferHandlerUploadNotCompressedSkip(); | |
176 if (isSelected("test_FileTransferHandlerUploadCompressed")) | |
177 test_FileTransferHandlerUploadCompressed(); | |
178 if (isSelected("test_FileTransferHandlerUploadCompressedSkip")) | |
179 test_FileTransferHandlerUploadCompressedSkip(); | |
171 if (isSelected("FileTransferHandlerDownloadRefused")) | 180 if (isSelected("FileTransferHandlerDownloadRefused")) |
172 test_FileTransferHandlerDownloadRefused(); | 181 test_FileTransferHandlerDownloadRefused(); |
173 } catch (Failure e) { | 182 } catch (Failure e) { |
174 failures++; | 183 failures++; |
175 System.err.println(); | 184 System.err.println(); |
587 // Exception closes the connection | 596 // Exception closes the connection |
588 assertEq("connection is closed", conn.isClosed(), true); | 597 assertEq("connection is closed", conn.isClosed(), true); |
589 exitTest(); | 598 exitTest(); |
590 } | 599 } |
591 | 600 |
601 private void test_FileTransferHandlerUploadNotCompressed() throws IOException, SQLException, Failure { | |
602 initTest("FileTransferHandlerUploadNotCompressed"); | |
603 testFileTransferHandlerUploadCompressed(StandardCharsets.UTF_8, false, 0); | |
604 exitTest(); | |
605 } | |
606 | |
607 private void test_FileTransferHandlerUploadNotCompressedSkip() throws IOException, SQLException, Failure { | |
608 initTest("FileTransferHandlerUploadNotCompressedSkip"); | |
609 testFileTransferHandlerUploadCompressed(StandardCharsets.UTF_8, false, 2); | |
610 exitTest(); | |
611 } | |
612 | |
613 private void test_FileTransferHandlerUploadCompressed() throws IOException, SQLException, Failure { | |
614 initTest("FileTransferHandlerUploadCompressed"); | |
615 testFileTransferHandlerUploadCompressed(StandardCharsets.UTF_8, true, 0); | |
616 exitTest(); | |
617 } | |
618 | |
619 private void test_FileTransferHandlerUploadCompressedSkip() throws IOException, SQLException, Failure { | |
620 initTest("FileTransferHandlerUploadCompressedSkip"); | |
621 testFileTransferHandlerUploadCompressed(StandardCharsets.UTF_8, true, 2); | |
622 exitTest(); | |
623 } | |
624 | |
592 private void test_FileTransferHandlerUploadUtf8() throws IOException, SQLException, Failure { | 625 private void test_FileTransferHandlerUploadUtf8() throws IOException, SQLException, Failure { |
593 initTest("test_FileTransferHandlerUploadUtf8"); | 626 initTest("test_FileTransferHandlerUploadUtf8"); |
594 testFileTransferHandlerUpload(StandardCharsets.UTF_8, "UTF-8"); | 627 testFileTransferHandlerUploadEncoding(StandardCharsets.UTF_8, "UTF-8"); |
595 exitTest(); | 628 exitTest(); |
596 } | 629 } |
597 | 630 |
598 private void test_FileTransferHandlerUploadLatin1() throws IOException, SQLException, Failure { | 631 private void test_FileTransferHandlerUploadLatin1() throws IOException, SQLException, Failure { |
599 initTest("test_FileTransferHandlerUploadLatin1"); | 632 initTest("test_FileTransferHandlerUploadLatin1"); |
600 testFileTransferHandlerUpload(Charset.forName("latin1"), "latin1"); | 633 testFileTransferHandlerUploadEncoding(Charset.forName("latin1"), "latin1"); |
601 exitTest(); | 634 exitTest(); |
602 } | 635 } |
603 | 636 |
604 private void test_FileTransferHandlerUploadNull() throws IOException, SQLException, Failure { | 637 private void test_FileTransferHandlerUploadNull() throws IOException, SQLException, Failure { |
605 initTest("test_FileTransferHandlerUploadNull"); | 638 initTest("test_FileTransferHandlerUploadNull"); |
606 testFileTransferHandlerUpload(null, Charset.defaultCharset().name()); | 639 testFileTransferHandlerUploadEncoding(null, Charset.defaultCharset().name()); |
607 exitTest(); | 640 exitTest(); |
608 } | 641 } |
609 | 642 |
610 private String hexdump(String s) | 643 private String hexdump(String s) |
611 { | 644 { |
626 } | 659 } |
627 | 660 |
628 return "<" + buf.toString().trim() + ">"; | 661 return "<" + buf.toString().trim() + ">"; |
629 } | 662 } |
630 | 663 |
631 private void testFileTransferHandlerUpload(Charset handlerEncoding, String fileEncoding) throws IOException, SQLException, Failure { | 664 private void testFileTransferHandlerUploadEncoding(Charset handlerEncoding, String fileEncoding) throws IOException, SQLException, Failure { |
632 prepare(); | 665 prepare(); |
633 outBuffer.append("Default encoding is " + Charset.defaultCharset().displayName() + "\n"); | 666 outBuffer.append("Default encoding is " + Charset.defaultCharset().displayName() + "\n"); |
634 Path d = getTmpDir(currentTestName); | 667 Path d = getTmpDir(currentTestName); |
635 Path f = d.resolve("data.txt"); | 668 Path f = d.resolve("data.txt"); |
636 OutputStream s = Files.newOutputStream(f, CREATE_NEW); | 669 OutputStream s = Files.newOutputStream(f, CREATE_NEW); |
647 // | 680 // |
648 String hexTwo = hexdump(two); | 681 String hexTwo = hexdump(two); |
649 String hexResult = hexdump(result); | 682 String hexResult = hexdump(result); |
650 assertEq("query result hexdump", hexTwo, hexResult); | 683 assertEq("query result hexdump", hexTwo, hexResult); |
651 // assertEq("query result", two, result); | 684 // assertEq("query result", two, result); |
685 } | |
686 | |
687 private void testFileTransferHandlerUploadCompressed(Charset encoding, boolean compressed, int skipLines) throws IOException, SQLException, Failure { | |
688 prepare(); | |
689 Path d = getTmpDir(currentTestName); | |
690 String fileName = "data.txt"; | |
691 if (compressed) | |
692 fileName += ".gz"; | |
693 Path f = d.resolve(fileName); | |
694 OutputStream s = Files.newOutputStream(f, CREATE_NEW); | |
695 if (compressed) { | |
696 s = new GZIPOutputStream(s); | |
697 } | |
698 Writer w = new OutputStreamWriter(s, encoding); | |
699 PrintWriter ps = new PrintWriter(w); | |
700 String[] words = { "one", "twø", "three" }; | |
701 int i = 0; | |
702 int expectedSum = 0; | |
703 for (String word: words) { | |
704 int n = i + 1; | |
705 ps.println("" + n + "|" + word); | |
706 if (i >= skipLines) { | |
707 expectedSum += n; | |
708 } | |
709 i += 1; | |
710 } | |
711 ps.close(); | |
712 conn.setUploadHandler(new FileTransferHandler(d, encoding)); | |
713 String query = "COPY OFFSET " + (skipLines + 1) + " INTO foo FROM '" + fileName + "' ON CLIENT"; | |
714 update(query); | |
715 assertQueryInt("SELECT SUM(i) FROM foo", expectedSum); | |
652 } | 716 } |
653 | 717 |
654 private void test_FileTransferHandlerUploadRefused() throws IOException, SQLException, Failure { | 718 private void test_FileTransferHandlerUploadRefused() throws IOException, SQLException, Failure { |
655 initTest("test_FileTransferHandlerUploadRefused"); | 719 initTest("test_FileTransferHandlerUploadRefused"); |
656 prepare(); | 720 prepare(); |