Repository: kylin Updated Branches: refs/heads/master 3f3c42b3b -> 091aeaa33
KYLIN-2975 Close statement and connection in query acl test Signed-off-by: lidongsjtu <lid...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/kylin/repo Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/091aeaa3 Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/091aeaa3 Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/091aeaa3 Branch: refs/heads/master Commit: 091aeaa33bd7f9a59efe06dba21745116ebb0b46 Parents: 3f3c42b Author: liveandevil <1361605...@qq.com> Authored: Sun Nov 12 14:32:07 2017 +0800 Committer: lidongsjtu <lid...@apache.org> Committed: Sun Nov 12 20:28:41 2017 +0800 ---------------------------------------------------------------------- .../kylin/query/security/QuerACLTestUtil.java | 43 --------------- .../kylin/query/security/QueryACLTestUtil.java | 55 ++++++++++++++++++++ .../rest/security/QueryWithTableACLTest.java | 22 ++++---- 3 files changed, 66 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kylin/blob/091aeaa3/query/src/main/java/org/apache/kylin/query/security/QuerACLTestUtil.java ---------------------------------------------------------------------- diff --git a/query/src/main/java/org/apache/kylin/query/security/QuerACLTestUtil.java b/query/src/main/java/org/apache/kylin/query/security/QuerACLTestUtil.java deleted file mode 100644 index 123ae5b..0000000 --- a/query/src/main/java/org/apache/kylin/query/security/QuerACLTestUtil.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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.kylin.query.security; - -import org.apache.kylin.query.QueryConnection; -import org.apache.kylin.query.relnode.OLAPContext; - -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.HashMap; -import java.util.Map; - -public class QuerACLTestUtil { - public static void setUser(String username) { - Map<String, String> auth = new HashMap<>(); - auth.put(OLAPContext.PRM_USER_AUTHEN_INFO, username); - OLAPContext.setParameters(auth); - } - - public static ResultSet mockQuery(String project, String sql) throws SQLException { - Connection conn = QueryConnection.getConnection(project); - Statement statement = conn.createStatement(); - return statement.executeQuery(sql); - } -} http://git-wip-us.apache.org/repos/asf/kylin/blob/091aeaa3/query/src/main/java/org/apache/kylin/query/security/QueryACLTestUtil.java ---------------------------------------------------------------------- diff --git a/query/src/main/java/org/apache/kylin/query/security/QueryACLTestUtil.java b/query/src/main/java/org/apache/kylin/query/security/QueryACLTestUtil.java new file mode 100644 index 0000000..071e7c7 --- /dev/null +++ b/query/src/main/java/org/apache/kylin/query/security/QueryACLTestUtil.java @@ -0,0 +1,55 @@ +/* + * 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.kylin.query.security; + +import org.apache.kylin.query.QueryConnection; +import org.apache.kylin.query.relnode.OLAPContext; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.HashMap; +import java.util.Map; + +public class QueryACLTestUtil { + public static void setUser(String username) { + Map<String, String> auth = new HashMap<>(); + auth.put(OLAPContext.PRM_USER_AUTHEN_INFO, username); + OLAPContext.setParameters(auth); + } + + public static void mockQuery(String project, String sql) throws SQLException { + Connection conn = null; + Statement statement = null; + try { + conn = QueryConnection.getConnection(project); + statement = conn.createStatement(); + statement.executeQuery(sql); + } catch (SQLException ex) { + throw ex; + } finally { + if (statement != null) { + statement.close(); + } + if (conn != null) { + conn.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/kylin/blob/091aeaa3/server/src/test/java/org/apache/kylin/rest/security/QueryWithTableACLTest.java ---------------------------------------------------------------------- diff --git a/server/src/test/java/org/apache/kylin/rest/security/QueryWithTableACLTest.java b/server/src/test/java/org/apache/kylin/rest/security/QueryWithTableACLTest.java index 950f586..414a241 100644 --- a/server/src/test/java/org/apache/kylin/rest/security/QueryWithTableACLTest.java +++ b/server/src/test/java/org/apache/kylin/rest/security/QueryWithTableACLTest.java @@ -25,7 +25,7 @@ import org.apache.kylin.common.KylinConfig; import org.apache.kylin.common.util.LocalFileMetadataTestCase; import org.apache.kylin.metadata.acl.TableACLManager; import org.apache.kylin.query.security.AccessDeniedException; -import org.apache.kylin.query.security.QuerACLTestUtil; +import org.apache.kylin.query.security.QueryACLTestUtil; import org.hamcrest.CoreMatchers; import org.junit.After; import org.junit.Before; @@ -49,32 +49,32 @@ public class QueryWithTableACLTest extends LocalFileMetadataTestCase { @Test public void testNormalQuery() throws SQLException { - QuerACLTestUtil.setUser(ADMIN); - QuerACLTestUtil.mockQuery(PROJECT, "select * from STREAMING_TABLE"); + QueryACLTestUtil.setUser(ADMIN); + QueryACLTestUtil.mockQuery(PROJECT, "select * from STREAMING_TABLE"); } @Test public void testFailQuery() throws SQLException, IOException { - QuerACLTestUtil.setUser(MODELER); - QuerACLTestUtil.mockQuery(PROJECT, "select * from STREAMING_TABLE"); + QueryACLTestUtil.setUser(MODELER); + QueryACLTestUtil.mockQuery(PROJECT, "select * from STREAMING_TABLE"); - QuerACLTestUtil.setUser(ADMIN); + QueryACLTestUtil.setUser(ADMIN); TableACLManager.getInstance(KylinConfig.getInstanceFromEnv()).addTableACL(PROJECT, "ADMIN", STREAMING_TABLE); thrown.expectCause(CoreMatchers.isA(AccessDeniedException.class)); thrown.expectMessage(CoreMatchers.containsString("Query failed.Access table:DEFAULT.STREAMING_TABLE denied")); - QuerACLTestUtil.mockQuery(PROJECT, "select * from STREAMING_TABLE"); + QueryACLTestUtil.mockQuery(PROJECT, "select * from STREAMING_TABLE"); } @Test public void testFailQueryWithCountStar() throws SQLException, IOException { - QuerACLTestUtil.setUser(MODELER); - QuerACLTestUtil.mockQuery(PROJECT, "select count(*) from STREAMING_TABLE"); + QueryACLTestUtil.setUser(MODELER); + QueryACLTestUtil.mockQuery(PROJECT, "select count(*) from STREAMING_TABLE"); - QuerACLTestUtil.setUser(ADMIN); + QueryACLTestUtil.setUser(ADMIN); TableACLManager.getInstance(KylinConfig.getInstanceFromEnv()).addTableACL(PROJECT, "ADMIN", STREAMING_TABLE); thrown.expectCause(CoreMatchers.isA(AccessDeniedException.class)); thrown.expectMessage(CoreMatchers.containsString("Query failed.Access table:DEFAULT.STREAMING_TABLE denied")); - QuerACLTestUtil.mockQuery(PROJECT, "select count(*) from STREAMING_TABLE"); + QueryACLTestUtil.mockQuery(PROJECT, "select count(*) from STREAMING_TABLE"); } @After