8313612: Use JUnit in lib-test/jdk tests

Reviewed-by: cstein, asotona
This commit is contained in:
Qing Xiao 2023-09-21 06:24:49 +00:00 committed by Christian Stein
parent bee7524251
commit a35e96a3fa
5 changed files with 269 additions and 252 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,22 +23,20 @@
package jdk.test.lib.format;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Test;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
/*
* @test
* @summary Check ArrayDiff formatting
* @library /test/lib
* @run testng jdk.test.lib.format.ArrayDiffTest
* @run junit jdk.test.lib.format.ArrayDiffTest
*/
public class ArrayDiffTest {
class ArrayDiffTest {
@Test
public void testEqualArrays() {
void testEqualArrays() {
char[] first = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
char[] second = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
@ -46,7 +44,7 @@ public class ArrayDiffTest {
}
@Test
public void testOutputFitsWidth() {
void testOutputFitsWidth() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
@ -62,7 +60,7 @@ public class ArrayDiffTest {
}
@Test
public void testIntegers() {
void testIntegers() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
@ -78,7 +76,7 @@ public class ArrayDiffTest {
}
@Test
public void testLongs() {
void testLongs() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
@ -94,7 +92,7 @@ public class ArrayDiffTest {
}
@Test
public void testFirstElementIsWrong() {
void testFirstElementIsWrong() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
@ -110,7 +108,7 @@ public class ArrayDiffTest {
}
@Test
public void testOneElementIsEmpty() {
void testOneElementIsEmpty() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
@ -126,7 +124,7 @@ public class ArrayDiffTest {
}
@Test
public void testOutputDoesntFitWidth() {
void testOutputDoesntFitWidth() {
new AssertBuilder()
.withParams(20, Integer.MAX_VALUE)
.withArrays(
@ -142,7 +140,7 @@ public class ArrayDiffTest {
}
@Test
public void testVariableElementWidthOutputDoesntFitWidth() {
void testVariableElementWidthOutputDoesntFitWidth() {
new AssertBuilder()
.withParams(20, Integer.MAX_VALUE)
.withArrays(
@ -158,7 +156,7 @@ public class ArrayDiffTest {
}
@Test
public void testContextBefore() {
void testContextBefore() {
new AssertBuilder()
.withParams(20, 2)
.withArrays(
@ -174,7 +172,7 @@ public class ArrayDiffTest {
}
@Test
public void testBoundedBytesWithDifferentWidth() {
void testBoundedBytesWithDifferentWidth() {
new AssertBuilder()
.withParams(24, 2)
.withArrays(
@ -190,7 +188,7 @@ public class ArrayDiffTest {
}
@Test
public void testBoundedFirstElementIsWrong() {
void testBoundedFirstElementIsWrong() {
new AssertBuilder()
.withParams(25, 2)
.withArrays(
@ -206,7 +204,7 @@ public class ArrayDiffTest {
}
@Test
public void testBoundedOneArchiveIsEmpty() {
void testBoundedOneArchiveIsEmpty() {
new AssertBuilder()
.withParams(10, 2)
.withArrays(
@ -222,7 +220,7 @@ public class ArrayDiffTest {
}
@Test
public void testUnboundedOneArchiveIsEmpty() {
void testUnboundedOneArchiveIsEmpty() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
@ -238,7 +236,7 @@ public class ArrayDiffTest {
}
@Test
public void testUnprintableCharFormatting() {
void testUnprintableCharFormatting() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
@ -254,7 +252,7 @@ public class ArrayDiffTest {
}
@Test
public void testStringElements() {
void testStringElements() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
@ -270,7 +268,7 @@ public class ArrayDiffTest {
}
@Test
public void testToStringableObjects() {
void testToStringableObjects() {
class StrObj {
private final String value;
public boolean equals(Object another) { return ((StrObj)another).value.equals(value); }
@ -294,7 +292,7 @@ public class ArrayDiffTest {
}
@Test
public void testNullElements() {
void testNullElements() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
@ -309,14 +307,14 @@ public class ArrayDiffTest {
.assertTwoWay();
}
@Test (expectedExceptions = NullPointerException.class)
public void testFirstArrayIsNull() {
var diff = ArrayDiff.of(null, new String[] {"a", "b"});
@Test
void testFirstArrayIsNull() {
assertThrows(NullPointerException.class, () -> ArrayDiff.of(null, new String[] {"a", "b"}));
}
@Test (expectedExceptions = NullPointerException.class)
public void testSecondArrayIsNull() {
var diff = ArrayDiff.of(null, new String[] {"a", "b"});
@Test
void testSecondArrayIsNull() {
assertThrows(NullPointerException.class, () -> ArrayDiff.of(new String[] {"a", "b"}, null));
}
class AssertBuilder {
@ -331,30 +329,30 @@ public class ArrayDiffTest {
private String secondFormattedArray;
private String failureMark;
public AssertBuilder withDefaultParams() {
AssertBuilder withDefaultParams() {
defaultParameters = true;
return this;
}
public AssertBuilder withParams(int width, int contextBefore) {
AssertBuilder withParams(int width, int contextBefore) {
defaultParameters = false;
this.width = width;
this.contextBefore = contextBefore;
return this;
}
public AssertBuilder withArrays(Object first, Object second) {
AssertBuilder withArrays(Object first, Object second) {
firstArray = first;
secondArray = second;
return this;
}
public AssertBuilder thatResultIs(boolean result) {
AssertBuilder thatResultIs(boolean result) {
expectedResult = result;
return this;
}
public AssertBuilder thatFormattedValuesAre(
AssertBuilder thatFormattedValuesAre(
int idx, String first, String second, String mark) {
expectedIndex = idx;
firstFormattedArray = first;
@ -363,7 +361,7 @@ public class ArrayDiffTest {
return this;
}
public void assertTwoWay() {
void assertTwoWay() {
ArrayDiff<?> diff;
// Direct
@ -382,7 +380,7 @@ public class ArrayDiffTest {
expectedIndex, firstFormattedArray, secondFormattedArray, failureMark);
assertFalse(diff.areEqual());
assertEquals(diff.format(), expected);
assertEquals(expected, diff.format());
}
// Reversed
@ -401,7 +399,7 @@ public class ArrayDiffTest {
expectedIndex, secondFormattedArray, firstFormattedArray, failureMark);
assertFalse(diff.areEqual());
assertEquals(diff.format(), expected);
assertEquals(expected, diff.format());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,8 +23,7 @@
package jdk.test.lib.hexdump;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Test;
import java.io.DataInputStream;
import java.io.EOFException;
@ -34,7 +33,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import static org.testng.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
/*
* @test
@ -42,14 +41,13 @@ import static org.testng.Assert.*;
* @modules java.base/sun.security.util
* @library /test/lib
* @compile ASN1FormatterTest.java
* @run testng jdk.test.lib.hexdump.ASN1FormatterTest
* @run junit jdk.test.lib.hexdump.ASN1FormatterTest
*/
@Test
public class ASN1FormatterTest {
class ASN1FormatterTest {
private static final String DIR = System.getProperty("test.src", ".");
@Test
static void testPEM() throws IOException {
void testPEM() throws IOException {
String certFile = "openssl.p12.pem";
Path certPath = Path.of(DIR, certFile);
System.out.println("certPath: " + certPath);
@ -64,18 +62,18 @@ public class ASN1FormatterTest {
String result = ASN1Formatter.formatter().annotate(is);
System.out.println(result);
Assert.assertEquals(result.lines().count(), 76, "Lines");
Assert.assertEquals(result.lines().filter(s -> s.contains("SEQUENCE")).count(),24, "Sequences");
Assert.assertEquals(result.lines().filter(s -> s.contains("OBJECT ID")).count(), 17, "ObjectIDs");
Assert.assertEquals(result.lines().filter(s -> s.contains("UTCTIME")).count(), 2, "UTCTIME");
Assert.assertEquals(result.lines().filter(s -> s.contains("BIT STRING")).count(), 3, "BitStrings");
assertEquals(76, result.lines().count(), "Lines");
assertEquals(24, result.lines().filter(s -> s.contains("SEQUENCE")).count(),"Sequences");
assertEquals(17, result.lines().filter(s -> s.contains("OBJECT ID")).count(), "ObjectIDs");
assertEquals(2, result.lines().filter(s -> s.contains("UTCTIME")).count(), "UTCTIME");
assertEquals(3, result.lines().filter(s -> s.contains("BIT STRING")).count(), "BitStrings");
} catch (EOFException eof) {
// done
}
}
@Test
static void dumpPEM() throws IOException {
void dumpPEM() throws IOException {
String file = "openssl.p12.pem";
Path path = Path.of(DIR, file);
System.out.println("path: " + path);
@ -92,34 +90,34 @@ public class ASN1FormatterTest {
String result = p.toString(wis);
System.out.println(result);
Assert.assertEquals(result.lines().count(), 126, "Lines");
Assert.assertEquals(result.lines().filter(s -> s.contains("SEQUENCE")).count(), 24, "Sequences");
Assert.assertEquals(result.lines().filter(s -> s.contains("OBJECT ID")).count(), 17, "ObjectIDs");
Assert.assertEquals(result.lines().filter(s -> s.contains("UTCTIME")).count(), 2, "UTCTIME");
Assert.assertEquals(result.lines().filter(s -> s.contains("BIT STRING")).count(), 3, "BitStrings");
assertEquals(126, result.lines().count(), "Lines");
assertEquals(24, result.lines().filter(s -> s.contains("SEQUENCE")).count(), "Sequences");
assertEquals(17, result.lines().filter(s -> s.contains("OBJECT ID")).count(), "ObjectIDs");
assertEquals(2, result.lines().filter(s -> s.contains("UTCTIME")).count(), "UTCTIME");
assertEquals(3, result.lines().filter(s -> s.contains("BIT STRING")).count(), "BitStrings");
} catch (EOFException eof) {
// done
}
}
@Test
static void testIndefinite() {
void testIndefinite() {
byte[] bytes = {0x24, (byte) 0x80, 4, 2, 'a', 'b', 4, 2, 'c', 'd', 0, 0};
HexPrinter p = HexPrinter.simple()
.formatter(ASN1Formatter.formatter(), "; ", 100);
String result = p.toString(bytes);
System.out.println(result);
Assert.assertEquals(result.lines().filter(s -> s.contains("OCTET STRING [INDEFINITE]")).count(),
1, "Indefinite length");
Assert.assertEquals(result.lines().filter(s -> s.contains("; OCTET STRING [2]")).count(),
2, "Octet Sequences");
Assert.assertEquals(result.lines().filter(s -> s.contains("; END-OF-CONTENT")).count(),
1, "end of content");
assertEquals(1, result.lines().filter(s -> s.contains("OCTET STRING [INDEFINITE]")).count(),
"Indefinite length");
assertEquals(2, result.lines().filter(s -> s.contains("; OCTET STRING [2]")).count(),
"Octet Sequences");
assertEquals(1, result.lines().filter(s -> s.contains("; END-OF-CONTENT")).count(),
"end of content");
}
@Test
static void testMain() {
void testMain() {
String file = "openssl.p12.pem";
Path path = Path.of(DIR, file);
String[] args = { path.toString() };

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,9 +25,10 @@ package jdk.test.lib.hexdump;
import jdk.test.lib.hexdump.HexPrinter;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.Arguments;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -36,6 +37,9 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
/*
@ -43,12 +47,12 @@ import java.nio.ByteBuffer;
* @summary Check HexPrinter formatting
* @library /test/lib
* @compile HexPrinterTest.java
* @run testng jdk.test.lib.hexdump.HexPrinterTest
* @run junit jdk.test.lib.hexdump.HexPrinterTest
*/
public class HexPrinterTest {
class HexPrinterTest {
@Test
static void testMinimalToStringByteArray() {
void testMinimalToStringByteArray() {
int len = 16;
byte[] bytes = genData(len);
StringBuilder expected = new StringBuilder(bytes.length * 2);
@ -56,35 +60,34 @@ public class HexPrinterTest {
expected.append(String.format("%02x", bytes[i]));
String actual = HexPrinter.minimal().toString(bytes);
System.out.println(actual);
Assert.assertEquals(actual, expected.toString(), "Minimal format incorrect");
assertEquals(expected.toString(), actual, "Minimal format incorrect");
}
@DataProvider(name = "ColumnParams")
Object[][] columnParams() {
return new Object[][]{
{"%4d: ", "%d ", 10, " ; ", 50, HexPrinter.Formatters.PRINTABLE, "\n"},
{"%03o: ", "%d ", 16, " ; ", 50, HexPrinter.Formatters.ofPrimitive(byte.class, ""), "\n"},
{"%5d: ", "%02x:", 16, " ; ", 50, HexPrinter.Formatters.ofPrimitive(byte.class, ""), "\n"},
{"%5d: ", "%3d", 16, " ; ", 50, HexPrinter.Formatters.ofPrimitive(byte.class, ""), "\n"},
{"%05o: ", "%3o", 8, " ; ", 50, HexPrinter.Formatters.ofPrimitive(byte.class, ""), "\n"},
{"%6x: ", "%02x", 8, " | ", 50, HexPrinter.Formatters.ofPrimitive(byte.class, "%d "), "\n"},
{"%2x: ", "%02x", 8, " | ", 50, HexPrinter.Formatters.PRINTABLE, "\n"},
{"%5d: ", "%02x", 16, " | ", 50, HexPrinter.Formatters.ofPrimitive(short.class, "%d "), "\n"},
};
static Stream<Arguments> columnParams() {
return Stream.of(
Arguments.of("%4d: ", "%d ", 10, " ; ", 50, HexPrinter.Formatters.PRINTABLE, "\n"),
Arguments.of("%03o: ", "%d ", 16, " ; ", 50, HexPrinter.Formatters.ofPrimitive(byte.class, ""), "\n"),
Arguments.of("%5d: ", "%02x:", 16, " ; ", 50, HexPrinter.Formatters.ofPrimitive(byte.class, ""), "\n"),
Arguments.of("%5d: ", "%3d", 16, " ; ", 50, HexPrinter.Formatters.ofPrimitive(byte.class, ""), "\n"),
Arguments.of("%05o: ", "%3o", 8, " ; ", 50, HexPrinter.Formatters.ofPrimitive(byte.class, ""), "\n"),
Arguments.of("%6x: ", "%02x", 8, " | ", 50, HexPrinter.Formatters.ofPrimitive(byte.class, "%d "), "\n"),
Arguments.of("%2x: ", "%02x", 8, " | ", 50, HexPrinter.Formatters.PRINTABLE, "\n"),
Arguments.of("%5d: ", "%02x", 16, " | ", 50, HexPrinter.Formatters.ofPrimitive(short.class, "%d "), "\n")
);
}
@DataProvider(name = "BuiltinParams")
Object[][] builtinParams() {
return new Object[][]{
{"minimal", "", "%02x", 16, "", 64, HexPrinter.Formatters.NONE, ""},
{"canonical", "%08x ", "%02x ", 16, "|", 31, HexPrinter.Formatters.PRINTABLE, "|" + System.lineSeparator()},
{"simple", "%04x: ", "%02x ", 16, " // ", 64, HexPrinter.Formatters.ASCII, System.lineSeparator()},
{"source", " ", "(byte)%3d, ", 8, " // ", 64, HexPrinter.Formatters.PRINTABLE, System.lineSeparator()},
};
static Stream<Arguments> builtinParams() {
return Stream.of(
Arguments.of("minimal", "", "%02x", 16, "", 64, HexPrinter.Formatters.NONE, ""),
Arguments.of("canonical", "%08x ", "%02x ", 16, "|", 31, HexPrinter.Formatters.PRINTABLE, "|" + System.lineSeparator()),
Arguments.of("simple", "%04x: ", "%02x ", 16, " // ", 64, HexPrinter.Formatters.ASCII, System.lineSeparator()),
Arguments.of("source", " ", "(byte)%3d, ", 8, " // ", 64, HexPrinter.Formatters.PRINTABLE, System.lineSeparator())
);
}
@Test(dataProvider = "BuiltinParams")
public void testBuiltins(String name, String offsetFormat, String binFormat, int colWidth,
@ParameterizedTest
@MethodSource("builtinParams")
void testBuiltins(String name, String offsetFormat, String binFormat, int colWidth,
String annoDelim, int annoWidth,
HexPrinter.Formatter mapper, String lineSep) {
HexPrinter f = switch (name) {
@ -104,11 +107,12 @@ public class HexPrinterTest {
.formatter(mapper, annoDelim, annoWidth)
.withLineSeparator(lineSep);
String expected = f2.toString();
Assert.assertEquals(actual, expected, "toString of " + name + " does not match");
assertEquals(expected, actual, "toString of " + name + " does not match");
}
@Test(dataProvider = "ColumnParams")
public void testToStringTwoLines(String offsetFormat, String binFormat, int colWidth,
@ParameterizedTest
@MethodSource("columnParams")
void testToStringTwoLines(String offsetFormat, String binFormat, int colWidth,
String annoDelim, int annoWidth,
HexPrinter.Formatter mapper, String lineSep) {
HexPrinter f = HexPrinter.simple()
@ -119,7 +123,7 @@ public class HexPrinterTest {
testParams(f, offsetFormat, binFormat, colWidth, annoDelim, annoWidth, mapper, lineSep);
}
public static void testParams(HexPrinter printer, String offsetFormat, String binFormat, int colWidth,
static void testParams(HexPrinter printer, String offsetFormat, String binFormat, int colWidth,
String annoDelim, int annoWidth,
HexPrinter.Formatter mapper, String lineSep) {
byte[] bytes = genData(colWidth * 2);
@ -136,22 +140,22 @@ public class HexPrinterTest {
if (i % colWidth == 0) {
String offset = String.format(offsetFormat, i);
l = offset.length();
Assert.assertEquals(out.substring(ndx, ndx + l), offset,
assertEquals(offset, out.substring(ndx, ndx + l),
"offset format mismatch: " + ndx);
ndx += l;
valuesStart = ndx;
}
String value = String.format(binFormat, (0xff & bytes[i]));
l = value.length();
Assert.assertEquals(out.substring(ndx, ndx + l), value,
assertEquals(value, out.substring(ndx, ndx + l),
"value format mismatch: " + ndx + ", i: " + i);
ndx += l;
if (((i + 1) % colWidth) == 0) {
// Rest of line is for padding, delimiter, formatter
String padding = " ".repeat(padToWidth - (ndx - valuesStart));
Assert.assertEquals(out.substring(ndx, ndx + padding.length()), padding, "padding");
assertEquals(padding, out.substring(ndx, ndx + padding.length()), "padding");
ndx += padding.length();
Assert.assertEquals(out.substring(ndx, ndx + annoDelim.length()), annoDelim,
assertEquals(annoDelim, out.substring(ndx, ndx + annoDelim.length()),
"delimiter mismatch");
ndx += annoDelim.length();
@ -162,7 +166,7 @@ public class HexPrinterTest {
}
@Test
static void testPrintable() {
void testPrintable() {
String expected =
"................................" +
" !\"#$%&'()*+,-./0123456789:;<=>?" +
@ -179,11 +183,11 @@ public class HexPrinterTest {
.withBytesFormat("", 256)
.formatter(HexPrinter.Formatters.PRINTABLE, "", 512);
String actual = p.toString(bytes);
Assert.assertEquals(actual, expected, "Formatters.Printable mismatch");
assertEquals(expected, actual, "Formatters.Printable mismatch");
}
@Test
static void testASCII() {
void testASCII() {
String expected = "\\nul\\soh\\stx\\etx\\eot\\enq\\ack\\bel\\b\\t\\n\\vt\\f\\r\\so\\si\\dle" +
"\\dc1\\dc2\\dc3\\dc4\\nak\\syn\\etb\\can\\em\\sub\\esc\\fs\\gs\\rs\\us" +
" !\"#$%&'()*+,-./0123456789:;<=>?" +
@ -204,25 +208,25 @@ public class HexPrinterTest {
.withBytesFormat("", 256)
.formatter(HexPrinter.Formatters.ASCII, "", 256);
String actual = p.toString(bytes);
Assert.assertEquals(actual, expected, "Formatters.ASCII mismatch");
assertEquals(expected, actual, "Formatters.ASCII mismatch");
}
@DataProvider(name = "PrimitiveFormatters")
Object[][] formatterParams() {
return new Object[][]{
{byte.class, ""},
{byte.class, "%02x: "},
{short.class, "%d "},
{int.class, "%08x, "},
{long.class, "%16x "},
{float.class, "%3.4f "},
{double.class, "%6.3g "},
{boolean.class, "%b "},
};
static Stream<Arguments> formatterParams() {
return Stream.of(
Arguments.of(byte.class, ""),
Arguments.of(byte.class, "%02x: "),
Arguments.of(short.class, "%d "),
Arguments.of(int.class, "%08x, "),
Arguments.of(long.class, "%16x "),
Arguments.of(float.class, "%3.4f "),
Arguments.of(double.class, "%6.3g "),
Arguments.of(boolean.class, "%b ")
);
}
@Test(dataProvider = "PrimitiveFormatters")
public void testFormatter(Class<?> primClass, String fmtString) {
@ParameterizedTest
@MethodSource("formatterParams")
void testFormatter(Class<?> primClass, String fmtString) {
HexPrinter.Formatter formatter = HexPrinter.Formatters.ofPrimitive(primClass, fmtString);
// Create a byte array with data for two lines
int colWidth = 8;
@ -235,22 +239,23 @@ public class HexPrinterTest {
formatter.annotate(in, sb);
Object n = readPrimitive(primClass, in2);
String expected = String.format(fmtString, n);
Assert.assertEquals(sb.toString(), expected, "mismatch");
assertEquals(expected, sb.toString(), "mismatch");
sb.setLength(0);
}
} catch (IOException ioe) {
// EOF is done
}
try {
Assert.assertEquals(in.available(), 0, "not all input consumed");
Assert.assertEquals(in2.available(), 0, "not all 2nd stream input consumed");
assertEquals(0, in.available(), "not all input consumed");
assertEquals(0, in2.available(), "not all 2nd stream input consumed");
} catch (IOException ioe) {
//
}
}
@Test(dataProvider = "PrimitiveFormatters")
static void testHexPrinterPrimFormatter(Class<?> primClass, String fmtString) {
@ParameterizedTest
@MethodSource("formatterParams")
void testHexPrinterPrimFormatter(Class<?> primClass, String fmtString) {
// Create a byte array with data for two lines
int colWidth = 8;
byte[] bytes = genData(colWidth);
@ -264,7 +269,7 @@ public class HexPrinterTest {
String expected = HexPrinter.simple()
.formatter(HexPrinter.Formatters.ofPrimitive(primClass, fmtString))
.toString(bytes);
Assert.assertEquals(actual, expected, "mismatch");
assertEquals(expected, actual, "mismatch");
}
private static Object readPrimitive(Class<?> primClass, DataInputStream in) throws IOException {
@ -289,22 +294,20 @@ public class HexPrinterTest {
}
}
@DataProvider(name = "sources")
Object[][] sources() {
return new Object[][]{
{genBytes(21), 0, -1},
{genBytes(21), 5, 12},
};
static Stream<Arguments> sources() {
return Stream.of(
Arguments.of(genBytes(21), 0, -1),
Arguments.of(genBytes(21), 5, 12)
);
}
@DataProvider(name = "badsources")
Object[][] badSources() {
return new Object[][]{
{genBytes(21), 5, 22},
};
static Stream<Arguments> badSources() {
return Stream.of(
Arguments.of(genBytes(21), 5, 22)
);
}
public static byte[] genData(int len) {
static byte[] genData(int len) {
// Create a byte array with data for two lines
byte[] bytes = new byte[len];
for (int i = 0; i < len / 2; i++) {
@ -314,7 +317,7 @@ public class HexPrinterTest {
return bytes;
}
public static byte[] genFloat(int len) {
static byte[] genFloat(int len) {
byte[] bytes = null;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos)) {
@ -327,7 +330,7 @@ public class HexPrinterTest {
return bytes;
}
public static byte[] genDouble(int len) {
static byte[] genDouble(int len) {
byte[] bytes = null;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos)) {
@ -340,33 +343,38 @@ public class HexPrinterTest {
return bytes;
}
public static byte[] genBytes(int len) {
static byte[] genBytes(int len) {
byte[] bytes = new byte[len];
for (int i = 0; i < len; i++)
bytes[i] = (byte) ('A' + i);
return bytes;
}
public ByteBuffer genByteBuffer(int len) {
ByteBuffer genByteBuffer(int len) {
return ByteBuffer.wrap(genBytes(len));
}
public InputStream genInputStream(int len) {
InputStream genInputStream(int len) {
return new ByteArrayInputStream(genBytes(len));
}
@Test
public void testNilPrinterBigBuffer() {
void testNilPrinterBigBuffer() {
byte[] bytes = new byte[1024];
HexPrinter p = HexPrinter.minimal();
String r = p.toString(bytes);
Assert.assertEquals(r.length(), bytes.length * 2, "encoded byte wrong size");
Assert.assertEquals(r.replace("00", "").length(), 0, "contents not all zeros");
assertEquals(bytes.length * 2, r.length(), "encoded byte wrong size");
assertEquals(0, r.replace("00", "").length(), "contents not all zeros");
}
@Test(dataProvider = "badsources",
expectedExceptions = java.lang.IndexOutOfBoundsException.class)
public void testBadToStringByteBuffer(byte[] bytes, int offset, int length) {
@ParameterizedTest
@MethodSource("badSources")
void testBadToStringByteBuffer(byte[] bytes, int offset, int length) {
assertThrows(java.lang.IndexOutOfBoundsException.class,
() -> testThrowsForBadToStringByteBuffer(bytes, offset, length));
}
void testThrowsForBadToStringByteBuffer(byte[] bytes, int offset, int length) {
if (length < 0)
length = bytes.length - offset;
ByteBuffer bb = ByteBuffer.wrap(bytes, 0, bytes.length);
@ -381,11 +389,12 @@ public class HexPrinterTest {
actual = HexPrinter.simple().toString(bb, offset, length);
System.out.println(actual);
String expected = HexPrinter.simple().toString(bytes, offset, length);
Assert.assertEquals(actual, expected, "mismatch in format()");
assertEquals(expected, actual, "mismatch in format()");
}
@Test(dataProvider = "sources")
public void testToStringByteBuffer(byte[] bytes, int offset, int length) {
@ParameterizedTest
@MethodSource("sources")
void testToStringByteBuffer(byte[] bytes, int offset, int length) {
if (length < 0)
length = bytes.length - offset;
ByteBuffer bb = ByteBuffer.wrap(bytes, 0, bytes.length);
@ -400,11 +409,12 @@ public class HexPrinterTest {
actual = HexPrinter.simple().toString(bb, offset, length);
System.out.println(actual);
String expected = HexPrinter.simple().toString(bytes, offset, length);
Assert.assertEquals(actual, expected, "mismatch in format()");
assertEquals(expected, actual, "mismatch in format()");
}
@Test(dataProvider = "sources")
public void testFormatBytes(byte[] bytes, int offset, int length) {
@ParameterizedTest
@MethodSource("sources")
void testFormatBytes(byte[] bytes, int offset, int length) {
int len = length >= 0 ? length : bytes.length;
System.out.printf("Source: %s, off: %d, len: %d%n",
"bytes", offset, len);
@ -416,11 +426,12 @@ public class HexPrinterTest {
String actual = sb.toString();
System.out.println(actual);
String expected = HexPrinter.simple().toString(bytes, offset, len);
Assert.assertEquals(actual, expected, "mismatch in format()");
assertEquals(expected, actual, "mismatch in format()");
}
@Test(dataProvider = "sources")
public void testFormatByteBuffer(byte[] bytes, int offset, int length) {
@ParameterizedTest
@MethodSource("sources")
void testFormatByteBuffer(byte[] bytes, int offset, int length) {
if (length < 0)
length = bytes.length - offset;
ByteBuffer bb = ByteBuffer.wrap(bytes, 0, bytes.length);
@ -436,11 +447,12 @@ public class HexPrinterTest {
String actual = sb.toString();
System.out.println(actual);
String expected = HexPrinter.simple().toString(bytes, offset, length);
Assert.assertEquals(actual, expected, "mismatch in format()");
assertEquals(expected, actual, "mismatch in format()");
}
@Test(dataProvider = "sources")
public void testFormatInputStream(byte[] bytes, int offset, int length) {
@ParameterizedTest
@MethodSource("sources")
void testFormatInputStream(byte[] bytes, int offset, int length) {
// Offset is ignored
InputStream is = new ByteArrayInputStream(bytes, 0, length);
StringBuilder sb = new StringBuilder();
@ -450,32 +462,37 @@ public class HexPrinterTest {
String actual = sb.toString();
System.out.println(actual);
String expected = HexPrinter.simple().toString(bytes, 0, length);
Assert.assertEquals(actual, expected, "mismatch in format()");
assertEquals(expected, actual, "mismatch in format()");
}
@Test(expectedExceptions = NullPointerException.class)
public void testNullByteArray() {
HexPrinter.simple().dest(System.out).format((byte[]) null);
@Test
void testNullByteArray() {
assertThrows(NullPointerException.class,
() -> HexPrinter.simple().dest(System.out).format((byte[]) null));
}
@Test(expectedExceptions = NullPointerException.class)
public void testNullByteArrayOff() {
HexPrinter.simple().dest(System.out).format((byte[]) null, 0, 1);
@Test
void testNullByteArrayOff() {
assertThrows(NullPointerException.class,
() -> HexPrinter.simple().dest(System.out).format((byte[]) null, 0, 1));
}
@Test(expectedExceptions = NullPointerException.class)
public void testNullByteBuffer() {
HexPrinter.simple().dest(System.out).format((ByteBuffer) null);
@Test
void testNullByteBuffer() {
assertThrows(NullPointerException.class,
() -> HexPrinter.simple().dest(System.out).format((ByteBuffer) null));
}
@Test(expectedExceptions = NullPointerException.class)
public void testNullByteBufferOff() {
HexPrinter.simple().dest(System.out).format((ByteBuffer) null, 0, 1);
@Test
void testNullByteBufferOff() {
assertThrows(NullPointerException.class,
() -> HexPrinter.simple().dest(System.out).format((ByteBuffer) null, 0, 1));
}
@Test(expectedExceptions = NullPointerException.class)
public void testNullInputStream() {
HexPrinter.simple().dest(System.out).format((InputStream) null);
@Test
void testNullInputStream() {
assertThrows(NullPointerException.class, () ->
HexPrinter.simple().dest(System.out).format((InputStream) null));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,8 +23,10 @@
package jdk.test.lib.hexdump;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.Arguments;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -47,54 +49,52 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import static org.testng.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
/*
* @test
* @summary Check ObjectStreamPrinter formatting
* @library /test/lib
* @run testng/othervm -DDEBUG=true jdk.test.lib.hexdump.ObjectStreamPrinterTest
* @run junit/othervm -DDEBUG=true jdk.test.lib.hexdump.ObjectStreamPrinterTest
*/
/**
* Test of the formatter is fairly coarse, formatting several
* sample classes and spot checking the result string for key strings.
*/
@Test
public class ObjectStreamPrinterTest {
class ObjectStreamPrinterTest {
// Override with (-DDEBUG=true) to see all the output
private static boolean DEBUG = Boolean.getBoolean("DEBUG");
@DataProvider(name = "serializables")
Object[][] serializables() {
return new Object[][]{
{new Object[]{"abc", "def"}, 0, 0, 2},
{new Object[]{0, 1}, 2, 2, 0},
{new Object[]{TimeUnit.DAYS, TimeUnit.SECONDS}, 2, 0, 2},
{new Object[]{List.of("one", "two", "three")}, 1, 1, 3},
{new Object[]{genList()}, 1, 1, 2},
{new Object[]{genMap()}, 1, 1, 5},
{new Object[]{genProxy()}, 5, 2, 9},
{new Object[]{new char[]{'x', 'y', 'z'},
static Stream<Arguments> serializables() {
return Stream.of(
Arguments.of(new Object[]{"abc", "def"}, 0, 0, 2),
Arguments.of(new Object[]{0, 1}, 2, 2, 0),
Arguments.of(new Object[]{TimeUnit.DAYS, TimeUnit.SECONDS}, 2, 0, 2),
Arguments.of(new Object[]{List.of("one", "two", "three")}, 1, 1, 3),
Arguments.of(new Object[]{genList()}, 1, 1, 2),
Arguments.of(new Object[]{genMap()}, 1, 1, 5),
Arguments.of(new Object[]{genProxy()}, 5, 2, 9),
Arguments.of(new Object[]{new char[]{'x', 'y', 'z'},
new byte[]{0x61, 0x62, 0x63}, new int[]{4, 5, 6},
new float[]{1.0f, 2.0f, 3.1415927f},
new boolean[]{true, false, true},
new Object[]{"first", 3, 3.14159f}}, 9, 2, 1},
{ new Object[] {new XYPair(3, 5)}, 1, 1, 0},
};
new Object[]{"first", 3, 3.14159f}}, 9, 2, 1),
Arguments.of(new Object[] {new XYPair(3, 5)}, 1, 1, 0)
);
}
@DataProvider(name = "SingleObjects")
Object[][] sources() {
return new Object[][]{
{"A Simple", new A(), 1, 1, 0},
{"BNoDefaultRO has no call to defaultReadObject", new BNoDefaultRO(), 2, 1, 1},
{"BDefaultRO has call to defaultReadObject", new BDefaultRO(), 2, 1, 1},
{"CNoDefaultRO extends BNoDefaultRO with no fields", new CNoDefaultRO(), 3, 1, 3},
{"CDefaultRO extends BDefaultRO with no fields", new CDefaultRO(), 3, 1, 3},
};
static Stream<Arguments> sources(){
return Stream.of(
Arguments.of("A Simple", new A(), 1, 1, 0),
Arguments.of("BNoDefaultRO has no call to defaultReadObject", new BNoDefaultRO(), 2, 1, 1),
Arguments.of("BDefaultRO has call to defaultReadObject", new BDefaultRO(), 2, 1, 1),
Arguments.of("CNoDefaultRO extends BNoDefaultRO with no fields", new CNoDefaultRO(), 3, 1, 3),
Arguments.of("CDefaultRO extends BDefaultRO with no fields", new CDefaultRO(), 3, 1, 3)
);
}
@ -109,8 +109,9 @@ public class ObjectStreamPrinterTest {
* @param strings the expected count of strings
* @throws IOException if any I/O exception occurs
*/
@Test(dataProvider = "serializables")
public void testFormat(Object[] objs, int descriptors, int objects, int strings) throws IOException {
@ParameterizedTest
@MethodSource("serializables")
void testFormat(Object[] objs, int descriptors, int objects, int strings) throws IOException {
byte[] bytes = serializeObjects(objs);
String result = HexPrinter.simple()
@ -133,8 +134,9 @@ public class ObjectStreamPrinterTest {
* @param strings the expected count of strings
* @throws IOException if any I/O exception occurs
*/
@Test(dataProvider = "serializables", enabled=true)
static void standAlonePrinter(Object[] objs, int descriptors, int objects, int strings) throws IOException{
@ParameterizedTest
@MethodSource("serializables")
void standAlonePrinter(Object[] objs, int descriptors, int objects, int strings) throws IOException{
byte[] bytes = serializeObjects(objs);
StringBuilder sb = new StringBuilder();
@ -170,8 +172,9 @@ public class ObjectStreamPrinterTest {
* @param strings the expected count of strings
* @throws IOException if any I/O exception occurs
*/
@Test(dataProvider = "SingleObjects")
static void singleObjects(String label, Object o, int descriptors, int objects, int strings) throws IOException {
@ParameterizedTest
@MethodSource("sources")
void singleObjects(String label, Object o, int descriptors, int objects, int strings) throws IOException {
if (DEBUG)
System.out.println("Case: " + label);
ByteArrayOutputStream boas = new ByteArrayOutputStream();
@ -198,7 +201,7 @@ public class ObjectStreamPrinterTest {
* @throws IOException if any I/O exception occurs
*/
@Test
static void longString() throws IOException {
void longString() throws IOException {
String large = " 123456789abcedf".repeat(0x1000);
ByteArrayOutputStream boas = new ByteArrayOutputStream();
@ -230,7 +233,7 @@ public class ObjectStreamPrinterTest {
* @throws IOException if an I/O exception occurs
*/
@Test
static void testMain() throws IOException {
void testMain() throws IOException {
Object[] objs = {genList()};
byte[] bytes = serializeObjects(objs); // A serialized List
Path p = Path.of("scratch.tmp");
@ -269,17 +272,17 @@ public class ObjectStreamPrinterTest {
for (int i = result.indexOf(key); i >= 0; i = result.indexOf(key, i + 1)) {
count++;
}
assertEquals(count, expectedCount, "Occurrences of " + key);
assertEquals(expectedCount, count, "Occurrences of " + key);
}
public static List<String> genList() {
static List<String> genList() {
List<String> l = new ArrayList<>();
l.add("abc");
l.add("def");
return l;
}
public static Map<String, String> genMap() {
static Map<String, String> genMap() {
Map<String, String> map = new HashMap<>();
map.put("1", "One");
map.put("2", "Two");
@ -287,7 +290,7 @@ public class ObjectStreamPrinterTest {
return map;
}
public static Object genProxy() {
static Object genProxy() {
InvocationHandler h = (InvocationHandler & Serializable) (Object proxy, Method method, Object[] args) -> null;
Class<?>[] intf = new Class<?>[]{Serializable.class, DataInput.class, DataOutput.class};
return Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), intf, h);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,9 +23,10 @@
package jdk.test.lib.hexdump;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.Arguments;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -50,23 +51,23 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import static org.testng.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
/*
* @test
* @summary Test StreamDump utility
* @library /test/lib
* @build jdk.test.lib.hexdump.StreamDump
* @run testng jdk.test.lib.hexdump.StreamDumpTest
* @run junit jdk.test.lib.hexdump.StreamDumpTest
*/
/**
* Test of the formatter is fairly coarse, formatting several
* sample classes and spot checking the result string for key strings.
*/
@Test
public class StreamDumpTest {
class StreamDumpTest {
private final static Path workDir = Path.of(".");
@ -99,24 +100,23 @@ public class StreamDumpTest {
* Arg list and the expected exit status, stdout line count, and stderr line count.
* @return array of argument list arrays.
*/
@DataProvider(name = "serializables")
Object[][] serializables() {
return new Object[][] {
{new String[]{testSRC + "/openssl.p12.pem"},
0, 126, 0},
{new String[]{"--formatter", "jdk.test.lib.hexdump.ASN1Formatter", testSRC + "/openssl.p12.pem"},
0, 126, 0},
{new String[]{serializedListPath},
0, 19, 0},
{new String[]{"--formatter", "jdk.test.lib.hexdump.ObjectStreamPrinter", serializedListPath},
0, 19, 0},
{new String[]{},
1, 2, 0}, // no file arguments
{new String[]{"--formatter"},
1, 2, 0}, // --formatter option requires a class name
{new String[]{"-formatter", "jdk.test.lib.hexdump.ObjectStreamPrinter"},
1, 2, 0}, // options start with double "--"
};
static Stream<Arguments> serializables() {
return Stream.of(
Arguments.of(new String[]{testSRC + "/openssl.p12.pem"},
0, 126, 0),
Arguments.of(new String[]{"--formatter", "jdk.test.lib.hexdump.ASN1Formatter", testSRC + "/openssl.p12.pem"},
0, 126, 0),
Arguments.of(new String[]{serializedListPath},
0, 19, 0),
Arguments.of(new String[]{"--formatter", "jdk.test.lib.hexdump.ObjectStreamPrinter", serializedListPath},
0, 19, 0),
Arguments.of(new String[]{},
1, 2, 0), // no file arguments
Arguments.of(new String[]{"--formatter"},
1, 2, 0), // --formatter option requires a class name
Arguments.of(new String[]{"-formatter", "jdk.test.lib.hexdump.ObjectStreamPrinter"},
1, 2, 0) // options start with double "--"
);
}
@ -126,8 +126,9 @@ public class StreamDumpTest {
* Each file should be formatted to stdout with no exceptions
* @throws IOException if an I/O exception occurs
*/
@Test(dataProvider="serializables")
static void testStreamDump(String[] args, int expectedStatus, int expectedStdout, int expectedStderr) throws IOException {
@ParameterizedTest
@MethodSource("serializables")
void testStreamDump(String[] args, int expectedStatus, int expectedStdout, int expectedStderr) throws IOException {
List<String> argList = new ArrayList<>();
argList.add(testJDK + "/bin/" + "java");
argList.add("-classpath");
@ -148,7 +149,7 @@ public class StreamDumpTest {
int actualStatus = p.waitFor();
fileCheck(stdoutPath, expectedStdout);
fileCheck(stderrPath, expectedStderr);
assertEquals(actualStatus, expectedStatus, "Unexpected exit status");
assertEquals(expectedStatus, actualStatus, "Unexpected exit status");
} catch (InterruptedException ie) {
ie.printStackTrace();
}
@ -168,7 +169,7 @@ public class StreamDumpTest {
Files.newBufferedReader(path).lines().forEach(s -> System.out.println(s));
System.out.println("----End----");
}
assertEquals(actualLines, expectedLines, "Unexpected line count");
assertEquals(expectedLines, actualLines, "Unexpected line count");
}
/**
@ -190,14 +191,14 @@ public class StreamDumpTest {
return bytes;
}
public static List<String> genList() {
static List<String> genList() {
List<String> l = new ArrayList<>();
l.add("abc");
l.add("def");
return l;
}
public static Map<String, String> genMap() {
static Map<String, String> genMap() {
Map<String, String> map = new HashMap<>();
map.put("1", "One");
map.put("2", "Two");