Java Code Examples for org.easymock.EasyMock.expect()

The following are Jave code examples for showing how to use expect() of the org.easymock.EasyMock class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: kafka-0.11.0.0-src-with-comment   File: WorkerSourceTaskTest.java   Source Code and License Vote up 6 votes
private void expectApplyTransformationChain(boolean anyTimes) {
    final Capture<SourceRecord> recordCapture = EasyMock.newCapture();
    IExpectationSetters<SourceRecord> convertKeyExpect = EasyMock.expect(transformationChain.apply(EasyMock.capture(recordCapture)));
    if (anyTimes)
        convertKeyExpect.andStubAnswer(new IAnswer<SourceRecord>() {
            @Override
            public SourceRecord answer() {
                return recordCapture.getValue();
            }
        });
    else
        convertKeyExpect.andAnswer(new IAnswer<SourceRecord>() {
            @Override
            public SourceRecord answer() {
                return recordCapture.getValue();
            }
        });
}
 
Example 2
Project: kafka-0.11.0.0-src-with-comment   File: WorkerSourceTaskTest.java   Source Code and License Vote up 6 votes
@SuppressWarnings("unchecked")
private void expectOffsetFlush(boolean succeed) throws Exception {
    EasyMock.expect(offsetWriter.beginFlush()).andReturn(true);
    Future<Void> flushFuture = PowerMock.createMock(Future.class);
    EasyMock.expect(offsetWriter.doFlush(EasyMock.anyObject(Callback.class))).andReturn(flushFuture);
    // Should throw for failure
    IExpectationSetters<Void> futureGetExpect = EasyMock.expect(
            flushFuture.get(EasyMock.anyLong(), EasyMock.anyObject(TimeUnit.class)));
    if (succeed) {
        sourceTask.commit();
        EasyMock.expectLastCall();
        futureGetExpect.andReturn(null);
    } else {
        futureGetExpect.andThrow(new TimeoutException());
        offsetWriter.cancelFlush();
        PowerMock.expectLastCall();
    }
}
 
Example 3
Project: kafka-0.11.0.0-src-with-comment   File: WorkerSourceTaskTest.java   Source Code and License Vote up 5 votes
private void expectConvertKeyValue(boolean anyTimes) {
    IExpectationSetters<byte[]> convertKeyExpect = EasyMock.expect(keyConverter.fromConnectData(TOPIC, KEY_SCHEMA, KEY));
    if (anyTimes)
        convertKeyExpect.andStubReturn(SERIALIZED_KEY);
    else
        convertKeyExpect.andReturn(SERIALIZED_KEY);
    IExpectationSetters<byte[]> convertValueExpect = EasyMock.expect(valueConverter.fromConnectData(TOPIC, RECORD_SCHEMA, RECORD));
    if (anyTimes)
        convertValueExpect.andStubReturn(SERIALIZED_RECORD);
    else
        convertValueExpect.andReturn(SERIALIZED_RECORD);
}
 
Example 4
Project: kafka-0.11.0.0-src-with-comment   File: WorkerSourceTaskTest.java   Source Code and License Vote up 4 votes
private Capture<ProducerRecord<byte[], byte[]>> expectSendRecord(boolean anyTimes, boolean isRetry, boolean succeed) throws InterruptedException {
    expectConvertKeyValue(anyTimes);
    expectApplyTransformationChain(anyTimes);

    Capture<ProducerRecord<byte[], byte[]>> sent = EasyMock.newCapture();

    // 1. Offset data is passed to the offset storage.
    if (!isRetry) {
        offsetWriter.offset(PARTITION, OFFSET);
        if (anyTimes)
            PowerMock.expectLastCall().anyTimes();
        else
            PowerMock.expectLastCall();
    }

    // 2. Converted data passed to the producer, which will need callbacks invoked for flush to work
    IExpectationSetters<Future<RecordMetadata>> expect = EasyMock.expect(
            producer.send(EasyMock.capture(sent),
                    EasyMock.capture(producerCallbacks)));
    IAnswer<Future<RecordMetadata>> expectResponse = new IAnswer<Future<RecordMetadata>>() {
        @Override
        public Future<RecordMetadata> answer() throws Throwable {
            synchronized (producerCallbacks) {
                for (org.apache.kafka.clients.producer.Callback cb : producerCallbacks.getValues()) {
                    cb.onCompletion(new RecordMetadata(new TopicPartition("foo", 0), 0, 0,
                                                       0L, 0L, 0, 0), null);
                }
                producerCallbacks.reset();
            }
            return sendFuture;
        }
    };
    if (anyTimes)
        expect.andStubAnswer(expectResponse);
    else
        expect.andAnswer(expectResponse);

    // 3. As a result of a successful producer send callback, we'll notify the source task of the record commit
    expectTaskCommitRecord(anyTimes, succeed);

    return sent;
}