Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickfav committed Sep 26, 2017
1 parent df1bd49 commit 750fe5b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/main/java/at/favre/tools/dice/RndTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/at/favre/tools/dice/RndToolTest.java
Original file line number Diff line number Diff line change
@@ -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));
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
31 changes: 26 additions & 5 deletions src/test/java/at/favre/tools/dice/ui/ColumnRendererTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -51,7 +54,7 @@ public void renderAutoFill() throws Exception {

for (int i = 1; i < maxWordLength; i += 2) {
List<String> elements = generateRnd(i, count + 20);
testRender(count, targetWidth, elements, true);
testRender(encoderFormat, count, targetWidth, elements, true, false);
}
}

Expand All @@ -63,7 +66,7 @@ public void renderSingleColumn() throws Exception {

for (int i = 1; i < maxWordLength; i += 2) {
List<String> elements = generateRnd(i, count + 20);
testRender(count, targetWidth, elements, true);
testRender(encoderFormat, count, targetWidth, elements, true, false);
}
}

Expand All @@ -80,13 +83,13 @@ public void renderMany() throws Exception {
System.out.println();
}

private void testRender(int count, int targetWidth, List<String> elements, boolean auto) throws UnsupportedEncodingException {
private void testRender(EncoderFormat encoderFormat, int count, int targetWidth, List<String> 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");
Expand All @@ -107,4 +110,22 @@ private List<String> generateRnd(int wordLength, int count) {
return list;
}

@Test
public void testAllEncoders() throws Exception {
int count = 9;
for (Encoder encoder : new EncoderHandler().load()) {
List<String> rnds = generateRnd(encoder, 6, count);
testRender(encoder.getEncoderFormat(), count, 60, rnds, false, true);
testRender(encoder.getEncoderFormat(), count, 60, rnds, false, false);
}
}

private List<String> generateRnd(Encoder encoder, int length, int count) {
List<String> out = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
out.add(encoder.encode(ByteUtils.unsecureRandomBytes(length)));
}
return out;
}

}

0 comments on commit 750fe5b

Please sign in to comment.