package com.oracle.svm.test;
import java.util.ServiceLoader;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;
import org.junit.Assert;
import org.junit.Test;
public class ServiceLoaderTest {
public static class TestFeature implements Feature {
@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
RuntimeClassInitialization.initializeAtBuildTime(ServiceLoaderTest.class);
RuntimeClassInitialization.initializeAtBuildTime(ServiceLoaderTest.ServiceA.class);
RuntimeClassInitialization.initializeAtBuildTime(ServiceLoaderTest.ServiceB.class);
RuntimeClassInitialization.initializeAtBuildTime(ServiceLoaderTest.ServiceC.class);
RuntimeClassInitialization.initializeAtBuildTime(ServiceLoaderTest.ServiceD.class);
RuntimeClassInitialization.initializeAtBuildTime(ServiceLoaderTest.ServiceHostedOnly.class);
RuntimeClassInitialization.initializeAtBuildTime(ServiceLoaderTest.ServiceInterface.class);
}
}
static {
testServicesInHotSpot();
}
interface ServiceInterface {
}
public static class ServiceA implements ServiceInterface {
}
public static class ServiceB implements ServiceInterface {
}
@Platforms(Platform.HOSTED_ONLY.class)
public static class ServiceHostedOnly implements ServiceInterface {
}
public static class ServiceC implements ServiceInterface {
}
public static class ServiceD implements ServiceInterface {
}
@Test
public void test01OwnService() {
allocateServiceD();
ServiceLoader<ServiceInterface> loader = ServiceLoader.load(ServiceInterface.class);
int numFound = 0;
boolean foundA = false;
boolean foundB = false;
boolean foundHostedOnly = false;
boolean foundC = false;
boolean foundD = false;
for (ServiceInterface instance : loader) {
numFound++;
String name = instance.getClass().getSimpleName();
foundA |= name.equals("ServiceA");
foundB |= name.equals("ServiceB");
foundHostedOnly |= name.equals("ServiceHostedOnly");
foundC |= name.equals("ServiceC");
foundD |= name.equals("ServiceD");
}
Assert.assertTrue("Should find ServiceA", foundA);
Assert.assertTrue("Should find ServiceB", foundB);
Assert.assertFalse("Should not find ServiceHostedOnly", foundHostedOnly);
Assert.assertFalse("Should not find ServiceC", foundC);
Assert.assertFalse("Should not find ServiceD", foundD);
Assert.assertEquals(2, numFound);
}
static void testServicesInHotSpot() {
ServiceLoader<ServiceInterface> loader = ServiceLoader.load(ServiceInterface.class);
int numFound = 0;
boolean foundA = false;
boolean foundB = false;
boolean foundHostedOnly = false;
boolean foundC = false;
boolean foundD = false;
for (ServiceInterface instance : loader) {
numFound++;
String name = instance.getClass().getSimpleName();
foundA |= name.equals("ServiceA");
foundB |= name.equals("ServiceB");
foundHostedOnly |= name.equals("ServiceHostedOnly");
foundC |= name.equals("ServiceC");
foundD |= name.equals("ServiceD");
}
Assert.assertEquals(3, numFound);
Assert.assertTrue(foundA);
Assert.assertTrue(foundB);
Assert.assertTrue(foundHostedOnly);
Assert.assertFalse(foundC);
Assert.assertFalse(foundD);
}
private static Object allocateServiceD() {
return new ServiceD();
}
@Test
public void test02JDKService() {
ServiceLoader<?> loader = ServiceLoader.load(java.nio.file.spi.FileSystemProvider.class);
boolean foundZip = false;
for (Object instance : loader) {
String name = instance.getClass().getSimpleName();
foundZip |= name.equals("ZipFileSystemProvider");
}
Assert.assertTrue(foundZip);
}
}