Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spring-data-mock-sample-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<spring-data-commons.version>1.12.1.RELEASE</spring-data-commons.version>
<spring-data-jpa.version>1.10.1.RELEASE</spring-data-jpa.version>
<persistence-api.version>1.0.2</persistence-api.version>
<spring-data-mock.version>1.1.4</spring-data-mock.version>
<spring-data-mock.version>1.1.5</spring-data-mock.version>
<testng.version>6.9.6</testng.version>
<hamcrest.version>1.3</hamcrest.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mock/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

<groupId>com.mmnaseri.utils</groupId>
<artifactId>spring-data-mock</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>

<name>Spring Data Mock</name>
<description>A framework for mocking Spring Data repositories</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
Expand Down Expand Up @@ -50,9 +51,12 @@ public SignatureDataOperationResolver(List<TypeMapping<?>> mappings) {
return null;
}

private static Method findMethod(Class<?> type, String name, Class<?>... parameterTypes) {
private Method findMethod(Class<?> type, String name, Class<?>... parameterTypes) {
log.debug("Attempting to look for the actual declaration of the method named '" + name + "' with parameter types " + Arrays.toString(parameterTypes) + " on the child type " + type);
Class<?> searchType = type;

List<Method> matchingMethods = new LinkedList<>();

while (searchType != null) {
log.trace("Looking at type " + type + " for method " + name);
final Method[] methods = searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods();
Expand All @@ -61,19 +65,56 @@ private static Method findMethod(Class<?> type, String name, Class<?>... paramet
boolean matches = true;
for (int i = 0; i < parameterTypes.length; i++) {
final Class<?> parameterType = parameterTypes[i];
if (!PropertyUtils.getTypeOf(method.getParameterTypes()[i]).isAssignableFrom(PropertyUtils.getTypeOf(parameterType))) {
if (!doParamsMatchByAssignability(method.getParameterTypes()[i], PropertyUtils.getTypeOf(parameterType))) {
matches = false;
break;
}
}
if (matches) {
return method;
matchingMethods.add(method);
}
}
}
searchType = searchType.getSuperclass();
}

if (matchingMethods.size() == 0) {
return null;
} else if (matchingMethods.size() == 1) {
return matchingMethods.get(0);
} else {
Method method = exactlyMatchingMethod(matchingMethods, parameterTypes);
if (method == null) {
return matchingMethods.get(matchingMethods.size() - 1);
} else {
return method;
}
}
}

private Method exactlyMatchingMethod(List<Method> methods, Class<?>[] parameterTypes) {
for (Method method : methods) {
boolean matches = true;
for (int i = 0; i < parameterTypes.length; i++) {
final Class<?> parameterType = parameterTypes[i];
if (!(doParamsMatchExactly(method.getParameterTypes()[i], PropertyUtils.getTypeOf(parameterType)))) {
matches = false;
break;
}
}
if (matches) {
return method;
}
}
return null;
}

protected boolean doParamsMatchByAssignability(Class<?> thisParam, Class<?> thatParam) {
return PropertyUtils.getTypeOf(thisParam).isAssignableFrom(thatParam);
}


protected boolean doParamsMatchExactly(Class<?> thisParam, Class<?> thatParam) {
return thisParam.getCanonicalName().equals(thatParam.getCanonicalName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -30,6 +32,31 @@ public void setUp() throws Exception {
resolver = new SignatureDataOperationResolver(mappings);
}

@Test
public void testWeResolveTheCorrectMethod_whenMethodWithObjectParam_comesFirstInMethodList() throws Exception {
Method method = ProxiedClass.class.getMethod("somethingToAnObject", Iterable.class);
final DataStoreOperation<?, ?, ?> operation = resolver.resolve(method);

assertThat(operation, is(notNullValue()));
assertThat(operation, is(instanceOf(MethodInvocationDataStoreOperation.class)));
final MethodInvocationDataStoreOperation invocationOperation = (MethodInvocationDataStoreOperation) operation;
assertThat(invocationOperation.getInstance(), is(instanceOf(SuperInterface.class)));
assertThat(invocationOperation.getMethod(), is(SuperInterface.class.getMethod("somethingToAnObject", Iterable.class)));
}


@Test
public void testWeResolveTheCorrectMethod_whenMethodWithObjectParam_comesFirstInMethodList_andNotAnExactMatch() throws Exception {
Method method = ProxiedClass.class.getMethod("somethingToAnObject", Collection.class);
final DataStoreOperation<?, ?, ?> operation = resolver.resolve(method);

assertThat(operation, is(notNullValue()));
assertThat(operation, is(instanceOf(MethodInvocationDataStoreOperation.class)));
final MethodInvocationDataStoreOperation invocationOperation = (MethodInvocationDataStoreOperation) operation;
assertThat(invocationOperation.getInstance(), is(instanceOf(SuperInterface.class)));
assertThat(invocationOperation.getMethod(), is(SuperInterface.class.getMethod("somethingToAnObject", Iterable.class)));
}

@Test
public void testLookingForExactMatchViaInterface() throws Exception {
final DataStoreOperation<?, ?, ?> operation = resolver.resolve(ProxiedClass.class.getMethod("saySomething", String.class, Double.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mmnaseri.utils.spring.data.sample.usecases.proxy.resolvers;

import java.util.Collection;

/**
* @author Milad Naseri (mmnaseri@programmer.net)
* @since 1.0 (4/12/16, 6:36 PM)
Expand All @@ -16,4 +18,8 @@ public interface ProxiedClass {

void doSomething();

void somethingToAnObject(Iterable iterable);

void somethingToAnObject(Collection iterable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
*/
public interface SuperInterface {

void somethingToAnObject(Object object);

void somethingToAnObject(Iterable iterable);


void saySomething(CharSequence sequence, Double number);

void doSomething();


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
*/
public class SuperInterfaceImpl implements SuperInterface {

@Override
public void somethingToAnObject(Object object) {

}

@Override
public void somethingToAnObject(Iterable iterable) {

}

@Override
public void saySomething(CharSequence sequence, Double number) {

Expand All @@ -15,4 +25,5 @@ public void saySomething(CharSequence sequence, Double number) {
public void doSomething() {

}

}