Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 57F33200B21 for ; Fri, 10 Jun 2016 23:41:00 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 56854160A38; Fri, 10 Jun 2016 21:41:00 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 9B60C160A15 for ; Fri, 10 Jun 2016 23:40:59 +0200 (CEST) Received: (qmail 2825 invoked by uid 500); 10 Jun 2016 21:40:58 -0000 Mailing-List: contact commits-help@calcite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@calcite.apache.org Delivered-To: mailing list commits@calcite.apache.org Received: (qmail 2815 invoked by uid 99); 10 Jun 2016 21:40:58 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Jun 2016 21:40:58 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 91E09DFC74; Fri, 10 Jun 2016 21:40:58 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: elserj@apache.org To: commits@calcite.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: calcite git commit: [CALCITE-1287] TCK test for binary columns Date: Fri, 10 Jun 2016 21:40:58 +0000 (UTC) archived-at: Fri, 10 Jun 2016 21:41:00 -0000 Repository: calcite Updated Branches: refs/heads/master 717f59177 -> 113a034ec [CALCITE-1287] TCK test for binary columns Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/113a034e Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/113a034e Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/113a034e Branch: refs/heads/master Commit: 113a034ec1689dd8313af5d2f2d2431f1e666eaa Parents: 717f591 Author: Josh Elser Authored: Fri Jun 10 17:11:30 2016 -0400 Committer: Josh Elser Committed: Fri Jun 10 17:40:39 2016 -0400 ---------------------------------------------------------------------- .../calcite/avatica/tck/tests/BinaryTest.java | 105 +++++++++++++++++++ 1 file changed, 105 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/113a034e/avatica/tck/src/main/java/org/apache/calcite/avatica/tck/tests/BinaryTest.java ---------------------------------------------------------------------- diff --git a/avatica/tck/src/main/java/org/apache/calcite/avatica/tck/tests/BinaryTest.java b/avatica/tck/src/main/java/org/apache/calcite/avatica/tck/tests/BinaryTest.java new file mode 100644 index 0000000..50c7082 --- /dev/null +++ b/avatica/tck/src/main/java/org/apache/calcite/avatica/tck/tests/BinaryTest.java @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.calcite.avatica.tck.tests; + +import org.junit.Test; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Statement; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import static java.nio.charset.StandardCharsets.UTF_8; + +/** + * TCK test case to verify binary data can be written and read correctly. + */ +public class BinaryTest extends BaseTckTest { + + @Test public void readWriteBinaryData() throws Exception { + final String tableName = getTableName(); + try (Statement stmt = connection.createStatement()) { + assertFalse(stmt.execute("DROP TABLE IF EXISTS " + tableName)); + String sql = "CREATE TABLE " + tableName + + " (pk integer not null primary key, col1 binary(10))"; + assertFalse(stmt.execute(sql)); + + try (PreparedStatement pstmt = connection.prepareStatement("INSERT INTO " + tableName + + " values (?,?)")) { + for (int i = 0; i < 10; i++) { + pstmt.setInt(1, i); + pstmt.setBytes(2, ("bytes" + i).getBytes(UTF_8)); + assertEquals(1, pstmt.executeUpdate()); + } + } + + ResultSet results = stmt.executeQuery("SELECT * FROM " + tableName); + assertNotNull(results); + for (int i = 0; i < 10; i++) { + assertTrue(results.next()); + assertEquals(i, results.getInt(1)); + byte[] expectedContent = ("bytes" + i).getBytes(UTF_8); + byte[] expected = new byte[10]; + System.arraycopy(expectedContent, 0, expected, 0, expectedContent.length); + assertArrayEquals(expected, results.getBytes(2)); + } + assertFalse(results.next()); + results.close(); + } + } + + @Test public void selectivelyReadBinaryData() throws Exception { + final String tableName = getTableName(); + try (Statement stmt = connection.createStatement()) { + assertFalse(stmt.execute("DROP TABLE IF EXISTS " + tableName)); + String sql = "CREATE TABLE " + tableName + + " (pk integer not null primary key, col1 binary(10))"; + assertFalse(stmt.execute(sql)); + + try (PreparedStatement pstmt = connection.prepareStatement("INSERT INTO " + tableName + + " values (?,?)")) { + for (int i = 0; i < 10; i++) { + pstmt.setInt(1, i); + pstmt.setBytes(2, ("bytes" + i).getBytes(UTF_8)); + assertEquals(1, pstmt.executeUpdate()); + } + } + + try (PreparedStatement pstmt = connection.prepareStatement("SELECT * FROM " + tableName + + " WHERE col1 = ?")) { + byte[] expectedContent = ("bytes" + 4).getBytes(UTF_8); + byte[] expected = new byte[10]; + System.arraycopy(expectedContent, 0, expected, 0, expectedContent.length); + pstmt.setBytes(1, expected); + ResultSet results = pstmt.executeQuery(); + assertNotNull(results); + assertTrue(results.next()); + assertEquals(4, results.getInt(1)); + assertArrayEquals(expected, results.getBytes(2)); + assertFalse(results.next()); + results.close(); + } + } + } +} + +// End BinaryTest.java