Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #70 from SvenBunge/delegationTestHelper
Browse files Browse the repository at this point in the history
test method delegation of TraceeHttpClientDecorator
  • Loading branch information
danielwegener committed Dec 29, 2014
2 parents 5b50240 + c324de8 commit 044ca3d
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.tracee.outbound.httpclient;


import io.tracee.DelegationTestUtil;
import io.tracee.SimpleTraceeBackend;
import io.tracee.TraceeBackend;
import io.tracee.TraceeConstants;
Expand All @@ -15,6 +16,7 @@
import java.util.Arrays;
import java.util.Map;

import static io.tracee.DelegationTestUtil.assertDelegationToSpy;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyMapOf;
import static org.mockito.Matchers.anyString;
Expand Down Expand Up @@ -58,4 +60,12 @@ public void testContextParsedFromResponse() throws IOException {
verify(transportSerializationMock).parse(eq(Arrays.asList("foo=bar")));
verify(backendMock).putAll(Mockito.anyMapOf(String.class, String.class));
}

@Test
public void shouldDelegateAllPublicMethods() {
final HttpClient client = mock(HttpClient.class);
final HttpClient wrappedClient = TraceeHttpClientDecorator.wrap(client);

assertDelegationToSpy(client).by(wrappedClient).ignore("executeMethod").verify();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import static io.tracee.configuration.TraceeFilterConfiguration.Channel.AsyncDispatch;

public final class TraceeMessageProducer implements MessageProducer {
public class TraceeMessageProducer implements MessageProducer {


private final MessageProducer delegate;
Expand Down
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();
}
}
27 changes: 26 additions & 1 deletion testhelper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
<artifactId>tracee-api</artifactId>
<version>0.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${hamcrest.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -30,7 +42,20 @@
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
116 changes: 116 additions & 0 deletions testhelper/src/main/java/io/tracee/DelegationTestUtil.java
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 testhelper/src/test/java/io/tracee/DelegationTestUtilTest.java
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);
}
}
}

0 comments on commit 044ca3d

Please sign in to comment.