From 4077177b7e1d0209c71ee097bb59bcc2d5593361 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 8 Mar 2016 14:01:25 +0100 Subject: [PATCH 1/3] DATAKV-129 - Allow usage of @AliasFor with @KeySpace. prepare issue branch. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 21c9e3f3..7d75b50e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-keyvalue - 1.1.0.BUILD-SNAPSHOT + 1.1.0.DATAKV-129-SNAPSHOT Spring Data KeyValue From 019cbd2e6936efad9f421a6702d0f3f6432ed161 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 8 Mar 2016 14:02:30 +0100 Subject: [PATCH 2/3] DATAKV-129 - Allow usage of @AliasFor with @KeySpace. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now allow using Spring's meta-annotation programming model. Own meta-annotations can be composed using @KeySpace and @AliasFor annotation. Example for a custom meta-annotation corresponding to @KeySpace("sessions") when it's used with @SessionsKeyspace class MyEntity {…}: @Persistent @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE }) @KeySpace public @interface SessionsKeyspace { @AliasFor(annotation = KeySpace.class, attribute = "value") String name() default "sessions"; } Original pull request: #19. --- .../AnnotationBasedKeySpaceResolver.java | 2 +- .../CustomKeySpaceAnnotationWithAliasFor.java | 40 +++++++++ ...mposedKeySpaceAnnotationUsingAliasFor.java | 83 +++++++++++++++++++ .../core/KeyValueTemplateUnitTests.java | 16 +++- ...otationBasedKeySpaceResolverUnitTests.java | 41 ++++++++- 5 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/springframework/data/keyvalue/CustomKeySpaceAnnotationWithAliasFor.java create mode 100644 src/test/java/org/springframework/data/keyvalue/TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor.java diff --git a/src/main/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolver.java b/src/main/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolver.java index 5103a276..50a51087 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolver.java +++ b/src/main/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolver.java @@ -59,7 +59,7 @@ public String resolveKeySpace(Class type) { private static Object getKeySpace(Class type) { - KeySpace keyspace = AnnotationUtils.findAnnotation(type, KeySpace.class); + KeySpace keyspace = AnnotatedElementUtils.findMergedAnnotation(type, KeySpace.class); if (keyspace != null) { return AnnotationUtils.getValue(keyspace); diff --git a/src/test/java/org/springframework/data/keyvalue/CustomKeySpaceAnnotationWithAliasFor.java b/src/test/java/org/springframework/data/keyvalue/CustomKeySpaceAnnotationWithAliasFor.java new file mode 100644 index 00000000..95f4b219 --- /dev/null +++ b/src/test/java/org/springframework/data/keyvalue/CustomKeySpaceAnnotationWithAliasFor.java @@ -0,0 +1,40 @@ +/* + * Copyright 2016 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.keyvalue; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.core.annotation.AliasFor; +import org.springframework.data.annotation.Persistent; +import org.springframework.data.keyvalue.annotation.KeySpace; + +/** + * Custom composed {@link Persistent} annotation using {@link AliasFor} on name attribute. + * + * @author Christoph Strobl + */ +@Persistent +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE }) +@KeySpace +public @interface CustomKeySpaceAnnotationWithAliasFor { + + @AliasFor(annotation = KeySpace.class, attribute = "value") + String name() default ""; +} diff --git a/src/test/java/org/springframework/data/keyvalue/TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor.java b/src/test/java/org/springframework/data/keyvalue/TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor.java new file mode 100644 index 00000000..33ad6d6b --- /dev/null +++ b/src/test/java/org/springframework/data/keyvalue/TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor.java @@ -0,0 +1,83 @@ +/* + * Copyright 2016 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.keyvalue; + +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Persistent; +import org.springframework.util.ObjectUtils; + +/** + * A {@link Persistent} type with {@link CustomKeySpaceAnnotationWithAliasFor}. + * + * @author Christoph Strobl + */ +@CustomKeySpaceAnnotationWithAliasFor(name = "aliased") +public class TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor { + + @Id String id; + String name; + + public TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor(String name) { + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ObjectUtils.nullSafeHashCode(this.id); + result = prime * result + ObjectUtils.nullSafeHashCode(this.name); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor)) { + return false; + } + TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor other = (TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor) obj; + if (!ObjectUtils.nullSafeEquals(this.id, other.id)) { + return false; + } + if (!ObjectUtils.nullSafeEquals(this.name, other.name)) { + return false; + } + return true; + } + +} diff --git a/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java b/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java index 59ee40b7..ec872d0a 100644 --- a/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 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. @@ -42,6 +42,7 @@ import org.springframework.data.annotation.Id; import org.springframework.data.keyvalue.SubclassOfTypeWithCustomComposedKeySpaceAnnotation; import org.springframework.data.keyvalue.TypeWithCustomComposedKeySpaceAnnotation; +import org.springframework.data.keyvalue.TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor; import org.springframework.data.keyvalue.core.event.KeyValueEvent; import org.springframework.data.keyvalue.core.event.KeyValueEvent.AfterDeleteEvent; import org.springframework.data.keyvalue.core.event.KeyValueEvent.AfterDropKeySpaceEvent; @@ -69,6 +70,8 @@ public class KeyValueTemplateUnitTests { private static final Foo FOO_TWO = new Foo("two"); private static final TypeWithCustomComposedKeySpaceAnnotation ALIASED = new TypeWithCustomComposedKeySpaceAnnotation( "super"); + private static final TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor ALIASED_USING_ALIAS_FOR = new TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor( + "super"); private static final SubclassOfTypeWithCustomComposedKeySpaceAnnotation SUBCLASS_OF_ALIASED = new SubclassOfTypeWithCustomComposedKeySpaceAnnotation( "sub"); private static final KeyValueQuery STRING_QUERY = new KeyValueQuery("foo == 'two'"); @@ -697,6 +700,17 @@ public void shouldPublishDropKeyspaceEventCorrectly() { assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName())); } + /** + * @see DATAKV-129 + */ + @Test + public void insertShouldRespectTypeAliasUsingAliasFor() { + + template.insert("1", ALIASED_USING_ALIAS_FOR); + + verify(adapterMock, times(1)).put("1", ALIASED_USING_ALIAS_FOR, "aliased"); + } + @SuppressWarnings("rawtypes") private void setEventsToPublish(Class... events) { template.setEventTypesToPublish(new HashSet>(Arrays.asList(events))); diff --git a/src/test/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolverUnitTests.java b/src/test/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolverUnitTests.java index 2d4dd418..f0c79ec2 100644 --- a/src/test/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolverUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolverUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 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. @@ -25,6 +25,7 @@ import org.junit.Before; import org.junit.Test; +import org.springframework.core.annotation.AliasFor; import org.springframework.data.annotation.Persistent; import org.springframework.data.keyvalue.TypeWithDirectKeySpaceAnnotation; import org.springframework.data.keyvalue.TypeWithInhteritedPersistentAnnotationNotHavingKeySpace; @@ -102,6 +103,22 @@ public void shouldResolveDirectKeySpaceAnnotationCorrectly() { assertThat(resolver.resolveKeySpace(TypeWithDirectKeySpaceAnnotation.class), is("rhaegar")); } + /** + * @see DATAKV-129 + */ + @Test + public void shouldResolveKeySpaceUsingAliasForCorrectly() { + assertThat(resolver.resolveKeySpace(EntityWithSetKeySpaceUsingAliasFor.class), is("viserys")); + } + + /** + * @see DATAKV-129 + */ + @Test + public void shouldResolveKeySpaceUsingAliasForCorrectlyOnSubClass() { + assertThat(resolver.resolveKeySpace(EntityWithInheritedKeySpaceUsingAliasFor.class), is("viserys")); + } + @PersistentAnnotationWithExplicitKeySpace static class EntityWithDefaultKeySpace { @@ -116,6 +133,15 @@ static class EntityWithInheritedKeySpace extends EntityWithSetKeySpace { } + @PersistentAnnotationWithExplicitKeySpace(firstname = "viserys") + static class EntityWithSetKeySpaceUsingAliasFor { + + } + + static class EntityWithInheritedKeySpaceUsingAliasFor extends EntityWithSetKeySpaceUsingAliasFor { + + } + @Persistent @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE }) @@ -127,6 +153,19 @@ static class EntityWithInheritedKeySpace extends EntityWithSetKeySpace { String lastnamne() default "targaryen"; } + @Persistent + @Retention(RetentionPolicy.RUNTIME) + @Target({ ElementType.TYPE }) + @KeySpace + static @interface PersistentAnnotationWithExplicitKeySpaceUsingAliasFor { + + @AliasFor(annotation = KeySpace.class, attribute = "value") + String firstname() default "daenerys"; + + String lastnamne() default "targaryen"; + + } + static class TypeWithoutKeySpace { String foo; From 7e603b514aab7182f0f8bd8c337bb1a08aa5aa31 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 10 Mar 2016 16:21:00 +0100 Subject: [PATCH 3/3] DATAKV-129 - Polishing. Updated license header. Original pull request: #19. --- .../keyvalue/core/mapping/AnnotationBasedKeySpaceResolver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolver.java b/src/main/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolver.java index 50a51087..1c444478 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolver.java +++ b/src/main/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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.