1919package org .apache .parquet .cli .commands ;
2020
2121import 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 ;
2228import org .apache .commons .logging .LogFactory ;
2329import org .apache .log4j .PropertyConfigurator ;
30+ import org .apache .parquet .Version ;
2431import org .junit .Rule ;
2532import org .junit .rules .TemporaryFolder ;
2633import org .slf4j .Logger ;
2734import 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
2941public 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}
0 commit comments