From f24249955483f54f749f3487a079d0ffebe21ba1 Mon Sep 17 00:00:00 2001 From: Michael Nitschinger Date: Mon, 17 Dec 2012 15:52:12 +0100 Subject: [PATCH] Adding more initial code and first tests on properties. --- .../BasicCouchbasePersistentProperty.java | 35 ++++++- .../mapping/CouchbasePersistentProperty.java | 7 ++ .../spring/core/mapping/Document.java | 41 ++++++++ .../couchbase/spring/core/mapping/Field.java | 23 +++++ .../BasicCouchbasePersistentPropertyTest.java | 96 +++++++++++++++++++ 5 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/couchbase/spring/core/mapping/Document.java create mode 100644 src/main/java/com/couchbase/spring/core/mapping/Field.java create mode 100644 src/test/java/com/couchbase/spring/core/mapping/BasicCouchbasePersistentPropertyTest.java diff --git a/src/main/java/com/couchbase/spring/core/mapping/BasicCouchbasePersistentProperty.java b/src/main/java/com/couchbase/spring/core/mapping/BasicCouchbasePersistentProperty.java index 94e711ea..b2db7d6b 100644 --- a/src/main/java/com/couchbase/spring/core/mapping/BasicCouchbasePersistentProperty.java +++ b/src/main/java/com/couchbase/spring/core/mapping/BasicCouchbasePersistentProperty.java @@ -27,21 +27,54 @@ import java.lang.reflect.Field; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; import org.springframework.data.mapping.model.SimpleTypeHolder; +import org.springframework.util.StringUtils; - +/** + * Implements annotated property representations of a given Field instance. + * + * This object is used to gather information out of properties on objects + * that need to be persisted. For example, it supports overriding of the + * actual property name by providing custom annotations. + */ public class BasicCouchbasePersistentProperty extends AnnotationBasedPersistentProperty implements CouchbasePersistentProperty { + /** + * Create a new instance of the BasicCouchbasePersistentProperty class. + * + * @param field the field of the original reflection. + * @param propertyDescriptor the PropertyDescriptor. + * @param owner the original owner of the property. + * @param simpleTypeHolder the type holder. + */ public BasicCouchbasePersistentProperty(Field field, PropertyDescriptor propertyDescriptor, CouchbasePersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { super(field, propertyDescriptor, owner, simpleTypeHolder); } + /** + * Creates a new Association. + */ @Override protected Association createAssociation() { return new Association(this, null); } + /** + * Returns the field name of the property. + * + * The field name can be different from the actual property name by using a + * custom annotation. + */ + @Override + public String getFieldName() { + com.couchbase.spring.core.mapping.Field annotation = getField(). + getAnnotation(com.couchbase.spring.core.mapping.Field.class); + + return annotation != null && StringUtils.hasText(annotation.value()) + ? annotation.value() : field.getName(); + } + } diff --git a/src/main/java/com/couchbase/spring/core/mapping/CouchbasePersistentProperty.java b/src/main/java/com/couchbase/spring/core/mapping/CouchbasePersistentProperty.java index df9e7223..cc99cd4c 100644 --- a/src/main/java/com/couchbase/spring/core/mapping/CouchbasePersistentProperty.java +++ b/src/main/java/com/couchbase/spring/core/mapping/CouchbasePersistentProperty.java @@ -28,4 +28,11 @@ import org.springframework.data.mapping.PersistentProperty; public interface CouchbasePersistentProperty extends PersistentProperty { + /** + * Returns the field name of the property. + * + * The field name can be different from the actual property name by using a + * custom annotation. + */ + String getFieldName(); } diff --git a/src/main/java/com/couchbase/spring/core/mapping/Document.java b/src/main/java/com/couchbase/spring/core/mapping/Document.java new file mode 100644 index 00000000..e0b4dd19 --- /dev/null +++ b/src/main/java/com/couchbase/spring/core/mapping/Document.java @@ -0,0 +1,41 @@ +/** + * Copyright (C) 2009-2012 Couchbase, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING + * IN THE SOFTWARE. + */ + +package com.couchbase.spring.core.mapping; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import org.springframework.data.annotation.Persistent; + +/** + * Identifies a domain object to be persisted to Couchbase. + */ +@Persistent +@Inherited +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE }) +public @interface Document { + +} diff --git a/src/main/java/com/couchbase/spring/core/mapping/Field.java b/src/main/java/com/couchbase/spring/core/mapping/Field.java new file mode 100644 index 00000000..8fe39a49 --- /dev/null +++ b/src/main/java/com/couchbase/spring/core/mapping/Field.java @@ -0,0 +1,23 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.couchbase.spring.core.mapping; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Annotation to define custom metadata for document fields. + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +public @interface Field { + + /** + * The key to be used to store the field inside the document. + */ + String value() default ""; + +} diff --git a/src/test/java/com/couchbase/spring/core/mapping/BasicCouchbasePersistentPropertyTest.java b/src/test/java/com/couchbase/spring/core/mapping/BasicCouchbasePersistentPropertyTest.java new file mode 100644 index 00000000..6766b9f2 --- /dev/null +++ b/src/test/java/com/couchbase/spring/core/mapping/BasicCouchbasePersistentPropertyTest.java @@ -0,0 +1,96 @@ +/** + * Copyright (C) 2009-2012 Couchbase, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING + * IN THE SOFTWARE. + */ + +package com.couchbase.spring.core.mapping; + +import java.lang.reflect.Field; +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import org.springframework.data.annotation.Id; +import org.springframework.data.mapping.model.SimpleTypeHolder; +import org.springframework.data.util.ClassTypeInformation; +import org.springframework.util.ReflectionUtils; + +/** + * Verifies the correct behavior of properties on persistable objects. + */ +public class BasicCouchbasePersistentPropertyTest { + + /** + * Holds the entity to test against (contains the properties). + */ + CouchbasePersistentEntity entity; + + /** + * Create an instance of the demo entity. + */ + @Before + public void setUp() { + entity = new BasicCouchbasePersistentEntity( + ClassTypeInformation.from(Beer.class)); + } + + /** + * Verifies the name of the property without annotations. + */ + @Test + public void usesPropertyFieldName() { + Field field = ReflectionUtils.findField(Beer.class, "description"); + assertEquals("description", getPropertyFor(field).getFieldName()); + } + + /** + * Verifies the name of the property with custom name annotation. + */ + @Test + public void usesAnnotatedFieldName() { + Field field = ReflectionUtils.findField(Beer.class, "name"); + assertEquals("foobar", getPropertyFor(field).getFieldName()); + } + + /** + * Helper method to create a property out of the field. + * + * @param field the field to retrieve the properties from. + * @return the actual BasicCouchbasePersistentProperty instance. + */ + private CouchbasePersistentProperty getPropertyFor(Field field) { + return new BasicCouchbasePersistentProperty(field, null, entity, + new SimpleTypeHolder()); + } + + /** + * Simple POJO to test attribute properties and annotations. + */ + public class Beer { + + @Id + private String id; + + @com.couchbase.spring.core.mapping.Field("foobar") + String name; + + String description; + + } +}