Skip to content

Commit 538013a

Browse files
committed
8251126: nsk.share.GoldChecker should read golden file from ${test.src}
Backport-of: b37b1a3
1 parent fac0f5a commit 538013a

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed
Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2020, 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
@@ -20,52 +20,39 @@
2020
* or visit www.oracle.com if you need additional information or have any
2121
* questions.
2222
*/
23+
2324
package nsk.share;
2425

25-
import java.io.*;
26+
import jdk.test.lib.Utils;
27+
28+
import java.io.IOException;
29+
import java.nio.charset.StandardCharsets;
30+
import java.nio.file.Files;
31+
import java.nio.file.Path;
2632

27-
public class GoldChecker extends AbstractGoldChecker
28-
{
33+
public class GoldChecker extends AbstractGoldChecker {
2934
private final String goldOutput;
3035

31-
public GoldChecker(String main_class_name) {
32-
goldOutput = readGoldStr(main_class_name + ".gold");
36+
public GoldChecker(String mainClassName) {
37+
goldOutput = readGoldStr(Path.of(Utils.TEST_SRC, mainClassName + ".gold"));
3338
}
3439

3540
@Override
3641
protected String getGoldenString() {
3742
return goldOutput;
3843
}
3944

40-
private String readGoldStr(String gold_file_name) {
41-
RandomAccessFile f;
42-
43-
try {
44-
f = new RandomAccessFile(gold_file_name, "r");
45-
} catch (FileNotFoundException e) {
46-
throw new TestBug("Unable to open golden file '" + gold_file_name + "' for reading");
45+
private String readGoldStr(Path goldenFile) {
46+
if (Files.notExists(goldenFile)) {
47+
throw new TestBug("Unable to open golden file '" + goldenFile + "' for reading");
4748
}
48-
4949
byte[] data;
50-
5150
try {
52-
int len = (int)f.length();
53-
data = new byte[len];
54-
f.read(data);
51+
data = Files.readAllBytes(goldenFile);
5552
} catch (IOException e) {
56-
throw new TestBug("Error reading from golden file'" + gold_file_name + "'");
57-
}
58-
59-
try {
60-
f.close();
61-
} catch (IOException e) {
62-
}
63-
64-
try {
65-
return new String(data, "US-ASCII");
66-
} catch (UnsupportedEncodingException e) {
67-
throw new TestFailure( e );
53+
throw new TestBug("Error reading from golden file '" + goldenFile + "'", e);
6854
}
55+
return new String(data, StandardCharsets.US_ASCII);
6956
}
7057

7158
}

0 commit comments

Comments
 (0)