Skip to content

Commit

Permalink
HIVE-28656: Improve HPL/SQL tests (#5381)(Piotr P. Karwasz, reviewed …
Browse files Browse the repository at this point in the history
…by Dmitriy Fingerman, Butao Zhang)
  • Loading branch information
ppkarwasz authored Dec 5, 2024
1 parent 2ff3ed6 commit c82e1ef
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 67 deletions.
59 changes: 59 additions & 0 deletions hplsql/src/test/java/org/apache/hive/hplsql/TestConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.hive.hplsql;

import java.util.regex.Pattern;

/**
* {@link Console} implementation used in tests to capture the output of the {@link Exec} class.
*/
class TestConsole implements Console {

StringBuilder out = new StringBuilder();
StringBuilder err = new StringBuilder();
private final Pattern ignorePattern;

TestConsole(String ignorePattern) {
this.ignorePattern = Pattern.compile(ignorePattern);
}

private boolean isNotIgnored(String msg) {
return msg == null || !ignorePattern.matcher(msg).matches();
}

@Override
public void print(String msg) {
if (isNotIgnored(msg)) {
out.append(msg);
}
}

@Override
public void printLine(String msg) {
if (isNotIgnored(msg)) {
out.append(msg).append("\n");
}
}

@Override
public void printError(String msg) {
if (isNotIgnored(msg)) {
err.append(msg).append("\n");
}
}
}
37 changes: 5 additions & 32 deletions hplsql/src/test/java/org/apache/hive/hplsql/TestHplsqlLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

package org.apache.hive.hplsql;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringReader;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -31,9 +27,6 @@
*/
public class TestHplsqlLocal {

private final ByteArrayOutputStream out = new ByteArrayOutputStream();
private final ByteArrayOutputStream err = new ByteArrayOutputStream();

@Test
public void testAdd() throws Exception {
run("add");
Expand Down Expand Up @@ -468,38 +461,18 @@ public void testConversion() throws Exception {
* Run a test file
*/
void run(String testFile) throws Exception {
System.setOut(new PrintStream(out));
System.setErr(new PrintStream(err));
TestConsole console = new TestConsole("(Configuration file|Parser tree):.*");
Exec exec = new Exec();
exec.console = console;

String[] args = { "-f", "src/test/queries/local/" + testFile + ".sql", "-trace" };
exec.run(args);
String sout = getTestOutput(out.toString());
String serr = getTestOutput(err.toString());
String sout = console.out.toString();
String serr = console.err.toString();
String output = (sout + (serr.isEmpty() ? "" : serr));
FileUtils.writeStringToFile(new java.io.File("target/tmp/log/" + testFile + ".out.txt"), output);
String t = FileUtils.readFileToString(new java.io.File("src/test/results/local/" + testFile + ".out.txt"), "utf-8");
System.setOut(null);
Assert.assertEquals(t, output);
}

/**
* Get test output
*/
String getTestOutput(String s) throws Exception {
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new StringReader(s));
String line = null;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("log4j:")
&& !line.contains("INFO Log4j")
&& !line.startsWith("SLF4J")
&& !line.contains(" StatusLogger ")
&& !line.contains("Configuration file: ")
&& !line.contains("Parser tree: ")) {
sb.append(line);
sb.append("\n");
}
}
return sb.toString();
}
}
50 changes: 15 additions & 35 deletions hplsql/src/test/java/org/apache/hive/hplsql/TestHplsqlOffline.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

package org.apache.hive.hplsql;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringReader;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -31,8 +27,6 @@
*/
public class TestHplsqlOffline {

private final ByteArrayOutputStream out = new ByteArrayOutputStream();

@Test
public void testCreateTable() throws Exception {
run("create_table");
Expand All @@ -42,17 +36,17 @@ public void testCreateTable() throws Exception {
public void testCreateTableDb2() throws Exception {
run("create_table_db2");
}

@Test
public void testCreateTableMssql() throws Exception {
run("create_table_mssql");
}

@Test
public void testCreateTableMssql2() throws Exception {
run("create_table_mssql2");
}

@Test
public void testCreateTableMysql() throws Exception {
run("create_table_mysql");
Expand All @@ -62,27 +56,27 @@ public void testCreateTableMysql() throws Exception {
public void testCreateTableOra() throws Exception {
run("create_table_ora");
}

@Test
public void testCreateTableOra2() throws Exception {
run("create_table_ora2");
}

@Test
public void testCreateTablePg() throws Exception {
run("create_table_pg");
}

@Test
public void testCreateTableTd() throws Exception {
run("create_table_td");
}

@Test
public void testDeleteAll() throws Exception {
run("delete_all");
}

@Test
public void testInsertMysql() throws Exception {
run("insert_mysql");
Expand All @@ -92,17 +86,17 @@ public void testInsertMysql() throws Exception {
public void testSelect() throws Exception {
run("select");
}

@Test
public void testSelectDb2() throws Exception {
run("select_db2");
}

@Test
public void testSelectTeradata() throws Exception {
run("select_teradata");
}

@Test
public void testUpdate() throws Exception {
run("update");
Expand All @@ -112,30 +106,16 @@ public void testUpdate() throws Exception {
* Run a test file
*/
void run(String testFile) throws Exception {
System.setOut(new PrintStream(out));
TestConsole console = new TestConsole("(Configuration file|Parser tree):.*");
Exec exec = new Exec();
exec.console = console;

String[] args = { "-f", "src/test/queries/offline/" + testFile + ".sql", "-trace", "-offline" };
exec.run(args);
String s = getTestOutput(out.toString()).trim();
String s = console.out.toString().trim();
FileUtils.writeStringToFile(new java.io.File("target/tmp/log/" + testFile + ".out.txt"), s);
String t = FileUtils.readFileToString(new java.io.File("src/test/results/offline/" + testFile + ".out.txt"), "utf-8").trim();
System.setOut(null);
Assert.assertEquals(t, s);
}

/**
* Get test output
*/
String getTestOutput(String s) throws Exception {
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new StringReader(s));
String line = null;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("log4j:") && !line.contains("INFO Log4j")) {
sb.append(line);
sb.append("\n");
}
}
return sb.toString();
}
}

0 comments on commit c82e1ef

Please sign in to comment.