Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import java.math.RoundingMode;
import java.text.ParsePosition;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class NumberFormatDataDrivenTest {

private static ULocale EN = new ULocale("en");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -65,17 +66,16 @@ public void TestGetResources() {
JarEntry je = jc.getJarEntry();
logln("jar entry: " + je.toString());
} else {
BufferedReader br =
new BufferedReader(new InputStreamReader(c.getInputStream()));
logln("input stream:");
try {
try (BufferedReader br =
new BufferedReader(
new InputStreamReader(
c.getInputStream(), StandardCharsets.UTF_8))) {
logln("input stream:");
String line = null;
int n = 0;
while ((line = br.readLine()) != null) {
logln(" " + ++n + ": " + line);
}
} finally {
br.close();
}
}
}
Expand Down
111 changes: 41 additions & 70 deletions icu4j/main/core/src/main/java/com/ibm/icu/impl/URLHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -33,63 +33,38 @@ public abstract class URLHandler {
private static final boolean DEBUG = ICUDebug.enabled("URLHandler");

static {
Map<String, Method> h = null;

BufferedReader br = null;
try {
@SuppressWarnings("resource") // Closed by BufferedReader.
ClassLoader loader = ClassLoaderUtil.getClassLoader(URLHandler.class);
InputStream is = loader.getResourceAsStream(PROPNAME);

if (is != null) {
Class<?>[] params = {URL.class};
br = new BufferedReader(new InputStreamReader(is));

for (String line = br.readLine(); line != null; line = br.readLine()) {
line = line.trim();

if (line.length() == 0 || line.charAt(0) == '#') {
continue;
}

int ix = line.indexOf('=');

if (ix == -1) {
if (DEBUG) System.err.println("bad urlhandler line: '" + line + "'");
break;
}

String key = line.substring(0, ix).trim();
String value = line.substring(ix + 1).trim();

try {
Class<?> cl = Class.forName(value);
Method m = cl.getDeclaredMethod("get", params);

if (h == null) {
h = new HashMap<String, Method>();
}

h.put(key, m);
} catch (ClassNotFoundException e) {
if (DEBUG) System.err.println(e);
} catch (NoSuchMethodException e) {
if (DEBUG) System.err.println(e);
} catch (SecurityException e) {
if (DEBUG) System.err.println(e);
}
}
br.close();
}
final Map<String, Method> h = new HashMap<>();

Class<?>[] params = {URL.class};
ClassLoader loader = ClassLoaderUtil.getClassLoader(URLHandler.class);
try (InputStream is = loader.getResourceAsStream(PROPNAME);
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr)) {
br.lines()
.map(String::trim)
.filter(line -> !line.isEmpty() && !line.startsWith("#"))
.forEach(
line -> {
int ix = line.indexOf('=');
if (ix == -1) {
if (DEBUG)
System.err.println("bad urlhandler line: '" + line + "'");
return;
}
String key = line.substring(0, ix).trim();
String value = line.substring(ix + 1).trim();
try {
Class<?> cl = Class.forName(value);
Method m = cl.getDeclaredMethod("get", params);
h.put(key, m);
} catch (ClassNotFoundException
| NoSuchMethodException
| SecurityException e) {
if (DEBUG) System.err.println(e);
}
});
} catch (Throwable t) {
if (DEBUG) System.err.println(t);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ignored) {
}
}
}

handlers = h;
Expand All @@ -102,23 +77,19 @@ public static URLHandler get(URL url) {

String protocol = url.getProtocol();

if (handlers != null) {
Method m = handlers.get(protocol);
Method m = handlers.get(protocol);

if (m != null) {
try {
URLHandler handler = (URLHandler) m.invoke(null, new Object[] {url});
if (m != null) {
try {
URLHandler handler = (URLHandler) m.invoke(null, new Object[] {url});

if (handler != null) {
return handler;
}
} catch (IllegalAccessException e) {
if (DEBUG) System.err.println(e);
} catch (IllegalArgumentException e) {
if (DEBUG) System.err.println(e);
} catch (InvocationTargetException e) {
if (DEBUG) System.err.println(e);
if (handler != null) {
return handler;
}
} catch (IllegalAccessException
| IllegalArgumentException
| InvocationTargetException e) {
if (DEBUG) System.err.println(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
* A reader for text resource data in the current package or the package of a given class object.
Expand Down Expand Up @@ -95,7 +96,7 @@ public ResourceReader(InputStream is, String resourceName, String encoding) {
try {
InputStreamReader isr =
(encoding == null)
? new InputStreamReader(is)
? new InputStreamReader(is, StandardCharsets.UTF_8)
: new InputStreamReader(is, encoding);

this.reader = new BufferedReader(isr);
Expand Down Expand Up @@ -233,7 +234,7 @@ private void _reset() throws UnsupportedEncodingException {

InputStreamReader isr =
(encoding == null)
? new InputStreamReader(is)
? new InputStreamReader(is, StandardCharsets.UTF_8)
: new InputStreamReader(is, encoding);
reader = new BufferedReader(isr);
lineNo = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -249,9 +250,9 @@ private void run(String fileName, RunMode runMode) {
} else if (!shouldFail && errorMessage != null) {
if (err != null) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8);
err.printStackTrace(ps);
String stackTrace = os.toString();
String stackTrace = os.toString(StandardCharsets.UTF_8);
showError(
errorMessage
+ " Stack trace: "
Expand All @@ -266,9 +267,9 @@ private void run(String fileName, RunMode runMode) {
}
} catch (Exception e) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8);
e.printStackTrace(ps);
String stackTrace = os.toString();
String stackTrace = os.toString(StandardCharsets.UTF_8);
showError(
"MAJOR ERROR: "
+ e.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,24 @@ private static Map<String, String> makeKnownIssuesList() {

static List<String> readTestCases() throws Exception {
List<String> tests = new ArrayList<>();
InputStream catalogFileStream =
TestUtil.class.getResourceAsStream(DATA_PATH + "catalog.txt");
LineNumberReader catalogFile =
new LineNumberReader(new InputStreamReader(catalogFileStream));
String filename = null;
while ((filename = catalogFile.readLine()) != null) {
if (filename.startsWith("#")) { // comment line, skip without logging
continue;
}
if (!filename.endsWith(".txt")) {
logln("Skipping " + filename + "...");
continue;
}
tests.add(filename);
try (InputStream catalogFileStream =
TestUtil.class.getResourceAsStream(DATA_PATH + "catalog.txt");
LineNumberReader catalogFile =
new LineNumberReader(
new InputStreamReader(catalogFileStream, StandardCharsets.UTF_8))) {
catalogFile
.lines()
.filter(
filename ->
!filename.startsWith("#")) // comment line, skip without logging
.forEach(
filename -> {
if (!filename.endsWith(".txt")) {
logln("Skipping " + filename + "...");
return;
}
tests.add(filename);
});
}
return tests;
}
Expand All @@ -72,41 +76,45 @@ static List<String> readTestCases() throws Exception {
@Parameters(method = "readTestCases")
public void TestPersonNames(String filename) throws IOException {
String knownIssue = KNOWN_ISSUES.get(filename);
LineNumberReader in =
try (LineNumberReader in =
new LineNumberReader(
new InputStreamReader(
TestUtil.class.getResourceAsStream(DATA_PATH + filename),
StandardCharsets.UTF_8));
String line = null;
PersonNameTester tester = new PersonNameTester(filename);

int errors = 0;
try {
while ((line = in.readLine()) != null) {
tester.processLine(line, filename, in.getLineNumber());
}
errors = tester.getErrorCount();
} catch (Exception e) {
if (knownIssue != null) {
logKnownIssue(knownIssue, "Exception thrown on " + filename + ": " + e.toString());
} else if (RUN_ALL_TESTS) {
logln("Exception thrown on " + filename + ": " + e.toString());
} else {
throw e;
StandardCharsets.UTF_8))) {
String line = null;
PersonNameTester tester = new PersonNameTester(filename);

int errors = 0;
try {
while ((line = in.readLine()) != null) {
tester.processLine(line, filename, in.getLineNumber());
}
errors = tester.getErrorCount();
} catch (Exception e) {
if (knownIssue != null) {
logKnownIssue(
knownIssue, "Exception thrown on " + filename + ": " + e.toString());
} else if (RUN_ALL_TESTS) {
logln("Exception thrown on " + filename + ": " + e.toString());
} else {
throw e;
}
}
}

if (errors != 0) {
if (knownIssue != null) {
logKnownIssue(
knownIssue, "Failure in " + filename + ": Found " + errors + " errors.");
} else if (RUN_ALL_TESTS) {
logln("Failure in " + filename + ": Found " + errors + " errors.");
} else {
errln("Failure in " + filename + ": Found " + errors + " errors.");
}
if (!isVerbose()) {
System.out.println("Note: Use verbose ( -DICU.logging=3 ) to get verbose output");
if (errors != 0) {
if (knownIssue != null) {
logKnownIssue(
knownIssue,
"Failure in " + filename + ": Found " + errors + " errors.");
} else if (RUN_ALL_TESTS) {
logln("Failure in " + filename + ": Found " + errors + " errors.");
} else {
errln("Failure in " + filename + ": Found " + errors + " errors.");
}
if (!isVerbose()) {
System.out.println(
"Note: Use verbose ( -DICU.logging=3 ) to get verbose output");
}
}
}
}
Expand Down
Loading