1 /* 2 * Copyright (c) 2015, 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 * Asm plugin testing. 26 * @test 27 * @summary Test basic functionality. 28 * @author Jean-Francois Denise 29 * @modules java.base/jdk.internal.org.objectweb.asm 30 * jdk.jlink/jdk.tools.jlink.internal 31 * jdk.jlink/jdk.tools.jlink.internal.plugins.asm 32 * @build AsmPluginTestBase 33 * @run main BasicTest 34 */ 35 36 import java.io.IOException; 37 import java.io.UncheckedIOException; 38 import java.util.ArrayList; 39 import java.util.HashSet; 40 import java.util.List; 41 import java.util.Set; 42 import java.util.logging.Level; 43 import java.util.logging.Logger; 44 45 import jdk.internal.org.objectweb.asm.ClassReader; 46 import jdk.tools.jlink.internal.plugins.asm.AsmModulePool; 47 import jdk.tools.jlink.internal.plugins.asm.AsmPool; 48 import jdk.tools.jlink.plugin.ModuleEntry; 49 import jdk.tools.jlink.plugin.ModulePool; 50 51 public class BasicTest extends AsmPluginTestBase { 52 53 public static void main(String[] args) throws Exception { 54 if (!isImageBuild()) { 55 System.err.println("Test not run. Not image build."); 56 return; 57 } 58 new BasicTest().test(); 59 } 60 61 @Override 62 public void test() throws Exception { 63 BasicPlugin basicPlugin = new BasicPlugin(getClasses()); 64 ModulePool res = basicPlugin.visit(getPool()); 65 basicPlugin.test(getPool(), res); 66 } 67 68 private class BasicPlugin extends TestPlugin { 69 70 private final List<String> classes; 71 72 public BasicPlugin(List<String> classes) { 73 this.classes = classes; 74 } 75 76 @Override 77 public void visit() { 78 for (String m : MODULES.keySet()) { 79 AsmModulePool pool = getPools().getModulePool(m); 80 if (pool == null) { 81 throw new AssertionError(m + " pool not found"); 82 } 83 if(!pool.getModuleName().equals(m)) { 84 throw new AssertionError("Invalid module name " + 85 pool.getModuleName() + " should be "+ m); 86 } 87 if (pool.getClasses().size() == 0 && !m.equals(TEST_MODULE)) { 88 throw new AssertionError("Empty pool " + m); 89 } 90 pool.addPackage("toto"); 91 if (!pool.getTransformedClasses().getClasses().isEmpty()) { 92 throw new AssertionError("Should be empty"); 93 } 94 for(String res : MODULES.get(m)) { 95 AsmPool.ResourceFile resFile = pool.getResourceFile(res); 96 if(resFile == null) { 97 throw new AssertionError("No resource file for " + res); 98 } 99 } 100 } 101 try { 102 testPools(); 103 testVisitor(); 104 } catch (IOException ex) { 105 throw new UncheckedIOException(ex); 106 } 107 } 108 109 @Override 110 public void test(ModulePool inResources, ModulePool outResources) throws Exception { 111 if (!isVisitCalled()) { 112 throw new AssertionError("Resources not visited"); 113 } 114 if (inResources.getEntryCount() != outResources.getEntryCount()) { 115 throw new AssertionError("Input size " + inResources.getEntryCount() + 116 " != to " + outResources.getEntryCount()); 117 } 118 } 119 120 private void testVisitor() throws IOException { 121 List<String> seen = new ArrayList<>(); 122 getPools().getGlobalPool().visitClassReaders((reader) -> { 123 String className = reader.getClassName(); 124 // Wrong naming of module-info.class in ASM 125 if (className.endsWith("module-info")) { 126 return null; 127 } 128 if (!classes.contains(className)) { 129 throw new AssertionError("Class is not expected " + className); 130 } 131 if (getPools().getGlobalPool().getClassReader(className) == null) { 132 throw new AssertionError("Class not found in pool " + className); 133 } 134 seen.add(className); 135 return null; 136 }); 137 138 if (!seen.equals(classes)) { 139 throw new AssertionError("Expected and seen are not equal"); 140 } 141 } 142 143 private void testPools() throws IOException { 144 Set<String> remain = new HashSet<>(classes); 145 for (ModuleEntry res : getPools().getGlobalPool().getClasses()) { 146 ClassReader reader = getPools().getGlobalPool().getClassReader(res); 147 String className = reader.getClassName(); 148 // Wrong naming of module-info.class in ASM 149 if (className.endsWith("module-info")) { 150 continue; 151 } 152 if (!classes.contains(className)) { 153 throw new AssertionError("Class is not expected " + className); 154 } 155 if (getPools().getGlobalPool().getClassReader(className) == null) { 156 throw new AssertionError("Class " + className + " not found in pool "); 157 } 158 // Check the module pool 159 boolean found = false; 160 for(AsmModulePool mp : getPools().getModulePools()) { 161 if(mp.getClassReader(className) != null) { 162 found = true; 163 break; 164 } 165 } 166 if(!found) { 167 throw new AssertionError("No modular pool for " + 168 className); 169 } 170 remain.remove(className); 171 } 172 if (!remain.isEmpty()) { 173 throw new AssertionError("Remaining classes " + remain); 174 } 175 } 176 } 177 }