Skip to content

Commit

Permalink
TRUNK-4201 Tests in ReflectTest fail when executed with sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
rkorytkowski committed Jan 2, 2014
1 parent 388ca06 commit 1f9bb47
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions api/src/test/java/org/openmrs/util/ReflectTest.java
Expand Up @@ -43,9 +43,9 @@ public class ReflectTest {
public void xhasField_shouldReturnTrueIfGivenFieldIsDeclaredInParameterizedClassOrSubClass() throws Exception {
Reflect reflect = new Reflect(OpenmrsObject.class);
List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);
Assert.assertEquals("genericCollectionField", allFields.get(0).getName());
Assert.assertTrue(reflect.hasField(allFields.get(0)));

Field genericCollectionField = findFieldByName(allFields, "genericCollectionField");
Assert.assertTrue(reflect.hasField(genericCollectionField));
}

/**
Expand All @@ -56,9 +56,19 @@ public void xhasField_shouldReturnTrueIfGivenFieldIsDeclaredInParameterizedClass
public void xhasField_shouldReturnFalseIfGivenFieldIsNotDeclaredInParameterizedClassOrItsSubClass() throws Exception {
Reflect reflect = new Reflect(OpenmrsObject.class);
List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);

Assert.assertEquals("normalClassField", allFields.get(3).getName());
Assert.assertFalse(reflect.hasField(allFields.get(3)));

Field normalClassField = findFieldByName(allFields, "normalClassField");

Assert.assertFalse(reflect.hasField(normalClassField));
}

private Field findFieldByName(List<Field> fields, String name) {
for (Field field: fields) {
if (name.equals(field.getName())) {
return field;
}
}
throw new IllegalArgumentException("Field not found!");
}

/**
Expand All @@ -68,10 +78,11 @@ public void xhasField_shouldReturnFalseIfGivenFieldIsNotDeclaredInParameterizedC
@Verifies(value = "should return all fields include private and super classes", method = "getAllFields(Class<*>)")
public void getAllFields_shouldReturnAllFieldsIncludePrivateAndSuperClasses() throws Exception {
List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);

Assert.assertEquals(4, allFields.size());
Assert.assertEquals("subClassField", allFields.get(1).getName());
Assert.assertEquals("normalClassField", allFields.get(3).getName());

findFieldByName(allFields, "subClassField");
findFieldByName(allFields, "normalClassField");
findFieldByName(allFields, "nonCollectionField");
findFieldByName(allFields, "genericCollectionField");
}

/**
Expand All @@ -82,9 +93,9 @@ public void getAllFields_shouldReturnAllFieldsIncludePrivateAndSuperClasses() th
public void isCollection_shouldReturnFalseIfGivenFieldClassIsNotACollectionClass() throws Exception {
Reflect reflect = new Reflect(OpenmrsObject.class);
List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);
Assert.assertEquals("nonCollectionField", allFields.get(2).getName());
Assert.assertFalse(reflect.isCollectionField(allFields.get(2)));

Field nonCollectionField = findFieldByName(allFields, "nonCollectionField");
Assert.assertFalse(reflect.isCollectionField(nonCollectionField));
}

/**
Expand Down Expand Up @@ -133,10 +144,13 @@ public void getInheritedFields_shouldReturnOnlyTheSubClassFieldsOfGivenParameter
List<Field> fields = reflect.getInheritedFields(OpenmrsObjectImp.class);

List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);

Assert.assertEquals(3, fields.size());
Assert.assertEquals("normalClassField", allFields.get(3).getName());
Assert.assertFalse(fields.contains(allFields.get(3)));

findFieldByName(fields, "subClassField");
findFieldByName(fields, "nonCollectionField");
findFieldByName(fields, "genericCollectionField");

Field normalClassField = findFieldByName(allFields, "normalClassField");
Assert.assertFalse(fields.contains(normalClassField));
}

/**
Expand Down Expand Up @@ -171,8 +185,8 @@ public void isCollectionField_shouldReturnFalseIfGivenFieldIsCollectionAndElemen
Reflect reflect = new Reflect(OpenmrsObject.class);
List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);

Assert.assertEquals("genericCollectionField", allFields.get(0).getName());
Assert.assertFalse(reflect.isCollectionField(allFields.get(0)));
Field genericCollectionField = findFieldByName(allFields, "genericCollectionField");
Assert.assertFalse(reflect.isCollectionField(genericCollectionField));
}

/**
Expand Down

0 comments on commit 1f9bb47

Please sign in to comment.