DATACMNS-621 - Polishing.

Simplified implementation of Path conversion. Inlined helper domain types to not to pollute the packages with types that are only used within that very one test class.

Original pull request: #111.
This commit is contained in:
Oliver Gierke
2015-01-20 16:24:30 +01:00
parent 0ce87189ce
commit dacf34e796
5 changed files with 42 additions and 105 deletions

View File

@@ -19,14 +19,15 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.mysema.query.types.Expression;
import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.Path;
import com.mysema.query.types.PathMetadata;
/**
* Sort option for queries that wraps a querydsl {@link OrderSpecifier}.
@@ -145,24 +146,24 @@ public class QSort extends Sort implements Serializable {
return and(Arrays.asList(orderSpecifiers));
}
/**
* Recursively creates a dot-separated path for the property path.
*
* @param path must not be {@literal null}.
* @return
*/
private static String preparePropertyPath(Path<?> path) {
Stack<String> stack = new Stack<String>();
Path<?> pathElement = path;
while (pathElement.getMetadata() != null && pathElement.getMetadata().getParent() != null) {
PathMetadata<?> metadata = path.getMetadata();
Path<?> parent = metadata.getParent();
stack.push(pathElement.getMetadata().getElement().toString());
pathElement = pathElement.getMetadata().getParent();
if (parent == null) {
return "";
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pop());
if (!stack.isEmpty()) {
sb.append(".");
}
}
return sb.toString();
String basPath = preparePropertyPath(parent);
String element = metadata.getElement().toString();
return StringUtils.hasText(basPath) ? basPath.concat(".").concat(element) : basPath.concat(element);
}
}

View File

@@ -17,6 +17,9 @@ package org.springframework.data.querydsl;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.querydsl.QQSortUnitTests_WrapperToWrapWrapperForUserWrapper.*;
import static org.springframework.data.querydsl.QQSortUnitTests_WrapperToWrapWrapperForUserWrapper_WrapperForUserWrapper.*;
import static org.springframework.data.querydsl.QQSortUnitTests_WrapperToWrapWrapperForUserWrapper_WrapperForUserWrapper_UserWrapper.*;
import java.util.List;
@@ -26,6 +29,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import com.mysema.query.annotations.QueryInit;
import com.mysema.query.types.OrderSpecifier;
/**
@@ -171,7 +175,7 @@ public class QSortUnitTests {
@Test
public void shouldCreateSortForNestedPathCorrectly() {
QSort sort = new QSort(QUserWrapper.userWrapper.user.firstname.asc());
QSort sort = new QSort(userWrapper.user.firstname.asc());
assertThat(sort, hasItems(new Order(Direction.ASC, "user.firstname")));
}
@@ -182,7 +186,7 @@ public class QSortUnitTests {
@Test
public void shouldCreateSortForDeepNestedPathCorrectly() {
QSort sort = new QSort(QWrapperForUserWrapper.wrapperForUserWrapper.wrapper.user.firstname.asc());
QSort sort = new QSort(wrapperForUserWrapper.wrapper.user.firstname.asc());
assertThat(sort, hasItems(new Order(Direction.ASC, "wrapper.user.firstname")));
}
@@ -193,10 +197,27 @@ public class QSortUnitTests {
@Test
public void shouldCreateSortForReallyDeepNestedPathCorrectly() {
QSort sort = new QSort(
QWrapperToWrapWrapperForUserWrapper.wrapperToWrapWrapperForUserWrapper.wrapperForUserWrapper.wrapper.user.firstname
.asc());
QSort sort = new QSort(wrapperToWrapWrapperForUserWrapper.wrapperForUserWrapper.wrapper.user.firstname.asc());
assertThat(sort, hasItems(new Order(Direction.ASC, "wrapperForUserWrapper.wrapper.user.firstname")));
}
@com.mysema.query.annotations.QueryEntity
static class WrapperToWrapWrapperForUserWrapper {
@QueryInit("wrapper.user")//
WrapperForUserWrapper wrapperForUserWrapper;
@com.mysema.query.annotations.QueryEntity
static class WrapperForUserWrapper {
UserWrapper wrapper;
@com.mysema.query.annotations.QueryEntity
static class UserWrapper {
User user;
}
}
}
}

View File

@@ -1,28 +0,0 @@
/*
* Copyright 2015 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.querydsl;
import com.mysema.query.annotations.QueryEntity;
/**
* @author Christoph Strobl
*/
@QueryEntity
public class UserWrapper {
User user;
}

View File

@@ -1,28 +0,0 @@
/*
* Copyright 2015 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.querydsl;
import com.mysema.query.annotations.QueryEntity;
/**
* @author Christoph Strobl
*/
@QueryEntity
public class WrapperForUserWrapper {
UserWrapper wrapper;
}

View File

@@ -1,29 +0,0 @@
/*
* Copyright 2015 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.querydsl;
import com.mysema.query.annotations.QueryEntity;
import com.mysema.query.annotations.QueryInit;
/**
* @author Christoph Strobl
*/
@QueryEntity
public class WrapperToWrapWrapperForUserWrapper {
@QueryInit("wrapper.user")//
WrapperForUserWrapper wrapperForUserWrapper;
}