From 89b2aa162afc9f49c6319929106af28eec8eb34a Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Mon, 10 Oct 2011 10:29:49 +0200 Subject: [PATCH] DATACMNS-85 - Extended PersistentPropertyPath with getLength() and getParentPath(). --- .../DefaultPersistentPropertyPath.java | 16 ++++++++++ .../context/PersistentPropertyPath.java | 15 +++++++++ .../DefaultPersistenPropertyPathUnitTest.java | 31 +++++++++++++++---- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPath.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPath.java index fd4ce11c3..5e2d225af 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPath.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPath.java @@ -164,6 +164,13 @@ class DefaultPersistentPropertyPath> implements return new DefaultPersistentPropertyPath(properties); } + public PersistentPropertyPath getParentPath() { + int size = properties.size(); + if (size <= 1) { + return this; + } + return new DefaultPersistentPropertyPath(properties.subList(0,size-1)); + } /* * (non-Javadoc) * @see java.lang.Iterable#iterator() @@ -200,4 +207,13 @@ class DefaultPersistentPropertyPath> implements public int hashCode() { return properties.hashCode(); } + + @Override + public String toString() { + return toDotPath(); + } + + public int getLength() { + return properties.size(); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPath.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPath.java index 4cfe13c5c..f9f8acde3 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPath.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPath.java @@ -95,4 +95,19 @@ public interface PersistentPropertyPath> extends * @return */ PersistentPropertyPath getExtensionForBaseOf(PersistentPropertyPath base); + + /** + * Returns the parent path of the current {@link PersistentPropertyPath}, i.e. the path without the leaf property. This happens up to the base + * property. So for a direct property reference calling this method will result in returning the property. + * + * @return + */ + PersistentPropertyPath getParentPath(); + + /** + * Returns the length of the {@link PersistentPropertyPath}. + * + * @return + */ + int getLength(); } \ No newline at end of file diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/context/DefaultPersistenPropertyPathUnitTest.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/context/DefaultPersistenPropertyPathUnitTest.java index 3644d5b20..81088b1c0 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/context/DefaultPersistenPropertyPathUnitTest.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/context/DefaultPersistenPropertyPathUnitTest.java @@ -21,6 +21,7 @@ import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; import java.util.Arrays; +import java.util.Collections; import org.junit.Before; import org.junit.Test; @@ -29,7 +30,6 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.core.convert.converter.Converter; import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.mapping.context.PersistentPropertyPath; /** * Unit tests for {@link DefaultPersistentPropertyPath}. @@ -44,13 +44,15 @@ public class DefaultPersistenPropertyPathUnitTest converter; - + + PersistentPropertyPath noLeg; PersistentPropertyPath oneLeg; PersistentPropertyPath twoLegs; - + @Before @SuppressWarnings("unchecked") public void setUp() { + noLeg = new DefaultPersistentPropertyPath(Collections. emptyList()); oneLeg = new DefaultPersistentPropertyPath(Arrays.asList(first)); twoLegs = new DefaultPersistentPropertyPath(Arrays.asList(first, second)); } @@ -84,7 +86,7 @@ public class DefaultPersistenPropertyPathUnitTest extension = twoLegs.getExtensionForBaseOf(oneLeg); assertThat(extension, is((PersistentPropertyPath) new DefaultPersistentPropertyPath(Arrays.asList(second)))); } + + @Test + public void returnsTheCorrectParentPath() { + assertThat(twoLegs.getParentPath(), is(oneLeg)); + } + + @Test + public void returnsItselfAsParentPathIfSizeOne() { + assertThat(oneLeg.getParentPath(), is(oneLeg)); + } + + @Test + public void pathReturnsCorrectSize() { + assertThat(noLeg.getLength(), is(0)); + assertThat(oneLeg.getLength(), is(1)); + assertThat(twoLegs.getLength(), is(2)); + } }