Skip to content

DATAKV-129 - Allow usage of @AliasFor with @KeySpace. #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-keyvalue</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAKV-129-SNAPSHOT</version>

<name>Spring Data KeyValue</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 "";
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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> STRING_QUERY = new KeyValueQuery<String>("foo == 'two'");
Expand Down Expand Up @@ -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<? extends KeyValueEvent>... events) {
template.setEventTypesToPublish(new HashSet<Class<? extends KeyValueEvent>>(Arrays.asList(events)));
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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 {

Expand All @@ -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 })
Expand All @@ -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;
Expand Down