martin | 20c7e44 | 2008-10-24 20:34:40 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6761678 |
| 27 | * @summary Check properties of Annotations returned from |
| 28 | * getParameterAnnotations, including freedom from security |
| 29 | * exceptions. |
| 30 | * @author Martin Buchholz |
| 31 | */ |
| 32 | |
| 33 | import java.lang.annotation.Annotation; |
| 34 | import java.lang.annotation.ElementType; |
| 35 | import java.lang.annotation.Retention; |
| 36 | import java.lang.annotation.RetentionPolicy; |
| 37 | import java.lang.annotation.Target; |
| 38 | import java.lang.reflect.Method; |
| 39 | import java.security.Permission; |
| 40 | import java.security.Policy; |
| 41 | import java.security.ProtectionDomain; |
| 42 | |
| 43 | @Retention(RetentionPolicy.RUNTIME) |
| 44 | @Target({ ElementType.FIELD, ElementType.PARAMETER }) |
| 45 | @interface Named { |
| 46 | String value(); |
| 47 | } |
| 48 | |
| 49 | public class ParameterAnnotations { |
| 50 | |
| 51 | // A security policy that differs from the default only in that it |
| 52 | // allows a security manager to be uninstalled. |
| 53 | static class MyPolicy extends Policy { |
| 54 | final Policy defaultPolicy; |
| 55 | MyPolicy(Policy defaultPolicy) { |
| 56 | this.defaultPolicy = defaultPolicy; |
| 57 | } |
| 58 | public boolean implies(ProtectionDomain pd, Permission p) { |
| 59 | return p.getName().equals("setSecurityManager") || |
| 60 | defaultPolicy.implies(pd, p); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public void nop(@Named("foo") Object foo, |
| 65 | @Named("bar") Object bar) { |
| 66 | } |
| 67 | |
| 68 | void test(String[] args) throws Throwable { |
| 69 | // Test without a security manager |
| 70 | test1(); |
| 71 | |
| 72 | // Test with a security manager |
| 73 | Policy defaultPolicy = Policy.getPolicy(); |
| 74 | Policy.setPolicy(new MyPolicy(defaultPolicy)); |
| 75 | System.setSecurityManager(new SecurityManager()); |
| 76 | try { |
| 77 | test1(); |
| 78 | } finally { |
| 79 | System.setSecurityManager(null); |
| 80 | Policy.setPolicy(defaultPolicy); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void test1() throws Throwable { |
| 85 | for (Method m : thisClass.getMethods()) { |
| 86 | if (m.getName().equals("nop")) { |
| 87 | Annotation[][] ann = m.getParameterAnnotations(); |
| 88 | equal(ann.length, 2); |
| 89 | Annotation foo = ann[0][0]; |
| 90 | Annotation bar = ann[1][0]; |
| 91 | equal(foo.toString(), "@Named(value=foo)"); |
| 92 | equal(bar.toString(), "@Named(value=bar)"); |
| 93 | check(foo.equals(foo)); |
| 94 | check(! foo.equals(bar)); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | //--------------------- Infrastructure --------------------------- |
| 100 | volatile int passed = 0, failed = 0; |
| 101 | void pass() {passed++;} |
| 102 | void fail() {failed++; Thread.dumpStack();} |
| 103 | void fail(String msg) {System.err.println(msg); fail();} |
| 104 | void unexpected(Throwable t) {failed++; t.printStackTrace();} |
| 105 | void check(boolean cond) {if (cond) pass(); else fail();} |
| 106 | void equal(Object x, Object y) { |
| 107 | if (x == null ? y == null : x.equals(y)) pass(); |
| 108 | else fail(x + " not equal to " + y);} |
| 109 | static Class<?> thisClass = new Object(){}.getClass().getEnclosingClass(); |
| 110 | public static void main(String[] args) throws Throwable { |
| 111 | try {thisClass.getMethod("instanceMain",String[].class) |
| 112 | .invoke(thisClass.newInstance(), (Object) args);} |
| 113 | catch (Throwable e) {throw e.getCause();}} |
| 114 | public void instanceMain(String[] args) throws Throwable { |
| 115 | try {test(args);} catch (Throwable t) {unexpected(t);} |
| 116 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 117 | if (failed > 0) throw new AssertionError("Some tests failed");} |
| 118 | } |