org.jboss.arquillian.core.spi.ServiceLoader.onlyOne()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(111)

本文整理了Java中org.jboss.arquillian.core.spi.ServiceLoader.onlyOne()方法的一些代码示例,展示了ServiceLoader.onlyOne()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ServiceLoader.onlyOne()方法的具体详情如下:
包路径:org.jboss.arquillian.core.spi.ServiceLoader
类名称:ServiceLoader
方法名:onlyOne

ServiceLoader.onlyOne介绍

[英]Load a single service implementation.

Method should throw IllegalStateException if multiple instances of serviceClass found.
[中]加载单个服务实现。
如果找到serviceClass的多个实例,方法应引发IllegalStateException。

代码示例

代码示例来源:origin: arquillian/arquillian-core

protected CommandService getCommandService() {
    ServiceLoader loader = serviceLoader.get();
    if (loader == null) {
      throw new IllegalStateException("No " + ServiceLoader.class.getName() + " found in context");
    }
    CommandService service = loader.onlyOne(CommandService.class);
    if (service == null) {
      throw new IllegalStateException("No " + CommandService.class.getName() + " found in context");
    }
    return service;
  }
}

代码示例来源:origin: arquillian/arquillian-core

private CommandService getCommandService() {
    ServiceLoader loader = serviceLoader.get();
    if (loader == null) {
      throw new IllegalStateException("No " + ServiceLoader.class.getName() + " found in context");
    }
    CommandService service = loader.onlyOne(CommandService.class);
    if (service == null) {
      throw new IllegalStateException("No " + CommandService.class.getName() + " found in context");
    }
    return service;
  }
}

代码示例来源:origin: org.jboss.arquillian.container/arquillian-container-test-impl-base

private CommandService getCommandService() {
    ServiceLoader loader = serviceLoader.get();
    if (loader == null) {
      throw new IllegalStateException("No " + ServiceLoader.class.getName() + " found in context");
    }
    CommandService service = loader.onlyOne(CommandService.class);
    if (service == null) {
      throw new IllegalStateException("No " + CommandService.class.getName() + " found in context");
    }
    return service;
  }
}

代码示例来源:origin: arquillian/arquillian-core

protected CommandService getCommandService() {
    ServiceLoader loader = serviceLoader.get();
    if (loader == null) {
      throw new IllegalStateException("No " + ServiceLoader.class.getName() + " found in context");
    }
    CommandService service = loader.onlyOne(CommandService.class);
    if (service == null) {
      throw new IllegalStateException("No " + CommandService.class.getName() + " found in context");
    }
    return service;
  }
}

代码示例来源:origin: org.jboss.arquillian.extension/arquillian-persistence-core

/**
  * @param dataSourceName
  * @return
  * @throws  IllegalStateException when more than one data source provider exists on the classpath
  */
  private DataSource loadDataSource(String dataSourceName)
  {
   final DataSourceProvider dataSourceProvider = serviceLoaderInstance.get()
      .onlyOne(DataSourceProvider.class, JndiDataSourceProvider.class);

   return dataSourceProvider.lookupDataSource(dataSourceName);
  }
}

代码示例来源:origin: org.jboss.as/jboss-as-arquillian-common-domain

@Override
public <X> X onlyOne(Class<X> serviceClass) {
  if (serviceClass == DeployableContainer.class) {
    return serviceClass.cast(injectorInst.get().inject(
        new ServerGroupContainer(managementClient, archiveDeployer, domain, serverGroup, operationTimeout)));
  }
  return serviceLoaderInstance.get().onlyOne(serviceClass);
}

代码示例来源:origin: arquillian/arquillian-core

@Before
public void addServiceLoader() {
  Mockito.when(serviceLoader.onlyOne(DeployableContainer.class)).thenReturn(deployableContainer);
  bind(ApplicationScoped.class, ServiceLoader.class, serviceLoader);
}

