Skip to content

Commit d6610b2

Browse files
committed
update
1 parent 2a58300 commit d6610b2

11 files changed

Lines changed: 462 additions & 8 deletions

File tree

parquet-cli/src/main/java/org/apache/parquet/cli/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public class Main extends Configured implements Tool {
8282
@VisibleForTesting
8383
final JCommander jc;
8484

85-
Main(Logger console) {
85+
@VisibleForTesting
86+
public Main(Logger console) {
8687
this.console = console;
8788
this.jc = new JCommander(this);
8889
this.help = new Help(jc, console);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.parquet.cli;
20+
21+
import java.io.File;
22+
import org.apache.parquet.cli.testing.CliTestBase;
23+
import org.junit.Test;
24+
25+
public class ShowSizeStatisticsCliTest extends CliTestBase {
26+
27+
@Test
28+
public void showSizeStatistics() throws Exception {
29+
File file = parquetFile();
30+
31+
cli("size-stats " + file.getAbsolutePath())
32+
.ok()
33+
.matchOutputFromFile("src/test/resources/cli-outputs/size-stats.txt");
34+
}
35+
36+
@Test
37+
public void showsHelpMessage() throws Exception {
38+
cli("help size-stats")
39+
.ok()
40+
.matchOutputFromFile("src/test/resources/cli-outputs/size-stats-help.txt");
41+
}
42+
43+
@Test
44+
public void showsSchemaOutput() throws Exception {
45+
File file = parquetFile();
46+
cli("schema " + file.getAbsolutePath())
47+
.ok()
48+
.matchOutputFromFile("src/test/resources/cli-outputs/size-stats-column.txt");
49+
}
50+
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333

3434
public abstract class FileTest {
3535

36-
static final String INT32_FIELD = "int32_field";
37-
static final String INT64_FIELD = "int64_field";
38-
static final String FLOAT_FIELD = "float_field";
39-
static final String DOUBLE_FIELD = "double_field";
40-
static final String BINARY_FIELD = "binary_field";
41-
static final String FIXED_LEN_BYTE_ARRAY_FIELD = "flba_field";
42-
static final String DATE_FIELD = "date_field";
36+
public static final String INT32_FIELD = "int32_field";
37+
public static final String INT64_FIELD = "int64_field";
38+
public static final String FLOAT_FIELD = "float_field";
39+
public static final String DOUBLE_FIELD = "double_field";
40+
public static final String BINARY_FIELD = "binary_field";
41+
public static final String FIXED_LEN_BYTE_ARRAY_FIELD = "flba_field";
42+
public static final String DATE_FIELD = "date_field";
4343

4444
static final String[] COLORS = {"RED", "BLUE", "YELLOW", "GREEN", "WHITE"};
4545

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.parquet.cli.testing;
20+
21+
import org.slf4j.helpers.MarkerIgnoringBase;
22+
import org.slf4j.helpers.MessageFormatter;
23+
24+
// CapturingLogger is a wrapper around the slf4j logger to capture CLI ourput to use with tests.
25+
final class CapturingLogger extends MarkerIgnoringBase implements org.slf4j.Logger {
26+
private final StringBuilder buf = new StringBuilder();
27+
28+
@Override
29+
public String getName() {
30+
return "CliTestLogger";
31+
}
32+
33+
private void append(String msg) {
34+
if (msg != null && !msg.isEmpty()) {
35+
buf.append(msg).append('\n');
36+
}
37+
}
38+
39+
private void log(String fmt, Object... args) {
40+
String message = MessageFormatter.arrayFormat(fmt, args).getMessage();
41+
append(message);
42+
}
43+
44+
String dump() {
45+
return buf.toString();
46+
}
47+
48+
49+
// Since the CLI logic can call any console method, this is some needed delegator code to
50+
// ensure all methods are coverted and that the test harness does not miss anything.
51+
// Unfortunately slf4j API does not make this easy to do in a generic way, so we
52+
// have to manually add each method.
53+
54+
@Override
55+
public boolean isTraceEnabled() {
56+
return true;
57+
}
58+
59+
@Override
60+
public boolean isDebugEnabled() {
61+
return true;
62+
}
63+
64+
@Override
65+
public boolean isInfoEnabled() {
66+
return true;
67+
}
68+
69+
@Override
70+
public boolean isWarnEnabled() {
71+
return true;
72+
}
73+
74+
@Override
75+
public boolean isErrorEnabled() {
76+
return true;
77+
}
78+
79+
@Override
80+
public void trace(String msg) {
81+
append(msg);
82+
}
83+
84+
@Override
85+
public void trace(String format, Object arg) {
86+
log(format, arg);
87+
}
88+
89+
@Override
90+
public void trace(String format, Object arg1, Object arg2) {
91+
log(format, arg1, arg2);
92+
}
93+
94+
@Override
95+
public void trace(String format, Object... arguments) {
96+
log(format, arguments);
97+
}
98+
99+
@Override
100+
public void trace(String msg, Throwable t) {
101+
append(msg);
102+
}
103+
104+
@Override
105+
public void debug(String msg) {
106+
append(msg);
107+
}
108+
109+
@Override
110+
public void debug(String format, Object arg) {
111+
log(format, arg);
112+
}
113+
114+
@Override
115+
public void debug(String format, Object arg1, Object arg2) {
116+
log(format, arg1, arg2);
117+
}
118+
119+
@Override
120+
public void debug(String format, Object... arguments) {
121+
log(format, arguments);
122+
}
123+
124+
@Override
125+
public void debug(String msg, Throwable t) {
126+
append(msg);
127+
}
128+
129+
@Override
130+
public void info(String msg) {
131+
append(msg);
132+
}
133+
134+
@Override
135+
public void info(String format, Object arg) {
136+
log(format, arg);
137+
}
138+
139+
@Override
140+
public void info(String format, Object arg1, Object arg2) {
141+
log(format, arg1, arg2);
142+
}
143+
144+
@Override
145+
public void info(String format, Object... arguments) {
146+
log(format, arguments);
147+
}
148+
149+
@Override
150+
public void info(String msg, Throwable t) {
151+
append(msg);
152+
}
153+
154+
@Override
155+
public void warn(String msg) {
156+
append(msg);
157+
}
158+
159+
@Override
160+
public void warn(String format, Object arg) {
161+
log(format, arg);
162+
}
163+
164+
@Override
165+
public void warn(String format, Object arg1, Object arg2) {
166+
log(format, arg1, arg2);
167+
}
168+
169+
@Override
170+
public void warn(String format, Object... arguments) {
171+
log(format, arguments);
172+
}
173+
174+
@Override
175+
public void warn(String msg, Throwable t) {
176+
append(msg);
177+
}
178+
179+
@Override
180+
public void error(String msg) {
181+
append(msg);
182+
}
183+
184+
@Override
185+
public void error(String format, Object arg) {
186+
log(format, arg);
187+
}
188+
189+
@Override
190+
public void error(String format, Object arg1, Object arg2) {
191+
log(format, arg1, arg2);
192+
}
193+
194+
@Override
195+
public void error(String format, Object... arguments) {
196+
log(format, arguments);
197+
}
198+
199+
@Override
200+
public void error(String msg, Throwable t) {
201+
append(msg);
202+
}
203+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.parquet.cli.testing;
20+
21+
import org.apache.hadoop.conf.Configuration;
22+
import org.apache.parquet.cli.Main;
23+
import org.slf4j.Logger;
24+
25+
public final class CliHarness {
26+
public CliResult run(String[] args) throws Exception {
27+
CapturingLogger logger = new CapturingLogger();
28+
Main main = new Main((Logger) logger);
29+
main.setConf(new Configuration());
30+
int code = main.run(args);
31+
32+
CliResult result = new CliResult(code, logger.dump());
33+
return result;
34+
}
35+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.parquet.cli.testing;
20+
21+
import static org.junit.Assert.*;
22+
import java.nio.file.Files;
23+
import java.nio.file.Paths;
24+
import java.nio.charset.StandardCharsets;
25+
26+
public final class CliResult {
27+
public final int exitCode;
28+
public final String text;
29+
30+
CliResult(int exitCode, String text) {
31+
this.exitCode = exitCode;
32+
this.text = text;
33+
}
34+
35+
public CliResult ok() {
36+
assertEquals("exit", 0, exitCode);
37+
return this;
38+
}
39+
40+
public CliResult fails(int code) {
41+
assertEquals("exit", code, exitCode);
42+
return this;
43+
}
44+
45+
public CliResult outputContains(String... parts) {
46+
for (String p : parts) assertTrue("missing: " + p, text.contains(p));
47+
return this;
48+
}
49+
50+
public CliResult outputNotContains(String... parts) {
51+
for (String p : parts) assertFalse("should not contain: " + p, text.contains(p));
52+
return this;
53+
}
54+
55+
public CliResult lineCount(int expected) {
56+
long cnt = 0;
57+
for (String line : text.split("\n")) {
58+
if (!line.trim().isEmpty()) {
59+
cnt++;
60+
}
61+
}
62+
assertEquals(expected, cnt);
63+
return this;
64+
}
65+
66+
public CliResult matchOutputFromFile(String filePath) throws Exception {
67+
String expected = new String(
68+
Files.readAllBytes(Paths.get(filePath)),
69+
StandardCharsets.UTF_8);
70+
return outputContains(expected);
71+
}
72+
}

0 commit comments

Comments
 (0)