Skip to content

Commit

Permalink
8313612: Use JUnit in lib-test/jdk tests
Browse files Browse the repository at this point in the history
Reviewed-by: cstein, asotona
  • Loading branch information
Qing Xiao authored and sormuras committed Sep 21, 2023
1 parent bee7524 commit a35e96a
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 256 deletions.
74 changes: 36 additions & 38 deletions test/lib-test/jdk/test/lib/format/ArrayDiffTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,30 +23,28 @@

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'};

assertTrue(ArrayDiff.of(first, second).areEqual());
}

@Test
public void testOutputFitsWidth() {
void testOutputFitsWidth() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
Expand All @@ -62,7 +60,7 @@ public void testOutputFitsWidth() {
}

@Test
public void testIntegers() {
void testIntegers() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
Expand All @@ -78,7 +76,7 @@ public void testIntegers() {
}

@Test
public void testLongs() {
void testLongs() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
Expand All @@ -94,7 +92,7 @@ public void testLongs() {
}

@Test
public void testFirstElementIsWrong() {
void testFirstElementIsWrong() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
Expand All @@ -110,7 +108,7 @@ public void testFirstElementIsWrong() {
}

@Test
public void testOneElementIsEmpty() {
void testOneElementIsEmpty() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
Expand All @@ -126,7 +124,7 @@ public void testOneElementIsEmpty() {
}

@Test
public void testOutputDoesntFitWidth() {
void testOutputDoesntFitWidth() {
new AssertBuilder()
.withParams(20, Integer.MAX_VALUE)
.withArrays(
Expand All @@ -142,7 +140,7 @@ public void testOutputDoesntFitWidth() {
}

@Test
public void testVariableElementWidthOutputDoesntFitWidth() {
void testVariableElementWidthOutputDoesntFitWidth() {
new AssertBuilder()
.withParams(20, Integer.MAX_VALUE)
.withArrays(
Expand All @@ -158,7 +156,7 @@ public void testVariableElementWidthOutputDoesntFitWidth() {
}

@Test
public void testContextBefore() {
void testContextBefore() {
new AssertBuilder()
.withParams(20, 2)
.withArrays(
Expand All @@ -174,7 +172,7 @@ public void testContextBefore() {
}

@Test
public void testBoundedBytesWithDifferentWidth() {
void testBoundedBytesWithDifferentWidth() {
new AssertBuilder()
.withParams(24, 2)
.withArrays(
Expand All @@ -190,7 +188,7 @@ public void testBoundedBytesWithDifferentWidth() {
}

@Test
public void testBoundedFirstElementIsWrong() {
void testBoundedFirstElementIsWrong() {
new AssertBuilder()
.withParams(25, 2)
.withArrays(
Expand All @@ -206,7 +204,7 @@ public void testBoundedFirstElementIsWrong() {
}

@Test
public void testBoundedOneArchiveIsEmpty() {
void testBoundedOneArchiveIsEmpty() {
new AssertBuilder()
.withParams(10, 2)
.withArrays(
Expand All @@ -222,7 +220,7 @@ public void testBoundedOneArchiveIsEmpty() {
}

@Test
public void testUnboundedOneArchiveIsEmpty() {
void testUnboundedOneArchiveIsEmpty() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
Expand All @@ -238,7 +236,7 @@ public void testUnboundedOneArchiveIsEmpty() {
}

@Test
public void testUnprintableCharFormatting() {
void testUnprintableCharFormatting() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
Expand All @@ -254,7 +252,7 @@ public void testUnprintableCharFormatting() {
}

@Test
public void testStringElements() {
void testStringElements() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
Expand All @@ -270,7 +268,7 @@ public void testStringElements() {
}

@Test
public void testToStringableObjects() {
void testToStringableObjects() {
class StrObj {
private final String value;
public boolean equals(Object another) { return ((StrObj)another).value.equals(value); }
Expand All @@ -294,7 +292,7 @@ class StrObj {
}

@Test
public void testNullElements() {
void testNullElements() {
new AssertBuilder()
.withDefaultParams()
.withArrays(
Expand All @@ -309,14 +307,14 @@ public void testNullElements() {
.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 {
Expand All @@ -331,30 +329,30 @@ class AssertBuilder {
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;
Expand All @@ -363,7 +361,7 @@ public AssertBuilder thatFormattedValuesAre(
return this;
}

public void assertTwoWay() {
void assertTwoWay() {
ArrayDiff<?> diff;

// Direct
Expand All @@ -382,7 +380,7 @@ public void assertTwoWay() {
expectedIndex, firstFormattedArray, secondFormattedArray, failureMark);

assertFalse(diff.areEqual());
assertEquals(diff.format(), expected);
assertEquals(expected, diff.format());
}

// Reversed
Expand All @@ -401,7 +399,7 @@ public void assertTwoWay() {
expectedIndex, secondFormattedArray, firstFormattedArray, failureMark);

assertFalse(diff.areEqual());
assertEquals(diff.format(), expected);
assertEquals(expected, diff.format());
}
}

Expand Down
52 changes: 25 additions & 27 deletions test/lib-test/jdk/test/lib/hexdump/ASN1FormatterTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -34,22 +33,21 @@
import java.nio.file.Path;
import java.util.Base64;

import static org.testng.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

/*
* @test
* @summary ASN.1 formatting
* @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);
Expand All @@ -64,18 +62,18 @@ static void testPEM() throws IOException {
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);
Expand All @@ -92,34 +90,34 @@ static void dumpPEM() throws IOException {
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() };
Expand Down
Loading

0 comments on commit a35e96a

Please sign in to comment.