Skip to content

Commit 178f9be

Browse files
christophstroblmp911de
authored andcommitted
DATAKV-129 - Allow usage of @AliasFor with @keyspace.
Undo quick hacks from previous commit and add test explicitly testing keyspace resolution. Original pull request: #19.
1 parent b10d2ba commit 178f9be

File tree

6 files changed

+179
-6
lines changed

6 files changed

+179
-6
lines changed

src/main/java/org/springframework/data/keyvalue/annotation/KeySpace.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
@Documented
5959
@Retention(RetentionPolicy.RUNTIME)
6060
@Target(value = { METHOD, TYPE })
61-
@Persistent
6261
public @interface KeySpace {
6362

6463
String value() default "";

src/test/java/org/springframework/data/keyvalue/CustomKeySpaceAnnotation.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.lang.annotation.RetentionPolicy;
2121
import java.lang.annotation.Target;
2222

23-
import org.springframework.core.annotation.AliasFor;
2423
import org.springframework.data.annotation.Persistent;
2524
import org.springframework.data.keyvalue.annotation.KeySpace;
2625

@@ -32,9 +31,8 @@
3231
@Persistent
3332
@Retention(RetentionPolicy.RUNTIME)
3433
@Target({ ElementType.TYPE })
35-
@KeySpace
3634
public @interface CustomKeySpaceAnnotation {
3735

38-
@AliasFor(annotation = KeySpace.class, attribute = "value")
36+
@KeySpace
3937
String name() default "";
4038
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.keyvalue;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
import org.springframework.core.annotation.AliasFor;
24+
import org.springframework.data.annotation.Persistent;
25+
import org.springframework.data.keyvalue.annotation.KeySpace;
26+
27+
/**
28+
* Custom composed {@link Persistent} annotation using {@link AliasFor} on name attribute.
29+
*
30+
* @author Christoph Strobl
31+
*/
32+
@Persistent
33+
@Retention(RetentionPolicy.RUNTIME)
34+
@Target({ ElementType.TYPE })
35+
@KeySpace
36+
public @interface CustomKeySpaceAnnotationWithAliasFor {
37+
38+
@AliasFor(annotation = KeySpace.class, attribute = "value")
39+
String name() default "";
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.keyvalue;
17+
18+
import org.springframework.data.annotation.Id;
19+
import org.springframework.data.annotation.Persistent;
20+
import org.springframework.util.ObjectUtils;
21+
22+
/**
23+
* A {@link Persistent} type with {@link CustomKeySpaceAnnotationWithAliasFor}.
24+
*
25+
* @author Christoph Strobl
26+
*/
27+
@CustomKeySpaceAnnotationWithAliasFor(name = "aliased")
28+
public class TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor {
29+
30+
@Id String id;
31+
String name;
32+
33+
public TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor(String name) {
34+
this.name = name;
35+
}
36+
37+
public String getId() {
38+
return id;
39+
}
40+
41+
public void setId(String id) {
42+
this.id = id;
43+
}
44+
45+
public String getName() {
46+
return name;
47+
}
48+
49+
public void setName(String name) {
50+
this.name = name;
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
final int prime = 31;
56+
int result = 1;
57+
result = prime * result + ObjectUtils.nullSafeHashCode(this.id);
58+
result = prime * result + ObjectUtils.nullSafeHashCode(this.name);
59+
return result;
60+
}
61+
62+
@Override
63+
public boolean equals(Object obj) {
64+
if (this == obj) {
65+
return true;
66+
}
67+
if (obj == null) {
68+
return false;
69+
}
70+
if (!(obj instanceof TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor)) {
71+
return false;
72+
}
73+
TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor other = (TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor) obj;
74+
if (!ObjectUtils.nullSafeEquals(this.id, other.id)) {
75+
return false;
76+
}
77+
if (!ObjectUtils.nullSafeEquals(this.name, other.name)) {
78+
return false;
79+
}
80+
return true;
81+
}
82+
83+
}

src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@
4242
import org.springframework.data.annotation.Id;
4343
import org.springframework.data.keyvalue.SubclassOfTypeWithCustomComposedKeySpaceAnnotation;
4444
import org.springframework.data.keyvalue.TypeWithCustomComposedKeySpaceAnnotation;
45+
import org.springframework.data.keyvalue.TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor;
4546
import org.springframework.data.keyvalue.core.event.KeyValueEvent;
4647
import org.springframework.data.keyvalue.core.event.KeyValueEvent.AfterDeleteEvent;
4748
import org.springframework.data.keyvalue.core.event.KeyValueEvent.AfterDropKeySpaceEvent;
@@ -69,6 +70,8 @@ public class KeyValueTemplateUnitTests {
6970
private static final Foo FOO_TWO = new Foo("two");
7071
private static final TypeWithCustomComposedKeySpaceAnnotation ALIASED = new TypeWithCustomComposedKeySpaceAnnotation(
7172
"super");
73+
private static final TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor ALIASED_USING_ALIAS_FOR = new TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor(
74+
"super");
7275
private static final SubclassOfTypeWithCustomComposedKeySpaceAnnotation SUBCLASS_OF_ALIASED = new SubclassOfTypeWithCustomComposedKeySpaceAnnotation(
7376
"sub");
7477
private static final KeyValueQuery<String> STRING_QUERY = new KeyValueQuery<String>("foo == 'two'");
@@ -697,6 +700,17 @@ public void shouldPublishDropKeyspaceEventCorrectly() {
697700
assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName()));
698701
}
699702

703+
/**
704+
* @see DATAKV-129
705+
*/
706+
@Test
707+
public void insertShouldRespectTypeAliasUsingAliasFor() {
708+
709+
template.insert("1", ALIASED_USING_ALIAS_FOR);
710+
711+
verify(adapterMock, times(1)).put("1", ALIASED_USING_ALIAS_FOR, "aliased");
712+
}
713+
700714
@SuppressWarnings("rawtypes")
701715
private void setEventsToPublish(Class<? extends KeyValueEvent>... events) {
702716
template.setEventTypesToPublish(new HashSet<Class<? extends KeyValueEvent>>(Arrays.asList(events)));

src/test/java/org/springframework/data/keyvalue/core/mapping/AnnotationBasedKeySpaceResolverUnitTests.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525

2626
import org.junit.Before;
2727
import org.junit.Test;
28+
import org.springframework.core.annotation.AliasFor;
2829
import org.springframework.data.annotation.Persistent;
2930
import org.springframework.data.keyvalue.TypeWithDirectKeySpaceAnnotation;
3031
import org.springframework.data.keyvalue.TypeWithInhteritedPersistentAnnotationNotHavingKeySpace;
@@ -102,6 +103,22 @@ public void shouldResolveDirectKeySpaceAnnotationCorrectly() {
102103
assertThat(resolver.resolveKeySpace(TypeWithDirectKeySpaceAnnotation.class), is("rhaegar"));
103104
}
104105

106+
/**
107+
* @see DATAKV-129
108+
*/
109+
@Test
110+
public void shouldResolveKeySpaceUsingAliasForCorrectly() {
111+
assertThat(resolver.resolveKeySpace(EntityWithSetKeySpaceUsingAliasFor.class), is("viserys"));
112+
}
113+
114+
/**
115+
* @see DATAKV-129
116+
*/
117+
@Test
118+
public void shouldResolveKeySpaceUsingAliasForCorrectlyOnSubClass() {
119+
assertThat(resolver.resolveKeySpace(EntityWithInheritedKeySpaceUsingAliasFor.class), is("viserys"));
120+
}
121+
105122
@PersistentAnnotationWithExplicitKeySpace
106123
static class EntityWithDefaultKeySpace {
107124

@@ -116,6 +133,15 @@ static class EntityWithInheritedKeySpace extends EntityWithSetKeySpace {
116133

117134
}
118135

136+
@PersistentAnnotationWithExplicitKeySpace(firstname = "viserys")
137+
static class EntityWithSetKeySpaceUsingAliasFor {
138+
139+
}
140+
141+
static class EntityWithInheritedKeySpaceUsingAliasFor extends EntityWithSetKeySpaceUsingAliasFor {
142+
143+
}
144+
119145
@Persistent
120146
@Retention(RetentionPolicy.RUNTIME)
121147
@Target({ ElementType.TYPE })
@@ -127,6 +153,19 @@ static class EntityWithInheritedKeySpace extends EntityWithSetKeySpace {
127153
String lastnamne() default "targaryen";
128154
}
129155

156+
@Persistent
157+
@Retention(RetentionPolicy.RUNTIME)
158+
@Target({ ElementType.TYPE })
159+
@KeySpace
160+
static @interface PersistentAnnotationWithExplicitKeySpaceUsingAliasFor {
161+
162+
@AliasFor(annotation = KeySpace.class, attribute = "value")
163+
String firstname() default "daenerys";
164+
165+
String lastnamne() default "targaryen";
166+
167+
}
168+
130169
static class TypeWithoutKeySpace {
131170

132171
String foo;

0 commit comments

Comments
 (0)