Skip to content

Commit

Permalink
DATACMNS-206 - Guard against PropertyDescriptor being null.
Browse files Browse the repository at this point in the history
Added guards to PropertyDescriptor access to prevent NullPointerExceptions in cases it is not given in the first place. Polished JavaDoc for test case.
  • Loading branch information
odrotbohm committed Jul 31, 2012
1 parent fc57f60 commit f532404
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public PropertyDescriptor getPropertyDescriptor() {
*/
public Method getGetter() {

if (propertyDescriptor == null) {
return null;
}

Method getter = propertyDescriptor.getReadMethod();

if (getter == null) {
Expand All @@ -163,6 +167,10 @@ public Method getGetter() {
*/
public Method getSetter() {

if (propertyDescriptor == null) {
return null;
}

Method setter = propertyDescriptor.getWriteMethod();

if (setter == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public void doesNotConsiderJavaTransientFieldsTransient() {
assertThat(property.isTransient(), is(false));
}

/**
* @see DATACMNS-206
*/
@Test
public void findsSimpleGettersAndASetters() {

Expand All @@ -136,6 +139,9 @@ public void findsSimpleGettersAndASetters() {
assertThat(property.getSetter(), is(notNullValue()));
}

/**
* @see DATACMNS-206
*/
@Test
public void doesNotUseInvalidGettersAndASetters() {

Expand All @@ -147,6 +153,9 @@ public void doesNotUseInvalidGettersAndASetters() {
assertThat(property.getSetter(), is(nullValue()));
}

/**
* @see DATACMNS-206
*/
@Test
public void usesCustomGetter() {

Expand All @@ -158,6 +167,9 @@ public void usesCustomGetter() {
assertThat(property.getSetter(), is(nullValue()));
}

/**
* @see DATACMNS-206
*/
@Test
public void usesCustomSetter() {

Expand All @@ -169,6 +181,20 @@ public void usesCustomSetter() {
assertThat(property.getSetter(), is(notNullValue()));
}

/**
* @see DATACMNS-206
*/
@Test
public void returnsNullGetterAndSetterIfNoPropertyDescriptorGiven() {

Field field = ReflectionUtils.findField(AccessorTestClass.class, "id");
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, null, entity,
typeHolder);

assertThat(property.getGetter(), is(nullValue()));
assertThat(property.getSetter(), is(nullValue()));
}

private static PropertyDescriptor getPropertyDescriptor(Class<?> type, String propertyName) {

try {
Expand Down

0 comments on commit f532404

Please sign in to comment.