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         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     /**
 110      * Test dropLookupMode on the public Lookup.

 111      */
 112     public void testPublicLookup() {
 113         final Lookup publicLookup = MethodHandles.publicLookup();
 114         final Class<?> lc = publicLookup.lookupClass();
 115         assertTrue(publicLookup.lookupModes() == (PUBLIC|UNCONDITIONAL));
 116 
 117         Lookup lookup = publicLookup.dropLookupMode(PRIVATE);
 118         assertTrue(lookup.lookupClass() == lc);
 119         assertTrue(lookup.lookupModes() == PUBLIC);
 120 
 121         lookup = publicLookup.dropLookupMode(PROTECTED);
 122         assertTrue(lookup.lookupClass() == lc);
 123         assertTrue(lookup.lookupModes() == PUBLIC);
 124 
 125         lookup = publicLookup.dropLookupMode(PACKAGE);
 126         assertTrue(lookup.lookupClass() == lc);
 127         assertTrue(lookup.lookupModes() == PUBLIC);
 128 
 129         lookup = publicLookup.dropLookupMode(MODULE);
 130         assertTrue(lookup.lookupClass() == lc);
 131         assertTrue(lookup.lookupModes() == PUBLIC);
 132 
 133         lookup = publicLookup.dropLookupMode(PUBLIC);
 134         assertTrue(lookup.lookupClass() == lc);
 135         assertTrue(lookup.lookupModes() == 0);

 136 
 137         lookup = publicLookup.dropLookupMode(UNCONDITIONAL);
 138         assertTrue(lookup.lookupClass() == lc);
 139         assertTrue(lookup.lookupModes() == PUBLIC);
 140     }
 141 
 142     @DataProvider(name = "badInput")
 143     public Object[][] badInput() {
 144         return new Object[][] {
 145                 { 0,                        null },
 146                 { (PACKAGE|PRIVATE),        null },    // two modes
 147                 { Integer.MAX_VALUE,        null },
 148                 { Integer.MIN_VALUE,        null },
 149         };
 150     }
 151 
 152     /**
 153      * Check that IllegalArgumentException is thrown for bad input
 154      */
 155     @Test(dataProvider = "badInput", expectedExceptions = {IllegalArgumentException.class})
 156     public void testBadInput(Integer modeToDrop, Object ignore) {
 157         MethodHandles.lookup().dropLookupMode(modeToDrop);
 158     }
 159 
 160 }
--- EOF ---