Skip to content

Commit 902162c

Browse files
justin-curtis-luBrent Christian
authored and
Brent Christian
committed
8295239: Refactor java/util/Formatter/Basic script into a Java native test launcher
Reviewed-by: lancea, bchristi, naoto
1 parent f5dabf9 commit 902162c

File tree

3 files changed

+122
-79
lines changed

3 files changed

+122
-79
lines changed

test/jdk/java/util/Formatter/Basic.java

+22-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,18 +21,6 @@
2121
* questions.
2222
*/
2323

24-
/* @test
25-
* @summary Unit test for formatter
26-
* @bug 4906370 4962433 4973103 4989961 5005818 5031150 4970931 4989491 5002937
27-
* 5005104 5007745 5061412 5055180 5066788 5088703 6317248 6318369 6320122
28-
* 6344623 6369500 6534606 6282094 6286592 6476425 5063507 6469160 6476168
29-
* 8059175 8204229
30-
*
31-
* @run shell/timeout=240 Basic.sh
32-
*/
33-
34-
import static java.lang.System.out;
35-
3624
public class Basic {
3725

3826
private static int fail = 0;
@@ -45,18 +33,20 @@ static void pass() {
4533
}
4634

4735
static void fail(String fs, Class ex) {
48-
String s = "'" + fs + "': " + ex.getName() + " not thrown";
49-
if (first == null)
50-
setFirst(s);
51-
System.err.println("FAILED: " + s);
36+
String message = "'%s': %s not thrown".formatted(fs, ex.getName());
37+
if (first == null) {
38+
setFirst(message);
39+
}
40+
System.err.printf("FAILED: %s%n", message);
5241
fail++;
5342
}
5443

5544
static void fail(String fs, String exp, String got) {
56-
String s = "'" + fs + "': Expected '" + exp + "', got '" + got + "'";
57-
if (first == null)
58-
setFirst(s);
59-
System.err.println("FAILED: " + s);
45+
String message = "'%s': Expected '%s', got '%s'".formatted(fs, exp, got);
46+
if (first == null) {
47+
setFirst(message);
48+
}
49+
System.err.printf("FAILED: %s%n", message);
6050
fail++;
6151
}
6252

@@ -69,10 +59,11 @@ private static void setFirst(String s) {
6959
}
7060

7161
static void ck(String fs, String exp, String got) {
72-
if (!exp.equals(got))
62+
if (!exp.equals(got)) {
7363
fail(fs, exp, got);
74-
else
64+
} else {
7565
pass();
66+
}
7667
}
7768

7869
public static void main(String[] args) {
@@ -94,13 +85,15 @@ public static void main(String[] args) {
9485
BasicDouble.test();
9586
BasicDoubleObject.test();
9687
BasicBigDecimal.test();
97-
9888
BasicDateTime.test();
9989

100-
if (fail != 0)
101-
throw new RuntimeException((fail + pass) + " tests: "
102-
+ fail + " failure(s), first", first);
103-
else
104-
out.println("all " + (fail + pass) + " tests passed");
90+
if (fail != 0) {
91+
var tests_message = "%d tests: %d failure(s)%n".formatted(fail + pass, fail);
92+
var trace_message = "Traceback of the first error located";
93+
var message = "%s %s".formatted(tests_message, trace_message);
94+
throw new RuntimeException(message, first);
95+
} else {
96+
System.out.printf("All %d tests passed", pass);
97+
}
10598
}
10699
}

test/jdk/java/util/Formatter/Basic.sh

-50
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import jdk.test.lib.process.OutputAnalyzer;
25+
import jdk.test.lib.process.ProcessTools;
26+
import java.io.IOException;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.ValueSource;
29+
30+
/* @test
31+
* @summary Unit tests for formatter
32+
* @library /test/lib
33+
* @compile Basic.java
34+
* @compile BasicBoolean.java
35+
* @compile BasicBooleanObject.java
36+
* @compile BasicByte.java
37+
* @compile BasicByteObject.java
38+
* @compile BasicChar.java
39+
* @compile BasicCharObject.java
40+
* @compile BasicShort.java
41+
* @compile BasicShortObject.java
42+
* @compile BasicInt.java
43+
* @compile BasicIntObject.java
44+
* @compile BasicLong.java
45+
* @compile BasicLongObject.java
46+
* @compile BasicBigInteger.java
47+
* @compile BasicFloat.java
48+
* @compile BasicFloatObject.java
49+
* @compile BasicDouble.java
50+
* @compile BasicDoubleObject.java
51+
* @compile BasicBigDecimal.java
52+
* @compile BasicDateTime.java
53+
* @bug 4906370 4962433 4973103 4989961 5005818 5031150 4970931 4989491 5002937
54+
* 5005104 5007745 5061412 5055180 5066788 5088703 6317248 6318369 6320122
55+
* 6344623 6369500 6534606 6282094 6286592 6476425 5063507 6469160 6476168
56+
* 8059175 8204229
57+
*
58+
* @run junit BasicTestLauncher
59+
*/
60+
public class BasicTestLauncher {
61+
// Locale flag for testJVM
62+
private static final String JAVA_OPTS = "-Djava.locale.providers=CLDR";
63+
// Test class
64+
private static final String TEST_CLASS = "Basic";
65+
66+
/**
67+
* Executes Formatter Basic tests
68+
* @param timeZone the time zone to run tests against
69+
*/
70+
@ParameterizedTest
71+
@ValueSource(strings = { "US/Pacific", "Asia/Novosibirsk" })
72+
void testTimeZone(String timeZone) throws IOException{
73+
System.out.printf("$$$ Testing against %s!%n", timeZone);
74+
OutputAnalyzer output = RunTest(timeZone);
75+
CheckTest(output);
76+
System.out.printf("$$$ %s passed as expected!%n", timeZone);
77+
}
78+
79+
/**
80+
* Creates and runs the testJVM process using Basic class
81+
* @param timeZone the time zone to be set in the testJVM environment
82+
*/
83+
private static OutputAnalyzer RunTest(String timeZone) throws IOException{
84+
// Build and run Basic class with correct configuration
85+
ProcessBuilder pb = ProcessTools.createTestJvm(JAVA_OPTS, TEST_CLASS);
86+
pb.environment().put("TZ", timeZone);
87+
Process process = pb.start();
88+
return new OutputAnalyzer(process);
89+
}
90+
91+
/**
92+
* Validates if the testJVM process passed all tests
93+
* @param output is an Output Analyzer for the testJVM
94+
* @throws RuntimeException for all testJVM failures
95+
*/
96+
private static void CheckTest(OutputAnalyzer output){
97+
output.shouldHaveExitValue(0)
98+
.reportDiagnosticSummary();
99+
}
100+
}

0 commit comments

Comments
 (0)