Skip to content

Commit a73dda0

Browse files
DATAKV-268 - Polishing.
Add test for non annotation driven keyspace resolution and update documentation. Original Pull Request: #46
1 parent 07d2b8c commit a73dda0

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/main/asciidoc/key-value-repositories.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ template.findAllOf(User.class); <2>
8989
<2> Returns only elements of type `User` stored in `persons` keyspace.
9090
====
9191

92+
TIP: `@KeySpace` supports https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/core.html#expressions[SpEL] expressions allowing dynamic keyspace configuration.
93+
94+
5.2.0.M3
95+
9296
[[key-value.keyspaces-custom]]
9397
=== Custom KeySpace Annotation
9498

src/main/java/org/springframework/data/keyvalue/core/mapping/BasicKeyValuePersistentEntity.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ public BasicKeyValuePersistentEntity(TypeInformation<T> information,
5555
String keySpace = AnnotationBasedKeySpaceResolver.INSTANCE.resolveKeySpace(type);
5656

5757
if (StringUtils.hasText(keySpace)) {
58+
5859
this.keyspace = keySpace;
5960
this.keyspaceExpression = detectExpression(keySpace);
6061
} else {
62+
6163
this.keyspace = resolveKeyspace(fallbackKeySpaceResolver, type);
6264
this.keyspaceExpression = null;
6365
}
@@ -67,8 +69,8 @@ public BasicKeyValuePersistentEntity(TypeInformation<T> information,
6769
* Returns a SpEL {@link Expression} if the given {@link String} is actually an expression that does not evaluate to a
6870
* {@link LiteralExpression} (indicating that no subsequent evaluation is necessary).
6971
*
70-
* @param potentialExpression can be {@literal null}
71-
* @return
72+
* @param potentialExpression must not be {@literal null}
73+
* @return the parsed {@link Expression} or {@literal null}.
7274
*/
7375
@Nullable
7476
private static Expression detectExpression(String potentialExpression) {

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Map;
2323

2424
import org.junit.Test;
25-
2625
import org.springframework.data.keyvalue.annotation.KeySpace;
2726
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
2827
import org.springframework.data.mapping.context.MappingContext;
@@ -41,28 +40,38 @@ public class BasicKeyValuePersistentEntityUnitTests {
4140
@Test // DATAKV-268
4241
public void shouldDeriveKeyspaceFromClassName() {
4342

44-
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getPersistentEntity(KeyspaceEntity.class);
45-
46-
assertThat(persistentEntity.getKeySpace()).isEqualTo(KeyspaceEntity.class.getName());
43+
assertThat(mappingContext.getPersistentEntity(KeyspaceEntity.class).getKeySpace())
44+
.isEqualTo(KeyspaceEntity.class.getName());
4745
}
4846

4947
@Test // DATAKV-268
5048
public void shouldEvaluateKeyspaceExpression() {
5149

5250
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getPersistentEntity(ExpressionEntity.class);
53-
5451
persistentEntity.setEvaluationContextProvider(
5552
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension())));
5653

5754
assertThat(persistentEntity.getKeySpace()).isEqualTo("some");
5855
}
5956

57+
@Test // DATAKV-268
58+
public void shouldEvaluateEntityWithoutKeyspace() {
59+
60+
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getPersistentEntity(NoKeyspaceEntity.class);
61+
persistentEntity.setEvaluationContextProvider(
62+
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension())));
63+
64+
assertThat(persistentEntity.getKeySpace()).isEqualTo(NoKeyspaceEntity.class.getName());
65+
}
66+
6067
@KeySpace("#{myProperty}")
6168
static class ExpressionEntity {}
6269

6370
@KeySpace
6471
static class KeyspaceEntity {}
6572

73+
static class NoKeyspaceEntity {}
74+
6675
static class SampleExtension implements EvaluationContextExtension {
6776

6877
@Override

0 commit comments

Comments
 (0)