DATAKV-22
+ add adapter unit test
This commit is contained in:
@@ -166,26 +166,31 @@ public class MessageListenerAdapter implements MessageListener {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void onMessage(Message message, byte[] pattern) {
|
||||
// Check whether the delegate is a MessageListener impl itself.
|
||||
// In that case, the adapter will simply act as a pass-through.
|
||||
if (delegate != this) {
|
||||
if (delegate instanceof MessageListener) {
|
||||
((MessageListener) delegate).onMessage(message, pattern);
|
||||
try {
|
||||
|
||||
// Check whether the delegate is a MessageListener impl itself.
|
||||
// In that case, the adapter will simply act as a pass-through.
|
||||
if (delegate != this) {
|
||||
if (delegate instanceof MessageListener) {
|
||||
((MessageListener) delegate).onMessage(message, pattern);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Regular case: find a handler method reflectively.
|
||||
Object convertedMessage = extractMessage(message);
|
||||
String methodName = getListenerMethodName(message, convertedMessage);
|
||||
if (methodName == null) {
|
||||
throw new InvalidDataAccessApiUsageException("No default listener method specified: "
|
||||
+ "Either specify a non-null value for the 'defaultListenerMethod' property or "
|
||||
+ "override the 'getListenerMethodName' method.");
|
||||
}
|
||||
// Regular case: find a handler method reflectively.
|
||||
Object convertedMessage = extractMessage(message);
|
||||
String methodName = getListenerMethodName(message, convertedMessage);
|
||||
if (methodName == null) {
|
||||
throw new InvalidDataAccessApiUsageException("No default listener method specified: "
|
||||
+ "Either specify a non-null value for the 'defaultListenerMethod' property or "
|
||||
+ "override the 'getListenerMethodName' method.");
|
||||
}
|
||||
|
||||
// Invoke the handler method with appropriate arguments.
|
||||
Object[] listenerArguments = buildListenerArguments(convertedMessage);
|
||||
invokeListenerMethod(methodName, listenerArguments);
|
||||
// Invoke the handler method with appropriate arguments.
|
||||
Object[] listenerArguments = buildListenerArguments(convertedMessage);
|
||||
invokeListenerMethod(methodName, listenerArguments);
|
||||
} catch (Throwable th) {
|
||||
handleListenerException(th);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.keyvalue.redis.listener.adapter;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.data.keyvalue.redis.connection.DefaultMessage;
|
||||
import org.springframework.data.keyvalue.redis.connection.Message;
|
||||
import org.springframework.data.keyvalue.redis.connection.MessageListener;
|
||||
import org.springframework.data.keyvalue.redis.serializer.JdkSerializationRedisSerializer;
|
||||
import org.springframework.data.keyvalue.redis.serializer.RedisSerializer;
|
||||
|
||||
/**
|
||||
* Unit test for MessageListenerAdapter.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class MessageListenerTest {
|
||||
|
||||
private static final RedisSerializer serializer = new JdkSerializationRedisSerializer();
|
||||
private static final String CHANNEL = "some::test:";
|
||||
private static final byte[] RAW_CHANNEL = serializer.serialize(CHANNEL);
|
||||
private static final String PAYLOAD = "do re mi";
|
||||
private static final byte[] RAW_PAYLOAD = serializer.serialize(PAYLOAD);
|
||||
private static final Message STRING_MSG = new DefaultMessage(RAW_PAYLOAD, RAW_CHANNEL);
|
||||
|
||||
private MessageListenerAdapter adapter;
|
||||
|
||||
interface Delegate {
|
||||
void handleMessage(String argument);
|
||||
|
||||
void customMethod(String arg);
|
||||
}
|
||||
|
||||
@Mock
|
||||
private Delegate target;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
this.adapter = new MessageListenerAdapter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThatWhenNoDelegateIsSuppliedTheDelegateIsAssumedToBeTheMessageListenerAdapterItself()
|
||||
throws Exception {
|
||||
assertSame(adapter, adapter.getDelegate());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThatTheDefaultMessageHandlingMethodNameIsTheConstantDefault() throws Exception {
|
||||
assertEquals(MessageListenerAdapter.ORIGINAL_DEFAULT_LISTENER_METHOD, adapter.getDefaultListenerMethod());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdapterWithListenerAndDefaultMessage() throws Exception {
|
||||
MessageListener mock = mock(MessageListener.class);
|
||||
|
||||
MessageListenerAdapter adapter = new MessageListenerAdapter(mock);
|
||||
adapter.onMessage(STRING_MSG, null);
|
||||
|
||||
verify(mock).onMessage(STRING_MSG, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRawMessage() throws Exception {
|
||||
MessageListenerAdapter adapter = new MessageListenerAdapter(target);
|
||||
adapter.onMessage(STRING_MSG, null);
|
||||
|
||||
verify(target).handleMessage(PAYLOAD);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomMethod() throws Exception {
|
||||
MessageListenerAdapter adapter = new MessageListenerAdapter(target);
|
||||
adapter.setDefaultListenerMethod("customMethod");
|
||||
adapter.onMessage(STRING_MSG, null);
|
||||
|
||||
verify(target).customMethod(PAYLOAD);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user