DATAKV-268 - Polishing.
Add test for non annotation driven keyspace resolution and update documentation. Original Pull Request: #46
This commit is contained in:
@@ -89,6 +89,10 @@ template.findAllOf(User.class); <2>
|
||||
<2> Returns only elements of type `User` stored in `persons` keyspace.
|
||||
====
|
||||
|
||||
TIP: `@KeySpace` supports https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/core.html#expressions[SpEL] expressions allowing dynamic keyspace configuration.
|
||||
|
||||
5.2.0.M3
|
||||
|
||||
[[key-value.keyspaces-custom]]
|
||||
=== Custom KeySpace Annotation
|
||||
|
||||
|
||||
@@ -55,9 +55,11 @@ public class BasicKeyValuePersistentEntity<T, P extends KeyValuePersistentProper
|
||||
String keySpace = AnnotationBasedKeySpaceResolver.INSTANCE.resolveKeySpace(type);
|
||||
|
||||
if (StringUtils.hasText(keySpace)) {
|
||||
|
||||
this.keyspace = keySpace;
|
||||
this.keyspaceExpression = detectExpression(keySpace);
|
||||
} else {
|
||||
|
||||
this.keyspace = resolveKeyspace(fallbackKeySpaceResolver, type);
|
||||
this.keyspaceExpression = null;
|
||||
}
|
||||
@@ -67,8 +69,8 @@ public class BasicKeyValuePersistentEntity<T, P extends KeyValuePersistentProper
|
||||
* Returns a SpEL {@link Expression} if the given {@link String} is actually an expression that does not evaluate to a
|
||||
* {@link LiteralExpression} (indicating that no subsequent evaluation is necessary).
|
||||
*
|
||||
* @param potentialExpression can be {@literal null}
|
||||
* @return
|
||||
* @param potentialExpression must not be {@literal null}
|
||||
* @return the parsed {@link Expression} or {@literal null}.
|
||||
*/
|
||||
@Nullable
|
||||
private static Expression detectExpression(String potentialExpression) {
|
||||
|
||||
@@ -22,7 +22,6 @@ import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.data.keyvalue.annotation.KeySpace;
|
||||
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
@@ -41,28 +40,38 @@ public class BasicKeyValuePersistentEntityUnitTests {
|
||||
@Test // DATAKV-268
|
||||
public void shouldDeriveKeyspaceFromClassName() {
|
||||
|
||||
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getPersistentEntity(KeyspaceEntity.class);
|
||||
|
||||
assertThat(persistentEntity.getKeySpace()).isEqualTo(KeyspaceEntity.class.getName());
|
||||
assertThat(mappingContext.getPersistentEntity(KeyspaceEntity.class).getKeySpace())
|
||||
.isEqualTo(KeyspaceEntity.class.getName());
|
||||
}
|
||||
|
||||
@Test // DATAKV-268
|
||||
public void shouldEvaluateKeyspaceExpression() {
|
||||
|
||||
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getPersistentEntity(ExpressionEntity.class);
|
||||
|
||||
persistentEntity.setEvaluationContextProvider(
|
||||
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension())));
|
||||
|
||||
assertThat(persistentEntity.getKeySpace()).isEqualTo("some");
|
||||
}
|
||||
|
||||
@Test // DATAKV-268
|
||||
public void shouldEvaluateEntityWithoutKeyspace() {
|
||||
|
||||
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getPersistentEntity(NoKeyspaceEntity.class);
|
||||
persistentEntity.setEvaluationContextProvider(
|
||||
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension())));
|
||||
|
||||
assertThat(persistentEntity.getKeySpace()).isEqualTo(NoKeyspaceEntity.class.getName());
|
||||
}
|
||||
|
||||
@KeySpace("#{myProperty}")
|
||||
static class ExpressionEntity {}
|
||||
|
||||
@KeySpace
|
||||
static class KeyspaceEntity {}
|
||||
|
||||
static class NoKeyspaceEntity {}
|
||||
|
||||
static class SampleExtension implements EvaluationContextExtension {
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user