Skip to content

Commit

Permalink
Simplified ExtraMethodExecutorData
Browse files Browse the repository at this point in the history
  • Loading branch information
daivanov committed Apr 14, 2015
1 parent be0d4b2 commit e9ab2c5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 59 deletions.
19 changes: 16 additions & 3 deletions src/main/java/uk/co/jemos/podam/api/AbstractClassInfoStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
package uk.co.jemos.podam.api;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;

import uk.co.jemos.podam.test.unit.issue86.ExtraMethodsPojo;

/**
* Default abstract implementation of a {@link ClassInfoStrategy}
* <p>
Expand Down Expand Up @@ -86,18 +89,28 @@ public AbstractClassInfoStrategy addExcludedField(
/**
* It adds an extra method to execute
* @param pojoClass The pojo class where to execute the method
* @param extraMethodExecutorData Data required to execute the method
* @param methodName name to be scheduled for execution
* @param methodArgs list of method arguments
* @return this object
* @throws SecurityException
* @throws NoSuchMethodException
*/
public AbstractClassInfoStrategy addExtraMethod(
Class<?> pojoClass, ExtraMethodExecutorData extraMethodExecutorData) {
Class<?> pojoClass, String methodName, Class<?> ... methodArgs)
throws NoSuchMethodException, SecurityException {

Method method =
ExtraMethodsPojo.class.getMethod(methodName, methodArgs);
ExtraMethodExecutorData methodExecutorData =
new ExtraMethodExecutorData(method);

Set<ExtraMethodExecutorData> methods = extraMethods.get(pojoClass);
if (methods == null) {
methods = new HashSet<ExtraMethodExecutorData>();
extraMethods.put(pojoClass, methods);
}

methods.add(extraMethodExecutorData);
methods.add(methodExecutorData);

return this;
}
Expand Down
47 changes: 22 additions & 25 deletions src/main/java/uk/co/jemos/podam/api/ExtraMethodExecutorData.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,66 @@
*/
public class ExtraMethodExecutorData implements Serializable {

private final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;

private final Method method;

private final Object target;

private final Object[] methodArgs;
private final Class<?>[] argTypes;


/**
* Full constructor.
* @param method The method name
* @param target The instance on which the method will be invoked
* @param args The list of args that will be passed to the method
* @param argTypes The list of args that will be passed to the method
*/
public ExtraMethodExecutorData(Method method, Object target, Object[] args) {
public ExtraMethodExecutorData(Method method, Class<?> ... argTypes) {
this.method = method;
this.target = target;
this.methodArgs = args;
this.argTypes = argTypes;
}


public Method getMethod() {
return method;
}

public Object getTarget() {
return target;
}

public Object[] getMethodArgs() {
return methodArgs;
public Class<?>[] getMethodArgTypes() {
return argTypes;
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

ExtraMethodExecutorData that = (ExtraMethodExecutorData) o;

if (!getMethod().equals(that.getMethod())) return false;
if (!getTarget().equals(that.getTarget())) return false;
if (!getMethod().equals(that.getMethod())) {
return false;
}
// Probably incorrect - comparing Object[] arrays with Arrays.equals
return Arrays.equals(getMethodArgs(), that.getMethodArgs());

return Arrays.equals(getMethodArgTypes(), that.getMethodArgTypes());
}

@Override
public int hashCode() {

int result = getMethod().hashCode();
result = 31 * result + getTarget().hashCode();
result = 31 * result + Arrays.hashCode(getMethodArgs());
result = 31 * result + Arrays.hashCode(getMethodArgTypes());
return result;
}

@Override
public String toString() {

final StringBuilder sb = new StringBuilder("ExtraMethodExecutorData{");
sb.append("methodName='").append(method).append('\'');
sb.append(", target=").append(target);
sb.append(", methodArgs=").append(Arrays.toString(methodArgs));
sb.append(", methodArgTypes=").append(Arrays.toString(argTypes));
sb.append('}');
return sb.toString();
}
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/uk/co/jemos/podam/api/PodamFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1648,17 +1648,27 @@ private <T> T populatePojo(T pojo, Map<Class<?>, Integer> pojos,
}
}

//It executes any extra methods if any
// It executes any extra methods, if any
Set<ExtraMethodExecutorData> extraMethods = classInfoStrategy.getExtraMethods(pojoClass);
if (null != extraMethods) {
for (ExtraMethodExecutorData extraMethod : extraMethods) {

extraMethod.getMethod().invoke(pojo, extraMethod.getMethodArgs());
Class<?>[] argTypes = extraMethod.getMethod().getParameterTypes();
Type[] genericTypes = extraMethod.getMethod().getGenericParameterTypes();
Annotation[][] annotations = extraMethod.getMethod().getParameterAnnotations();

Object[] args = new Object[argTypes.length];
for (int i = 0; i < argTypes.length; i++) {

List<Annotation> paramAnnotations = Arrays.asList(annotations[i]);
args[i] = manufactureParameterValue(pojos, argTypes[i],
genericTypes[i], paramAnnotations, typeArgsMap,
genericTypeArgs);
}
extraMethod.getMethod().invoke(pojo, args);
}
}


return pojo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
* Created by tedonema on 12/04/2015.
*/
public class ExtraMethodsPojo implements Serializable {

private final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;

private static class Holder {
private String holdingString = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import org.junit.Test;
import uk.co.jemos.podam.api.*;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
* Created by tedonema on 12/04/2015.
*/
Expand All @@ -21,30 +17,11 @@ public class ExtraMethodsUnitTest {

private PodamFactory podam = new PodamFactoryImpl(dataProviderStrategy);

private ExtraMethodsPojo extraMethodsPojo = null;

@Before
public void setup() throws Exception {

//Using Podam to get an empty pojo
extraMethodsPojo = podam.manufacturePojo(ExtraMethodsPojo.class);
Assert.assertNotNull("The initial pojo cannot be null", extraMethodsPojo);

Set<ExtraMethodExecutorData> extraMethods = new HashSet<ExtraMethodExecutorData>();

Method setMyStringMethod = ExtraMethodsPojo.class.getMethod("setMyString", String.class);

ExtraMethodExecutorData setMyStringExecutorData =
new ExtraMethodExecutorData(setMyStringMethod, extraMethodsPojo, new String[] {"Hello World"});

Method setMyLongMethod = ExtraMethodsPojo.class.getMethod("setMyLong", Long.class);
ExtraMethodExecutorData setMyLongExecutorData =
new ExtraMethodExecutorData(setMyLongMethod, extraMethodsPojo, new Long[] {podam.manufacturePojo(Long.class)});

classInfoStrategy.addExtraMethod(ExtraMethodsPojo.class, setMyStringExecutorData);
classInfoStrategy.addExtraMethod(ExtraMethodsPojo.class, setMyLongExecutorData);


classInfoStrategy.addExtraMethod(ExtraMethodsPojo.class, "setMyString", String.class);
classInfoStrategy.addExtraMethod(ExtraMethodsPojo.class, "setMyLong", Long.class);
}

@Test
Expand All @@ -55,6 +32,5 @@ public void testExtraMethods() throws Exception {
Assert.assertNotNull("The long value cannot be zero", pojo.getMyLong());
Assert.assertNotNull("The string value cannot be null", pojo.getMyString());
Assert.assertTrue("The string value cannot be empty", pojo.getMyString().length() > 0);

}
}

0 comments on commit e9ab2c5

Please sign in to comment.