Skip to content

Instantly share code, notes, and snippets.

@bmchild
Created July 11, 2012 15:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bmchild/3091277 to your computer and use it in GitHub Desktop.
Save bmchild/3091277 to your computer and use it in GitHub Desktop.
Use reflection to compare getters on two objects
/**
*
*/
package com.mypackage.utilities;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.builder.EqualsBuilder;
/**
* Class that utilizes reflection to help in testing
*
* @author bchild
*
*/
public class MyReflectionTestUtils {
private static final String NOT_EQUALS = "%s: [ %s ] != [ %s ]";
private static final String REFLECT_ERROR = "%s: ERROR -> %s";
/**
* Uses reflection to get a list of getters from <b>base</b> and invokes
* that method on <b>base</b> and <b>compareTo</b>. If the results of the
* getters !=, or the compareTo class doesn't have that getter, then that
* method name would be returned in the list
*
* @param base
* object to get the list of getters
* @param compareTo
* object that the list of getters is compared to
* @return list of methods that weren't equal
*/
public static List<String> compareGetters(Object base, Object compareTo, List<String> ignoreFields) {
List<String> notEquals = new ArrayList<String>();
for(Method method : getGetters(base.getClass(), ignoreFields)) {
try {
Method compareMethod = compareTo.getClass().getMethod(method.getName());
Object baseResult = method.invoke(base);
Object compareResult = compareMethod.invoke(compareTo);
if( !new EqualsBuilder().append(baseResult, compareResult).isEquals() ) {
notEquals.add(String.format(NOT_EQUALS, method.getName(), baseResult, compareResult));
}
} catch (NullPointerException e) {
throw e;
} catch (Exception e) {
notEquals.add(String.format(REFLECT_ERROR, method.getName(), e.getMessage()));
}
}
return notEquals;
}
/**
* finds the getters of a class
*
* @param clazz
* @return list of Method
*/
@SuppressWarnings("rawtypes")
public static List<Method> getGetters(Class clazz, List<String> ignoreFields) {
if(ignoreFields == null) {
ignoreFields = new ArrayList<String>();
}
ignoreFields.add("class");
List<Method> getters = new ArrayList<Method>();
// get getters
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (isGetter(method) && !listContainsString(ignoreFields, method.getName()) ) {
getters.add(method);
}
}
return getters;
}
/**
* Runs an upper case contains for each element of <b>list</b> for <b>string</b>
* i.e.: <code>item.toUpperCase().contains(string.toUpperCase())</code>
*
* @param list
* @param string
* @return true or false
*/
private static boolean listContainsString(List<String> list, String string) {
boolean contains = false;
for(String item: list) {
if(string.toUpperCase().contains(item.toUpperCase())) {
contains = true;
break;
}
}
return contains;
}
/**
* @param method
* @return true or false if the method is a getter
*/
public static boolean isGetter(Method method) {
if (!method.getName().startsWith("get")) {
return false;
}
if (method.getParameterTypes().length != 0) {
return false;
}
if (void.class.equals(method.getReturnType())) {
return false;
}
return true;
}
}
/**
*
*/
package com.mypacakge.utilities;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
/**
* @author bchild
*
*/
public class MyReflectionTestUtilsTest {
/**
* Test method for {@link com.mypackage.utilities.MyReflectionTestUtils#compareGetters(java.lang.Object, java.lang.Object, java.util.List)}.
*/
@Test
public void testCompareGetters() {
Test1 test1 = new Test1();
test1.setOne("one");
test1.setTwo("two");
test1.setThree("three");
Test2 test2 = new Test2();
test2.setOne("one");
test2.setTwo("two");
List<String> notEquals = MyReflectionTestUtils.compareGetters(test1, test2, new ArrayList<String>());
assertEquals(1, notEquals.size());
List<String> ignores = new ArrayList<String>();
ignores.add("three");
notEquals = MyReflectionTestUtils.compareGetters(test1, test2, ignores);
assertEquals(0, notEquals.size());
test1.setOne("two");
notEquals = MyReflectionTestUtils.compareGetters(test1, test2, ignores);
assertEquals(1, notEquals.size());
}
/**
* Test method for {@link com.mypackage.utilities.MyReflectionTestUtils#getGetters(java.lang.Class, java.util.List)}.
*/
@Test
public void testGetGetters() {
List<Method> methods = MyReflectionTestUtils.getGetters(Test1.class, new ArrayList<String>());
assertEquals(3, methods.size());
List<String> ignores = new ArrayList<String>();
ignores.add("three");
List<Method> methods2 = MyReflectionTestUtils.getGetters(Test1.class, ignores);
assertEquals(2, methods2.size());
}
/**
* Test method for {@link com.mypacakge.utilities.MyReflectionTestUtils#isGetter(java.lang.reflect.Method)}.
*/
@Test
public void testIsGetter() throws Exception {
Method method = Test1.class.getMethod("getOne");
assertTrue( MyReflectionTestUtils.isGetter(method) );
method = Test1.class.getMethod("setOne", String.class);
assertFalse( MyReflectionTestUtils.isGetter(method) );
}
public class Test1 {
private String one;
private String two;
private String three;
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
public String getThree() {
return three;
}
public void setThree(String three) {
this.three = three;
}
}
public class Test2 {
private String one;
private String two;
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
}
}
@wfnuser
Copy link

wfnuser commented Nov 26, 2019

Cool! It helps a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment