From 750fe5bca60e44a8385effe32320c998a31fb5bb Mon Sep 17 00:00:00 2001 From: Patrick Favre-Bulle Date: Tue, 26 Sep 2017 15:23:23 +0700 Subject: [PATCH] More tests --- .../java/at/favre/tools/dice/RndTool.java | 4 ++- .../java/at/favre/tools/dice/RndToolTest.java | 25 ++++++++++++++- .../AnuQuantumServiceHandlerTest.java | 7 +++-- .../tools/dice/ui/ColumnRendererTest.java | 31 ++++++++++++++++--- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/main/java/at/favre/tools/dice/RndTool.java b/src/main/java/at/favre/tools/dice/RndTool.java index f7518c1..eafc739 100644 --- a/src/main/java/at/favre/tools/dice/RndTool.java +++ b/src/main/java/at/favre/tools/dice/RndTool.java @@ -214,7 +214,9 @@ private static void printRandoms(Arg arguments, Encoder encoder, DeterministicRa actualCount = new ColumnRenderer(encoder.getEncoderFormat()).render(outputList, printStream, arguments.outFile() != null); } } finally { - printStream.close(); + if (printStream != System.out) { + printStream.close(); + } } print(System.lineSeparator() + "[" + new Date().toString() + "][" + jarVersion() + "] " + actualCount * arguments.length() + " bytes generated in " + (System.currentTimeMillis() - startTime) + " ms.", arguments); diff --git a/src/test/java/at/favre/tools/dice/RndToolTest.java b/src/test/java/at/favre/tools/dice/RndToolTest.java index 5ee5e0c..a43d8a5 100644 --- a/src/test/java/at/favre/tools/dice/RndToolTest.java +++ b/src/test/java/at/favre/tools/dice/RndToolTest.java @@ -1,11 +1,21 @@ package at.favre.tools.dice; import at.favre.tools.dice.ui.Arg; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; public class RndToolTest { + @Rule + public TemporaryFolder testFolder = new TemporaryFolder(); + @Test - public void execute() throws Exception { + public void smokeTestRndTool() throws Exception { RndTool.execute(Arg.create("java", null, 12, 10, true, false, false, false, false, false, null)); RndTool.execute(Arg.create("base36", null, 4, null, true, false, false, false, false, false, null)); RndTool.execute(Arg.create("base64", "verybaadseed", 87, null, true, false, false, false, false, false, null)); @@ -14,4 +24,17 @@ public void execute() throws Exception { RndTool.execute(Arg.create("hex", null, 10, null, true, false, false, true, false, true, null)); } + @Test + public void testOutFile() throws Exception { + File tempFile = testFolder.newFile("out-test.txt"); + assertTrue(tempFile.exists()); + assertTrue(tempFile.isFile()); + assertEquals(0, tempFile.length()); + + int length = 100; + int count = 1024; + RndTool.execute(Arg.create("raw", null, length, count, true, false, false, false, false, false, tempFile)); + + assertEquals(length * count, tempFile.length()); + } } \ No newline at end of file diff --git a/src/test/java/at/favre/tools/dice/service/anuquantum/AnuQuantumServiceHandlerTest.java b/src/test/java/at/favre/tools/dice/service/anuquantum/AnuQuantumServiceHandlerTest.java index 0847355..955d486 100644 --- a/src/test/java/at/favre/tools/dice/service/anuquantum/AnuQuantumServiceHandlerTest.java +++ b/src/test/java/at/favre/tools/dice/service/anuquantum/AnuQuantumServiceHandlerTest.java @@ -6,8 +6,8 @@ import java.net.UnknownHostException; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static junit.framework.TestCase.assertFalse; +import static org.junit.Assert.*; public class AnuQuantumServiceHandlerTest { @Test @@ -28,6 +28,9 @@ public void getRandom() throws Exception { System.out.println(ByteUtils.bytesToHex(response.seed)); assertTrue(response.seed.length == AnuQuantumServiceHandler.ENTROPY_SEED_LENGTH_BYTE); assertTrue(response.response.success); + assertNotEquals(0, response.response.hashCode()); + assertNotNull(response.response.toString()); + assertFalse(response.response.equals(new AnuQuantomResponse("", -1, null, null, false))); assertTrue(response.response.length == 1); assertTrue(response.response.size == AnuQuantumServiceHandler.ENTROPY_SEED_LENGTH_BYTE); } diff --git a/src/test/java/at/favre/tools/dice/ui/ColumnRendererTest.java b/src/test/java/at/favre/tools/dice/ui/ColumnRendererTest.java index 7aa7e52..607552d 100644 --- a/src/test/java/at/favre/tools/dice/ui/ColumnRendererTest.java +++ b/src/test/java/at/favre/tools/dice/ui/ColumnRendererTest.java @@ -1,8 +1,11 @@ package at.favre.tools.dice.ui; import at.favre.tools.dice.encode.DefaultEncoderFormat; +import at.favre.tools.dice.encode.Encoder; import at.favre.tools.dice.encode.EncoderFormat; +import at.favre.tools.dice.encode.EncoderHandler; import at.favre.tools.dice.encode.byteencoder.Base36Encoder; +import at.favre.tools.dice.util.ByteUtils; import org.junit.Test; import java.io.ByteArrayOutputStream; @@ -51,7 +54,7 @@ public void renderAutoFill() throws Exception { for (int i = 1; i < maxWordLength; i += 2) { List elements = generateRnd(i, count + 20); - testRender(count, targetWidth, elements, true); + testRender(encoderFormat, count, targetWidth, elements, true, false); } } @@ -63,7 +66,7 @@ public void renderSingleColumn() throws Exception { for (int i = 1; i < maxWordLength; i += 2) { List elements = generateRnd(i, count + 20); - testRender(count, targetWidth, elements, true); + testRender(encoderFormat, count, targetWidth, elements, true, false); } } @@ -80,13 +83,13 @@ public void renderMany() throws Exception { System.out.println(); } - private void testRender(int count, int targetWidth, List elements, boolean auto) throws UnsupportedEncodingException { + private void testRender(EncoderFormat encoderFormat, int count, int targetWidth, List elements, boolean auto, boolean isFile) throws UnsupportedEncodingException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (auto) { - new ColumnRenderer(encoderFormat, targetWidth).renderAutoColumn(count, elements, new PrintStream(baos), false); + new ColumnRenderer(encoderFormat, targetWidth).renderAutoColumn(count, elements, new PrintStream(baos), isFile); } else { - new ColumnRenderer(encoderFormat, targetWidth).render(elements, new PrintStream(baos), false); + new ColumnRenderer(encoderFormat, targetWidth).render(elements, new PrintStream(baos), isFile); } String out = baos.toString("UTF-8"); @@ -107,4 +110,22 @@ private List generateRnd(int wordLength, int count) { return list; } + @Test + public void testAllEncoders() throws Exception { + int count = 9; + for (Encoder encoder : new EncoderHandler().load()) { + List rnds = generateRnd(encoder, 6, count); + testRender(encoder.getEncoderFormat(), count, 60, rnds, false, true); + testRender(encoder.getEncoderFormat(), count, 60, rnds, false, false); + } + } + + private List generateRnd(Encoder encoder, int length, int count) { + List out = new ArrayList<>(count); + for (int i = 0; i < count; i++) { + out.add(encoder.encode(ByteUtils.unsecureRandomBytes(length))); + } + return out; + } + } \ No newline at end of file