This repository has been archived by the owner on Feb 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from SvenBunge/delegationTestHelper
test method delegation of TraceeHttpClientDecorator
- Loading branch information
Showing
6 changed files
with
290 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
binding/jms/src/test/java/io/tracee/jms/out/TestMessageDelegation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.tracee.jms.out; | ||
|
||
import io.tracee.DelegationTestUtil; | ||
import org.junit.Test; | ||
|
||
import javax.jms.MessageProducer; | ||
import javax.jms.QueueSender; | ||
import javax.jms.TopicPublisher; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
public class TestMessageDelegation { | ||
|
||
@Test | ||
public void delegateTraceeMessageProducerToMessageProducer() { | ||
final MessageProducer messageProducer = mock(MessageProducer.class); | ||
DelegationTestUtil.assertDelegationToSpy(messageProducer).by(new TraceeMessageProducer(messageProducer)).verify(); | ||
} | ||
|
||
@Test | ||
public void delegateTraceeMessageProducerToMessageQueueSender() { | ||
final QueueSender queueSender = mock(QueueSender.class); | ||
final TraceeMessageProducer traceeMessageProducer = new TraceeMessageProducer(queueSender); | ||
DelegationTestUtil.assertDelegationToSpy(queueSender).by(new TraceeQueueSender(traceeMessageProducer, queueSender)).verify(); | ||
} | ||
|
||
@Test | ||
public void delegateTraceeTopicPublisherToTopicPublisher() { | ||
final TopicPublisher topicPublisher = mock(TopicPublisher.class); | ||
final TraceeTopicPublisher unit = new TraceeTopicPublisher(new TraceeMessageProducer(topicPublisher), topicPublisher); | ||
DelegationTestUtil.assertDelegationToSpy(topicPublisher).by(unit).verify(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
testhelper/src/main/java/io/tracee/DelegationTestUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package io.tracee; | ||
|
||
import org.mockito.Mockito; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
public final class DelegationTestUtil { | ||
|
||
private static final Set<String> BLACKLIST_METHOD = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList( | ||
"class$", "finalize", "equals", "hashCode", "toString", "clone", "newInstance" | ||
))); | ||
|
||
public static <I, W> Delegation<I, W> assertDelegationToSpy(I innerObj) { | ||
return new Delegation<I, W>(innerObj); | ||
} | ||
|
||
public static class Delegation<I, W> { | ||
private final I innerObj; | ||
private W wrapperObj; | ||
private final Set<String> ignoreMethods = new HashSet<String>(); | ||
|
||
Delegation(I innerObj) { | ||
this.innerObj = innerObj; | ||
} | ||
|
||
public Delegation<I, W> by(final W wrapperObj) { | ||
this.wrapperObj = wrapperObj; | ||
return this; | ||
} | ||
|
||
public Delegation<I, W> ignore(String methodName) { | ||
ignoreMethods.add(methodName); | ||
return this; | ||
} | ||
|
||
public void verify() { | ||
if(!Mockito.mockingDetails(innerObj).isMock()) { | ||
throw new IllegalStateException("Inner object is no Mockito mock!"); | ||
} | ||
if(Mockito.mockingDetails(wrapperObj).isMock()) { | ||
throw new IllegalStateException("Wrapper objecgt should be real class with mocked inner object inside"); | ||
} | ||
|
||
String errorMsg = ""; | ||
try { | ||
final Method[] wrapperMethods = wrapperObj.getClass().getDeclaredMethods(); | ||
final Map<String, Method> innerMethods = new HashMap<String, Method>(); | ||
for (Method innerMethod : innerObj.getClass().getDeclaredMethods()) { | ||
if (Modifier.isPublic(innerMethod.getModifiers())) { | ||
innerMethods.put(innerMethod.getName() + " :: " + paramsToStr(innerMethod.getParameterTypes()), innerMethod); | ||
} | ||
} | ||
|
||
for (Method wrapperMethod : wrapperMethods) { | ||
if (innerMethods.containsKey(wrapperMethod.getName() + " :: " + paramsToStr(wrapperMethod.getParameterTypes())) | ||
&& !BLACKLIST_METHOD.contains(wrapperMethod.getName()) | ||
&& !ignoreMethods.contains(wrapperMethod.getName())) { | ||
errorMsg = "Method not delegated: " + wrapperMethod.getName(); | ||
|
||
final Object[] arguments = generateMockedParams(wrapperMethod); | ||
|
||
wrapperMethod.invoke(wrapperObj, arguments); | ||
innerMethods.get(wrapperMethod.getName() + " :: " + paramsToStr(wrapperMethod.getParameterTypes())).invoke(Mockito.verify(innerObj), arguments); | ||
} | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(errorMsg + "\n" + e.getMessage(), e); | ||
} | ||
} | ||
} | ||
|
||
private static Object[] generateMockedParams(Method wrapperMethod) { | ||
final Class<?>[] parameterTypes = wrapperMethod.getParameterTypes(); | ||
final List<Object> arguments = new ArrayList<Object>(); | ||
for (Class<?> parameterType : parameterTypes) { | ||
if ("boolean".equals(parameterType.getName())) { | ||
arguments.add(Boolean.FALSE); | ||
} else if ("int".equals(parameterType.getName())) { | ||
arguments.add(0); | ||
} else if ("long".equals(parameterType.getName())) { | ||
arguments.add(0L); | ||
} else if (parameterType == String.class) { | ||
arguments.add(""); | ||
} else { | ||
arguments.add(mock(parameterType)); | ||
} | ||
} | ||
return arguments.toArray(new Object[arguments.size()]); | ||
} | ||
|
||
private static String paramsToStr(Class<?>[] parameterTypes) { | ||
final StringBuilder sb = new StringBuilder(); | ||
for (int i = 0, parameterTypesLength = parameterTypes.length; i < parameterTypesLength; i++) { | ||
Class<?> type = parameterTypes[i]; | ||
sb.append(type.getName()); | ||
if (i < (parameterTypesLength - 1)) { | ||
sb.append(", "); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private DelegationTestUtil() { | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
testhelper/src/test/java/io/tracee/DelegationTestUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package io.tracee; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.mockito.Mockito; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class DelegationTestUtilTest { | ||
|
||
@Rule | ||
public ExpectedException exeptionRule = ExpectedException.none(); | ||
|
||
@Test | ||
public void shouldFindAllDelegations() { | ||
final DelegationClass delegate = Mockito.spy(new DelegationClass()); | ||
final RightWrapper rightWrapper = new RightWrapper(delegate); | ||
DelegationTestUtil.assertDelegationToSpy(delegate).by(rightWrapper).verify(); | ||
assertThat(delegate.getInvotionCount(), is(3)); | ||
} | ||
|
||
@Test | ||
public void shouldFindErrorsInDelegate() { | ||
exeptionRule.expect(RuntimeException.class); | ||
exeptionRule.expectMessage("Method not delegated: getInteger"); | ||
|
||
final DelegationClass delegate = Mockito.spy(new DelegationClass()); | ||
final WrongWrapper rightWrapper = new WrongWrapper(delegate); | ||
DelegationTestUtil.assertDelegationToSpy(delegate).by(rightWrapper).verify(); | ||
} | ||
|
||
public class DelegationClass { | ||
|
||
private int invocationCount = 0; | ||
|
||
public int getInvotionCount() { | ||
return invocationCount; | ||
} | ||
|
||
public void setString(String str) { | ||
invocationCount++; | ||
} | ||
|
||
public void setInteger(int i) { | ||
invocationCount++; | ||
} | ||
|
||
public int getInteger() { | ||
invocationCount++; | ||
return 1; | ||
} | ||
} | ||
|
||
public class WrongWrapper extends DelegationClass { | ||
|
||
private final DelegationClass delegate; | ||
|
||
public WrongWrapper(DelegationClass delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void setString(String str) { | ||
delegate.setString(str); | ||
} | ||
|
||
@Override | ||
public int getInteger() { | ||
return -1; | ||
} | ||
|
||
@Override | ||
public void setInteger(int i) { | ||
delegate.setInteger(i); | ||
} | ||
} | ||
|
||
public class RightWrapper extends DelegationClass { | ||
|
||
private final DelegationClass delegate; | ||
|
||
public RightWrapper(DelegationClass delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void setString(String str) { | ||
delegate.setString(str); | ||
} | ||
|
||
@Override | ||
public int getInteger() { | ||
return delegate.getInteger(); | ||
} | ||
|
||
@Override | ||
public void setInteger(int i) { | ||
delegate.setInteger(i); | ||
} | ||
} | ||
} |