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 * @library /lib/testlibrary 27 * @build LayerControllerTest ModuleUtils 28 * @run testng LayerControllerTest 29 * @summary Basic tests for java.lang.reflect.Layer.Controller 30 */ 31 32 import java.lang.module.Configuration; 33 import java.lang.module.ModuleDescriptor; 34 import java.lang.module.ModuleFinder; 35 import java.lang.reflect.Layer; 36 import java.lang.reflect.Module; 37 import java.util.List; 38 import java.util.Set; 39 40 import org.testng.annotations.Test; 41 import static org.testng.Assert.*; 42 43 @Test 44 public class LayerControllerTest { 45 46 /** 47 * Creates a Controller for a module layer containing modules m1 and m2. 48 * Module m1 contains p1, reads java.base, does not export/open any package 49 * Module m2 contains p2, reads java.base, does not export/open any package 50 */ 51 private Layer.Controller createTestLayer() { 52 ModuleDescriptor descriptor1 53 = ModuleDescriptor.newModule("m1") 54 .packages(Set.of("p1")) 55 .requires("java.base") 56 .build(); 57 58 ModuleDescriptor descriptor2 59 = ModuleDescriptor.newModule("m2") 60 .requires("java.base") 61 .packages(Set.of("p2")) 62 .build(); 63 64 ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2); 65 Layer bootLayer = Layer.boot(); 66 67 Configuration cf = bootLayer.configuration() 68 .resolve(finder, ModuleFinder.of(), Set.of("m1", "m2")); 69 70 ClassLoader scl = ClassLoader.getSystemClassLoader(); 71 72 Layer.Controller controller 73 = Layer.defineModulesWithOneLoader(cf, List.of(bootLayer), scl); 74 75 Layer layer = controller.layer(); 76 77 assertTrue(layer.modules().size() == 2); 78 assertTrue(layer.findModule("m1").isPresent()); 79 assertTrue(layer.findModule("m2").isPresent()); 80 81 return controller; 82 } 83 84 /** 85 * Basic test of Layer.Controller to update modules m1 and m2 to read and 86 * open packages to each other. 87 */ 88 public void testBasic() { 89 Layer.Controller controller = createTestLayer(); 90 Layer layer = controller.layer(); 91 Module m1 = layer.findModule("m1").orElseThrow(RuntimeException::new); 92 Module m2 = layer.findModule("m2").orElseThrow(RuntimeException::new); 93 94 assertFalse(m1.canRead(m2)); 95 assertFalse(m1.isExported("p1")); 96 assertFalse(m1.isOpen("p1")); 97 assertFalse(m1.isExported("p1", m2)); 98 assertFalse(m1.isOpen("p1", m2)); 99 100 assertFalse(m2.canRead(m1)); 101 assertFalse(m2.isExported("p2")); 102 assertFalse(m2.isOpen("p2")); 103 assertFalse(m2.isExported("p2", m1)); 104 assertFalse(m2.isOpen("p2", m1)); 105 106 // update m1 to read m2 107 assertTrue(controller.addReads(m1, m2) == controller); 108 assertTrue(m1.canRead(m2)); 109 assertFalse(m2.canRead(m1)); 110 111 // update m2 to read m1 112 assertTrue(controller.addReads(m2, m1) == controller); 113 assertTrue(m1.canRead(m2)); 114 assertTrue(m1.canRead(m1)); 115 116 // update m1 to open p1 to m2 117 assertTrue(controller.addOpens(m1, "p1", m2) == controller); 118 assertTrue(m1.isExported("p1", m2)); 119 assertTrue(m1.isOpen("p1", m2)); 120 assertFalse(m1.isExported("p1")); 121 assertFalse(m1.isOpen("p1")); 122 123 // update m2 to open p2 to m1 124 assertTrue(controller.addOpens(m2, "p2", m1) == controller); 125 assertTrue(m2.isExported("p2", m1)); 126 assertTrue(m2.isOpen("p2", m1)); 127 assertFalse(m2.isExported("p2")); 128 assertFalse(m2.isOpen("p2")); 129 } 130 131 /** 132 * Test invalid argument handling 133 */ 134 public void testBadArguments() { 135 Layer.Controller controller = createTestLayer(); 136 Layer layer = controller.layer(); 137 Module m1 = layer.findModule("m1").orElseThrow(RuntimeException::new); 138 Module m2 = layer.findModule("m2").orElseThrow(RuntimeException::new); 139 Module base = Object.class.getModule(); 140 141 // java.base is not in layer 142 try { 143 controller.addReads(base, m2); 144 assertTrue(false); 145 } catch (IllegalArgumentException expected) { } 146 147 // java.base is not in layer 148 try { 149 controller.addOpens(base, "java.lang", m2); 150 assertTrue(false); 151 } catch (IllegalArgumentException expected) { } 152 153 // m1 does not contain java.lang 154 try { 155 controller.addOpens(m1, "java.lang", m2); 156 assertTrue(false); 157 } catch (IllegalArgumentException expected) { } 158 } 159 160 /** 161 * Test null handling 162 */ 163 public void testNulls() { 164 Layer.Controller controller = createTestLayer(); 165 Layer layer = controller.layer(); 166 Module m1 = layer.findModule("m1").orElseThrow(RuntimeException::new); 167 Module m2 = layer.findModule("m2").orElseThrow(RuntimeException::new); 168 assertTrue(m1 != null); 169 assertTrue(m2 != null); 170 171 try { 172 controller.addReads(null, m2); 173 assertTrue(false); 174 } catch (NullPointerException expected) { } 175 176 try { 177 controller.addReads(m1, null); 178 assertTrue(false); 179 } catch (NullPointerException expected) { } 180 181 try { 182 controller.addOpens(null, "p1", m2); 183 assertTrue(false); 184 } catch (NullPointerException expected) { } 185 186 try { 187 controller.addOpens(m1, null, m2); 188 assertTrue(false); 189 } catch (NullPointerException expected) { } 190 191 try { 192 controller.addOpens(m1, "p1", null); 193 assertTrue(false); 194 } catch (NullPointerException expected) { } 195 } 196 }