jdk/test/java/lang/instrument/RedefineModuleTest.java
changeset 44545 83b611b88ac8
parent 44423 306c020eb154
child 45652 33342314ce89
equal deleted inserted replaced
44480:2c33418a6d57 44545:83b611b88ac8
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    32  * @run testng/othervm -javaagent:RedefineModuleAgent.jar RedefineModuleTest
    32  * @run testng/othervm -javaagent:RedefineModuleAgent.jar RedefineModuleTest
    33  */
    33  */
    34 
    34 
    35 import java.lang.TestProvider;
    35 import java.lang.TestProvider;
    36 import java.lang.instrument.Instrumentation;
    36 import java.lang.instrument.Instrumentation;
    37 import java.lang.reflect.Layer;
       
    38 import java.lang.reflect.Module;
       
    39 import java.net.URLStreamHandler;
    37 import java.net.URLStreamHandler;
    40 import java.net.spi.URLStreamHandlerProvider;
    38 import java.net.spi.URLStreamHandlerProvider;
    41 import java.nio.file.FileSystems;
    39 import java.nio.file.FileSystems;
    42 import java.util.ArrayList;
    40 import java.util.ArrayList;
    43 import java.util.Collection;
    41 import java.util.Collection;
    66                                            extraOpens,
    64                                            extraOpens,
    67                                            extraUses,
    65                                            extraUses,
    68                                            extraProvides);
    66                                            extraProvides);
    69     }
    67     }
    70 
    68 
       
    69     static boolean isModifiableModule(Module module) {
       
    70         return RedefineModuleAgent.isModifiableModule(module);
       
    71     }
       
    72 
    71 
    73 
    72     /**
    74     /**
    73      * Use redefineModule to update java.base to read java.instrument
    75      * Use redefineModule to update java.base to read java.instrument
    74      */
    76      */
    75     public void testAddReads() {
    77     public void testAddReads() {
   136         Class<TestProvider> service = TestProvider.class;
   138         Class<TestProvider> service = TestProvider.class;
   137 
   139 
   138         // pre-conditions
   140         // pre-conditions
   139         assertFalse(baseModule.canUse(service));
   141         assertFalse(baseModule.canUse(service));
   140         assertTrue(collect(ServiceLoader.load(service)).isEmpty());
   142         assertTrue(collect(ServiceLoader.load(service)).isEmpty());
   141         assertTrue(collect(ServiceLoader.load(Layer.boot(), service)).isEmpty());
   143         assertTrue(collect(ServiceLoader.load(ModuleLayer.boot(), service)).isEmpty());
   142 
   144 
   143         // update java.base to use TestProvider
   145         // update java.base to use TestProvider
   144         redefineModule(baseModule, Set.of(), Map.of(), Map.of(), Set.of(service), Map.of());
   146         redefineModule(baseModule, Set.of(), Map.of(), Map.of(), Set.of(service), Map.of());
   145         assertTrue(baseModule.canUse(service));
   147         assertTrue(baseModule.canUse(service));
   146         assertTrue(collect(ServiceLoader.load(service)).isEmpty());
   148         assertTrue(collect(ServiceLoader.load(service)).isEmpty());
   147         assertTrue(collect(ServiceLoader.load(Layer.boot(), service)).isEmpty());
   149         assertTrue(collect(ServiceLoader.load(ModuleLayer.boot(), service)).isEmpty());
   148 
   150 
   149         // update java.base to provide an implementation of TestProvider
   151         // update java.base to provide an implementation of TestProvider
   150         Class<?> type1 = Class.forName("jdk.internal.test.TestProviderImpl1");
   152         Class<?> type1 = Class.forName("jdk.internal.test.TestProviderImpl1");
   151         Map<Class<?>, List<Class<?>>> extraProvides = Map.of(service, List.of(type1));
   153         Map<Class<?>, List<Class<?>>> extraProvides = Map.of(service, List.of(type1));
   152         redefineModule(baseModule, Set.of(), Map.of(), Map.of(), Set.of(), extraProvides);
   154         redefineModule(baseModule, Set.of(), Map.of(), Map.of(), Set.of(), extraProvides);
   160         providers = collect(ServiceLoader.load(service));
   162         providers = collect(ServiceLoader.load(service));
   161         assertTrue(collect(providers).size() == 1);
   163         assertTrue(collect(providers).size() == 1);
   162         assertTrue(containsInstanceOf(collect(providers), type1));
   164         assertTrue(containsInstanceOf(collect(providers), type1));
   163 
   165 
   164         // use ServiceLoader to load implementations in the boot layer
   166         // use ServiceLoader to load implementations in the boot layer
   165         providers = collect(ServiceLoader.load(Layer.boot(), service));
   167         providers = collect(ServiceLoader.load(ModuleLayer.boot(), service));
   166         assertTrue(collect(providers).size() == 1);
   168         assertTrue(collect(providers).size() == 1);
   167         assertTrue(containsInstanceOf(collect(providers), type1));
   169         assertTrue(containsInstanceOf(collect(providers), type1));
   168 
   170 
   169         // update java.base to provide a second implementation of TestProvider
   171         // update java.base to provide a second implementation of TestProvider
   170         Class<?> type2 = Class.forName("jdk.internal.test.TestProviderImpl2");
   172         Class<?> type2 = Class.forName("jdk.internal.test.TestProviderImpl2");
   182         assertTrue(collect(providers).size() == 2);
   184         assertTrue(collect(providers).size() == 2);
   183         assertTrue(containsInstanceOf(providers, type1));
   185         assertTrue(containsInstanceOf(providers, type1));
   184         assertTrue(containsInstanceOf(providers, type2));
   186         assertTrue(containsInstanceOf(providers, type2));
   185 
   187 
   186         // use ServiceLoader to load implementations in the boot layer
   188         // use ServiceLoader to load implementations in the boot layer
   187         providers = collect(ServiceLoader.load(Layer.boot(), service));
   189         providers = collect(ServiceLoader.load(ModuleLayer.boot(), service));
   188         assertTrue(collect(providers).size() == 2);
   190         assertTrue(collect(providers).size() == 2);
   189         assertTrue(containsInstanceOf(providers, type1));
   191         assertTrue(containsInstanceOf(providers, type1));
   190         assertTrue(containsInstanceOf(providers, type2));
   192         assertTrue(containsInstanceOf(providers, type2));
   191     }
   193     }
   192 
   194 
   277         Map<Class<?>, List<Class<?>>> extraProvides = Map.of(service, List.of(impl));
   279         Map<Class<?>, List<Class<?>>> extraProvides = Map.of(service, List.of(impl));
   278         redefineModule(baseModule, Set.of(), Map.of(), Map.of(), Set.of(), extraProvides);
   280         redefineModule(baseModule, Set.of(), Map.of(), Map.of(), Set.of(), extraProvides);
   279     }
   281     }
   280 
   282 
   281     /**
   283     /**
       
   284      * Exercise IsModifiableModule
       
   285      */
       
   286     @Test
       
   287     public void testIsModifiableModule() {
       
   288         ClassLoader pcl = ClassLoader.getPlatformClassLoader();
       
   289         ClassLoader scl = ClassLoader.getSystemClassLoader();
       
   290         assertTrue(isModifiableModule(pcl.getUnnamedModule()));
       
   291         assertTrue(isModifiableModule(scl.getUnnamedModule()));
       
   292         assertTrue(isModifiableModule(RedefineModuleTest.class.getModule()));
       
   293         assertTrue(isModifiableModule(Object.class.getModule()));
       
   294     }
       
   295 
       
   296     /**
   282      * Test redefineClass with null
   297      * Test redefineClass with null
   283      */
   298      */
   284     public void testNulls() {
   299     public void testNulls() {
   285         Module baseModule = Object.class.getModule();
   300         Module baseModule = Object.class.getModule();
   286 
   301