Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HIVE-28656: Improve HPL/SQL tests #5381

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
}
39 changes: 6 additions & 33 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,10 +27,7 @@
*/
public class TestHplsqlLocal {

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

@Test
@Test
ppkarwasz marked this conversation as resolved.
Show resolved Hide resolved
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();
}
}
30 changes: 5 additions & 25 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,9 +27,7 @@
*/
public class TestHplsqlOffline {

private final ByteArrayOutputStream out = new ByteArrayOutputStream();

@Test
@Test
ppkarwasz marked this conversation as resolved.
Show resolved Hide resolved
public void testCreateTable() throws Exception {
run("create_table");
}
Expand Down Expand Up @@ -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();
}
}
Loading