8159039: sun/net/httpclient/hpack/HeaderTableTest.java fails on some locales
Reviewed-by: chegar
--- a/jdk/src/java.httpclient/share/classes/sun/net/httpclient/hpack/HeaderTable.java Mon Jun 13 14:02:04 2016 +0200
+++ b/jdk/src/java.httpclient/share/classes/sun/net/httpclient/hpack/HeaderTable.java Mon Jun 13 15:48:17 2016 +0100
@@ -310,14 +310,14 @@
@Override
public int hashCode() {
- return 31 * (name.hashCode()) + value.hashCode();
+ return 31 * name.hashCode() + value.hashCode();
}
}
//
- // In order to be able to find an index of an entry with the given contents
- // in the dynamic table an effective inverse mapping is needed. Here's a
- // simple idea behind such a mapping.
+ // To quickly find an index of an entry in the dynamic table with the given
+ // contents an effective inverse mapping is needed. Here's a simple idea
+ // behind such a mapping.
//
// # The problem:
//
@@ -325,21 +325,21 @@
//
// get: index -> x
//
- // What we also want is an O(1) reverse lookup:
+ // What we want is an O(1) reverse lookup:
//
// indexOf: x -> index
//
// # Solution:
//
- // Let's store an inverse mapping as a Map<X, Integer>. This have a problem
- // that when a new element is added to the queue all indexes in the map
- // becomes invalid. Namely, each i becomes shifted by 1 to the right:
+ // Let's store an inverse mapping in a Map<x, Integer>. This have a problem
+ // that when a new element is added to the queue, all indexes in the map
+ // become invalid. Namely, the new element is assigned with an index of 1,
+ // and each index i, i > 1 becomes shifted by 1 to the left:
//
- // i -> i + 1
+ // 1, 1, 2, 3, ... , n-1, n
//
- // And the new element is assigned with an index of 1. This would seem to
- // require a pass through the map incrementing all indexes (map values) by
- // 1, which is O(n).
+ // Re-establishing the invariant would seem to require a pass through the
+ // map incrementing all indexes (map values) by 1, which is O(n).
//
// The good news is we can do much better then this!
//
@@ -373,7 +373,7 @@
//
// Where 'recalibrate()' goes through the table doing this:
//
- // value -= counter
+ // value -= counter
//
// That's given, of course, the size of the table itself is less than
// Long.MAX_VALUE :-)
--- a/jdk/test/java/net/httpclient/http2/java.httpclient/sun/net/httpclient/hpack/HeaderTableTest.java Mon Jun 13 14:02:04 2016 +0200
+++ b/jdk/test/java/net/httpclient/http2/java.httpclient/sun/net/httpclient/hpack/HeaderTableTest.java Mon Jun 13 15:48:17 2016 +0100
@@ -28,6 +28,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
+import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
@@ -35,7 +36,10 @@
import static java.lang.String.format;
import static org.testng.Assert.assertEquals;
-import static sun.net.httpclient.hpack.TestHelper.*;
+import static sun.net.httpclient.hpack.TestHelper.assertExceptionMessageContains;
+import static sun.net.httpclient.hpack.TestHelper.assertThrows;
+import static sun.net.httpclient.hpack.TestHelper.assertVoidThrows;
+import static sun.net.httpclient.hpack.TestHelper.newRandom;
public class HeaderTableTest {
@@ -317,10 +321,24 @@
@Test
public void testToString() {
+ testToString0();
+ }
+
+ @Test
+ public void testToStringDifferentLocale() {
+ Locale.setDefault(Locale.FRENCH);
+ String s = format("%.1f", 3.1);
+ assertEquals("3,1", s); // assumption of the test, otherwise the test is useless
+ testToString0();
+ }
+
+ private void testToString0() {
HeaderTable table = new HeaderTable(0);
{
table.setMaxSize(2048);
- assertEquals("entries: 0; used 0/2048 (0.0%)", table.toString());
+ String expected =
+ format("entries: %d; used %s/%s (%.1f%%)", 0, 0, 2048, 0.0);
+ assertEquals(expected, table.toString());
}
{
@@ -335,7 +353,8 @@
int used = name.length() + value.length() + 32;
double ratio = used * 100.0 / size;
- String expected = format("entries: 1; used %s/%s (%.1f%%)", used, size, ratio);
+ String expected =
+ format("entries: 1; used %s/%s (%.1f%%)", used, size, ratio);
assertEquals(expected, s);
}
@@ -344,7 +363,9 @@
table.put(":method", "");
table.put(":status", "");
String s = table.toString();
- assertEquals("entries: 2; used 78/78 (100.0%)", s);
+ String expected =
+ format("entries: %d; used %s/%s (%.1f%%)", 2, 78, 78, 100.0);
+ assertEquals(expected, s);
}
}