forked from apache/drill
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor JDBC test infrastructure into a fluent API, JdbcAssert.
Signed-off-by: Jacques Nadeau <[email protected]>
- Loading branch information
1 parent
6a0cf9c
commit 46696a7
Showing
2 changed files
with
177 additions
and
92 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
sandbox/prototype/sqlparser/src/test/java/org/apache/drill/jdbc/test/JdbcAssert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/******************************************************************************* | ||
* 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.drill.jdbc.test; | ||
|
||
import com.google.common.base.Function; | ||
import junit.framework.Assert; | ||
|
||
import java.sql.*; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Fluent interface for writing JDBC and query-planning tests. | ||
*/ | ||
public class JdbcAssert { | ||
public static One withModel(String model, String schema) { | ||
final Properties info = new Properties(); | ||
info.setProperty("schema", schema); | ||
info.setProperty("model", "inline:" + model); | ||
return new One(info); | ||
} | ||
|
||
static String toString(ResultSet resultSet) throws SQLException { | ||
StringBuilder buf = new StringBuilder(); | ||
while (resultSet.next()) { | ||
int n = resultSet.getMetaData().getColumnCount(); | ||
String sep = ""; | ||
for (int i = 1; i <= n; i++) { | ||
buf.append(sep) | ||
.append(resultSet.getMetaData().getColumnLabel(i)) | ||
.append("=") | ||
.append(resultSet.getObject(i)); | ||
sep = "; "; | ||
} | ||
buf.append("\n"); | ||
} | ||
return buf.toString(); | ||
} | ||
|
||
public static class One { | ||
private final Properties info; | ||
private final ConnectionFactory connectionFactory; | ||
|
||
public One(Properties info) { | ||
this.info = info; | ||
this.connectionFactory = new ConnectionFactory() { | ||
public Connection createConnection() throws Exception { | ||
Class.forName("org.apache.drill.jdbc.Driver"); | ||
return DriverManager.getConnection("jdbc:drill:", One.this.info); | ||
} | ||
}; | ||
} | ||
|
||
public Two sql(String sql) { | ||
return new Two(connectionFactory, sql); | ||
} | ||
|
||
public <T> T withConnection(Function<Connection, T> function) | ||
throws Exception { | ||
Connection connection = null; | ||
try { | ||
connection = connectionFactory.createConnection(); | ||
return function.apply(connection); | ||
} finally { | ||
if (connection != null) { | ||
connection.close(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static class Two { | ||
private final ConnectionFactory connectionFactory; | ||
private final String sql; | ||
|
||
Two(ConnectionFactory connectionFactory, String sql) { | ||
this.connectionFactory = connectionFactory; | ||
this.sql = sql; | ||
} | ||
|
||
public Two returns(String expected) throws Exception { | ||
Connection connection = null; | ||
Statement statement = null; | ||
try { | ||
connection = connectionFactory.createConnection(); | ||
statement = connection.createStatement(); | ||
ResultSet resultSet = statement.executeQuery(sql); | ||
Assert.assertEquals(expected, JdbcAssert.toString(resultSet)); | ||
resultSet.close(); | ||
return this; | ||
} finally { | ||
if (statement != null) { | ||
statement.close(); | ||
} | ||
if (connection != null) { | ||
connection.close(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static interface ConnectionFactory { | ||
Connection createConnection() throws Exception; | ||
} | ||
} | ||
|
||
// End JdbcAssert.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters