Search in sources :

Example 31 with Answer

use of org.mockito.stubbing.Answer in project flink by apache.

the class YarnApplicationMasterRunnerTest method testCreateTaskExecutorContext.

@Test
public void testCreateTaskExecutorContext() throws Exception {
    File root = folder.getRoot();
    File home = new File(root, "home");
    boolean created = home.mkdir();
    assertTrue(created);
    Answer<?> getDefault = new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            return invocationOnMock.getArguments()[1];
        }
    };
    Configuration flinkConf = new Configuration();
    YarnConfiguration yarnConf = mock(YarnConfiguration.class);
    doAnswer(getDefault).when(yarnConf).get(anyString(), anyString());
    doAnswer(getDefault).when(yarnConf).getInt(anyString(), anyInt());
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            return new String[] { (String) invocationOnMock.getArguments()[1] };
        }
    }).when(yarnConf).getStrings(anyString(), Mockito.<String>anyVararg());
    Map<String, String> env = ImmutableMap.<String, String>builder().put(ENV_APP_ID, "foo").put(ENV_CLIENT_HOME_DIR, home.getAbsolutePath()).put(ENV_CLIENT_SHIP_FILES, "").put(ENV_FLINK_CLASSPATH, "").put(ENV_HADOOP_USER_NAME, "foo").put(FLINK_JAR_PATH, root.toURI().toString()).build();
    ContaineredTaskManagerParameters tmParams = mock(ContaineredTaskManagerParameters.class);
    Configuration taskManagerConf = new Configuration();
    String workingDirectory = root.getAbsolutePath();
    Class<?> taskManagerMainClass = YarnApplicationMasterRunnerTest.class;
    ContainerLaunchContext ctx = Utils.createTaskExecutorContext(flinkConf, yarnConf, env, tmParams, taskManagerConf, workingDirectory, taskManagerMainClass, LOG);
    assertEquals("file", ctx.getLocalResources().get("flink.jar").getResource().getScheme());
}
Also used : Configuration(org.apache.flink.configuration.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) ContaineredTaskManagerParameters(org.apache.flink.runtime.clusterframework.ContaineredTaskManagerParameters) Matchers.anyString(org.mockito.Matchers.anyString) ContainerLaunchContext(org.apache.hadoop.yarn.api.records.ContainerLaunchContext) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) InvocationOnMock(org.mockito.invocation.InvocationOnMock) File(java.io.File) Test(org.junit.Test)

Example 32 with Answer

use of org.mockito.stubbing.Answer in project flink by apache.

the class StreamTestSingleInputGate method setupInputChannels.

