From ace71dd2a47f8a65e59020ef296df2df8f9340f0 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Fri, 14 Jan 2011 18:55:13 +0200 Subject: [PATCH] DATAKV-22 + add adapter unit test --- .../adapter/MessageListenerAdapter.java | 39 ++++---- .../listener/adapter/MessageListenerTest.java | 99 +++++++++++++++++++ 2 files changed, 121 insertions(+), 17 deletions(-) create mode 100644 spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/listener/adapter/MessageListenerTest.java diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/listener/adapter/MessageListenerAdapter.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/listener/adapter/MessageListenerAdapter.java index ba3dfe171..9f6161fb6 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/listener/adapter/MessageListenerAdapter.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/listener/adapter/MessageListenerAdapter.java @@ -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); + } } /** diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/listener/adapter/MessageListenerTest.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/listener/adapter/MessageListenerTest.java new file mode 100644 index 000000000..05c46bbec --- /dev/null +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/listener/adapter/MessageListenerTest.java @@ -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); + } +} \ No newline at end of file