Repository: avro Updated Branches: refs/heads/master ddfcabcee -> 8336f7ece
AVRO-1990: CreateRandomFileTool should validate arguments This closes #226 Project: http://git-wip-us.apache.org/repos/asf/avro/repo Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/8336f7ec Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/8336f7ec Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/8336f7ec Branch: refs/heads/master Commit: 8336f7ece1dc65b1dc17edff5dab9d9ad7aa9666 Parents: ddfcabc Author: Nandor Kollar <[email protected]> Authored: Fri May 26 16:51:27 2017 +0200 Committer: Gabor Szadovszky <[email protected]> Committed: Wed Jun 14 10:28:42 2017 +0200 ---------------------------------------------------------------------- CHANGES.txt | 2 + .../apache/avro/tool/CreateRandomFileTool.java | 9 ++- .../avro/tool/TestCreateRandomFileTool.java | 67 ++++++++++++++++---- 3 files changed, 63 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/avro/blob/8336f7ec/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index 72b3133..ad82ed1 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -154,6 +154,8 @@ Trunk (not yet released) AVRO-1857: GenericDatumWriter.write using BufferedBinaryEncoder leaves ByteBuffer in indeterminate state (Nandor Kollar via gabor) + AVRO-1990: CreateRandomFileTool should validate arguments (Nandor Kollar via gabor) + Avro 1.8.1 (14 May 2016) INCOMPATIBLE CHANGES http://git-wip-us.apache.org/repos/asf/avro/blob/8336f7ec/lang/java/tools/src/main/java/org/apache/avro/tool/CreateRandomFileTool.java ---------------------------------------------------------------------- diff --git a/lang/java/tools/src/main/java/org/apache/avro/tool/CreateRandomFileTool.java b/lang/java/tools/src/main/java/org/apache/avro/tool/CreateRandomFileTool.java index a8c03c1..f9025cb 100644 --- a/lang/java/tools/src/main/java/org/apache/avro/tool/CreateRandomFileTool.java +++ b/lang/java/tools/src/main/java/org/apache/avro/tool/CreateRandomFileTool.java @@ -86,7 +86,14 @@ public class CreateRandomFileTool implements Tool { writer.setCodec(Util.codecFactory(opts, codec, level)); writer.create(schema, Util.fileOrStdout(args.get(0), out)); - for (Object datum : new RandomData(schema, (int)count.value(opts))) + Integer countValue = count.value(opts); + if (countValue == null) { + err.println("Need count (--count)"); + p.printHelpOn(err); + return 1; + } + + for (Object datum : new RandomData(schema, countValue)) writer.append(datum); writer.close(); http://git-wip-us.apache.org/repos/asf/avro/blob/8336f7ec/lang/java/tools/src/test/java/org/apache/avro/tool/TestCreateRandomFileTool.java ---------------------------------------------------------------------- diff --git a/lang/java/tools/src/test/java/org/apache/avro/tool/TestCreateRandomFileTool.java b/lang/java/tools/src/test/java/org/apache/avro/tool/TestCreateRandomFileTool.java index e752961..bb6f9e5 100644 --- a/lang/java/tools/src/test/java/org/apache/avro/tool/TestCreateRandomFileTool.java +++ b/lang/java/tools/src/test/java/org/apache/avro/tool/TestCreateRandomFileTool.java @@ -33,8 +33,11 @@ import org.apache.avro.generic.GenericDatumReader; import org.apache.trevni.avro.RandomData; import org.apache.trevni.TestUtil; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class TestCreateRandomFileTool { private static final String COUNT = System.getProperty("test.count", "200"); @@ -44,20 +47,39 @@ public class TestCreateRandomFileTool { private static final File SCHEMA_FILE = new File("../../../share/test/schemas/weather.avsc"); - private byte[] run(List<String> args) throws Exception { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream p = new PrintStream(baos); - PrintStream save = System.out; + private final Schema.Parser schemaParser = new Schema.Parser(); + + private ByteArrayOutputStream out; + private ByteArrayOutputStream err; + + @Before + public void before() { + out = new ByteArrayOutputStream(); + err = new ByteArrayOutputStream(); + } + + @After + public void after() throws Exception { + out.close(); + err.close(); + } + + private int run(List<String> args) throws Exception { + PrintStream output = new PrintStream(out); + PrintStream saveOut = System.out; + PrintStream error = new PrintStream(err); + PrintStream saveErr = System.err; try { - System.setOut(p); - new CreateRandomFileTool().run(null, p, null, args); + System.setOut(output); + System.setErr(error); + return new CreateRandomFileTool().run(null, output, error, args); } finally { - System.setOut(save); + System.setOut(saveOut); + System.setErr(saveErr); } - return baos.toByteArray(); } - public void check(String... extraArgs) throws Exception { + private void check(String... extraArgs) throws Exception { ArrayList<String> args = new ArrayList<String>(); args.addAll(Arrays.asList(new String[] { OUT_FILE.toString(), @@ -72,12 +94,23 @@ public class TestCreateRandomFileTool { Iterator<Object> found = reader.iterator(); for (Object expected : - new RandomData(Schema.parse(SCHEMA_FILE), Integer.parseInt(COUNT))) + new RandomData(schemaParser.parse(SCHEMA_FILE), Integer.parseInt(COUNT))) assertEquals(expected, found.next()); reader.close(); } + private void checkMissingCount(String... extraArgs) throws Exception { + ArrayList<String> args = new ArrayList<String>(); + args.addAll(Arrays.asList(new String[] { + OUT_FILE.toString(), + "--schema-file", SCHEMA_FILE.toString() + })); + args.addAll(Arrays.asList(extraArgs)); + run(args); + assertTrue(err.toString().contains("Need count (--count)")); + } + @Test public void testSimple() throws Exception { check(); @@ -89,11 +122,17 @@ public class TestCreateRandomFileTool { } @Test + public void testMissingCountParameter() throws Exception { + checkMissingCount(); + } + + @Test public void testStdOut() throws Exception { TestUtil.resetRandomSeed(); - byte[] file = - run(Arrays.asList(new String[] - { "-", "--count", COUNT, "--schema-file", SCHEMA_FILE.toString() })); + run(Arrays.asList(new String[] + { "-", "--count", COUNT, "--schema-file", SCHEMA_FILE.toString() })); + + byte[] file = out.toByteArray(); DataFileStream<Object> reader = new DataFileStream(new ByteArrayInputStream(file), @@ -101,7 +140,7 @@ public class TestCreateRandomFileTool { Iterator<Object> found = reader.iterator(); for (Object expected : - new RandomData(Schema.parse(SCHEMA_FILE), Integer.parseInt(COUNT))) + new RandomData(schemaParser.parse(SCHEMA_FILE), Integer.parseInt(COUNT))) assertEquals(expected, found.next()); reader.close();