@SuppressWarnings("unchecked")
private void setupInputChannels() throws IOException, InterruptedException {
    for (int i = 0; i < numInputChannels; i++) {
        final int channelIndex = i;
        final RecordSerializer<SerializationDelegate<Object>> recordSerializer = new SpanningRecordSerializer<SerializationDelegate<Object>>();
        final SerializationDelegate<Object> delegate = (SerializationDelegate<Object>) (SerializationDelegate<?>) new SerializationDelegate<StreamElement>(new StreamElementSerializer<T>(serializer));
        inputQueues[channelIndex] = new ConcurrentLinkedQueue<InputValue<Object>>();
        inputChannels[channelIndex] = new TestInputChannel(inputGate, i);
        final Answer<BufferAndAvailability> answer = new Answer<BufferAndAvailability>() {

            @Override
            public BufferAndAvailability answer(InvocationOnMock invocationOnMock) throws Throwable {
                InputValue<Object> input = inputQueues[channelIndex].poll();
                if (input != null && input.isStreamEnd()) {
                    when(inputChannels[channelIndex].getInputChannel().isReleased()).thenReturn(true);
                    return new BufferAndAvailability(EventSerializer.toBuffer(EndOfPartitionEvent.INSTANCE), false);
                } else if (input != null && input.isStreamRecord()) {
                    Object inputElement = input.getStreamRecord();
                    final Buffer buffer = new Buffer(MemorySegmentFactory.allocateUnpooledSegment(bufferSize), mock(BufferRecycler.class));
                    recordSerializer.setNextBuffer(buffer);
                    delegate.setInstance(inputElement);
                    recordSerializer.addRecord(delegate);
                    // Call getCurrentBuffer to ensure size is set
                    return new BufferAndAvailability(recordSerializer.getCurrentBuffer(), false);
                } else if (input != null && input.isEvent()) {
                    AbstractEvent event = input.getEvent();
                    return new BufferAndAvailability(EventSerializer.toBuffer(event), false);
                } else {
                    synchronized (inputQueues[channelIndex]) {
                        inputQueues[channelIndex].wait();
                        return answer(invocationOnMock);
                    }
                }
            }
        };
        when(inputChannels[channelIndex].getInputChannel().getNextBuffer()).thenAnswer(answer);
        inputGate.setInputChannel(new IntermediateResultPartitionID(), inputChannels[channelIndex].getInputChannel());
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) SerializationDelegate(org.apache.flink.runtime.plugable.SerializationDelegate) SpanningRecordSerializer(org.apache.flink.runtime.io.network.api.serialization.SpanningRecordSerializer) AbstractEvent(org.apache.flink.runtime.event.AbstractEvent) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BufferAndAvailability(org.apache.flink.runtime.io.network.partition.consumer.InputChannel.BufferAndAvailability) IntermediateResultPartitionID(org.apache.flink.runtime.jobgraph.IntermediateResultPartitionID)

Example 33 with Answer

use of org.mockito.stubbing.Answer in project camel by apache.

the class CouchbaseProducerTest method testTimeOutRetryThenSuccess.

@Test
public void testTimeOutRetryThenSuccess() throws Exception {
    OperationFuture of = mock(OperationFuture.class);
    when(of.get()).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            throw new RuntimeException("Timed out waiting for operation");
        }
    }).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            return true;
        }
    });
    when(client.set(org.mockito.Matchers.anyString(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyObject(), org.mockito.Matchers.any(PersistTo.class), org.mockito.Matchers.any(ReplicateTo.class))).thenReturn(of);
    when(endpoint.getId()).thenReturn("123");
    when(endpoint.getOperation()).thenReturn("CCB_PUT");
    when(exchange.getOut()).thenReturn(msg);
    producer.process(exchange);
    verify(of, times(2)).get();
    verify(msg).setBody(true);
}
Also used : Answer(org.mockito.stubbing.Answer) ReplicateTo(net.spy.memcached.ReplicateTo) PersistTo(net.spy.memcached.PersistTo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OperationFuture(net.spy.memcached.internal.OperationFuture) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Test(org.junit.Test)

Example 34 with Answer

use of org.mockito.stubbing.Answer in project camel by apache.

the class CouchbaseProducerTest method testTimeOutRetryTwiceThenSuccess.

@Test
public void testTimeOutRetryTwiceThenSuccess() throws Exception {
    OperationFuture of = mock(OperationFuture.class);
    when(of.get()).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            throw new RuntimeException("Timed out waiting for operation");
        }
    }).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            throw new RuntimeException("Timed out waiting for operation");
        }
    }).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            return true;
        }
    });
    when(client.set(org.mockito.Matchers.anyString(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyObject(), org.mockito.Matchers.any(PersistTo.class), org.mockito.Matchers.any(ReplicateTo.class))).thenReturn(of);
    when(endpoint.getId()).thenReturn("123");
    when(endpoint.getOperation()).thenReturn("CCB_PUT");
    when(exchange.getOut()).thenReturn(msg);
    producer.process(exchange);
    verify(of, times(3)).get();
    verify(msg).setBody(true);
}
Also used : Answer(org.mockito.stubbing.Answer) ReplicateTo(net.spy.memcached.ReplicateTo) PersistTo(net.spy.memcached.PersistTo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OperationFuture(net.spy.memcached.internal.OperationFuture) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Test(org.junit.Test)

Example 35 with Answer

use of org.mockito.stubbing.Answer in project byte-buddy by raphw.

the class AgentBuilderDefaultTest method testExecutingTransformerDoesNotRecurse.

@Test
@SuppressWarnings("unchecked")
public void testExecutingTransformerDoesNotRecurse() throws Exception {
    final AgentBuilder.Default.ExecutingTransformer executingTransformer = new AgentBuilder.Default.ExecutingTransformer(byteBuddy, listener, poolStrategy, typeStrategy, locationStrategy, mock(AgentBuilder.Default.NativeMethodStrategy.class), initializationStrategy, mock(AgentBuilder.Default.BootstrapInjectionStrategy.class), AgentBuilder.LambdaInstrumentationStrategy.DISABLED, AgentBuilder.DescriptionStrategy.Default.HYBRID, mock(AgentBuilder.FallbackStrategy.class), mock(AgentBuilder.InstallationListener.class), mock(AgentBuilder.RawMatcher.class), mock(AgentBuilder.Default.Transformation.class), new AgentBuilder.Default.CircularityLock.Default());
    final ClassLoader classLoader = mock(ClassLoader.class);
    final ProtectionDomain protectionDomain = mock(ProtectionDomain.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertThat(executingTransformer.transform(classLoader, FOO, Object.class, protectionDomain, new byte[0]), nullValue(byte[].class));
            return null;
        }
    }).when(listener).onComplete(FOO, classLoader, JavaModule.UNSUPPORTED, true);
    assertThat(executingTransformer.transform(classLoader, FOO, Object.class, protectionDomain, new byte[0]), nullValue(byte[].class));
    verify(listener).onComplete(FOO, classLoader, JavaModule.UNSUPPORTED, true);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Aggregations

Answer (org.mockito.stubbing.Answer)308 InvocationOnMock (org.mockito.invocation.InvocationOnMock)289 Test (org.junit.Test)180 Mockito.doAnswer (org.mockito.Mockito.doAnswer)114 Before (org.junit.Before)47 Matchers.anyString (org.mockito.Matchers.anyString)42 HashMap (java.util.HashMap)36 ArrayList (java.util.ArrayList)35 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)29 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)24 IOException (java.io.IOException)23 Context (android.content.Context)20 File (java.io.File)19 HashSet (java.util.HashSet)17 List (java.util.List)16 Map (java.util.Map)13 Test (org.testng.annotations.Test)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 Handler (android.os.Handler)11