1 /*
   2  * Copyright (c) 2016, 2019, 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         lookup = fullPowerLookup.dropLookupMode(UNCONDITIONAL);
  70         assertTrue(lookup.lookupClass() == lc);
  71         assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE|PRIVATE));
  72     }
  73 
  74     /**
  75      * Starting with a full power Lookup, use dropLookupMode to create new Lookups
  76      * with reduced access.
  77      */
  78     public void testReducingAccess() {
  79         Lookup lookup = MethodHandles.lookup();
  80         final Class<?> lc = lookup.lookupClass();
  81         assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE|PROTECTED|PRIVATE));
  82 
  83         lookup = lookup.dropLookupMode(PROTECTED);
  84         assertTrue(lookup.lookupClass() == lc);
  85         assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE|PRIVATE));
  86 
  87         lookup = lookup.dropLookupMode(PRIVATE);
  88         assertTrue(lookup.lookupClass() == lc);
  89         assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE));
  90 
  91         lookup = lookup.dropLookupMode(PACKAGE);
  92         assertTrue(lookup.lookupClass() == lc);
  93         assertTrue(lookup.lookupModes() == (PUBLIC|MODULE));
  94 
  95         lookup = lookup.dropLookupMode(MODULE);
  96         assertTrue(lookup.lookupClass() == lc);
  97         assertTrue(lookup.lookupModes() == PUBLIC);
  98 
  99         lookup = lookup.dropLookupMode(PUBLIC);
 100         assertTrue(lookup.lookupClass() == lc);
 101         assertTrue(lookup.lookupModes() == 0);
 102 
 103         // repeat with lookup has no access
 104         lookup = lookup.dropLookupMode(PUBLIC);
 105         assertTrue(lookup.lookupClass() == lc);
 106         assertTrue(lookup.lookupModes() == 0);
 107     }
 108 
 109     @DataProvider(name = "unconditionals")
 110     public Object[][] unconditionals() {
 111         Lookup publicLookup = MethodHandles.publicLookup();
 112         return new Object[][] {
 113             { publicLookup, Object.class },
 114             { publicLookup.in(String.class), String.class },
 115             { publicLookup.in(DropLookupModeTest.class), DropLookupModeTest.class },
 116         };
 117     }
 118 
 119     /**
 120      * Test dropLookupMode on the lookup with public lookup
 121      * and UNCONDITIONAL
 122      */
 123     @Test(dataProvider = "unconditionals")
 124     public void testUnconditionalLookup(Lookup unconditionalLookup, Class<?> expected) {
 125         assertTrue(unconditionalLookup.lookupModes() == UNCONDITIONAL);
 126 
 127         assertPublicLookup(unconditionalLookup.dropLookupMode(PRIVATE), expected);
 128         assertPublicLookup(unconditionalLookup.dropLookupMode(PROTECTED), expected);
 129         assertPublicLookup(unconditionalLookup.dropLookupMode(PACKAGE), expected);
 130         assertPublicLookup(unconditionalLookup.dropLookupMode(MODULE), expected);
 131         assertPublicLookup(unconditionalLookup.dropLookupMode(PUBLIC), expected);
 132 
 133         // drop all access
 134         Lookup lookup = unconditionalLookup.dropLookupMode(UNCONDITIONAL);
 135         assertTrue(lookup.lookupClass() == expected);
 136         assertTrue(lookup.lookupModes() == 0);
 137     }
 138 
 139     private void assertPublicLookup(Lookup lookup, Class<?> expected) {
 140         assertTrue(lookup.lookupClass() == expected);
 141         assertTrue(lookup.lookupModes() == UNCONDITIONAL);
 142     }
 143 
 144     @DataProvider(name = "badInput")
 145     public Object[][] badInput() {
 146         return new Object[][] {
 147                 { 0,                        null },
 148                 { (PACKAGE|PRIVATE),        null },    // two modes
 149                 { Integer.MAX_VALUE,        null },
 150                 { Integer.MIN_VALUE,        null },
 151         };
 152     }
 153 
 154     /**
 155      * Check that IllegalArgumentException is thrown for bad input
 156      */
 157     @Test(dataProvider = "badInput", expectedExceptions = {IllegalArgumentException.class})
 158     public void testBadInput(Integer modeToDrop, Object ignore) {
 159         MethodHandles.lookup().dropLookupMode(modeToDrop);
 160     }
 161 
 162 }