DATACMNS-266 - Use new common Maven build infrastructure.
Simplified project setup to be a single module build again. Using Spring Data Build parent POM to simplify project setup. See https://github.com/SpringSource/spring-data-build#spring-data-build-infrastructure
This commit is contained in:
31
src/test/java/org/springframework/data/mapping/Child.java
Normal file
31
src/test/java/org/springframework/data/mapping/Child.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.mapping;
|
||||
|
||||
import org.springframework.data.annotation.Persistent;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
@Persistent
|
||||
public class Child extends PersonWithId {
|
||||
|
||||
public Child(Integer ssn, String firstName, String lastName) {
|
||||
super(ssn, firstName, lastName);
|
||||
}
|
||||
|
||||
}
|
||||
33
src/test/java/org/springframework/data/mapping/Document.java
Normal file
33
src/test/java/org/springframework/data/mapping/Document.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.mapping;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.data.annotation.Persistent;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE, ElementType.FIELD })
|
||||
@Persistent
|
||||
public @interface Document {
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2011-2012 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.mapping;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mapping.context.AbstractMappingContext;
|
||||
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.MutablePersistentEntity;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
* Integration tests for Mapping metadata.
|
||||
*
|
||||
* @author Jon Brisbin
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class MappingMetadataTests {
|
||||
|
||||
SampleMappingContext ctx;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
ctx = new SampleMappingContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPojoWithId() {
|
||||
|
||||
ctx.setInitialEntitySet(Collections.singleton(PersonWithId.class));
|
||||
ctx.initialize();
|
||||
|
||||
PersistentEntity<?, SampleProperty> person = ctx.getPersistentEntity(PersonWithId.class);
|
||||
assertNotNull(person.getIdProperty());
|
||||
assertEquals(String.class, person.getIdProperty().getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssociations() {
|
||||
|
||||
ctx.setInitialEntitySet(Collections.singleton(PersonWithChildren.class));
|
||||
ctx.initialize();
|
||||
|
||||
PersistentEntity<?, SampleProperty> person = ctx.getPersistentEntity(PersonWithChildren.class);
|
||||
person.doWithAssociations(new AssociationHandler<MappingMetadataTests.SampleProperty>() {
|
||||
public void doWithAssociation(Association<SampleProperty> association) {
|
||||
assertEquals(Child.class, association.getInverse().getComponentType());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public interface SampleProperty extends PersistentProperty<SampleProperty> {
|
||||
}
|
||||
|
||||
public static class SampleMappingContext extends
|
||||
AbstractMappingContext<MutablePersistentEntity<?, SampleProperty>, SampleProperty> {
|
||||
|
||||
@Override
|
||||
protected <T> MutablePersistentEntity<?, SampleProperty> createPersistentEntity(TypeInformation<T> typeInformation) {
|
||||
|
||||
return new BasicPersistentEntity<T, MappingMetadataTests.SampleProperty>(typeInformation);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SampleProperty createPersistentProperty(Field field, PropertyDescriptor descriptor,
|
||||
MutablePersistentEntity<?, SampleProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
return new SamplePropertyImpl(field, descriptor, owner, simpleTypeHolder);
|
||||
}
|
||||
}
|
||||
|
||||
public static class SamplePropertyImpl extends AnnotationBasedPersistentProperty<SampleProperty> implements
|
||||
SampleProperty {
|
||||
|
||||
public SamplePropertyImpl(Field field, PropertyDescriptor propertyDescriptor,
|
||||
PersistentEntity<?, SampleProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
|
||||
super(field, propertyDescriptor, owner, simpleTypeHolder);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Association<SampleProperty> createAssociation() {
|
||||
|
||||
return new Association<MappingMetadataTests.SampleProperty>(this, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2012 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.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.mapping.PreferredConstructor.Parameter;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link Parameter}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ParameterUnitTests<P extends PersistentProperty<P>> {
|
||||
|
||||
@Mock
|
||||
PersistentEntity<Object, P> entity;
|
||||
@Mock
|
||||
PersistentEntity<String, P> stringEntity;
|
||||
|
||||
TypeInformation<Object> type = ClassTypeInformation.from(Object.class);
|
||||
Annotation[] annotations = new Annotation[0];
|
||||
|
||||
@Test
|
||||
public void twoParametersWithIdenticalSetupEqual() {
|
||||
|
||||
Parameter<Object, P> left = new Parameter<Object, P>("name", type, annotations, entity);
|
||||
Parameter<Object, P> right = new Parameter<Object, P>("name", type, annotations, entity);
|
||||
|
||||
assertThat(left, is(right));
|
||||
assertThat(left.hashCode(), is(right.hashCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void twoParametersWithIdenticalSetupAndNullNameEqual() {
|
||||
|
||||
Parameter<Object, P> left = new Parameter<Object, P>(null, type, annotations, entity);
|
||||
Parameter<Object, P> right = new Parameter<Object, P>(null, type, annotations, entity);
|
||||
|
||||
assertThat(left, is(right));
|
||||
assertThat(left.hashCode(), is(right.hashCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void twoParametersWithIdenticalAndNullEntitySetupEqual() {
|
||||
|
||||
Parameter<Object, P> left = new Parameter<Object, P>("name", type, annotations, null);
|
||||
Parameter<Object, P> right = new Parameter<Object, P>("name", type, annotations, null);
|
||||
|
||||
assertThat(left, is(right));
|
||||
assertThat(left.hashCode(), is(right.hashCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void twoParametersWithDifferentNameAreNotEqual() {
|
||||
|
||||
Parameter<Object, P> left = new Parameter<Object, P>("first", type, annotations, entity);
|
||||
Parameter<Object, P> right = new Parameter<Object, P>("second", type, annotations, entity);
|
||||
|
||||
assertThat(left, is(not(right)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void twoParametersWithDifferenTypeAreNotEqual() {
|
||||
|
||||
Parameter left = new Parameter<Object, P>("name", type, annotations, entity);
|
||||
Parameter right = new Parameter<String, P>("name", ClassTypeInformation.from(String.class), annotations,
|
||||
stringEntity);
|
||||
|
||||
assertThat(left, is(not(right)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Some test methods that define expected behaviour for {@link PersistentEntity} interface. Implementation test classes
|
||||
* can simply extend that class to get the specs tested against an instance of their implementation.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class PersistentEntitySpec {
|
||||
|
||||
public static void assertInvariants(PersistentEntity<?, ?> entity) {
|
||||
assertThat(entity.getName(), is(notNullValue()));
|
||||
}
|
||||
}
|
||||
58
src/test/java/org/springframework/data/mapping/Person.java
Normal file
58
src/test/java/org/springframework/data/mapping/Person.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.mapping;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
public abstract class Person {
|
||||
|
||||
private Integer ssn;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
protected Person(Integer ssn, String firstName, String lastName) {
|
||||
this.ssn = ssn;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Integer getSsn() {
|
||||
return ssn;
|
||||
}
|
||||
|
||||
public void setSsn(Integer ssn) {
|
||||
this.ssn = ssn;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.mapping;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
@Document
|
||||
public class PersonDocument extends Person {
|
||||
|
||||
public PersonDocument(Integer ssn, String firstName, String lastName) {
|
||||
super(ssn, firstName, lastName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.mapping;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
@Document
|
||||
public class PersonNoId extends Person {
|
||||
|
||||
public PersonNoId(Integer ssn, String firstName, String lastName) {
|
||||
super(ssn, firstName, lastName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.mapping;
|
||||
|
||||
import org.springframework.data.annotation.Persistent;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
@Persistent
|
||||
public class PersonPersistent extends PersonWithId {
|
||||
|
||||
public PersonPersistent(Integer ssn, String firstName, String lastName) {
|
||||
super(ssn, firstName, lastName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.mapping;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Reference;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
public class PersonWithChildren extends Person {
|
||||
|
||||
@Id
|
||||
String id;
|
||||
|
||||
@Reference
|
||||
private List<Child> children;
|
||||
|
||||
public PersonWithChildren(Integer ssn, String firstName, String lastName) {
|
||||
super(ssn, firstName, lastName);
|
||||
}
|
||||
|
||||
public List<Child> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(List<Child> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.mapping;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
public class PersonWithId extends Person {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
public PersonWithId(Integer ssn, String firstName, String lastName) {
|
||||
super(ssn, firstName, lastName);
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright 2011-2012 by the original author(s).
|
||||
*
|
||||
* 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.mapping;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
import org.springframework.data.mapping.PreferredConstructor.Parameter;
|
||||
import org.springframework.data.mapping.PreferredConstructorDiscovererUnitTests.Outer.Inner;
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PreferredConstructorDiscoverer}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class PreferredConstructorDiscovererUnitTests<P extends PersistentProperty<P>> {
|
||||
|
||||
@Test
|
||||
public void findsNoArgConstructorForClassWithoutExplicitConstructor() {
|
||||
|
||||
PreferredConstructorDiscoverer<EntityWithoutConstructor, P> discoverer = new PreferredConstructorDiscoverer<EntityWithoutConstructor, P>(
|
||||
EntityWithoutConstructor.class);
|
||||
PreferredConstructor<EntityWithoutConstructor, P> constructor = discoverer.getConstructor();
|
||||
|
||||
assertThat(constructor, is(notNullValue()));
|
||||
assertThat(constructor.isNoArgConstructor(), is(true));
|
||||
assertThat(constructor.isExplicitlyAnnotated(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsNoArgConstructorForClassWithMultipleConstructorsAndNoArgOne() {
|
||||
|
||||
PreferredConstructorDiscoverer<ClassWithEmptyConstructor, P> discoverer = new PreferredConstructorDiscoverer<ClassWithEmptyConstructor, P>(
|
||||
ClassWithEmptyConstructor.class);
|
||||
PreferredConstructor<ClassWithEmptyConstructor, P> constructor = discoverer.getConstructor();
|
||||
|
||||
assertThat(constructor, is(notNullValue()));
|
||||
assertThat(constructor.isNoArgConstructor(), is(true));
|
||||
assertThat(constructor.isExplicitlyAnnotated(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotThrowExceptionForMultipleConstructorsAndNoNoArgConstructorWithoutAnnotation() {
|
||||
|
||||
PreferredConstructorDiscoverer<ClassWithMultipleConstructorsWithoutEmptyOne, P> discoverer = new PreferredConstructorDiscoverer<ClassWithMultipleConstructorsWithoutEmptyOne, P>(
|
||||
ClassWithMultipleConstructorsWithoutEmptyOne.class);
|
||||
assertThat(discoverer.getConstructor(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesConstructorWithAnnotationOverEveryOther() {
|
||||
|
||||
PreferredConstructorDiscoverer<ClassWithMultipleConstructorsAndAnnotation, P> discoverer = new PreferredConstructorDiscoverer<ClassWithMultipleConstructorsAndAnnotation, P>(
|
||||
ClassWithMultipleConstructorsAndAnnotation.class);
|
||||
PreferredConstructor<ClassWithMultipleConstructorsAndAnnotation, P> constructor = discoverer.getConstructor();
|
||||
|
||||
assertThat(constructor, is(notNullValue()));
|
||||
assertThat(constructor.isNoArgConstructor(), is(false));
|
||||
assertThat(constructor.isExplicitlyAnnotated(), is(true));
|
||||
|
||||
assertThat(constructor.hasParameters(), is(true));
|
||||
Iterator<Parameter<Object, P>> parameters = constructor.getParameters().iterator();
|
||||
|
||||
Parameter<?, P> parameter = parameters.next();
|
||||
assertThat(parameter.getType().getType(), typeCompatibleWith(Long.class));
|
||||
assertThat(parameters.hasNext(), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-134
|
||||
*/
|
||||
@Test
|
||||
public void discoversInnerClassConstructorCorrectly() {
|
||||
|
||||
PersistentEntity<Inner, P> entity = new BasicPersistentEntity<Inner, P>(ClassTypeInformation.from(Inner.class));
|
||||
PreferredConstructorDiscoverer<Inner, P> discoverer = new PreferredConstructorDiscoverer<Inner, P>(entity);
|
||||
PreferredConstructor<Inner, P> constructor = discoverer.getConstructor();
|
||||
|
||||
Parameter<?, P> parameter = constructor.getParameters().iterator().next();
|
||||
assertThat(constructor.isEnclosingClassParameter(parameter), is(true));
|
||||
}
|
||||
|
||||
static class EntityWithoutConstructor {
|
||||
|
||||
}
|
||||
|
||||
static class ClassWithEmptyConstructor {
|
||||
|
||||
public ClassWithEmptyConstructor() {
|
||||
}
|
||||
}
|
||||
|
||||
static class ClassWithMultipleConstructorsAndEmptyOne {
|
||||
|
||||
public ClassWithMultipleConstructorsAndEmptyOne(String value) {
|
||||
}
|
||||
|
||||
public ClassWithMultipleConstructorsAndEmptyOne() {
|
||||
}
|
||||
}
|
||||
|
||||
static class ClassWithMultipleConstructorsWithoutEmptyOne {
|
||||
|
||||
public ClassWithMultipleConstructorsWithoutEmptyOne(String value) {
|
||||
}
|
||||
|
||||
public ClassWithMultipleConstructorsWithoutEmptyOne(Long value) {
|
||||
}
|
||||
}
|
||||
|
||||
static class ClassWithMultipleConstructorsAndAnnotation {
|
||||
|
||||
public ClassWithMultipleConstructorsAndAnnotation() {
|
||||
}
|
||||
|
||||
public ClassWithMultipleConstructorsAndAnnotation(String value) {
|
||||
}
|
||||
|
||||
@PersistenceConstructor
|
||||
public ClassWithMultipleConstructorsAndAnnotation(Long value) {
|
||||
}
|
||||
}
|
||||
|
||||
static class Outer {
|
||||
|
||||
class Inner {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
/*
|
||||
* Copyright 2011-2012 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.mapping;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PropertyPath}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class PropertyPathUnitTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void parsesSimplePropertyCorrectly() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userName", Foo.class);
|
||||
assertThat(reference.hasNext(), is(false));
|
||||
assertThat(reference.toDotPath(), is("userName"));
|
||||
assertThat(reference.getOwningType(), is((TypeInformation) ClassTypeInformation.from(Foo.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsesPathPropertyCorrectly() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userName", Bar.class);
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
assertThat(reference.toDotPath(), is("user.name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefersLongerMatches() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userName", Sample.class);
|
||||
assertThat(reference.hasNext(), is(false));
|
||||
assertThat(reference.toDotPath(), is("userName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testname() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userName", Sample2.class);
|
||||
assertThat(reference.getSegment(), is("user"));
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefersExplicitPaths() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("user_name", Sample.class);
|
||||
assertThat(reference.getSegment(), is("user"));
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesGenericsCorrectly() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("usersName", Bar.class);
|
||||
assertThat(reference.getSegment(), is("users"));
|
||||
assertThat(reference.isCollection(), is(true));
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesMapCorrectly() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userMapName", Bar.class);
|
||||
assertThat(reference.getSegment(), is("userMap"));
|
||||
assertThat(reference.isCollection(), is(false));
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesArrayCorrectly() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userArrayName", Bar.class);
|
||||
assertThat(reference.getSegment(), is("userArray"));
|
||||
assertThat(reference.isCollection(), is(true));
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesInvalidCollectionCompountTypeProperl() {
|
||||
|
||||
try {
|
||||
PropertyPath.from("usersMame", Bar.class);
|
||||
fail("Expected PropertyReferenceException!");
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getPropertyName(), is("mame"));
|
||||
assertThat(e.getBaseProperty(), is(PropertyPath.from("users", Bar.class)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesInvalidMapValueTypeProperly() {
|
||||
|
||||
try {
|
||||
PropertyPath.from("userMapMame", Bar.class);
|
||||
fail();
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getPropertyName(), is("mame"));
|
||||
assertThat(e.getBaseProperty(), is(PropertyPath.from("userMap", Bar.class)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsNested() {
|
||||
|
||||
PropertyPath from = PropertyPath.from("barUserName", Sample.class);
|
||||
|
||||
assertThat(from, is(notNullValue()));
|
||||
assertThat(from.getLeafProperty(), is(PropertyPath.from("name", FooBar.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-45
|
||||
*/
|
||||
@Test
|
||||
public void handlesEmptyUnderscoresCorrectly() {
|
||||
|
||||
PropertyPath propertyPath = PropertyPath.from("_foo", Sample2.class);
|
||||
assertThat(propertyPath.getSegment(), is("_foo"));
|
||||
assertThat(propertyPath.getType(), is(typeCompatibleWith(Foo.class)));
|
||||
|
||||
propertyPath = PropertyPath.from("_foo__email", Sample2.class);
|
||||
assertThat(propertyPath.toDotPath(), is("_foo._email"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsDotNotationAsWell() {
|
||||
|
||||
PropertyPath propertyPath = PropertyPath.from("bar.userMap.name", Sample.class);
|
||||
|
||||
assertThat(propertyPath, is(notNullValue()));
|
||||
assertThat(propertyPath.getSegment(), is("bar"));
|
||||
assertThat(propertyPath.getLeafProperty(), is(PropertyPath.from("name", FooBar.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsCorrectIteratorForSingleElement() {
|
||||
|
||||
PropertyPath propertyPath = PropertyPath.from("userName", Foo.class);
|
||||
|
||||
Iterator<PropertyPath> iterator = propertyPath.iterator();
|
||||
assertThat(iterator.hasNext(), is(true));
|
||||
assertThat(iterator.next(), is(propertyPath));
|
||||
assertThat(iterator.hasNext(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsCorrectIteratorForMultipleElement() {
|
||||
|
||||
PropertyPath propertyPath = PropertyPath.from("user.name", Bar.class);
|
||||
|
||||
Iterator<PropertyPath> iterator = propertyPath.iterator();
|
||||
assertThat(iterator.hasNext(), is(true));
|
||||
assertThat(iterator.next(), is(propertyPath));
|
||||
assertThat(iterator.hasNext(), is(true));
|
||||
assertThat(iterator.next(), is(propertyPath.next()));
|
||||
assertThat(iterator.hasNext(), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-139
|
||||
*/
|
||||
@Test
|
||||
public void rejectsInvalidPropertyWithLeadingUnderscore() {
|
||||
try {
|
||||
PropertyPath.from("_id", Foo.class);
|
||||
fail();
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getMessage(), containsString("property _id"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-139
|
||||
*/
|
||||
@Test
|
||||
public void rejectsNestedInvalidPropertyWithLeadingUnderscore() {
|
||||
try {
|
||||
PropertyPath.from("_foo_id", Sample2.class);
|
||||
fail();
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getMessage(), containsString("property id"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-139
|
||||
*/
|
||||
@Test
|
||||
public void rejectsNestedInvalidPropertyExplictlySplitWithLeadingUnderscore() {
|
||||
try {
|
||||
PropertyPath.from("_foo__id", Sample2.class);
|
||||
fail();
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getMessage(), containsString("property _id"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS 158
|
||||
*/
|
||||
@Test(expected = PropertyReferenceException.class)
|
||||
public void rejectsInvalidPathsContainingDigits() {
|
||||
PropertyPath.from("PropertyThatWillFail4Sure", Foo.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectsInvalidProperty() {
|
||||
|
||||
try {
|
||||
PropertyPath.from("bar", Foo.class);
|
||||
fail();
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getBaseProperty(), is(nullValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void samePathsEqual() {
|
||||
|
||||
PropertyPath left = PropertyPath.from("user.name", Bar.class);
|
||||
PropertyPath right = PropertyPath.from("user.name", Bar.class);
|
||||
|
||||
PropertyPath shortPath = PropertyPath.from("user", Bar.class);
|
||||
|
||||
assertThat(left, is(right));
|
||||
assertThat(right, is(left));
|
||||
assertThat(left, is(not(shortPath)));
|
||||
assertThat(shortPath, is(not(left)));
|
||||
|
||||
assertThat(left, is(not(new Object())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hashCodeTests() {
|
||||
|
||||
PropertyPath left = PropertyPath.from("user.name", Bar.class);
|
||||
PropertyPath right = PropertyPath.from("user.name", Bar.class);
|
||||
|
||||
PropertyPath shortPath = PropertyPath.from("user", Bar.class);
|
||||
|
||||
assertThat(left.hashCode(), is(right.hashCode()));
|
||||
assertThat(left.hashCode(), is(not(shortPath.hashCode())));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-257
|
||||
*/
|
||||
@Test
|
||||
public void findsAllUppercaseProperty() {
|
||||
|
||||
PropertyPath path = PropertyPath.from("UUID", Foo.class);
|
||||
|
||||
assertThat(path, is(notNullValue()));
|
||||
assertThat(path.getSegment(), is("UUID"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-257
|
||||
*/
|
||||
@Test
|
||||
public void findsNestedAllUppercaseProperty() {
|
||||
|
||||
PropertyPath path = PropertyPath.from("_fooUUID", Sample2.class);
|
||||
|
||||
assertThat(path, is(notNullValue()));
|
||||
assertThat(path.getSegment(), is("_foo"));
|
||||
assertThat(path.hasNext(), is(true));
|
||||
assertThat(path.next().getSegment(), is("UUID"));
|
||||
}
|
||||
|
||||
private class Foo {
|
||||
|
||||
String userName;
|
||||
String _email;
|
||||
String UUID;
|
||||
}
|
||||
|
||||
private class Bar {
|
||||
|
||||
private FooBar user;
|
||||
private Set<FooBar> users;
|
||||
private Map<String, FooBar> userMap;
|
||||
private FooBar[] userArray;
|
||||
}
|
||||
|
||||
private class FooBar {
|
||||
|
||||
private String name;
|
||||
}
|
||||
|
||||
private class Sample {
|
||||
|
||||
private String userName;
|
||||
private FooBar user;
|
||||
private Bar bar;
|
||||
}
|
||||
|
||||
private class Sample2 {
|
||||
|
||||
private String userNameWhatever;
|
||||
private FooBar user;
|
||||
private Foo _foo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link SimpleTypeHolder}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class SimpleTypeHolderUnitTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullCustomTypes() {
|
||||
new SimpleTypeHolder(null, false);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullOriginal() {
|
||||
new SimpleTypeHolder(new HashSet<Class<?>>(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-31
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullTypeForIsSimpleTypeCall() {
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder();
|
||||
holder.isSimpleType(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addsDefaultTypes() {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder();
|
||||
|
||||
assertThat(holder.isSimpleType(String.class), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotAddDefaultConvertersIfConfigured() {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder(new HashSet<Class<?>>(), false);
|
||||
|
||||
assertThat(holder.isSimpleType(String.class), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addsCustomTypesToSimpleOnes() {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolder.class), true);
|
||||
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolder.class), is(true));
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolderUnitTests.class), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createsHolderFromAnotherOneCorrectly() {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolder.class), true);
|
||||
SimpleTypeHolder second = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolderUnitTests.class), holder);
|
||||
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolder.class), is(true));
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolderUnitTests.class), is(false));
|
||||
assertThat(second.isSimpleType(SimpleTypeHolder.class), is(true));
|
||||
assertThat(second.isSimpleType(SimpleTypeHolderUnitTests.class), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void considersObjectToBeSimpleType() {
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder();
|
||||
assertThat(holder.isSimpleType(Object.class), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void considersSimpleEnumAsSimple() {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder();
|
||||
assertThat(holder.isSimpleType(SimpleEnum.FOO.getClass()), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void considersComplexEnumAsSimple() {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder();
|
||||
assertThat(holder.isSimpleType(ComplexEnum.FOO.getClass()), is(true));
|
||||
}
|
||||
|
||||
enum SimpleEnum {
|
||||
|
||||
FOO;
|
||||
}
|
||||
|
||||
enum ComplexEnum {
|
||||
|
||||
FOO {
|
||||
@Override
|
||||
boolean method() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
abstract boolean method();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* 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.mapping.context;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.PropertyHandler;
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link AbstractMappingContext}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AbstractMappingContextIntegrationTests<T extends PersistentProperty<T>> {
|
||||
|
||||
@Test
|
||||
public void foo() throws InterruptedException {
|
||||
|
||||
final DummyMappingContext context = new DummyMappingContext();
|
||||
|
||||
Thread a = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
context.getPersistentEntity(Person.class);
|
||||
}
|
||||
});
|
||||
|
||||
Thread b = new Thread(new Runnable() {
|
||||
|
||||
public void run() {
|
||||
|
||||
PersistentEntity<Object, T> entity = context.getPersistentEntity(Person.class);
|
||||
|
||||
entity.doWithProperties(new PropertyHandler<T>() {
|
||||
public void doWithPersistentProperty(T persistentProperty) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
a.start();
|
||||
Thread.sleep(2800);
|
||||
b.start();
|
||||
|
||||
a.join();
|
||||
b.join();
|
||||
}
|
||||
|
||||
class DummyMappingContext extends AbstractMappingContext<BasicPersistentEntity<Object, T>, T> {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <S> BasicPersistentEntity<Object, T> createPersistentEntity(TypeInformation<S> typeInformation) {
|
||||
return (BasicPersistentEntity<Object, T>) new BasicPersistentEntity<S, T>(typeInformation);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
protected T createPersistentProperty(final Field field, final PropertyDescriptor descriptor,
|
||||
final BasicPersistentEntity<Object, T> owner, final SimpleTypeHolder simpleTypeHolder) {
|
||||
|
||||
PersistentProperty prop = mock(PersistentProperty.class);
|
||||
|
||||
when(prop.getTypeInformation()).thenReturn((TypeInformation) owner.getTypeInformation());
|
||||
when(prop.getName()).thenReturn(field.getName());
|
||||
when(prop.getPersistentEntityType()).thenReturn(Collections.EMPTY_SET);
|
||||
|
||||
try {
|
||||
Thread.sleep(800);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return (T) prop;
|
||||
}
|
||||
}
|
||||
|
||||
class Person {
|
||||
|
||||
String firstname;
|
||||
String lastname;
|
||||
String email;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright 2011-2012 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.mapping.context;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import groovy.lang.MetaClass;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
* Unit test for {@link AbstractMappingContext}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AbstractMappingContextUnitTests {
|
||||
|
||||
final SimpleTypeHolder holder = new SimpleTypeHolder();
|
||||
SampleMappingContext context;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
context = new SampleMappingContext();
|
||||
context.setSimpleTypeHolder(holder);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotTryToLookupPersistentEntityForLeafProperty() {
|
||||
PersistentPropertyPath<SamplePersistentProperty> path = context.getPersistentPropertyPath(PropertyPath.from("name",
|
||||
Person.class));
|
||||
assertThat(path, is(notNullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-92
|
||||
*/
|
||||
@Test(expected = MappingException.class)
|
||||
public void doesNotAddInvalidEntity() {
|
||||
|
||||
context = new SampleMappingContext() {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <S> BasicPersistentEntity<Object, SamplePersistentProperty> createPersistentEntity(
|
||||
TypeInformation<S> typeInformation) {
|
||||
return new BasicPersistentEntity<Object, SamplePersistentProperty>((TypeInformation<Object>) typeInformation) {
|
||||
@Override
|
||||
public void verify() {
|
||||
if (Unsupported.class.isAssignableFrom(getType())) {
|
||||
throw new MappingException("Unsupported type!");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
context.getPersistentEntity(Unsupported.class);
|
||||
} catch (MappingException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
context.getPersistentEntity(Unsupported.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registersEntitiesOnContextRefreshedEvent() {
|
||||
|
||||
ApplicationContext context = mock(ApplicationContext.class);
|
||||
|
||||
SampleMappingContext mappingContext = new SampleMappingContext();
|
||||
mappingContext.setInitialEntitySet(Collections.singleton(Person.class));
|
||||
mappingContext.setApplicationContext(context);
|
||||
|
||||
verify(context, times(0)).publishEvent(Mockito.any(ApplicationEvent.class));
|
||||
|
||||
mappingContext.onApplicationEvent(new ContextRefreshedEvent(context));
|
||||
verify(context, times(1)).publishEvent(Mockito.any(ApplicationEvent.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-214
|
||||
*/
|
||||
@Test
|
||||
public void returnsNullPersistentEntityForSimpleTypes() {
|
||||
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
assertThat(context.getPersistentEntity(String.class), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-214
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullValueForGetPersistentEntityOfClass() {
|
||||
context.getPersistentEntity((Class<?>) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-214
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullValueForGetPersistentEntityOfTypeInformation() {
|
||||
context.getPersistentEntity((TypeInformation<?>) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-228
|
||||
*/
|
||||
@Test
|
||||
public void doesNotCreatePersistentPropertyForGroovyMetaClass() {
|
||||
|
||||
SampleMappingContext mappingContext = new SampleMappingContext();
|
||||
mappingContext.initialize();
|
||||
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Sample.class);
|
||||
assertThat(entity.getPersistentProperty("metaClass"), is(nullValue()));
|
||||
}
|
||||
|
||||
class Person {
|
||||
String name;
|
||||
}
|
||||
|
||||
class Unsupported {
|
||||
|
||||
}
|
||||
|
||||
class Sample {
|
||||
|
||||
MetaClass metaClass;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright 2011 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.mapping.context;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link DefaultPersistentPropertyPath}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DefaultPersistenPropertyPathUnitTests<T extends PersistentProperty<T>> {
|
||||
|
||||
@Mock
|
||||
T first, second;
|
||||
|
||||
@Mock
|
||||
Converter<T, String> converter;
|
||||
|
||||
PersistentPropertyPath<T> oneLeg;
|
||||
PersistentPropertyPath<T> twoLegs;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setUp() {
|
||||
oneLeg = new DefaultPersistentPropertyPath<T>(Arrays.asList(first));
|
||||
twoLegs = new DefaultPersistentPropertyPath<T>(Arrays.asList(first, second));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullProperties() {
|
||||
new DefaultPersistentPropertyPath<T>(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesPropertyNameForSimpleDotPath() {
|
||||
|
||||
when(first.getName()).thenReturn("foo");
|
||||
when(second.getName()).thenReturn("bar");
|
||||
|
||||
assertThat(twoLegs.toDotPath(), is("foo.bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void usesConverterToCreatePropertyPath() {
|
||||
|
||||
when(converter.convert((T) any())).thenReturn("foo");
|
||||
|
||||
assertThat(twoLegs.toDotPath(converter), is("foo.foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsCorrectLeafProperty() {
|
||||
|
||||
assertThat(twoLegs.getLeafProperty(), is(second));
|
||||
assertThat(oneLeg.getLeafProperty(), is(first));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsCorrectBaseProperty() {
|
||||
|
||||
assertThat(twoLegs.getBaseProperty(), is(first));
|
||||
assertThat(oneLeg.getBaseProperty(), is(first));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsBasePathCorrectly() {
|
||||
|
||||
assertThat(oneLeg.isBasePathOf(twoLegs), is(true));
|
||||
assertThat(twoLegs.isBasePathOf(oneLeg), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void calculatesExtensionCorrectly() {
|
||||
|
||||
PersistentPropertyPath<T> extension = twoLegs.getExtensionForBaseOf(oneLeg);
|
||||
assertThat(extension, is((PersistentPropertyPath<T>) new DefaultPersistentPropertyPath<T>(Arrays.asList(second))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsTheCorrectParentPath() {
|
||||
assertThat(twoLegs.getParentPath(), is(oneLeg));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsItselfAsParentPathIfSizeOne() {
|
||||
assertThat(oneLeg.getParentPath(), is(oneLeg));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathReturnsCorrectSize() {
|
||||
assertThat(oneLeg.getLength(), is(1));
|
||||
assertThat(twoLegs.getLength(), is(2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2012 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.mapping.context;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mapping.context.AbstractMappingContext.FieldMatch;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link FieldMatch}. Introduced for DATACMNS-228.
|
||||
*
|
||||
* @since 1.4
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class FieldMatchUnitTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsBothParametersBeingNull() {
|
||||
|
||||
new FieldMatch(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesFieldByConcreteNameAndType() throws Exception {
|
||||
|
||||
FieldMatch match = new FieldMatch("name", "java.lang.String");
|
||||
assertThat(match.matches(Sample.class.getField("this$0")), is(false));
|
||||
assertThat(match.matches(Sample.class.getField("this$1")), is(false));
|
||||
assertThat(match.matches(Sample.class.getField("name")), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesFieldByNamePattern() throws Exception {
|
||||
|
||||
FieldMatch match = new FieldMatch("this\\$.*", "java.lang.Object");
|
||||
assertThat(match.matches(Sample.class.getField("this$0")), is(true));
|
||||
assertThat(match.matches(Sample.class.getField("this$1")), is(true));
|
||||
assertThat(match.matches(Sample.class.getField("name")), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesFieldByNameOnly() throws Exception {
|
||||
|
||||
FieldMatch match = new FieldMatch("this\\$.*", null);
|
||||
assertThat(match.matches(Sample.class.getField("this$0")), is(true));
|
||||
assertThat(match.matches(Sample.class.getField("this$1")), is(true));
|
||||
assertThat(match.matches(Sample.class.getField("name")), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesFieldByTypeNameOnly() throws Exception {
|
||||
|
||||
FieldMatch match = new FieldMatch(null, "java.lang.Object");
|
||||
assertThat(match.matches(Sample.class.getField("this$0")), is(true));
|
||||
assertThat(match.matches(Sample.class.getField("this$1")), is(true));
|
||||
assertThat(match.matches(Sample.class.getField("name")), is(false));
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
public Object this$0;
|
||||
public Object this$1;
|
||||
public String name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2012 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.mapping.context;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mapping.context.MappingContextEvent;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MappingContextEvent}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MappingContextEventUnitTests<E extends PersistentEntity<?, P>, P extends PersistentProperty<P>> {
|
||||
|
||||
@Mock
|
||||
E entity;
|
||||
|
||||
@Mock
|
||||
MappingContext<?, ?> mappingContext, otherMappingContext;
|
||||
|
||||
@Test
|
||||
public void returnsPersistentEntityHandedToTheEvent() {
|
||||
|
||||
MappingContextEvent<E, P> event = new MappingContextEvent<E, P>(mappingContext, entity);
|
||||
assertThat(event.getPersistentEntity(), is(entity));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesMappingContextAsEventSource() {
|
||||
|
||||
MappingContextEvent<E, P> event = new MappingContextEvent<E, P>(mappingContext, entity);
|
||||
assertThat(event.getSource(), is((Object) mappingContext));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsEmittingMappingContextCorrectly() {
|
||||
|
||||
MappingContextEvent<E, P> event = new MappingContextEvent<E, P>(mappingContext, entity);
|
||||
assertThat(event.wasEmittedBy(mappingContext), is(true));
|
||||
assertThat(event.wasEmittedBy(otherMappingContext), is(false));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.springframework.data.mapping.context;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
class SampleMappingContext extends
|
||||
AbstractMappingContext<BasicPersistentEntity<Object, SamplePersistentProperty>, SamplePersistentProperty> {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <S> BasicPersistentEntity<Object, SamplePersistentProperty> createPersistentEntity(
|
||||
TypeInformation<S> typeInformation) {
|
||||
return new BasicPersistentEntity<Object, SamplePersistentProperty>((TypeInformation<Object>) typeInformation);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SamplePersistentProperty createPersistentProperty(final Field field, final PropertyDescriptor descriptor,
|
||||
final BasicPersistentEntity<Object, SamplePersistentProperty> owner, final SimpleTypeHolder simpleTypeHolder) {
|
||||
|
||||
return new SamplePersistentProperty(field, descriptor, owner, simpleTypeHolder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2012 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.mapping.context;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.springframework.data.mapping.Association;
|
||||
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
|
||||
class SamplePersistentProperty extends AnnotationBasedPersistentProperty<SamplePersistentProperty> {
|
||||
|
||||
public SamplePersistentProperty(Field field, PropertyDescriptor propertyDescriptor,
|
||||
BasicPersistentEntity<?, SamplePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
super(field, propertyDescriptor, owner, simpleTypeHolder);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Association<SamplePersistentProperty> createAssociation() {
|
||||
return new Association<SamplePersistentProperty>(this, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
/*
|
||||
* Copyright 2011-2012 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.mapping.model;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mapping.Association;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AbstractPersistentProperty}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AbstractPersistentPropertyUnitTests {
|
||||
|
||||
TypeInformation<TestClassComplex> typeInfo;
|
||||
PersistentEntity<TestClassComplex, SamplePersistentProperty> entity;
|
||||
SimpleTypeHolder typeHolder;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
typeInfo = ClassTypeInformation.from(TestClassComplex.class);
|
||||
entity = new BasicPersistentEntity<TestClassComplex, SamplePersistentProperty>(typeInfo);
|
||||
typeHolder = new SimpleTypeHolder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-68
|
||||
*/
|
||||
@Test
|
||||
public void discoversComponentTypeCorrectly() throws Exception {
|
||||
|
||||
Field field = ReflectionUtils.findField(TestClassComplex.class, "testClassSet");
|
||||
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
|
||||
property.getComponentType();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsNestedEntityTypeCorrectly() {
|
||||
|
||||
Field field = ReflectionUtils.findField(TestClassComplex.class, "testClassSet");
|
||||
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
|
||||
assertThat(property.getPersistentEntityType().iterator().hasNext(), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-132
|
||||
*/
|
||||
@Test
|
||||
public void isEntityWorksForUntypedMaps() throws Exception {
|
||||
|
||||
Field field = ReflectionUtils.findField(TestClassComplex.class, "map");
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
|
||||
assertThat(property.isEntity(), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-132
|
||||
*/
|
||||
@Test
|
||||
public void isEntityWorksForUntypedCollection() throws Exception {
|
||||
|
||||
Field field = ReflectionUtils.findField(TestClassComplex.class, "collection");
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
|
||||
assertThat(property.isEntity(), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-121
|
||||
*/
|
||||
@Test
|
||||
public void considersPropertiesEqualIfFieldEquals() {
|
||||
|
||||
Field first = ReflectionUtils.findField(FirstConcrete.class, "genericField");
|
||||
Field second = ReflectionUtils.findField(SecondConcrete.class, "genericField");
|
||||
|
||||
SamplePersistentProperty firstProperty = new SamplePersistentProperty(first, null, entity, typeHolder);
|
||||
SamplePersistentProperty secondProperty = new SamplePersistentProperty(second, null, entity, typeHolder);
|
||||
|
||||
assertThat(firstProperty, is(secondProperty));
|
||||
assertThat(firstProperty.hashCode(), is(secondProperty.hashCode()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-180
|
||||
*/
|
||||
@Test
|
||||
public void doesNotConsiderJavaTransientFieldsTransient() {
|
||||
|
||||
Field transientField = ReflectionUtils.findField(TestClassComplex.class, "transientField");
|
||||
|
||||
PersistentProperty<?> property = new SamplePersistentProperty(transientField, null, entity, typeHolder);
|
||||
assertThat(property.isTransient(), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-206
|
||||
*/
|
||||
@Test
|
||||
public void findsSimpleGettersAndASetters() {
|
||||
|
||||
Field field = ReflectionUtils.findField(AccessorTestClass.class, "id");
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
|
||||
AccessorTestClass.class, "id"), entity, typeHolder);
|
||||
|
||||
assertThat(property.getGetter(), is(notNullValue()));
|
||||
assertThat(property.getSetter(), is(notNullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-206
|
||||
*/
|
||||
@Test
|
||||
public void doesNotUseInvalidGettersAndASetters() {
|
||||
|
||||
Field field = ReflectionUtils.findField(AccessorTestClass.class, "anotherId");
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
|
||||
AccessorTestClass.class, "anotherId"), entity, typeHolder);
|
||||
|
||||
assertThat(property.getGetter(), is(nullValue()));
|
||||
assertThat(property.getSetter(), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-206
|
||||
*/
|
||||
@Test
|
||||
public void usesCustomGetter() {
|
||||
|
||||
Field field = ReflectionUtils.findField(AccessorTestClass.class, "yetAnotherId");
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
|
||||
AccessorTestClass.class, "yetAnotherId"), entity, typeHolder);
|
||||
|
||||
assertThat(property.getGetter(), is(notNullValue()));
|
||||
assertThat(property.getSetter(), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-206
|
||||
*/
|
||||
@Test
|
||||
public void usesCustomSetter() {
|
||||
|
||||
Field field = ReflectionUtils.findField(AccessorTestClass.class, "yetYetAnotherId");
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
|
||||
AccessorTestClass.class, "yetYetAnotherId"), entity, typeHolder);
|
||||
|
||||
assertThat(property.getGetter(), is(nullValue()));
|
||||
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 {
|
||||
|
||||
BeanInfo info = Introspector.getBeanInfo(type);
|
||||
|
||||
for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) {
|
||||
if (descriptor.getName().equals(propertyName)) {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
} catch (IntrospectionException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class Generic<T> {
|
||||
T genericField;
|
||||
|
||||
}
|
||||
|
||||
class FirstConcrete extends Generic<String> {
|
||||
|
||||
}
|
||||
|
||||
class SecondConcrete extends Generic<Integer> {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
class TestClassSet extends TreeSet<Object> {
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
class TestClassComplex {
|
||||
|
||||
String id;
|
||||
TestClassSet testClassSet;
|
||||
Map map;
|
||||
Collection collection;
|
||||
transient Object transientField;
|
||||
}
|
||||
|
||||
class AccessorTestClass {
|
||||
|
||||
// Valid getters and setters
|
||||
Long id;
|
||||
// Invalid getters and setters
|
||||
Long anotherId;
|
||||
|
||||
// Customized getter
|
||||
Number yetAnotherId;
|
||||
|
||||
// Customized setter
|
||||
Number yetYetAnotherId;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAnotherId() {
|
||||
return anotherId.toString();
|
||||
}
|
||||
|
||||
public void setAnotherId(String anotherId) {
|
||||
this.anotherId = Long.parseLong(anotherId);
|
||||
}
|
||||
|
||||
public Long getYetAnotherId() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setYetYetAnotherId(Object yetYetAnotherId) {
|
||||
this.yetYetAnotherId = null;
|
||||
}
|
||||
}
|
||||
|
||||
class SamplePersistentProperty extends AbstractPersistentProperty<SamplePersistentProperty> {
|
||||
|
||||
public SamplePersistentProperty(Field field, PropertyDescriptor propertyDescriptor,
|
||||
PersistentEntity<?, SamplePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
super(field, propertyDescriptor, owner, simpleTypeHolder);
|
||||
}
|
||||
|
||||
public boolean isIdProperty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isVersionProperty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Association<SamplePersistentProperty> createAssociation() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.annotation.TypeAlias;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentEntitySpec;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.Person;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
/**
|
||||
* Unit test for {@link BasicPersistentEntity}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
|
||||
@Mock
|
||||
T property;
|
||||
|
||||
@Test
|
||||
public void assertInvariants() {
|
||||
PersistentEntitySpec.assertInvariants(createEntity(null));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullTypeInformation() {
|
||||
new BasicPersistentEntity<Object, T>(null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullProperty() {
|
||||
createEntity(null).addPersistentProperty(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsNullForTypeAliasIfNoneConfigured() {
|
||||
|
||||
PersistentEntity<Entity, T> entity = new BasicPersistentEntity<Entity, T>(ClassTypeInformation.from(Entity.class));
|
||||
assertThat(entity.getTypeAlias(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsTypeAliasIfAnnotated() {
|
||||
|
||||
PersistentEntity<AliasedEntity, T> entity = new BasicPersistentEntity<AliasedEntity, T>(
|
||||
ClassTypeInformation.from(AliasedEntity.class));
|
||||
assertThat(entity.getTypeAlias(), is((Object) "foo"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-50
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void considersComparatorForPropertyOrder() {
|
||||
|
||||
BasicPersistentEntity<Person, T> entity = createEntity(new Comparator<T>() {
|
||||
public int compare(T o1, T o2) {
|
||||
return o1.getName().compareTo(o2.getName());
|
||||
}
|
||||
});
|
||||
|
||||
T lastName = (T) Mockito.mock(PersistentProperty.class);
|
||||
when(lastName.getName()).thenReturn("lastName");
|
||||
|
||||
T firstName = (T) Mockito.mock(PersistentProperty.class);
|
||||
when(firstName.getName()).thenReturn("firstName");
|
||||
|
||||
T ssn = (T) Mockito.mock(PersistentProperty.class);
|
||||
when(ssn.getName()).thenReturn("ssn");
|
||||
|
||||
entity.addPersistentProperty(lastName);
|
||||
entity.addPersistentProperty(firstName);
|
||||
entity.addPersistentProperty(ssn);
|
||||
|
||||
SortedSet<T> properties = (SortedSet<T>) ReflectionTestUtils.getField(entity, "properties");
|
||||
|
||||
assertThat(properties.size(), is(3));
|
||||
Iterator<T> iterator = properties.iterator();
|
||||
assertThat(iterator.next(), is(entity.getPersistentProperty("firstName")));
|
||||
assertThat(iterator.next(), is(entity.getPersistentProperty("lastName")));
|
||||
assertThat(iterator.next(), is(entity.getPersistentProperty("ssn")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-186
|
||||
*/
|
||||
@Test
|
||||
public void addingAndIdPropertySetsIdPropertyInternally() {
|
||||
|
||||
MutablePersistentEntity<Person, T> entity = createEntity(null);
|
||||
assertThat(entity.getIdProperty(), is(nullValue()));
|
||||
|
||||
when(property.isIdProperty()).thenReturn(true);
|
||||
entity.addPersistentProperty(property);
|
||||
assertThat(entity.getIdProperty(), is(property));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-186
|
||||
*/
|
||||
@Test
|
||||
public void rejectsIdPropertyIfAlreadySet() {
|
||||
|
||||
MutablePersistentEntity<Person, T> entity = createEntity(null);
|
||||
|
||||
when(property.isIdProperty()).thenReturn(true);
|
||||
|
||||
entity.addPersistentProperty(property);
|
||||
|
||||
try {
|
||||
entity.addPersistentProperty(property);
|
||||
fail("Expected MappingException!");
|
||||
} catch (MappingException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
private BasicPersistentEntity<Person, T> createEntity(Comparator<T> comparator) {
|
||||
return new BasicPersistentEntity<Person, T>(ClassTypeInformation.from(Person.class), comparator);
|
||||
}
|
||||
|
||||
@TypeAlias("foo")
|
||||
static class AliasedEntity {
|
||||
|
||||
}
|
||||
|
||||
static class Entity {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright 2012 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.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Version;
|
||||
import org.springframework.data.domain.Persistable;
|
||||
import org.springframework.data.mapping.MappingMetadataTests.SampleMappingContext;
|
||||
import org.springframework.data.mapping.model.MappingContextIsNewStrategyFactory.PropertyIsNullIsNewStrategy;
|
||||
import org.springframework.data.mapping.model.MappingContextIsNewStrategyFactory.PropertyIsNullOrZeroNumberIsNewStrategy;
|
||||
import org.springframework.data.support.IsNewStrategy;
|
||||
import org.springframework.data.support.IsNewStrategyFactory;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class MappingContextIsNewStrategyFactoryUnitTests {
|
||||
|
||||
IsNewStrategyFactory factory;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
factory = new MappingContextIsNewStrategyFactory(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsPropertyIsNullOrZeroIsNewStrategyForVersionedEntity() {
|
||||
|
||||
IsNewStrategy strategy = factory.getIsNewStrategy(VersionedEntity.class);
|
||||
assertThat(strategy, is(instanceOf(PropertyIsNullOrZeroNumberIsNewStrategy.class)));
|
||||
|
||||
VersionedEntity entity = new VersionedEntity();
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
|
||||
entity.id = 1L;
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
|
||||
entity.version = 0L;
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
|
||||
entity.version = 1L;
|
||||
assertThat(strategy.isNew(entity), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsPropertyIsNullOrZeroIsNewStrategyForPrimitiveVersionedEntity() {
|
||||
|
||||
IsNewStrategy strategy = factory.getIsNewStrategy(VersionedEntity.class);
|
||||
assertThat(strategy, is(instanceOf(PropertyIsNullOrZeroNumberIsNewStrategy.class)));
|
||||
|
||||
VersionedEntity entity = new VersionedEntity();
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
|
||||
entity.id = 1L;
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
|
||||
entity.version = 1L;
|
||||
assertThat(strategy.isNew(entity), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsPropertyIsNullIsNewStrategyForEntity() {
|
||||
|
||||
IsNewStrategy strategy = factory.getIsNewStrategy(Entity.class);
|
||||
assertThat(strategy, is(instanceOf(PropertyIsNullIsNewStrategy.class)));
|
||||
|
||||
Entity entity = new Entity();
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
|
||||
entity.id = 1L;
|
||||
assertThat(strategy.isNew(entity), is(false));
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
static class PersistableEntity implements Persistable<Long> {
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
boolean isNew = true;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean isNew() {
|
||||
return isNew;
|
||||
}
|
||||
}
|
||||
|
||||
static class VersionedEntity {
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
}
|
||||
|
||||
static class PrimitveVersionedEntity {
|
||||
|
||||
@Version
|
||||
long version = 0;
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
}
|
||||
|
||||
static class Entity {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2012 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.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.PreferredConstructor;
|
||||
import org.springframework.data.mapping.PreferredConstructor.Parameter;
|
||||
import org.springframework.data.mapping.model.PersistentEntityParameterValueProviderUnitTests.Outer.Inner;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PersistentEntityParameterValueProvider}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PersistentEntityParameterValueProviderUnitTests<P extends PersistentProperty<P>> {
|
||||
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Mock
|
||||
PropertyValueProvider<P> propertyValueProvider;
|
||||
@Mock
|
||||
P property;
|
||||
|
||||
/**
|
||||
* @see DATACMNS-134
|
||||
*/
|
||||
@Test
|
||||
public void usesParentObjectAsImplicitFirstConstructorArgument() {
|
||||
|
||||
Object outer = new Outer();
|
||||
|
||||
PersistentEntity<Inner, P> entity = new BasicPersistentEntity<Inner, P>(ClassTypeInformation.from(Inner.class)) {
|
||||
@Override
|
||||
public P getPersistentProperty(String name) {
|
||||
return property;
|
||||
}
|
||||
};
|
||||
PreferredConstructor<Inner, P> constructor = entity.getPersistenceConstructor();
|
||||
Iterator<Parameter<Object, P>> iterator = constructor.getParameters().iterator();
|
||||
|
||||
ParameterValueProvider<P> provider = new PersistentEntityParameterValueProvider<P>(entity, propertyValueProvider,
|
||||
outer);
|
||||
assertThat(provider.getParameterValue(iterator.next()), is(outer));
|
||||
assertThat(provider.getParameterValue(iterator.next()), is(nullValue()));
|
||||
assertThat(iterator.hasNext(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectsPropertyIfNameDoesNotMatch() {
|
||||
|
||||
PersistentEntity<Entity, P> entity = new BasicPersistentEntity<Entity, P>(ClassTypeInformation.from(Entity.class));
|
||||
ParameterValueProvider<P> provider = new PersistentEntityParameterValueProvider<P>(entity, propertyValueProvider,
|
||||
property);
|
||||
|
||||
PreferredConstructor<Entity, P> constructor = entity.getPersistenceConstructor();
|
||||
|
||||
exception.expect(MappingException.class);
|
||||
exception.expectMessage("bar");
|
||||
exception.expectMessage(Entity.class.getName());
|
||||
|
||||
provider.getParameterValue(constructor.getParameters().iterator().next());
|
||||
}
|
||||
|
||||
static class Outer {
|
||||
|
||||
class Inner {
|
||||
|
||||
Object myObject;
|
||||
|
||||
Inner(Object myObject) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class Entity {
|
||||
|
||||
String foo;
|
||||
|
||||
public Entity(String bar) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright 2012 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.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.mapping.PreferredConstructor.Parameter;
|
||||
import org.springframework.data.mapping.model.AbstractPersistentPropertyUnitTests.SamplePersistentProperty;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SpelExpressionParameterProviderUnitTests {
|
||||
|
||||
@Mock
|
||||
SpELExpressionEvaluator evaluator;
|
||||
@Mock
|
||||
ParameterValueProvider<SamplePersistentProperty> delegate;
|
||||
@Mock
|
||||
ConversionService conversionService;
|
||||
|
||||
SpELExpressionParameterValueProvider<SamplePersistentProperty> provider;
|
||||
|
||||
Parameter<Object, SamplePersistentProperty> parameter;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setUp() {
|
||||
provider = new SpELExpressionParameterValueProvider<SamplePersistentProperty>(evaluator, conversionService,
|
||||
delegate);
|
||||
|
||||
parameter = mock(Parameter.class);
|
||||
when(parameter.hasSpelExpression()).thenReturn(true);
|
||||
when(parameter.getRawType()).thenReturn(Object.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void delegatesIfParameterDoesNotHaveASpELExpression() {
|
||||
|
||||
Parameter<Object, SamplePersistentProperty> parameter = mock(Parameter.class);
|
||||
when(parameter.hasSpelExpression()).thenReturn(false);
|
||||
|
||||
provider.getParameterValue(parameter);
|
||||
verify(delegate, times(1)).getParameterValue(parameter);
|
||||
verify(evaluator, times(0)).evaluate("expression");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void evaluatesSpELExpression() {
|
||||
|
||||
when(parameter.getSpelExpression()).thenReturn("expression");
|
||||
|
||||
provider.getParameterValue(parameter);
|
||||
verify(delegate, times(0)).getParameterValue(parameter);
|
||||
verify(evaluator, times(1)).evaluate("expression");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handsSpELValueToConversionService() {
|
||||
|
||||
when(evaluator.evaluate(Mockito.any(String.class))).thenReturn("value");
|
||||
|
||||
provider.getParameterValue(parameter);
|
||||
verify(delegate, times(0)).getParameterValue(parameter);
|
||||
verify(conversionService, times(1)).convert("value", Object.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotConvertNullValue() {
|
||||
|
||||
when(evaluator.evaluate(Mockito.any(String.class))).thenReturn(null);
|
||||
|
||||
provider.getParameterValue(parameter);
|
||||
verify(delegate, times(0)).getParameterValue(parameter);
|
||||
verify(conversionService, times(0)).convert("value", Object.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsMassagedObjectOnOverride() {
|
||||
|
||||
provider = new SpELExpressionParameterValueProvider<SamplePersistentProperty>(evaluator, conversionService,
|
||||
delegate) {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <T> T potentiallyConvertSpelValue(Object object, Parameter<T, SamplePersistentProperty> parameter) {
|
||||
return (T) "FOO";
|
||||
}
|
||||
};
|
||||
|
||||
when(evaluator.evaluate(Mockito.anyString())).thenReturn("value");
|
||||
|
||||
Object result = provider.getParameterValue(parameter);
|
||||
assertThat(result, is((Object) "FOO"));
|
||||
verify(delegate, times(0)).getParameterValue(parameter);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user