< prev index next >

test/jdk/java/util/ServiceLoader/BadProvidersTest.java

Print this page

  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 /test/lib
 27  * @modules jdk.compiler
 28  * @build jdk.test.lib.compiler.CompilerUtils
 29  * @run junit/othervm BadProvidersTest
 30  * @summary Basic test of ServiceLoader with bad provider and bad provider
 31  *          factories deployed on the module path
 32  */
 33 
 34 import java.lang.classfile.ClassFile;
 35 import java.lang.constant.ClassDesc;
 36 import java.lang.constant.MethodTypeDesc;
 37 import java.lang.module.Configuration;
 38 import java.lang.module.ModuleFinder;
 39 import java.lang.reflect.AccessFlag;
 40 import java.nio.file.Files;
 41 import java.nio.file.Path;
 42 import java.nio.file.Paths;
 43 import java.nio.file.StandardCopyOption;
 44 import java.util.List;
 45 import java.util.ServiceConfigurationError;

179                 mods.resolve(TEST2_MODULE).resolve(classFile),
180                 StandardCopyOption.REPLACE_EXISTING);
181 
182         Assertions.assertThrows(ServiceConfigurationError.class,
183                 // load providers and instantiate each one
184                 () -> loadProviders(mods, TEST2_MODULE).forEach(Provider::get)
185         );
186     }
187 
188 
189     /**
190      * Test a service provider that defines more than one no-args
191      * public static "provider" method.
192      */
193     @Test
194     public void testWithTwoFactoryMethods() throws Exception {
195         Path mods = compileTest(TEST1_MODULE);
196 
197         var bytes = ClassFile.of().build(ClassDesc.of("p", "ProviderFactory"), clb -> {
198             clb.withSuperclass(CD_Object);
199             clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
200 
201             var providerFactory$1 = ClassDesc.of("p", "ProviderFactory$1");
202 
203             // public static p.Service provider()
204             clb.withMethodBody("provider", MethodTypeDesc.of(ClassDesc.of("p", "Service")),
205                     ACC_PUBLIC | ACC_STATIC, cob -> {
206                         cob.new_(providerFactory$1);
207                         cob.dup();
208                         cob.invokespecial(providerFactory$1, INIT_NAME, MTD_void);
209                         cob.areturn();
210                     });
211 
212             // public static p.ProviderFactory$1 provider()
213             clb.withMethodBody("provider", MethodTypeDesc.of(providerFactory$1),
214                     ACC_PUBLIC | ACC_STATIC, cob -> {
215                         cob.new_(providerFactory$1);
216                         cob.dup();
217                         cob.invokespecial(providerFactory$1, INIT_NAME, MTD_void);
218                         cob.areturn();
219                     });

  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  * @enablePreview
 27  * @library /test/lib
 28  * @modules jdk.compiler
 29  * @build jdk.test.lib.compiler.CompilerUtils
 30  * @run junit/othervm BadProvidersTest
 31  * @summary Basic test of ServiceLoader with bad provider and bad provider
 32  *          factories deployed on the module path
 33  */
 34 
 35 import java.lang.classfile.ClassFile;
 36 import java.lang.constant.ClassDesc;
 37 import java.lang.constant.MethodTypeDesc;
 38 import java.lang.module.Configuration;
 39 import java.lang.module.ModuleFinder;
 40 import java.lang.reflect.AccessFlag;
 41 import java.nio.file.Files;
 42 import java.nio.file.Path;
 43 import java.nio.file.Paths;
 44 import java.nio.file.StandardCopyOption;
 45 import java.util.List;
 46 import java.util.ServiceConfigurationError;

180                 mods.resolve(TEST2_MODULE).resolve(classFile),
181                 StandardCopyOption.REPLACE_EXISTING);
182 
183         Assertions.assertThrows(ServiceConfigurationError.class,
184                 // load providers and instantiate each one
185                 () -> loadProviders(mods, TEST2_MODULE).forEach(Provider::get)
186         );
187     }
188 
189 
190     /**
191      * Test a service provider that defines more than one no-args
192      * public static "provider" method.
193      */
194     @Test
195     public void testWithTwoFactoryMethods() throws Exception {
196         Path mods = compileTest(TEST1_MODULE);
197 
198         var bytes = ClassFile.of().build(ClassDesc.of("p", "ProviderFactory"), clb -> {
199             clb.withSuperclass(CD_Object);
200             clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
201 
202             var providerFactory$1 = ClassDesc.of("p", "ProviderFactory$1");
203 
204             // public static p.Service provider()
205             clb.withMethodBody("provider", MethodTypeDesc.of(ClassDesc.of("p", "Service")),
206                     ACC_PUBLIC | ACC_STATIC, cob -> {
207                         cob.new_(providerFactory$1);
208                         cob.dup();
209                         cob.invokespecial(providerFactory$1, INIT_NAME, MTD_void);
210                         cob.areturn();
211                     });
212 
213             // public static p.ProviderFactory$1 provider()
214             clb.withMethodBody("provider", MethodTypeDesc.of(providerFactory$1),
215                     ACC_PUBLIC | ACC_STATIC, cob -> {
216                         cob.new_(providerFactory$1);
217                         cob.dup();
218                         cob.invokespecial(providerFactory$1, INIT_NAME, MTD_void);
219                         cob.areturn();
220                     });
< prev index next >