Search in sources :

Example 61 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project glide by bumptech.

the class EngineJobTest method testRemovingCallbackDuringOnExceptionIsIgnoredIfCallbackHasAlreadyBeenCalled.

@Test
public void testRemovingCallbackDuringOnExceptionIsIgnoredIfCallbackHasAlreadyBeenCalled() {
    harness = new EngineJobHarness();
    final EngineJob<Object> job = harness.getJob();
    final ResourceCallback cb = mock(ResourceCallback.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            job.removeCallback(cb);
            return null;
        }
    }).when(cb).onLoadFailed(any(GlideException.class));
    GlideException exception = new GlideException("test");
    job.addCallback(cb);
    job.start(harness.decodeJob);
    job.onLoadFailed(exception);
    verify(cb, times(1)).onLoadFailed(eq(exception));
}
Also used : ResourceCallback(com.bumptech.glide.request.ResourceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 62 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project glide by bumptech.

the class EngineJobTest method testNotifiesNewCallbackOfResourceIfCallbackIsAddedDuringOnResourceReady.

@Test
public void testNotifiesNewCallbackOfResourceIfCallbackIsAddedDuringOnResourceReady() {
    final EngineJob<Object> job = harness.getJob();
    final ResourceCallback existingCallback = mock(ResourceCallback.class);
    final ResourceCallback newCallback = mock(ResourceCallback.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            job.addCallback(newCallback);
            return null;
        }
    }).when(existingCallback).onResourceReady(anyResource(), isADataSource());
    job.addCallback(existingCallback);
    job.start(harness.decodeJob);
    job.onResourceReady(harness.resource, harness.dataSource);
    verify(newCallback).onResourceReady(eq(harness.engineResource), eq(harness.dataSource));
}
Also used : ResourceCallback(com.bumptech.glide.request.ResourceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 63 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project glide by bumptech.

the class EngineJobTest method testRemovingCallbackDuringOnResourceReadyPreventsResourceFromBeingAcquiredForCallback.

@Test
public void testRemovingCallbackDuringOnResourceReadyPreventsResourceFromBeingAcquiredForCallback() {
    final EngineJob<Object> job = harness.getJob();
    final ResourceCallback notYetCalled = mock(ResourceCallback.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            job.removeCallback(notYetCalled);
            return null;
        }
    }).when(harness.cb).onResourceReady(anyResource(), isADataSource());
    job.addCallback(notYetCalled);
    job.start(harness.decodeJob);
    job.onResourceReady(harness.resource, harness.dataSource);
    // Once for notifying, once for called.
    verify(harness.engineResource, times(2)).acquire();
}
Also used : ResourceCallback(com.bumptech.glide.request.ResourceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 64 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project glide by bumptech.

the class EngineJobTest method testRemovingCallbackDuringOnResourceReadyPreventsCallbackFromBeingCalledIfNotYetCalled.

@Test
public void testRemovingCallbackDuringOnResourceReadyPreventsCallbackFromBeingCalledIfNotYetCalled() {
    final EngineJob<Object> job = harness.getJob();
    final ResourceCallback notYetCalled = mock(ResourceCallback.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            job.removeCallback(notYetCalled);
            return null;
        }
    }).when(harness.cb).onResourceReady(anyResource(), isADataSource());
    job.addCallback(notYetCalled);
    job.start(harness.decodeJob);
    job.onResourceReady(harness.resource, harness.dataSource);
    verify(notYetCalled, never()).onResourceReady(anyResource(), isADataSource());
}
Also used : ResourceCallback(com.bumptech.glide.request.ResourceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 65 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project spring-security by spring-projects.

the class WebExpressionVoterTests method grantsAccessIfExpressionIsTrueDeniesIfFalse.

@Test
public void grantsAccessIfExpressionIsTrueDeniesIfFalse() {
    WebExpressionVoter voter = new WebExpressionVoter();
    Expression ex = mock(Expression.class);
    EvaluationContextPostProcessor postProcessor = mock(EvaluationContextPostProcessor.class);
    when(postProcessor.postProcess(any(EvaluationContext.class), any(FilterInvocation.class))).thenAnswer(new Answer<EvaluationContext>() {

        public EvaluationContext answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArgumentAt(0, EvaluationContext.class);
        }
    });
    WebExpressionConfigAttribute weca = new WebExpressionConfigAttribute(ex, postProcessor);
    EvaluationContext ctx = mock(EvaluationContext.class);
    SecurityExpressionHandler eh = mock(SecurityExpressionHandler.class);
    FilterInvocation fi = new FilterInvocation("/path", "GET");
    voter.setExpressionHandler(eh);
    when(eh.createEvaluationContext(user, fi)).thenReturn(ctx);
    when(ex.getValue(ctx, Boolean.class)).thenReturn(Boolean.TRUE).thenReturn(Boolean.FALSE);
    ArrayList attributes = new ArrayList();
    attributes.addAll(SecurityConfig.createList("A", "B", "C"));
    attributes.add(weca);
    assertThat(voter.vote(user, fi, attributes)).isEqualTo(AccessDecisionVoter.ACCESS_GRANTED);
    // Second time false
    assertThat(voter.vote(user, fi, attributes)).isEqualTo(AccessDecisionVoter.ACCESS_DENIED);
}
Also used : SecurityExpressionHandler(org.springframework.security.access.expression.SecurityExpressionHandler) Expression(org.springframework.expression.Expression) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) EvaluationContext(org.springframework.expression.EvaluationContext) FilterInvocation(org.springframework.security.web.FilterInvocation) Test(org.junit.Test)

Aggregations

InvocationOnMock (org.mockito.invocation.InvocationOnMock)1088 Test (org.junit.Test)655 Answer (org.mockito.stubbing.Answer)287 Matchers.anyString (org.mockito.Matchers.anyString)145 HashMap (java.util.HashMap)124 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)113 Before (org.junit.Before)110 Mockito.doAnswer (org.mockito.Mockito.doAnswer)110 ArrayList (java.util.ArrayList)109 IOException (java.io.IOException)92 Context (android.content.Context)68 List (java.util.List)62 AtomicReference (java.util.concurrent.atomic.AtomicReference)61 CountDownLatch (java.util.concurrent.CountDownLatch)59 Test (org.testng.annotations.Test)59 File (java.io.File)55 UUID (java.util.UUID)46 Configuration (org.apache.hadoop.conf.Configuration)46 Activity (android.app.Activity)40 Semaphore (java.util.concurrent.Semaphore)38