/** * Creates a matcher that matches only when the examined object is the same instance as * the specified target object. * * @param target * the target instance against which others should be assessed */ public static <T> org.hamcrest.Matcher<T> sameInstance(T target) { return org.hamcrest.core.IsSame.<T>sameInstance(target); }
/** * Creates a matcher that matches only when the examined object is the same instance as * the specified target object. * * @param target * the target instance against which others should be assessed */ public static <T> Matcher<T> sameInstance(T target) { return new IsSame<T>(target); }
/** * Creates a matcher that matches only when the examined object is the same instance as * the specified target object. * * @param target * the target instance against which others should be assessed */ public static <T> org.hamcrest.Matcher<T> theInstance(T target) { return org.hamcrest.core.IsSame.<T>theInstance(target); }
@Test public void firstInterceptorThrowsExceptionBefore() throws Exception { RuntimeException expectedException = new RuntimeException("Some Error"); ProcessorInterceptor interceptor1 = prepareInterceptor(new TestProcessorInterceptor("outer") { @Override public void before(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event) { throw expectedException; } }); ProcessorInterceptor interceptor2 = prepareInterceptor(new TestProcessorInterceptor("inner") {}); startFlowWithInterceptors(interceptor1, interceptor2); expected.expectCause(sameInstance(expectedException)); try { process(flow, eventBuilder(muleContext).message(Message.of("")).build()); } finally { if (useMockInterceptor) { InOrder inOrder = inOrder(processor, interceptor1, interceptor2); inOrder.verify(interceptor1).before(any(), any(), any()); inOrder.verify(interceptor2, never()).before(any(), any(), any()); inOrder.verify(interceptor1, never()).around(any(), any(), any(), any()); inOrder.verify(interceptor2, never()).around(any(), any(), any(), any()); inOrder.verify(processor, never()).process(any()); inOrder.verify(interceptor2, never()).after(any(), any(), eq(of(expectedException))); inOrder.verify(interceptor1).after(any(), any(), eq(of(expectedException))); verifyParametersResolvedAndDisposed(times(1)); } } }
@Test public void shouldDelegateGetSegmentToParentIfIndexNotEqualToSizeMinusOne() { Path.Segment segment = mock(Path.Segment.class); parent = mock(Path.class); when(parent.size()).thenReturn(10); path = new ChildPath(parent, segment("d")); when(parent.getSegment(anyInt())).thenReturn(segment); for (int i = 0; i < path.size() - 1; ++i) { assertThat(path.getSegment(i), is(sameInstance(segment))); } verify(parent, times(parent.size())).getSegment(anyInt()); }
@Test public void shouldSubmitFederatedRequestToQueueIfFederatedRequestHasNoIncompleteRequests() { FederatedRequest request = mock(FederatedRequest.class); when(request.hasIncompleteRequests()).thenReturn(false); processor.submit(request); assertThat(federatedRequests.size(), is(1)); assertThat(federatedRequests.get(0), is(sameInstance(request))); }
@Test public void processorFailsAndfirstInterceptorThrowsExceptionAfter() throws Exception { when(processor.process(any())).thenThrow(new RuntimeException("Processor Error")); RuntimeException expectedException = new RuntimeException("Some Error"); ProcessorInterceptor interceptor1 = prepareInterceptor(new TestProcessorInterceptor("outer") { @Override public void after(ComponentLocation location, InterceptionEvent event, Optional<Throwable> thrown) { throw expectedException; } }); startFlowWithInterceptors(interceptor1); expected.expect(MessagingException.class); expected.expectCause(sameInstance(expectedException)); try { process(flow, eventBuilder(muleContext).message(Message.of("")).build()); } catch (MessagingException e) { assertThat(e.getInfo().getOrDefault(INFO_ALREADY_LOGGED_KEY, false), is(true)); throw e; } }
@Test public void testSupportsMultipleAuthoritativeTierProviders() throws Exception { ServiceLocator.DependencySet dependencySet = dependencySet(); OnHeapStore.Provider cachingTierProvider = new OnHeapStore.Provider(); OffHeapStore.Provider authoritativeTierProvider = new OffHeapStore.Provider(); OffHeapDiskStore.Provider diskStoreProvider = new OffHeapDiskStore.Provider(); dependencySet.with(cachingTierProvider); dependencySet.with(authoritativeTierProvider); dependencySet.with(diskStoreProvider); dependencySet.with(mock(DiskResourceService.class)); ServiceLocator serviceLocator = dependencySet.build(); serviceLocator.startAllServices(); assertThat(serviceLocator.getServicesOfType(CachingTier.Provider.class), IsCollectionContaining.<CachingTier.Provider>hasItem(IsSame.<CachingTier.Provider>sameInstance(cachingTierProvider))); assertThat(serviceLocator.getServicesOfType(AuthoritativeTier.Provider.class), IsCollectionContaining.<AuthoritativeTier.Provider>hasItem(IsSame.<AuthoritativeTier.Provider>sameInstance(authoritativeTierProvider))); assertThat(serviceLocator.getServicesOfType(OffHeapDiskStore.Provider.class), IsCollectionContaining.<OffHeapDiskStore.Provider>hasItem(IsSame.<OffHeapDiskStore.Provider>sameInstance(diskStoreProvider))); } }
@Test public void testProvidedInstanceReturned() { ResilienceStrategy<?, ?> resilienceStrategy = mock(ResilienceStrategy.class); DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration(); DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration); CacheConfiguration<?, ?> cacheConfiguration = mock(CacheConfiguration.class); when(cacheConfiguration.getServiceConfigurations()).thenReturn(Collections.singleton(new DefaultResilienceStrategyConfiguration(resilienceStrategy))); assertThat(provider.createResilienceStrategy("foo", cacheConfiguration, mock(RecoveryStore.class)), sameInstance(resilienceStrategy)); }
@Test public void testSysPropReplace() { System.getProperties().setProperty("ehcache.match", Number.class.getName()); XmlConfiguration xmlConfig = new XmlConfiguration(XmlConfigurationTest.class.getResource("/configs/systemprops.xml")); assertThat(xmlConfig.getCacheConfigurations().get("bar").getKeyType(), sameInstance((Class)Number.class)); DefaultPersistenceConfiguration persistenceConfiguration = (DefaultPersistenceConfiguration)xmlConfig.getServiceCreationConfigurations().iterator().next(); assertThat(persistenceConfiguration.getRootDirectory(), is(new File(System.getProperty("user.home") + "/ehcache"))); }
@Test public void whenSettingAnAssociationInASideEffectExpectItToWork() { try( UnitOfWork uow = unitOfWorkFactory.newUnitOfWork( UsecaseBuilder.newUsecase( "Purchase Steinway" ) ) ) { Pianist chris = uow.newEntity( Pianist.class, StringIdentity.identityOf( "Chris" ) ); Steinway modelD = uow.newEntity( Steinway.class, StringIdentity.identityOf( "ModelD-274" ) ); assertThat( modelD.owner().get(), is( nullValue() ) ); chris.purchase( modelD ); assertThat( modelD.owner().get(), is( theInstance( chris ) ) ); } }
@Test public void secondInterceptorThrowsExceptionBefore() throws Exception { RuntimeException expectedException = new RuntimeException("Some Error"); ProcessorInterceptor interceptor1 = prepareInterceptor(new TestProcessorInterceptor("outer") {}); ProcessorInterceptor interceptor2 = prepareInterceptor(new TestProcessorInterceptor("inner") { @Override public void before(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event) { throw expectedException; } }); startFlowWithInterceptors(interceptor1, interceptor2); expected.expectCause(sameInstance(expectedException)); try { process(flow, eventBuilder(muleContext).message(Message.of("")).build()); } finally { if (useMockInterceptor) { InOrder inOrder = inOrder(processor, interceptor1, interceptor2); inOrder.verify(interceptor1).before(any(), any(), any()); inOrder.verify(interceptor2).before(any(), any(), any()); inOrder.verify(interceptor1, never()).around(any(), any(), any(), any()); inOrder.verify(interceptor2, never()).around(any(), any(), any(), any()); inOrder.verify(processor, never()).process(any()); inOrder.verify(interceptor2).after(any(), any(), eq(of(expectedException))); inOrder.verify(interceptor1).after(any(), any(), eq(of(expectedException))); verifyParametersResolvedAndDisposed(times(1)); } } }
@Test public void shouldDelegateGetSegmentToParentIfIndexNotEqualToSizeMinusOne() { Path.Segment segment = mock(Path.Segment.class); parent = mock(Path.class); when(parent.size()).thenReturn(10); path = new ChildPath(parent, segment("d")); when(parent.getSegment(anyInt())).thenReturn(segment); for (int i = 0; i < path.size() - 1; ++i) { assertThat(path.getSegment(i), is(sameInstance(segment))); } verify(parent, times(parent.size())).getSegment(anyInt()); }
@Test public void testProvidedLoaderWriterInstanceReturned() { ResilienceStrategy<?, ?> resilienceStrategy = mock(ResilienceStrategy.class); DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration(); DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration); CacheConfiguration<?, ?> cacheConfiguration = mock(CacheConfiguration.class); when(cacheConfiguration.getServiceConfigurations()).thenReturn(Collections.singleton(new DefaultResilienceStrategyConfiguration(resilienceStrategy))); assertThat(provider.createResilienceStrategy("foo", cacheConfiguration, mock(RecoveryStore.class), mock(CacheLoaderWriter.class)), sameInstance(resilienceStrategy)); }
@Test public void testOneCacheConfigWithTemplate() throws Exception { final URL resource = XmlConfigurationTest.class.getResource("/configs/template-cache.xml"); XmlConfiguration xmlConfig = new XmlConfiguration(resource); assertThat(xmlConfig.getServiceCreationConfigurations(), hasSize(0)); assertThat(xmlConfig.getCacheConfigurations().keySet(), hasItem("bar")); assertThat(xmlConfig.getCacheConfigurations().get("bar").getServiceConfigurations(), IsCollectionContaining.hasItem(instanceOf(FooConfiguration.class))); assertThat(xmlConfig.getCacheConfigurations().get("bar").getKeyType(), sameInstance((Class) Number.class)); assertThat(xmlConfig.getCacheConfigurations().get("bar").getValueType(), sameInstance((Class)String.class)); final CacheConfigurationBuilder<String, String> example = xmlConfig.newCacheConfigurationBuilderFromTemplate("example", String.class, String.class, newResourcePoolsBuilder().heap(5, EntryUnit.ENTRIES)); assertThat(example.build().getExpiryPolicy(), equalTo((ExpiryPolicy) ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(30)))); try { xmlConfig.newCacheConfigurationBuilderFromTemplate("example", String.class, Number.class); fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), is("CacheTemplate 'example' declares value type of java.lang.String. Provided: class java.lang.Number")); } try { xmlConfig.newCacheConfigurationBuilderFromTemplate("example", Number.class, String.class); fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), is("CacheTemplate 'example' declares key type of java.lang.String. Provided: class java.lang.Number")); } assertThat(xmlConfig.newCacheConfigurationBuilderFromTemplate("bar", Object.class, Object.class), nullValue()); }
@Test public void interceptorThrowsExceptionBefore() throws Exception { RuntimeException expectedException = new RuntimeException("Some Error"); ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() { @Override public void before(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event) { throw expectedException; } }); startFlowWithInterceptors(interceptor); expected.expectCause(sameInstance(expectedException)); try { process(flow, eventBuilder(muleContext).message(Message.of("")).build()); } finally { if (useMockInterceptor) { InOrder inOrder = inOrder(processor, interceptor); inOrder.verify(interceptor).before(any(), any(), any()); inOrder.verify(interceptor, never()).around(any(), any(), any(), any()); inOrder.verify(processor, never()).process(any()); inOrder.verify(interceptor).after(any(), any(), eq(of(expectedException))); verifyParametersResolvedAndDisposed(times(1)); } } }
@Test public void shouldReturnSuppliedTextEncoderWhenNonNullPassedToGetEncoder() { assertThat(factory.getDecoder(), is(sameInstance(ValueFactory.DEFAULT_DECODER))); assertThat(factory.getDecoder(null), is(sameInstance(ValueFactory.DEFAULT_DECODER))); assertThat(factory.getDecoder(CUSTOM_DECODER), is(sameInstance(CUSTOM_DECODER))); }
/** * Creates a matcher that matches only when the examined object is the same instance as * the specified target object. * * @param target * the target instance against which others should be assessed */ public static <T> org.hamcrest.Matcher<T> sameInstance(T target) { return org.hamcrest.core.IsSame.<T>sameInstance(target); }
/** * Creates a matcher that matches only when the examined object is the same instance as * the specified target object. * * @param target * the target instance against which others should be assessed */ public static <T> Matcher<T> theInstance(T target) { return new IsSame<T>(target); } }
/** * Creates a matcher that matches only when the examined object is the same instance as * the specified target object. * * @param target * the target instance against which others should be assessed */ public static <T> org.hamcrest.Matcher<T> theInstance(T target) { return org.hamcrest.core.IsSame.<T>theInstance(target); }