XAStore.Provider provider = new XAStore.Provider(); provider.start(serviceProvider); Store<String, String> store = provider.createStore(configuration, mock(XAStoreConfiguration.class)); provider.initStore(store);
@Test public void testXAStoreProviderFailsToRankWhenNoTMProviderConfigured() throws Exception { XAStore.Provider provider = new XAStore.Provider(); provider.start(new ServiceProvider<Service>() { @Override public <U extends Service> U getService(Class<U> serviceType) { return null; } @Override public <U extends Service> Collection<U> getServicesOfType(Class<U> serviceType) { return emptySet(); } }); try { provider.wrapperStoreRank(Collections.singleton(mock(XAStoreConfiguration.class))); fail("Expected exception"); } catch (IllegalStateException e) { assertThat(e.getMessage(), containsString("TransactionManagerProvider")); } }
selectProvider(configuredTypes, serviceConfigList, xaServiceConfiguration);
@Override public int rank(final Set<ResourceType<?>> resourceTypes, final Collection<ServiceConfiguration<?>> serviceConfigs) { final XAStoreConfiguration xaServiceConfiguration = findSingletonAmongst(XAStoreConfiguration.class, serviceConfigs); if (xaServiceConfiguration == null) { // An XAStore must be configured for use return 0; } else { if (this.transactionManagerProvider == null) { throw new IllegalStateException("A TransactionManagerProvider is mandatory to use XA caches"); } } final Store.Provider candidateUnderlyingProvider = selectProvider(resourceTypes, serviceConfigs, xaServiceConfiguration); return 1000 + candidateUnderlyingProvider.rank(resourceTypes, serviceConfigs); }
@Override public XAStore.Provider create(ServiceCreationConfiguration<XAStore.Provider> configuration) { return new XAStore.Provider(); }
@Test public void testUnSupportedResourceType() { XAStore.Provider provider = new XAStore.Provider(); @SuppressWarnings("unchecked") Store.Configuration<Object, Object> configuration = mock(Store.Configuration.class); ResourcePools resourcePools = mock(ResourcePools.class); Set<ResourceType<?>> resourceTypes = Collections.singleton(new TestResourceType()); when(configuration.getResourcePools()).thenReturn(resourcePools); when(resourcePools.getResourceTypeSet()).thenReturn(resourceTypes); try { provider.createStore(configuration, (ServiceConfiguration<?>) null); fail("IllegalStateException expected"); } catch (IllegalStateException e) { } }
@Test public void testRank() throws Exception { XAStore.Provider provider = new XAStore.Provider(); XAStoreConfiguration configuration = new XAStoreConfiguration("testXAResourceId"); ServiceLocator serviceLocator = dependencySet() .with(provider) .with(Store.Provider.class) .with(mock(DiskResourceService.class)) .with(mock(TransactionManagerProvider.class)).build(); serviceLocator.startAllServices(); Set<ServiceConfiguration<?>> xaStoreConfigs = Collections.singleton(configuration); assertThat(provider.wrapperStoreRank(xaStoreConfigs), is(1)); Set<ServiceConfiguration<?>> emptyConfigs = emptySet(); assertThat(provider.wrapperStoreRank(emptyConfigs), is(0)); }
@Override public XAStore.Provider create(ServiceCreationConfiguration<XAStore.Provider> configuration) { return new XAStore.Provider(); }