Search in sources :

Example 91 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project camel by apache.

the class QuickfixjProducerTest method processInOutExchangeSendUnsuccessful.

@Test
public void processInOutExchangeSendUnsuccessful() throws Exception {
    Mockito.when(mockExchange.getPattern()).thenReturn(ExchangePattern.InOut);
    Mockito.when(mockExchange.getProperty(QuickfixjProducer.CORRELATION_CRITERIA_KEY)).thenReturn(new MessagePredicate(sessionID, MsgType.EMAIL));
    Mockito.when(mockExchange.getProperty(QuickfixjProducer.CORRELATION_TIMEOUT_KEY, 1000L, Long.class)).thenReturn(5000L);
    org.apache.camel.Message mockOutboundCamelMessage = Mockito.mock(org.apache.camel.Message.class);
    Mockito.when(mockExchange.getOut()).thenReturn(mockOutboundCamelMessage);
    final Message outboundFixMessage = new Email();
    outboundFixMessage.getHeader().setString(SenderCompID.FIELD, "TARGET");
    outboundFixMessage.getHeader().setString(TargetCompID.FIELD, "SENDER");
    Session mockSession = Mockito.spy(TestSupport.createSession(sessionID));
    Mockito.doReturn(mockSession).when(producer).getSession(MessageUtils.getSessionID(inboundFixMessage));
    Mockito.doAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            new Timer().schedule(new TimerTask() {

                @Override
                public void run() {
                    try {
                        quickfixjEngine.getMessageCorrelator().onEvent(QuickfixjEventCategory.AppMessageReceived, sessionID, outboundFixMessage);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }, 10);
            return false;
        }
    }).when(mockSession).send(Matchers.isA(Message.class));
    producer.process(mockExchange);
    Mockito.verify(mockOutboundCamelMessage, Mockito.never()).setBody(Matchers.isA(Message.class));
    Mockito.verify(mockSession).send(inboundFixMessage);
    Mockito.verify(mockExchange).setException(Matchers.isA(CannotSendException.class));
}
Also used : Email(quickfix.fix42.Email) Message(quickfix.Message) IOException(java.io.IOException) JMException(javax.management.JMException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Session(quickfix.Session) Test(org.junit.Test)

Example 92 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project hadoop by apache.

the class TestDatasetVolumeCheckerFailures method makeHungVolume.

/**
   * Create a mock FsVolumeSpi whose {@link FsVolumeSpi#check} routine
   * hangs forever.
   *
   * @return volume
   * @throws Exception
   */
private static FsVolumeSpi makeHungVolume() throws Exception {
    final FsVolumeSpi volume = mock(FsVolumeSpi.class);
    final FsVolumeReference reference = mock(FsVolumeReference.class);
    final StorageLocation location = mock(StorageLocation.class);
    when(reference.getVolume()).thenReturn(volume);
    when(volume.obtainReference()).thenReturn(reference);
    when(volume.getStorageLocation()).thenReturn(location);
    when(volume.check(anyObject())).thenAnswer(new Answer<VolumeCheckResult>() {

        @Override
        public VolumeCheckResult answer(InvocationOnMock invocation) throws Throwable {
            // Sleep forever.
            Thread.sleep(Long.MAX_VALUE);
            // unreachable.
            return VolumeCheckResult.HEALTHY;
        }
    });
    return volume;
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) StorageLocation(org.apache.hadoop.hdfs.server.datanode.StorageLocation)

Example 93 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project hadoop by apache.

the class TestDatasetVolumeCheckerTimeout method makeSlowVolume.

static FsVolumeSpi makeSlowVolume() throws Exception {
    final FsVolumeSpi volume = mock(FsVolumeSpi.class);
    final FsVolumeReference reference = mock(FsVolumeReference.class);
    final StorageLocation location = mock(StorageLocation.class);
    when(reference.getVolume()).thenReturn(volume);
    when(volume.obtainReference()).thenReturn(reference);
    when(volume.getStorageLocation()).thenReturn(location);
    when(volume.check(anyObject())).thenAnswer(new Answer<VolumeCheckResult>() {

        @Override
        public VolumeCheckResult answer(InvocationOnMock invocationOnMock) throws Throwable {
            // Wait for the disk check to timeout and then release lock.
            lock.lock();
            lock.unlock();
            return VolumeCheckResult.HEALTHY;
        }
    });
    return volume;
}
Also used : FsVolumeReference(org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeReference) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FsVolumeSpi(org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeSpi) StorageLocation(org.apache.hadoop.hdfs.server.datanode.StorageLocation)

Example 94 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project hadoop by apache.

the class TestRequestHedgingProxyProvider method testHedgingWhenOneFails.

@Test
public void testHedgingWhenOneFails() throws Exception {
    final NamenodeProtocols goodMock = Mockito.mock(NamenodeProtocols.class);
    Mockito.when(goodMock.getStats()).thenAnswer(new Answer<long[]>() {

        @Override
        public long[] answer(InvocationOnMock invocation) throws Throwable {
            Thread.sleep(1000);
            return new long[] { 1 };
        }
    });
    final NamenodeProtocols badMock = Mockito.mock(NamenodeProtocols.class);
    Mockito.when(badMock.getStats()).thenThrow(new IOException("Bad mock !!"));
    RequestHedgingProxyProvider<NamenodeProtocols> provider = new RequestHedgingProxyProvider<>(conf, nnUri, NamenodeProtocols.class, createFactory(badMock, goodMock));
    long[] stats = provider.getProxy().proxy.getStats();
    Assert.assertTrue(stats.length == 1);
    Mockito.verify(badMock).getStats();
    Mockito.verify(goodMock).getStats();
}
Also used : NamenodeProtocols(org.apache.hadoop.hdfs.server.protocol.NamenodeProtocols) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IOException(java.io.IOException) Test(org.junit.Test)

Example 95 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project hadoop by apache.

the class TestRequestHedgingProxyProvider method testHedgingWhenOneIsSlow.

@Test
public void testHedgingWhenOneIsSlow() throws Exception {
    final NamenodeProtocols goodMock = Mockito.mock(NamenodeProtocols.class);
    Mockito.when(goodMock.getStats()).thenAnswer(new Answer<long[]>() {

        @Override
        public long[] answer(InvocationOnMock invocation) throws Throwable {
            Thread.sleep(1000);
            return new long[] { 1 };
        }
    });
    final NamenodeProtocols badMock = Mockito.mock(NamenodeProtocols.class);
    Mockito.when(badMock.getStats()).thenThrow(new IOException("Bad mock !!"));
    RequestHedgingProxyProvider<NamenodeProtocols> provider = new RequestHedgingProxyProvider<>(conf, nnUri, NamenodeProtocols.class, createFactory(goodMock, badMock));
    long[] stats = provider.getProxy().proxy.getStats();
    Assert.assertTrue(stats.length == 1);
    Assert.assertEquals(1, stats[0]);
    Mockito.verify(badMock).getStats();
    Mockito.verify(goodMock).getStats();
}
Also used : NamenodeProtocols(org.apache.hadoop.hdfs.server.protocol.NamenodeProtocols) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IOException(java.io.IOException) 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