+ add adapter test plus some tests
+ added mockito to build
This commit is contained in:
Costin Leau
2011-08-19 12:15:32 +03:00
parent 7e3525bca3
commit 2d0d148bc4
4 changed files with 223 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
#
#Wed Aug 17 18:25:39 EEST 2011
#Fri Aug 19 11:36:57 EEST 2011
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve

View File

@@ -87,6 +87,7 @@ dependencies {
// Testing
testCompile "junit:junit:$junitVersion"
testCompile "org.mockito:mockito-core:$mockitoVersion"
testCompile "org.springframework:spring-test:$springVersion"
testCompile("javax.annotation:jsr250-api:1.0") { optional = true }
}

View File

@@ -99,45 +99,39 @@ public class QueryListenerAdapter implements QueryListener {
if (parameterTypes.length > 0) {
for (Class<?> paramType : parameterTypes) {
boolean typeFound = false;
if (Object.class.equals(paramType)) {
objects++;
if (objects > 2) {
return false;
}
typeFound = true;
}
else if (Operation.class.equals(paramType)) {
operations++;
if (operations > 2) {
return false;
}
typeFound = true;
}
else if (CqEvent.class.equals(paramType)) {
typeFound = true;
}
else if (Throwable.class.equals(paramType)) {
typeFound = true;
}
else if (byte[].class.equals(paramType)) {
typeFound = true;
}
else if (CqQuery.class.equals(paramType)) {
typeFound = true;
}
else if (!typeFound) {
else {
return false;
}
}
return true;
}
}
return false;
}
});
Assert.isTrue(methods.isEmpty(), "Cannot find a suitable methods named [" + methodName
Assert.isTrue(!methods.isEmpty(), "Cannot find a suitable methods named [" + methodName
+ "] - is the method public and has the proper arguments?");
}
@@ -182,9 +176,9 @@ public class QueryListenerAdapter implements QueryListener {
/**
* Out-of-the-box value for the default listener method: "handleMessage".
* Out-of-the-box value for the default listener method: "handleEvent".
*/
public static final String ORIGINAL_DEFAULT_LISTENER_METHOD = "handleMessage";
public static final String ORIGINAL_DEFAULT_LISTENER_METHOD = "handleEvent";
/** Logger available to subclasses */
@@ -240,7 +234,7 @@ public class QueryListenerAdapter implements QueryListener {
/**
* Specify the name of the default listener method to delegate to,
* for the case where no specific listener method has been determined.
* Out-of-the-box value is {@link #ORIGINAL_DEFAULT_LISTENER_METHOD "handleMessage"}.
* Out-of-the-box value is {@link #ORIGINAL_DEFAULT_LISTENER_METHOD "handleEvent}.
* @see #getListenerMethodName
*/
public void setDefaultListenerMethod(String defaultListenerMethod) {

View File

@@ -0,0 +1,215 @@
/*
* 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.gemfire.listener.adapter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.gemfire.listener.QueryListener;
import com.gemstone.gemfire.cache.Operation;
import com.gemstone.gemfire.cache.query.CqEvent;
import com.gemstone.gemfire.cache.query.CqQuery;
import com.gemstone.gemfire.cache.query.internal.CqQueryImpl;
/**
* @author Costin Leau
*/
public class QueryListenerAdapterTest {
private QueryListenerAdapter adapter;
@Before
public void setUp() {
adapter = new QueryListenerAdapter();
}
CqEvent event() {
CqEvent event = new CqEvent() {
final CqQuery cq = new CqQueryImpl();
final byte[] ba = new byte[0];
final Object key = new Object();
final Object value = new Object();
final Exception ex = new Exception();
public Operation getBaseOperation() {
return Operation.CACHE_CLOSE;
}
public CqQuery getCq() {
return cq;
}
public byte[] getDeltaValue() {
return ba;
}
public Object getKey() {
return key;
}
public Object getNewValue() {
return value;
}
public Operation getQueryOperation() {
return Operation.CACHE_CREATE;
}
public Throwable getThrowable() {
return ex;
}
};
return event;
}
public static interface Delegate {
void handleEvent(CqEvent event);
void handleQuery(CqQuery query);
void handleOperation(Operation op);
void handleArray(byte[] ba);
void handleKey(Object key);
void handleKV(Object k, Object v);
void handleEx(Throwable th);
void handleOps(Operation base, Operation query);
void handleAll(CqEvent event, CqQuery query, byte[] ba, Object key, Operation op, Throwable th, Operation qOp, Object v);
void handleInvalid(Object o1, Object o2, Object o3);
}
@Test
public void testThatWhenNoDelegateIsSuppliedTheDelegateIsAssumedToBeTheListenerAdapterItself() throws Exception {
assertSame(adapter, adapter.getDelegate());
}
@Test
public void testThatTheDefaultHandlingMethodNameIsTheConstantDefault() throws Exception {
assertEquals(QueryListenerAdapter.ORIGINAL_DEFAULT_LISTENER_METHOD, adapter.getDefaultListenerMethod());
}
@Test
public void testAdapterWithListenerAndDefaultMessage() throws Exception {
QueryListener mock = mock(QueryListener.class);
QueryListenerAdapter adapter = new QueryListenerAdapter(mock);
CqEvent event = event();
adapter.onEvent(event);
verify(mock).onEvent(event);
}
@Test
public void testHandleEvent() throws Exception {
Delegate mock = mock(Delegate.class);
QueryListenerAdapter adapter = new QueryListenerAdapter(mock);
CqEvent event = event();
adapter.onEvent(event);
verify(mock).handleEvent(event);
}
@Test
public void testHandleArray() throws Exception {
Delegate mock = mock(Delegate.class);
QueryListenerAdapter adapter = new QueryListenerAdapter(mock);
adapter.setDefaultListenerMethod("handleArray");
CqEvent event = event();
adapter.onEvent(event);
verify(mock).handleArray(event.getDeltaValue());
}
@Test
public void testHandleKey() throws Exception {
Delegate mock = mock(Delegate.class);
QueryListenerAdapter adapter = new QueryListenerAdapter(mock);
adapter.setDefaultListenerMethod("handleKey");
CqEvent event = event();
adapter.onEvent(event);
verify(mock).handleKey(event.getKey());
}
@Test
public void testHandleKV() throws Exception {
Delegate mock = mock(Delegate.class);
QueryListenerAdapter adapter = new QueryListenerAdapter(mock);
adapter.setDefaultListenerMethod("handleKV");
CqEvent event = event();
adapter.onEvent(event);
verify(mock).handleKV(event.getKey(), event.getNewValue());
}
@Test
public void testHandleEx() throws Exception {
Delegate mock = mock(Delegate.class);
QueryListenerAdapter adapter = new QueryListenerAdapter(mock);
adapter.setDefaultListenerMethod("handleEx");
CqEvent event = event();
adapter.onEvent(event);
verify(mock).handleEx(event.getThrowable());
}
@Test
public void testHandleOps() throws Exception {
Delegate mock = mock(Delegate.class);
QueryListenerAdapter adapter = new QueryListenerAdapter(mock);
adapter.setDefaultListenerMethod("handleOps");
CqEvent event = event();
adapter.onEvent(event);
verify(mock).handleOps(event.getBaseOperation(), event.getQueryOperation());
}
@Test
public void testHandleAll() throws Exception {
Delegate mock = mock(Delegate.class);
QueryListenerAdapter adapter = new QueryListenerAdapter(mock);
adapter.setDefaultListenerMethod("handleAll");
CqEvent event = event();
adapter.onEvent(event);
verify(mock).handleAll(event, event.getCq(), event.getDeltaValue(), event.getKey(), event.getBaseOperation(),
event.getThrowable(), event.getQueryOperation(), event.getNewValue());
}
@Test
public void testInvalid() throws Exception {
Delegate mock = mock(Delegate.class);
QueryListenerAdapter adapter = new QueryListenerAdapter(mock);
adapter.setDefaultListenerMethod("handleInvalid");
adapter.onEvent(event());
doThrow(new IllegalArgumentException()).when(mock);
}
}