DATACMNS-84 - Adapted refactorings in SD Commons.

This commit is contained in:
Oliver Gierke
2011-10-07 20:58:30 +02:00
parent 18c06c6ec4
commit bdf59ea18a
2 changed files with 47 additions and 46 deletions

View File

@@ -16,10 +16,10 @@
package org.springframework.data.neo4j.repository.query;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.neo4j.mapping.Neo4jMappingContext;
import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty;
import org.springframework.data.neo4j.mapping.RelationshipInfo;
import org.springframework.data.repository.query.parser.Property;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -33,18 +33,18 @@ class MatchClause {
private final Iterable<Neo4jPersistentProperty> properties;
/**
* Creates a new {@link MatchClause} using the given {@link org.springframework.data.neo4j.mapping.Neo4jMappingContext} and {@link Property}.
* Creates a new {@link MatchClause} using the given
* {@link org.springframework.data.neo4j.mapping.Neo4jMappingContext} and {@link PropertyPath}.
*
* @param context must not be {@literal null}.
* @param property must not be {@literal null}.
*/
public MatchClause(Neo4jMappingContext context, Property property) {
public MatchClause(Neo4jMappingContext context, PropertyPath property) {
Assert.notNull(context);
Assert.notNull(property);
Class<?> rootType = property.getOwningType().getType();
this.properties = context.getPersistentPropertyPath(rootType, property.toDotPath());
this.properties = context.getPersistentPropertyPath(property);
}
/*
@@ -65,9 +65,10 @@ class MatchClause {
RelationshipInfo info = property.getRelationshipInfo();
Class<?> ownerType = property.getOwner().getType();
intermediate = intermediate == null ? asVariableReference(StringUtils.uncapitalize(ownerType.getSimpleName()))
: intermediate;
intermediate = String.format(getPattern(info), intermediate, info.getType(), asVariableReference(property.getName()));
intermediate = intermediate == null ? asVariableReference(StringUtils.uncapitalize(ownerType
.getSimpleName())) : intermediate;
intermediate = String.format(getPattern(info), intermediate, info.getType(),
asVariableReference(property.getName()));
}
return intermediate.toString();

View File

@@ -25,11 +25,11 @@ import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.graphdb.Direction;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.neo4j.annotation.Indexed;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.RelatedTo;
import org.springframework.data.neo4j.mapping.Neo4jMappingContext;
import org.springframework.data.repository.query.parser.Property;
/**
*
@@ -37,52 +37,52 @@ import org.springframework.data.repository.query.parser.Property;
*/
public class MatchClauseUnitTest {
Neo4jMappingContext context;
Neo4jMappingContext context;
@Before
public void setUp() {
context = new Neo4jMappingContext();
context.setInitialEntitySet(Collections.singleton(Person.class));
context.afterPropertiesSet();
}
@Before
public void setUp() {
context = new Neo4jMappingContext();
context.setInitialEntitySet(Collections.singleton(Person.class));
context.afterPropertiesSet();
}
@Test
public void buildsMatchExpressionForSimpleTraversalCorrectly() {
@Test
public void buildsMatchExpressionForSimpleTraversalCorrectly() {
MatchClause clause = new MatchClause(context, Property.from("group", Person.class));
assertThat(clause.toString(), is("(person)<-[:members]-(group)"));
}
MatchClause clause = new MatchClause(context, PropertyPath.from("group", Person.class));
assertThat(clause.toString(), is("(person)<-[:members]-(group)"));
}
@Test
public void createsMatchClassForDeepTraversal() {
@Test
public void createsMatchClassForDeepTraversal() {
MatchClause clause = new MatchClause(context, Property.from("group.members.age", Person.class));
assertThat(clause.toString(), is("(person)<-[:members]-(group)-[:members]->(members)"));
}
MatchClause clause = new MatchClause(context, PropertyPath.from("group.members.age", Person.class));
assertThat(clause.toString(), is("(person)<-[:members]-(group)-[:members]->(members)"));
}
@Test
public void stopsAtNonRelationShipProperty() {
@Test
public void stopsAtNonRelationShipPropertyPath() {
MatchClause clause = new MatchClause(context, Property.from("group.name", Person.class));
assertThat(clause.toString(), is("(person)<-[:members]-(group)"));
}
@NodeEntity
class Person {
private int age;
@RelatedTo(type = "members", direction = Direction.INCOMING)
private Group group;
}
MatchClause clause = new MatchClause(context, PropertyPath.from("group.name", Person.class));
assertThat(clause.toString(), is("(person)<-[:members]-(group)"));
}
@NodeEntity
class Group {
class Person {
@Indexed
private String name;
private int age;
@RelatedTo(type = "members", direction = Direction.OUTGOING)
private Set<Person> members;
}
@RelatedTo(type = "members", direction = Direction.INCOMING)
private Group group;
}
@NodeEntity
class Group {
@Indexed
private String name;
@RelatedTo(type = "members", direction = Direction.OUTGOING)
private Set<Person> members;
}
}