1 /* 2 * Copyright (c) 2016, 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 * @run testng DropLookupModeTest 27 * @summary Basic unit tests Lookup::dropLookupMode 28 */ 29 30 import java.lang.invoke.MethodHandles; 31 import java.lang.invoke.MethodHandles.Lookup; 32 import static java.lang.invoke.MethodHandles.Lookup.*; 33 34 import org.testng.annotations.DataProvider; 35 import org.testng.annotations.Test; 36 import static org.testng.Assert.*; 37 38 @Test 39 public class DropLookupModeTest { 40 41 /** 42 * Basic test of dropLookupMode 43 */ 44 public void testBasic() { 45 final Lookup fullPowerLookup = MethodHandles.lookup(); 46 final Class<?> lc = fullPowerLookup.lookupClass(); 47 assertTrue(fullPowerLookup.lookupModes() == (PUBLIC|MODULE|PACKAGE|PROTECTED|PRIVATE)); 48 49 Lookup lookup = fullPowerLookup.dropLookupMode(PRIVATE); 50 assertTrue(lookup.lookupClass() == lc); 51 assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE)); 52 53 lookup = fullPowerLookup.dropLookupMode(PROTECTED); 54 assertTrue(lookup.lookupClass() == lc); 55 assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE|PRIVATE)); 56 57 lookup = fullPowerLookup.dropLookupMode(PACKAGE); 58 assertTrue(lookup.lookupClass() == lc); 59 assertTrue(lookup.lookupModes() == (PUBLIC|MODULE)); 60 61 lookup = fullPowerLookup.dropLookupMode(MODULE); 62 assertTrue(lookup.lookupClass() == lc); 63 assertTrue(lookup.lookupModes() == (PUBLIC)); 64 65 lookup = fullPowerLookup.dropLookupMode(PUBLIC); 66 assertTrue(lookup.lookupClass() == lc); 67 assertTrue(lookup.lookupModes() == 0); 68 } 69 70 /** 71 * Starting with a full power Lookup, use dropLookupMode to create new Lookups 72 * with reduced access. 73 */ 74 public void testReducingAccess() { 75 Lookup lookup = MethodHandles.lookup(); 76 final Class<?> lc = lookup.lookupClass(); 77 assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE|PROTECTED|PRIVATE)); 78 79 lookup = lookup.dropLookupMode(PROTECTED); 80 assertTrue(lookup.lookupClass() == lc); 81 assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE|PRIVATE)); 82 83 lookup = lookup.dropLookupMode(PRIVATE); 84 assertTrue(lookup.lookupClass() == lc); 85 assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE)); 86 87 lookup = lookup.dropLookupMode(PACKAGE); 88 assertTrue(lookup.lookupClass() == lc); 89 assertTrue(lookup.lookupModes() == (PUBLIC|MODULE)); 90 91 lookup = lookup.dropLookupMode(MODULE); 92 assertTrue(lookup.lookupClass() == lc); 93 assertTrue(lookup.lookupModes() == PUBLIC); 94 95 lookup = lookup.dropLookupMode(PUBLIC); 96 assertTrue(lookup.lookupClass() == lc); 97 assertTrue(lookup.lookupModes() == 0); 98 99 // repeat with lookup has no access 100 lookup = lookup.dropLookupMode(PUBLIC); 101 assertTrue(lookup.lookupClass() == lc); 102 assertTrue(lookup.lookupModes() == 0); 103 } 104 105 /** 106 * Test dropLookupMode on the public Lookup. 107 */ 108 public void testPublicLookup() { 109 final Lookup publicLookup = MethodHandles.publicLookup(); 110 final Class<?> lc = publicLookup.lookupClass(); 111 assertTrue(publicLookup.lookupModes() == PUBLIC); 112 113 Lookup lookup = publicLookup.dropLookupMode(PRIVATE); 114 assertTrue(lookup.lookupClass() == lc); 115 assertTrue(lookup.lookupModes() == PUBLIC); 116 117 lookup = publicLookup.dropLookupMode(PROTECTED); 118 assertTrue(lookup.lookupClass() == lc); 119 assertTrue(lookup.lookupModes() == PUBLIC); 120 121 lookup = publicLookup.dropLookupMode(PACKAGE); 122 assertTrue(lookup.lookupClass() == lc); 123 assertTrue(lookup.lookupModes() == PUBLIC); 124 125 lookup = publicLookup.dropLookupMode(MODULE); 126 assertTrue(lookup.lookupClass() == lc); 127 assertTrue(lookup.lookupModes() == PUBLIC); 128 129 lookup = publicLookup.dropLookupMode(PUBLIC); 130 assertTrue(lookup.lookupClass() == lc); 131 assertTrue(lookup.lookupModes() == 0); 132 } 133 134 @DataProvider(name = "badInput") 135 public Object[][] badInput() { 136 return new Object[][] { 137 { 0, null }, 138 { (PACKAGE|PRIVATE), null }, // two modes 139 { Integer.MAX_VALUE, null }, 140 { Integer.MIN_VALUE, null }, 141 }; 142 } 143 144 /** 145 * Check that IllegalArgumentException is thrown for bad input 146 */ 147 @Test(dataProvider = "badInput", expectedExceptions = {IllegalArgumentException.class}) 148 public void testBadInput(Integer modeToDrop, Object ignore) { 149 MethodHandles.lookup().dropLookupMode(modeToDrop); 150 } 151 152 }