From 0cbf213264d87ee75d7d9ae1fa5e2586700029e0 Mon Sep 17 00:00:00 2001 From: wlx5575 Date: Fri, 17 Jun 2022 19:14:17 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90opensource=E3=80=91add=20KeyspaceNotEx?= =?UTF-8?q?istTest=20#344?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/jd/jdbc/vitess/VitessDriver.java | 2 + .../jd/jdbc/vitess/KeyspaceNotExistTest.java | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/test/java/com/jd/jdbc/vitess/KeyspaceNotExistTest.java diff --git a/src/main/java/com/jd/jdbc/vitess/VitessDriver.java b/src/main/java/com/jd/jdbc/vitess/VitessDriver.java index 50f01f7..836f439 100755 --- a/src/main/java/com/jd/jdbc/vitess/VitessDriver.java +++ b/src/main/java/com/jd/jdbc/vitess/VitessDriver.java @@ -149,6 +149,8 @@ public Connection initConnect(String url, Properties info, boolean initOnly) thr } } return new VitessConnection(url, prop, topoServer, resolver, vSchemaManager, defaultKeyspace); + } catch (SQLException e) { + throw e; } catch (Exception e) { throw new SQLException(e); } diff --git a/src/test/java/com/jd/jdbc/vitess/KeyspaceNotExistTest.java b/src/test/java/com/jd/jdbc/vitess/KeyspaceNotExistTest.java new file mode 100644 index 0000000..cfd8031 --- /dev/null +++ b/src/test/java/com/jd/jdbc/vitess/KeyspaceNotExistTest.java @@ -0,0 +1,47 @@ +/* +Copyright 2021 JD Project Authors. + +Licensed 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 com.jd.jdbc.vitess; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.SQLSyntaxErrorException; +import java.sql.Statement; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import testsuite.TestSuite; +import testsuite.internal.TestSuiteShardSpec; + +public class KeyspaceNotExistTest extends TestSuite { + @Rule + public final ExpectedException thrown = ExpectedException.none(); + + @Test + public void case05() throws SQLException { + String keyspace = getKeyspace(Driver.of(TestSuiteShardSpec.TWO_SHARDS)); + String connectionUrl = getConnectionUrl(Driver.of(TestSuiteShardSpec.TWO_SHARDS)); + String keyspaceNotExist = keyspace + "not_exist"; + connectionUrl = connectionUrl.replaceAll(keyspace, keyspaceNotExist); + thrown.expect(SQLSyntaxErrorException.class); + thrown.expectMessage("Unknown database '" + keyspaceNotExist + "'"); + try (Connection connection = DriverManager.getConnection(connectionUrl); + Statement stmt = connection.createStatement();) { + stmt.executeQuery("select 1"); + } + } +}