DATACMNS-616 - Fixed field value lookup for private fields in AnnotationDetectionFieldCallback.

We now make the field detected by the callback accessible so that the value lookup doesn't fail for private fields. Added a few more unit tests to verify behavior.
This commit is contained in:
Oliver Gierke
2014-12-17 16:15:07 +01:00
parent 55d988e7b7
commit 013f97899e
2 changed files with 82 additions and 2 deletions

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.util;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ReflectionUtils;
/**
* Unit tests for {@link AnnotationDetectionFieldCallback}.
*
* @author Oliver Gierke
*/
public class AnnotationDetectionFieldCallbackUnitTests {
/**
* @see DATACMNS-616
*/
@Test(expected = IllegalArgumentException.class)
public void rejectsNullAnnotationType() {
new AnnotationDetectionFieldCallback(null);
}
/**
* @see DATACMNS-616
*/
@Test
@SuppressWarnings("rawtypes")
public void looksUpValueFromPrivateField() {
AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(Autowired.class);
ReflectionUtils.doWithFields(Sample.class, callback);
assertThat(callback.getType(), is(equalTo((Class) String.class)));
assertThat(callback.getValue(new Sample("foo")), is((Object) "foo"));
}
/**
* @see DATACMNS-616
*/
@Test
public void returnsNullForObjectNotContainingAFieldWithTheConfiguredAnnotation() {
AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(Autowired.class);
ReflectionUtils.doWithFields(Empty.class, callback);
assertThat(callback.getType(), is(nullValue()));
assertThat(callback.getValue(new Empty()), is(nullValue()));
}
static class Sample {
@Autowired private final String value;
public Sample(String value) {
this.value = value;
}
}
static class Empty {}
}