代码示例来源:origin: arquillian/arquillian-core

@Before
public void setup() throws Exception {
  Mockito.when(serviceLoader.onlyOne(Mockito.same(DeployableContainer.class))).thenReturn(deployableContainer);
  Mockito.when(deployableContainer.getConfigurationClass()).thenReturn(DummyContainerConfiguration.class);
}

代码示例来源:origin: arquillian/arquillian-core

@Test(expected = IllegalStateException.class)
public void shouldThrowExceptionIfMultipleDeployableContainersFoundOnClassapth() {
  Mockito.when(serviceLoader.onlyOne(DeployableContainer.class))
    .thenThrow(new IllegalStateException("Multiple service implementations found for ..."));
  try {
    fire(Descriptors.create(ArquillianDescriptor.class));
  } catch (IllegalStateException e) {
    Assert.assertTrue(e.getMessage().startsWith("Could not add a default container"));
    throw e;
  }
}

代码示例来源:origin: org.jboss.arquillian.extension/arquillian-transaction-impl-base

@Override
  public boolean isTransactionSupported(TestEvent testEvent) {

    final TransactionProvider transactionProvider =
        serviceLoaderInstance.get().onlyOne(TransactionProvider.class);
    if (transactionProvider == null) {
      return false;
    }

    return new ModeChecker(deploymentInstance.get(), containerInstance.get()).isClientMode(testEvent);
  }
}

代码示例来源:origin: org.arquillian.cube.q/arquillian-cube-q-core

public void install(@Observes(precedence = 100) CubeDockerConfiguration configuration) {
  StandaloneManager installer = serviceLoaderInst.get().onlyOne(StandaloneManager.class);
  if (installer != null) {
    DockerCompositions cubes = configuration.getDockerContainersContent();
    final StandaloneContainer install = installer.install();
    final CubeContainer cube = install.getCube();
    cubes.add(install.getName(), cube);
    standaloneContainerInst.set(install);
    System.out.println("STANDALONE CONTAINER INSTALLED");
    System.out.println(ConfigUtil.dump(cubes));
  }
}

代码示例来源:origin: org.arquillian.container/arquillian-droidium-container

/**
 * Sets {@link ActivityManagerProvider} for {@code AndroidDevice} and produces {@link AndroidApplicationManager}.
 *
 * @param event
 */
public void onAndroidDeviceReady(@Observes AndroidDeviceReady event) {
  ActivityManager activityManager = serviceLoader.get().onlyOne(ActivityManager.class, DefaultActivityManager.class);
  if (activityManager instanceof DefaultActivityManager) {
    androidDevice.get().setActivityManager(new DefaultActivityManager(androidDevice.get()));
  }
}

代码示例来源:origin: org.jboss.arquillian.core/arquillian-core-impl-base

@Test
public void shouldBeAbleToLoadDefaultIfNoneFound() throws Exception {
  ServiceRegistry registry = new ServiceRegistry(injector.get(), new LinkedHashMap<Class<?>, Set<Class<?>>>());
  Assert.assertNull(registry.getServiceLoader().onlyOne(FakeService.class));
  FakeService service = registry.getServiceLoader().onlyOne(FakeService.class, ShouldBeIncluded.class);
  Assert.assertNotNull(service);
  Assert.assertTrue(
    "Verify service is of expected type",
    service instanceof ShouldBeIncluded);
}

代码示例来源:origin: arquillian/arquillian-core

@Test
public void shouldBeAbleToLoadDefaultIfNoneFound() throws Exception {
  ServiceRegistry registry = new ServiceRegistry(injector.get(), new LinkedHashMap<Class<?>, Set<Class<?>>>());
  Assert.assertNull(registry.getServiceLoader().onlyOne(FakeService.class));
  FakeService service = registry.getServiceLoader().onlyOne(FakeService.class, ShouldBeIncluded.class);
  Assert.assertNotNull(service);
  Assert.assertTrue(
    "Verify service is of expected type",
    service instanceof ShouldBeIncluded);
}

