8114860: Behavior of java.net.URLPermission.getActions() contradicts spec
authorvtewari
Tue, 21 Jun 2016 16:52:16 +0100
changeset 39132 e9499c06e138
parent 39131 0d36cc772b88
child 39133 b5641ce64cf7
8114860: Behavior of java.net.URLPermission.getActions() contradicts spec Reviewed-by: chegar, prappo
jdk/src/java.base/share/classes/java/net/URLPermission.java
jdk/test/java/net/URLPermission/URLPermissionTest.java
--- a/jdk/src/java.base/share/classes/java/net/URLPermission.java	Tue Jun 21 16:42:33 2016 +0100
+++ b/jdk/src/java.base/share/classes/java/net/URLPermission.java	Tue Jun 21 16:52:16 2016 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -455,15 +455,11 @@
     }
 
     private String actions() {
-        StringBuilder b = new StringBuilder();
-        for (String s : methods) {
-            b.append(s);
+        String b = String.join(",", methods);
+        if (!requestHeaders.isEmpty()) {
+            b += ":" + String.join(",", requestHeaders);
         }
-        b.append(":");
-        for (String s : requestHeaders) {
-            b.append(s);
-        }
-        return b.toString();
+        return b;
     }
 
     /**
--- a/jdk/test/java/net/URLPermission/URLPermissionTest.java	Tue Jun 21 16:42:33 2016 +0100
+++ b/jdk/test/java/net/URLPermission/URLPermissionTest.java	Tue Jun 21 16:52:16 2016 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,7 @@
 
 /**
  * @test
- * @bug 8010464 8027570 8027687 8029354
+ * @bug 8010464 8027570 8027687 8029354 8114860
  */
 
 public class URLPermissionTest {
@@ -129,6 +129,28 @@
         }
     }
 
+    static ActionsStringTest actionstest(String arg, String expectedActions) {
+        return new ActionsStringTest(arg, expectedActions);
+    }
+
+    static class ActionsStringTest extends Test {
+
+        String expectedActions;
+        String arg;
+
+        public ActionsStringTest(String arg, String expectedActions) {
+            this.arg = arg;
+            this.expectedActions = expectedActions;
+        }
+
+        @Override
+        boolean execute() {
+            String url = "http://www.foo.com/";
+            URLPermission urlp = new URLPermission(url, arg);
+            return (expectedActions.equals(urlp.getActions()));
+        }
+    }
+
     static ActionImpliesTest actest(String arg1, String arg2, boolean expected) {
         return new ActionImpliesTest(arg1, arg2, expected);
     }
@@ -308,6 +330,20 @@
         actest("*:*", "GET:x-bar,x-foo", true)
     };
 
+    static Test[] actionsStringTest = {
+        actionstest("", ""),
+        actionstest(":X-Bar", ":X-Bar"),
+        actionstest("GET", "GET"),
+        actionstest("get", "GET"),
+        actionstest("GET,POST", "GET,POST"),
+        actionstest("GET,post", "GET,POST"),
+        actionstest("get,post", "GET,POST"),
+        actionstest("get,post,DELETE", "DELETE,GET,POST"),
+        actionstest("GET,POST:", "GET,POST"),
+        actionstest("GET:X-Foo,X-bar", "GET:X-Bar,X-Foo"),
+        actionstest("GET,POST,DELETE:X-Bar,X-Foo,X-Bar,Y-Foo", "DELETE,GET,POST:X-Bar,X-Bar,X-Foo,Y-Foo")
+    };
+
     static Test[] equalityTests = {
         eqtest("http://www.foo.com", "http://www.FOO.CoM", true),
         eqtest("http://[fe80:0:0::]:1-2", "HTTP://[FE80::]:1-2", true),
@@ -449,6 +485,23 @@
             System.out.println ("action test " + i + " OK");
         }
 
+        for (int i = 0; i < actionsStringTest.length; i++) {
+            ActionsStringTest test = (ActionsStringTest) actionsStringTest[i];
+            Exception caught = null;
+            boolean result = false;
+            try {
+                result = test.execute();
+            } catch (Exception e) {
+                caught = e;
+            }
+            if (!result) {
+                failed = true;
+                System.out.println("test failed: " + test.arg + ": "
+                        + test.expectedActions + " Exception: " + caught);
+            }
+            System.out.println("Actions String test " + i + " OK");
+        }
+
         serializationTest("http://www.foo.com/-", "GET,DELETE:*");
         serializationTest("https://www.foo.com/-", "POST:X-Foo");
         serializationTest("https:*", "*:*");