diff options
author | Neil Fuller <nfuller@google.com> | 2014-09-01 14:46:04 +0100 |
---|---|---|
committer | Neil Fuller <nfuller@google.com> | 2014-09-04 12:48:07 +0100 |
commit | c46b8c831e3c77c83aeaab54c83f914b6b471b0a (patch) | |
tree | a2cf3684e820f3c1f21d8f71956d0b64f6437d2c | |
parent | 4f10651fff7200d688e5dda1508448f8b34e4d9f (diff) | |
download | libcore-c46b8c831e3c77c83aeaab54c83f914b6b471b0a.zip libcore-c46b8c831e3c77c83aeaab54c83f914b6b471b0a.tar.gz libcore-c46b8c831e3c77c83aeaab54c83f914b6b471b0a.tar.bz2 |
Fix apache-harmony logging tests
Changed SocketHandler to re-introduce a "socket != 0"
check unintentionally removed by commit
232c91dce1760b180155256d1896d0d80375ff6d.
Some failures are the result of the test code being newer
than the libcore code. Android took its last libcore update
from apache-harmony some time after r820767 but before r929953
and its test-code update at r1097236. This explains a few
things.
Updated FileHandler to deal with null system properties.
This is an equivalent change to
http://svn.apache.org/viewvc?view=revision&revision=935099
Updated XMLFormatter to handle basic XML escaping. This is
an equivalent change to
http://svn.apache.org/viewvc?view=revision&revision=929953
Unsuppress tests that are fixed by this.
Bug: 13882147
Change-Id: I36321b488da6d4e08fbb0d28d0e366f9b06e6371
-rw-r--r-- | expectations/knownfailures.txt | 8 | ||||
-rw-r--r-- | luni/src/main/java/java/util/logging/FileHandler.java | 14 | ||||
-rw-r--r-- | luni/src/main/java/java/util/logging/SocketHandler.java | 6 | ||||
-rw-r--r-- | luni/src/main/java/java/util/logging/XMLFormatter.java | 66 |
4 files changed, 67 insertions, 27 deletions
diff --git a/expectations/knownfailures.txt b/expectations/knownfailures.txt index 9df8612..a2a518c 100644 --- a/expectations/knownfailures.txt +++ b/expectations/knownfailures.txt @@ -1418,14 +1418,6 @@ name: "com.android.org.apache.harmony.logging.tests.java.util.logging.LevelTest#testSerializationCompatibility" }, { - description: "java.util.logging: Tests that require java.util.logging code changes to fix.", - bug: 13882147, - names: [ - "com.android.org.apache.harmony.logging.tests.java.util.logging.SocketHandlerTest#testConstructor_NoProperties", - "com.android.org.apache.harmony.logging.tests.java.util.logging.XMLFormatterTest#test_TestFileHandlerClass_constructor" - ] -}, -{ description: "Known failure in GregorianCalendarTest", bug: 12778197, name: "org.apache.harmony.tests.java.util.GregorianCalendarTest#test_computeTime" diff --git a/luni/src/main/java/java/util/logging/FileHandler.java b/luni/src/main/java/java/util/logging/FileHandler.java index 6ffef87..1fd1dbf 100644 --- a/luni/src/main/java/java/util/logging/FileHandler.java +++ b/luni/src/main/java/java/util/logging/FileHandler.java @@ -261,15 +261,15 @@ public class FileHandler extends StreamHandler { boolean hasUniqueID = false; boolean hasGeneration = false; - // TODO privilege code? + String homePath = System.getProperty("user.home"); + if (homePath == null) { + throw new NullPointerException("System property \"user.home\" is null"); + } + boolean homePathHasSepEnd = homePath.endsWith(File.separator); String tempPath = System.getProperty("java.io.tmpdir"); - boolean tempPathHasSepEnd = (tempPath == null ? false : tempPath - .endsWith(File.separator)); - - String homePath = System.getProperty("user.home"); - boolean homePathHasSepEnd = (homePath == null ? false : homePath - .endsWith(File.separator)); + tempPath = tempPath == null ? homePath : tempPath; + boolean tempPathHasSepEnd = tempPath.endsWith(File.separator); StringBuilder sb = new StringBuilder(); pattern = pattern.replace('/', File.separatorChar); diff --git a/luni/src/main/java/java/util/logging/SocketHandler.java b/luni/src/main/java/java/util/logging/SocketHandler.java index 48bfc0e..c6d4be0 100644 --- a/luni/src/main/java/java/util/logging/SocketHandler.java +++ b/luni/src/main/java/java/util/logging/SocketHandler.java @@ -106,9 +106,13 @@ public class SocketHandler extends StreamHandler { throw new IllegalArgumentException("host == null || host.isEmpty()"); } // check the validity of the port number - int p = 0; + int p; try { p = Integer.parsePositiveInt(port); + // Must be >= 0 to get this far. 0 is invalid too. + if (p == 0) { + throw new IllegalArgumentException("Illegal port argument " + port); + } } catch (NumberFormatException e) { throw new IllegalArgumentException("Illegal port argument " + port); } diff --git a/luni/src/main/java/java/util/logging/XMLFormatter.java b/luni/src/main/java/java/util/logging/XMLFormatter.java index 0d80b3e..3952596 100644 --- a/luni/src/main/java/java/util/logging/XMLFormatter.java +++ b/luni/src/main/java/java/util/logging/XMLFormatter.java @@ -17,6 +17,7 @@ package java.util.logging; +import java.io.IOException; import java.text.MessageFormat; import java.util.Date; import java.util.ResourceBundle; @@ -50,7 +51,7 @@ public class XMLFormatter extends Formatter { // call a method of LogRecord to ensure not null long time = r.getMillis(); // format to date - String date = MessageFormat.format("{0, date} {0, time}", new Object[] { new Date(time) }); + String date = MessageFormat.format("{0, date} {0, time}", new Date(time)); String nl = System.lineSeparator(); StringBuilder sb = new StringBuilder(); @@ -59,21 +60,21 @@ public class XMLFormatter extends Formatter { append(sb, 1, "millis", time); append(sb, 1, "sequence", r.getSequenceNumber()); if (r.getLoggerName() != null) { - append(sb, 1, "logger", r.getLoggerName()); + escapeAndAppend(sb, 1, "logger", r.getLoggerName()); } append(sb, 1, "level", r.getLevel().getName()); if (r.getSourceClassName() != null) { append(sb, 1, "class", r.getSourceClassName()); } if (r.getSourceMethodName() != null) { - append(sb, 1, "method", r.getSourceMethodName()); + escapeAndAppend(sb, 1, "method", r.getSourceMethodName()); } append(sb, 1, "thread", r.getThreadID()); formatMessages(r, sb); Object[] params = r.getParameters(); if (params != null) { for (Object element : params) { - append(sb, 1, "param", element); + escapeAndAppend(sb, 1, "param", element); } } formatThrowable(r, sb); @@ -96,14 +97,14 @@ public class XMLFormatter extends Formatter { if (message == null) { message = pattern; - append(sb, 1, "message", message); + escapeAndAppend(sb, 1, "message", message); } else { - append(sb, 1, "message", message); - append(sb, 1, "key", pattern); - append(sb, 1, "catalog", r.getResourceBundleName()); + escapeAndAppend(sb, 1, "message", message); + escapeAndAppend(sb, 1, "key", pattern); + escapeAndAppend(sb, 1, "catalog", r.getResourceBundleName()); } } else if (pattern != null) { - append(sb, 1, "message", pattern); + escapeAndAppend(sb, 1, "message", pattern); } else { sb.append(indent).append("<message/>"); } @@ -114,13 +115,13 @@ public class XMLFormatter extends Formatter { if ((t = r.getThrown()) != null) { String nl = System.lineSeparator(); sb.append(indent).append("<exception>").append(nl); - append(sb, 2, "message", t.toString()); + escapeAndAppend(sb, 2, "message", t.toString()); // format throwable's stack trace StackTraceElement[] elements = t.getStackTrace(); for (StackTraceElement e : elements) { sb.append(indent).append(indent).append("<frame>").append(nl); append(sb, 3, "class", e.getClassName()); - append(sb, 3, "method", e.getMethodName()); + escapeAndAppend(sb, 3, "method", e.getMethodName()); append(sb, 3, "line", e.getLineNumber()); sb.append(indent).append(indent).append("</frame>").append(nl); } @@ -138,6 +139,49 @@ public class XMLFormatter extends Formatter { sb.append(System.lineSeparator()); } + private static void escapeAndAppend(StringBuilder sb, int indentCount, String tag, Object value) { + if (value == null) { + append(sb, indentCount, tag, value); + } else { + for (int i = 0; i < indentCount; ++i) { + sb.append(indent); + } + sb.append("<").append(tag).append(">"); + try { + escapeXml(sb, value.toString()); + } catch (IOException e) { + throw new AssertionError(); + } + sb.append("</").append(tag).append(">"); + sb.append(System.lineSeparator()); + } + } + + private static void escapeXml(Appendable valueBuilder, String value) throws IOException { + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + switch (c) { + case '\"': + valueBuilder.append("""); + break; + case '>': + valueBuilder.append(">"); + break; + case '<': + valueBuilder.append("<"); + break; + case '&': + valueBuilder.append("&"); + break; + case '\'': + valueBuilder.append("'"); + break; + default: + valueBuilder.append(c); + } + } + } + /** * Returns the header string for a set of log records formatted as XML * strings, using the output handler's encoding if it is defined, otherwise |