DATACMNS-452 - Added some helper types to simplify the implementations in store modules.
Added AnnotatedTypeScanner to easily scan for annotated types within a set of base packages. Picks up interfaces by default which had to be customized when using ClassPathScanningCanidateComponentProvider. Added DirectFieldAccessFallbackBeanWrapper that will use direct field access for beans in case no accessor method can be found. This allows to easily implement bean property values through accessors first but using the field directly if the accessors aren't available. Introduced AnnotationDetectionMethodCallback to easily find methods equipped with a given annotation.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.Persistent;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AnnotatedTypeScannerUnitTests}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AnnotatedTypeScannerUnitTests {
|
||||
|
||||
/**
|
||||
* @see DATACMNS-452
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void findsAnnotatedTypes() {
|
||||
|
||||
AnnotatedTypeScanner scanner = new AnnotatedTypeScanner(Persistent.class);
|
||||
Set<Class<?>> types = scanner.findTypes(AnnotatedTypeScanner.class.getPackage().getName());
|
||||
|
||||
assertThat(types, hasItem(Type.class));
|
||||
}
|
||||
|
||||
@Persistent
|
||||
static class Type {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AnnotationDetectionMethodCallback}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AnnotationDetectionMethodCallbackUnitTests {
|
||||
|
||||
public @Rule ExpectedException exception = ExpectedException.none();
|
||||
|
||||
/**
|
||||
* @see DATACMNS-452
|
||||
*/
|
||||
@Test
|
||||
public void findsMethodWithAnnotation() throws Exception {
|
||||
|
||||
AnnotationDetectionMethodCallback<Value> callback = new AnnotationDetectionMethodCallback<Value>(Value.class);
|
||||
ReflectionUtils.doWithMethods(Sample.class, callback);
|
||||
|
||||
assertThat(callback.hasFoundAnnotation(), is(true));
|
||||
assertThat(callback.getMethod(), is(Sample.class.getMethod("getValue")));
|
||||
assertThat(callback.getAnnotation(), is(notNullValue()));
|
||||
assertThat(callback.getAnnotation().value(), is("#{null}"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-452
|
||||
*/
|
||||
@Test
|
||||
public void detectsAmbiguousAnnotations() {
|
||||
|
||||
exception.expect(IllegalStateException.class);
|
||||
exception.expectMessage("Value");
|
||||
exception.expectMessage("getValue");
|
||||
exception.expectMessage("getOtherValue");
|
||||
|
||||
AnnotationDetectionMethodCallback<Value> callback = new AnnotationDetectionMethodCallback<Value>(Value.class, true);
|
||||
ReflectionUtils.doWithMethods(Multiple.class, callback);
|
||||
}
|
||||
|
||||
interface Sample {
|
||||
|
||||
@Value("#{null}")
|
||||
Object getValue();
|
||||
}
|
||||
|
||||
interface Multiple {
|
||||
|
||||
@Value("#{null}")
|
||||
Object getValue();
|
||||
|
||||
@Value("#{null}")
|
||||
Object getOtherValue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.NotReadablePropertyException;
|
||||
import org.springframework.beans.NotWritablePropertyException;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link DirectFieldAccessFallbackBeanWrapper}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class DirectFieldAccessFallbackBeanWrapperUnitTests {
|
||||
|
||||
/**
|
||||
* @see DATACMNS-452
|
||||
*/
|
||||
@Test
|
||||
public void usesFieldAccessForReadIfNoAccessorCanBeFound() {
|
||||
|
||||
Sample sample = new Sample();
|
||||
sample.firstname = "Dave";
|
||||
|
||||
BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper(sample);
|
||||
|
||||
assertThat(wrapper.getPropertyValue("firstname"), is((Object) "Dave"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-452
|
||||
*/
|
||||
@Test
|
||||
public void usesFieldAccessForWriteIfNoAccessorCanBeFound() {
|
||||
|
||||
Sample sample = new Sample();
|
||||
|
||||
BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper(sample);
|
||||
wrapper.setPropertyValue("firstname", "Dave");
|
||||
|
||||
assertThat(sample.firstname, is("Dave"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-452
|
||||
*/
|
||||
@Test(expected = NotReadablePropertyException.class)
|
||||
public void throwsAppropriateExceptionIfNoFieldFoundForRead() {
|
||||
|
||||
BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper(new Sample());
|
||||
wrapper.getPropertyValue("lastname");
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-452
|
||||
*/
|
||||
@Test(expected = NotWritablePropertyException.class)
|
||||
public void throwsAppropriateExceptionIfNoFieldFoundForWrite() {
|
||||
|
||||
BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper(new Sample());
|
||||
wrapper.setPropertyValue("lastname", "Matthews");
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
String firstname;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user