代码示例来源:origin: arquillian/arquillian-core

@SuppressWarnings("unchecked")
  @Test
  public void shouldBeAbleToLoadProtectedServices() throws Exception {
    ServiceRegistry registry = new ServiceRegistry(injector.get(), new LinkedHashMap<Class<?>, Set<Class<?>>>());
    registry.addService(
      FakeService.class,
      (Class<FakeService>) Class.forName("org.jboss.arquillian.core.impl.loadable.util.PackageProtectedService"));

    FakeService service = registry.getServiceLoader().onlyOne(FakeService.class);
    Assert.assertNotNull("Could load package protected service", service);
  }
}

代码示例来源:origin: org.jboss.arquillian.core/arquillian-core-impl-base

@Test
public void loadedDefaultServicesShouldBeStaticallyInjected() throws Exception {
  bind(ApplicationScoped.class, String.class, "TEST");
  ServiceRegistry registry = new ServiceRegistry(injector.get(), new LinkedHashMap<Class<?>, Set<Class<?>>>());
  Assert.assertNull(registry.getServiceLoader().onlyOne(FakeService.class));
  FakeService service = registry.getServiceLoader().onlyOne(FakeService.class, ShouldBeIncluded.class);
  Assert.assertTrue(
    "Verify service has been statically injected",
    service.isValid());
}

代码示例来源:origin: org.jboss.arquillian.core/arquillian-core-impl-base

@Test(expected = IllegalStateException.class)
public void shouldThrowExceptionIfMultipleFoundWhenTryingOnlyOne() throws Exception {
  ServiceRegistry registry = new ServiceRegistry(injector.get(), new LinkedHashMap<Class<?>, Set<Class<?>>>());
  registry.addService(FakeService.class, ShouldBeIncluded.class);
  registry.addService(FakeService.class, ShouldBeExcluded.class);
  // throws exception
  registry.getServiceLoader().onlyOne(FakeService.class);
}

代码示例来源:origin: arquillian/arquillian-core

@Test
public void shouldBeAbleToLoadOnlyOne() throws Exception {
  ServiceRegistry registry = new ServiceRegistry(injector.get(), new LinkedHashMap<Class<?>, Set<Class<?>>>());
  registry.addService(FakeService.class, ShouldBeIncluded.class);
  FakeService service = registry.getServiceLoader().onlyOne(FakeService.class);
  Assert.assertNotNull(service);
  Assert.assertTrue(
    "Verify service is of expected type",
    service instanceof ShouldBeIncluded);
}

代码示例来源:origin: org.jboss.arquillian.core/arquillian-core-impl-base

@Test
public void shouldBeAbleToLoadOnlyOne() throws Exception {
  ServiceRegistry registry = new ServiceRegistry(injector.get(), new LinkedHashMap<Class<?>, Set<Class<?>>>());
  registry.addService(FakeService.class, ShouldBeIncluded.class);
  FakeService service = registry.getServiceLoader().onlyOne(FakeService.class);
  Assert.assertNotNull(service);
  Assert.assertTrue(
    "Verify service is of expected type",
    service instanceof ShouldBeIncluded);
}

代码示例来源:origin: org.jboss.arquillian.core/arquillian-core-impl-base

@Test
public void loadedServicesShouldBeStaticallyInjected() throws Exception {
  bind(ApplicationScoped.class, String.class, "TEST");
  ServiceRegistry registry = new ServiceRegistry(injector.get(), new LinkedHashMap<Class<?>, Set<Class<?>>>());
  registry.addService(FakeService.class, ShouldBeIncluded.class);
  FakeService service = registry.getServiceLoader().onlyOne(FakeService.class);
  Assert.assertTrue(
    "Verify service has been statically injected",
    service.isValid());
}

相关文章