Skip to content

Commit 6c56513

Browse files
committed
[WIP][TEST] Verify outputs for parquet cli
1 parent 6fe139e commit 6c56513

File tree

4 files changed

+721
-2
lines changed

4 files changed

+721
-2
lines changed

parquet-cli/src/test/java/org/apache/parquet/cli/commands/FileTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,27 @@
1919
package org.apache.parquet.cli.commands;
2020

2121
import java.io.File;
22+
import java.io.IOException;
23+
import java.nio.charset.StandardCharsets;
24+
import java.util.Queue;
25+
import java.util.concurrent.LinkedBlockingQueue;
26+
import com.google.common.io.Resources;
27+
import org.apache.commons.lang3.StringUtils;
2228
import org.apache.commons.logging.LogFactory;
2329
import org.apache.log4j.PropertyConfigurator;
30+
import org.apache.parquet.Version;
2431
import org.junit.Rule;
2532
import org.junit.rules.TemporaryFolder;
2633
import org.slf4j.Logger;
2734
import org.slf4j.LoggerFactory;
35+
import org.slf4j.event.LoggingEvent;
36+
import org.slf4j.event.SubstituteLoggingEvent;
37+
import org.slf4j.helpers.SubstituteLoggerFactory;
38+
39+
import static org.junit.Assert.assertEquals;
2840

2941
public abstract class FileTest {
42+
static final String MASK = "*****";
3043

3144
static final String INT32_FIELD = "int32_field";
3245
static final String INT64_FIELD = "int64_field";
@@ -52,4 +65,32 @@ protected static Logger createLogger() {
5265
.setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");
5366
return console;
5467
}
68+
69+
@FunctionalInterface
70+
public interface ThrowableBiConsumer<T, U> {
71+
void accept(T t, U u) throws Exception;
72+
}
73+
74+
protected static void withLogger(ThrowableBiConsumer<Logger, Queue<? extends LoggingEvent>> body) {
75+
SubstituteLoggerFactory loggerFactory = new SubstituteLoggerFactory();
76+
LinkedBlockingQueue<SubstituteLoggingEvent> loggingEvents = loggerFactory.getEventQueue();
77+
Logger console = loggerFactory.getLogger(ParquetFileTest.class.getName());
78+
try {
79+
body.accept(console, loggingEvents);
80+
} catch (RuntimeException rethrow) {
81+
throw rethrow;
82+
} catch (Exception checkedException) {
83+
throw new RuntimeException(checkedException);
84+
} finally {
85+
loggerFactory.clear();
86+
}
87+
}
88+
89+
protected void checkOutput(String expectedFile, String actual) throws IOException {
90+
String original = Resources.toString(Resources.getResource(expectedFile), StandardCharsets.UTF_8);
91+
String expected = StringUtils.strip(original);
92+
assertEquals(
93+
expected.replace(Version.FULL_VERSION, MASK),
94+
actual.replace(Version.FULL_VERSION, MASK));
95+
}
5596
}

parquet-cli/src/test/java/org/apache/parquet/cli/commands/ShowFooterCommandTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,36 @@
2323

2424
import java.io.File;
2525
import java.io.IOException;
26+
import java.util.Queue;
2627
import org.apache.hadoop.conf.Configuration;
2728
import org.junit.Test;
29+
import org.slf4j.Logger;
30+
import org.slf4j.event.LoggingEvent;
2831

2932
public class ShowFooterCommandTest extends ParquetFileTest {
33+
3034
@Test
31-
public void testShowDirectoryCommand() throws IOException {
35+
public void testShowDirectoryCommand() {
36+
withLogger(this::testShowDirectoryCommand0);
37+
}
38+
39+
private void testShowDirectoryCommand0(
40+
Logger console, Queue<? extends LoggingEvent> loggingEvents) throws IOException {
3241
File file = parquetFile();
33-
ShowFooterCommand command = new ShowFooterCommand(createLogger());
42+
ShowFooterCommand command = new ShowFooterCommand(console);
3443
command.target = file.getAbsolutePath();
3544
command.raw = false;
3645
command.setConf(new Configuration());
3746
assertEquals(0, command.run());
47+
assertEquals(1, loggingEvents.size());
48+
LoggingEvent loggingEvent = loggingEvents.remove();
49+
checkOutput("cli-output/show_footer_command.txt", loggingEvent.getMessage());
50+
loggingEvents.clear();
3851

3952
command.raw = true;
4053
assertEquals(0, command.run());
54+
assertEquals(1, loggingEvents.size());
55+
loggingEvent = loggingEvents.remove();
56+
checkOutput("cli-output/show_footer_command_raw.txt", loggingEvent.getMessage());
4157
}
4258
}

0 commit comments

Comments
 (0)