|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigquery.jdbc.it; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +import java.sql.Connection; |
| 25 | +import java.sql.DatabaseMetaData; |
| 26 | +import java.sql.DriverManager; |
| 27 | +import java.sql.ResultSet; |
| 28 | +import java.sql.SQLException; |
| 29 | +import java.sql.Statement; |
| 30 | +import java.util.Random; |
| 31 | +import org.junit.jupiter.api.AfterAll; |
| 32 | +import org.junit.jupiter.api.BeforeAll; |
| 33 | +import org.junit.jupiter.api.Tag; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +@Tag("disable_tpc") |
| 37 | +public class ITPcntTest extends ITBase { |
| 38 | + |
| 39 | + private static final Random random = new Random(); |
| 40 | + private static final int randomNumber = random.nextInt(9999); |
| 41 | + private static final String PCNT_TABLE_NAME = "PCNT_TEST_TABLE_" + randomNumber; |
| 42 | + |
| 43 | + @BeforeAll |
| 44 | + public static void beforeClass() throws InterruptedException { |
| 45 | + ITBase.setUpPcntTable(ITBase.PCNT_SCHEMA, PCNT_TABLE_NAME); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterAll |
| 49 | + public static void afterClass() throws InterruptedException { |
| 50 | + ITBase.cleanUpPcntTable(ITBase.PCNT_SCHEMA, PCNT_TABLE_NAME); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testPcntDefaultDataset2TierNamespace() throws SQLException { |
| 55 | + String urlWithPcnt = ITBase.connectionUrl + ";DefaultDataset=" + ITBase.PCNT_SCHEMA + ";"; |
| 56 | + try (Connection connection = DriverManager.getConnection(urlWithPcnt)) { |
| 57 | + assertNotNull(connection); |
| 58 | + assertFalse(connection.isClosed()); |
| 59 | + try (Statement stmt = connection.createStatement(); |
| 60 | + ResultSet rs = stmt.executeQuery("SELECT * FROM " + PCNT_TABLE_NAME + " ORDER BY id")) { |
| 61 | + assertTrue(rs.next()); |
| 62 | + assertEquals(1, rs.getInt("id")); |
| 63 | + assertEquals("Alice", rs.getString("name")); |
| 64 | + assertTrue(rs.next()); |
| 65 | + assertEquals(2, rs.getInt("id")); |
| 66 | + assertEquals("Bob", rs.getString("name")); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testPcntDefaultDataset3TierNamespace() throws SQLException { |
| 73 | + String urlWithPcnt = |
| 74 | + ITBase.connectionUrl |
| 75 | + + ";DefaultDataset=" |
| 76 | + + DEFAULT_CATALOG |
| 77 | + + ":" |
| 78 | + + ITBase.PCNT_SCHEMA |
| 79 | + + ";"; |
| 80 | + try (Connection connection = DriverManager.getConnection(urlWithPcnt)) { |
| 81 | + assertNotNull(connection); |
| 82 | + assertFalse(connection.isClosed()); |
| 83 | + try (Statement stmt = connection.createStatement(); |
| 84 | + ResultSet rs = stmt.executeQuery("SELECT * FROM " + PCNT_TABLE_NAME + " ORDER BY id")) { |
| 85 | + assertTrue(rs.next()); |
| 86 | + assertEquals(1, rs.getInt("id")); |
| 87 | + assertEquals("Alice", rs.getString("name")); |
| 88 | + assertTrue(rs.next()); |
| 89 | + assertEquals(2, rs.getInt("id")); |
| 90 | + assertEquals("Bob", rs.getString("name")); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testDatabaseMetadataGetSchemasPcnt() throws SQLException { |
| 97 | + try (Connection connection = DriverManager.getConnection(ITBase.connectionUrl)) { |
| 98 | + DatabaseMetaData metaData = connection.getMetaData(); |
| 99 | + try (ResultSet rs = metaData.getSchemas(DEFAULT_CATALOG, PCNT_SCHEMA)) { |
| 100 | + assertNotNull(rs, "ResultSet from getSchemas() should not be null"); |
| 101 | + assertTrue( |
| 102 | + rs.next(), "Expected PCNT schema " + PCNT_SCHEMA + " in catalog " + DEFAULT_CATALOG); |
| 103 | + assertEquals(PCNT_SCHEMA, rs.getString("TABLE_SCHEM")); |
| 104 | + assertEquals(DEFAULT_CATALOG, rs.getString("TABLE_CATALOG")); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testDatabaseMetadataGetTablesPcnt() throws SQLException { |
| 111 | + try (Connection connection = DriverManager.getConnection(ITBase.connectionUrl)) { |
| 112 | + DatabaseMetaData metaData = connection.getMetaData(); |
| 113 | + try (ResultSet rs = metaData.getTables(DEFAULT_CATALOG, PCNT_SCHEMA, PCNT_TABLE_NAME, null)) { |
| 114 | + assertNotNull(rs, "ResultSet from getTables() should not be null"); |
| 115 | + assertTrue( |
| 116 | + rs.next(), "Expected PCNT table " + PCNT_TABLE_NAME + " under schema " + PCNT_SCHEMA); |
| 117 | + assertEquals(DEFAULT_CATALOG, rs.getString("TABLE_CAT")); |
| 118 | + assertEquals(PCNT_SCHEMA, rs.getString("TABLE_SCHEM")); |
| 119 | + assertEquals(PCNT_TABLE_NAME, rs.getString("TABLE_NAME")); |
| 120 | + assertEquals("TABLE", rs.getString("TABLE_TYPE")); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testDatabaseMetadataGetColumnsPcnt() throws SQLException { |
| 127 | + try (Connection connection = DriverManager.getConnection(ITBase.connectionUrl)) { |
| 128 | + DatabaseMetaData metaData = connection.getMetaData(); |
| 129 | + try (ResultSet rs = metaData.getColumns(DEFAULT_CATALOG, PCNT_SCHEMA, PCNT_TABLE_NAME, "%")) { |
| 130 | + assertNotNull(rs, "ResultSet from getColumns() should not be null"); |
| 131 | + int columnCount = 0; |
| 132 | + boolean foundId = false; |
| 133 | + boolean foundName = false; |
| 134 | + while (rs.next()) { |
| 135 | + columnCount++; |
| 136 | + assertEquals(DEFAULT_CATALOG, rs.getString("TABLE_CAT")); |
| 137 | + assertEquals(PCNT_SCHEMA, rs.getString("TABLE_SCHEM")); |
| 138 | + assertEquals(PCNT_TABLE_NAME, rs.getString("TABLE_NAME")); |
| 139 | + String colName = rs.getString("COLUMN_NAME"); |
| 140 | + String typeName = rs.getString("TYPE_NAME"); |
| 141 | + if ("id".equals(colName)) { |
| 142 | + foundId = true; |
| 143 | + assertEquals(1, rs.getInt("ORDINAL_POSITION")); |
| 144 | + assertTrue( |
| 145 | + typeName.equalsIgnoreCase("INT64") |
| 146 | + || typeName.equalsIgnoreCase("INTEGER") |
| 147 | + || typeName.equalsIgnoreCase("BIGINT")); |
| 148 | + } else if ("name".equals(colName)) { |
| 149 | + foundName = true; |
| 150 | + assertEquals(2, rs.getInt("ORDINAL_POSITION")); |
| 151 | + assertTrue( |
| 152 | + typeName.equalsIgnoreCase("STRING") |
| 153 | + || typeName.equalsIgnoreCase("NVARCHAR") |
| 154 | + || typeName.equalsIgnoreCase("VARCHAR")); |
| 155 | + } |
| 156 | + } |
| 157 | + assertEquals(2, columnCount, "Expected 2 columns in PCNT table " + PCNT_TABLE_NAME); |
| 158 | + assertTrue(foundId, "Expected column 'id' in PCNT table"); |
| 159 | + assertTrue(foundName, "Expected column 'name' in PCNT table"); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments