1/* 2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 6549506 27 * @summary Specification of Permission.toString() method contradicts with 28 * JDK implementation 29 */ 30 31import java.security.*; 32 33public class ToString { 34 35 public static void main(String[]args) throws Exception { 36 DummyWritePermission dummyPerm = new DummyWritePermission(); 37 NullActionPermission nullActionPerm = new NullActionPermission(); 38 System.out.println(dummyPerm.toString()); 39 System.out.println(dummyPerm.getDescription()); 40 System.out.println(nullActionPerm.toString()); 41 System.out.println(nullActionPerm.getDescription()); 42 if (!dummyPerm.toString().equals(dummyPerm.getDescription())) { 43 throw new Exception("The expected permission.toString() is " + 44 dummyPerm.getDescription() + ", but " + 45 dummyPerm.toString() + " returned!"); 46 } 47 48 if (!nullActionPerm.toString().equals(nullActionPerm.getDescription())) { 49 throw new Exception("The expected permission.toString() is " + 50 nullActionPerm.getDescription() + ", but " + 51 nullActionPerm.toString() + " returned!"); 52 } 53 } 54 55 private static abstract class SimplePermission extends Permission { 56 public SimplePermission(String name) { 57 super(name); 58 } 59 60 public boolean implies(Permission permission) { 61 return false; 62 } 63 64 public boolean equals(Object obj) { 65 return false; 66 } 67 68 public int hashCode() { 69 return 13; 70 } 71 } 72 73 private static class DummyWritePermission extends SimplePermission { 74 public DummyWritePermission() { 75 super("permit to"); 76 } 77 78 public String getActions() { 79 return "write"; 80 } 81 82 public String getDescription() { 83 return "(\"ToString$DummyWritePermission\" \"permit to\" \"write\")"; 84 } 85 } 86 87 private static class NullActionPermission extends SimplePermission { 88 public NullActionPermission() { 89 super("permit to"); 90 } 91 92 public String getActions() { 93 return null; 94 } 95 96 public String getDescription() { 97 return "(\"ToString$NullActionPermission\" \"permit to\")"; 98 } 99 } 100 101} 102