Skip to content

Commit

Permalink
Refactor the FacadeMethod test framework to make it less error prone …
Browse files Browse the repository at this point in the history
…to work with.
  • Loading branch information
chrisvest committed Aug 9, 2018
1 parent a9a4e09 commit 4004cc2
Show file tree
Hide file tree
Showing 25 changed files with 371 additions and 356 deletions.
Expand Up @@ -23,14 +23,12 @@


import org.neo4j.graphdb.index.Index; import org.neo4j.graphdb.index.Index;


import static org.neo4j.graphdb.NodeIndexFacadeMethods.ALL_NODE_INDEX_FACADE_METHODS;

public class MandatoryTransactionsForNodeIndexFacadeTest extends AbstractMandatoryTransactionsTest<Index<Node>> public class MandatoryTransactionsForNodeIndexFacadeTest extends AbstractMandatoryTransactionsTest<Index<Node>>
{ {
@Test @Test
public void shouldRequireTransactionsWhenCallingMethodsOnNodeIndexFacade() public void shouldRequireTransactionsWhenCallingMethodsOnNodeIndexFacade()
{ {
assertFacadeMethodsThrowNotInTransaction( obtainEntity(), ALL_NODE_INDEX_FACADE_METHODS ); assertFacadeMethodsThrowNotInTransaction( obtainEntity(), NodeIndexFacadeMethods.values() );
} }


@Override @Override
Expand Down
Expand Up @@ -23,14 +23,12 @@


import org.neo4j.graphdb.index.RelationshipIndex; import org.neo4j.graphdb.index.RelationshipIndex;


import static org.neo4j.graphdb.RelationshipIndexFacadeMethods.ALL_RELATIONSHIP_INDEX_FACADE_METHODS;

public class MandatoryTransactionsForRelationshipIndexFacadeTest extends AbstractMandatoryTransactionsTest<RelationshipIndex> public class MandatoryTransactionsForRelationshipIndexFacadeTest extends AbstractMandatoryTransactionsTest<RelationshipIndex>
{ {
@Test @Test
public void shouldRequireTransactionsWhenCallingMethodsOnRelationshipIndexFacade() public void shouldRequireTransactionsWhenCallingMethodsOnRelationshipIndexFacade()
{ {
assertFacadeMethodsThrowNotInTransaction( obtainEntity(), ALL_RELATIONSHIP_INDEX_FACADE_METHODS ); assertFacadeMethodsThrowNotInTransaction( obtainEntity(), RelationshipIndexFacadeMethods.values() );
} }


@Override @Override
Expand Down
Expand Up @@ -19,37 +19,38 @@
*/ */
package org.neo4j.graphdb; package org.neo4j.graphdb;


import org.neo4j.graphdb.index.Index; import java.util.function.Consumer;


import static java.util.Arrays.asList; import org.neo4j.graphdb.index.Index;
import static java.util.Collections.unmodifiableCollection;


public class NodeIndexFacadeMethods public enum NodeIndexFacadeMethods implements Consumer<Index<Node>>
{ {
private static final FacadeMethod<Index<Node>> GET = new FacadeMethod<>( "IndexHits<T> get( String key, Object value )", self -> self.get( "foo", "bar" ) ); GET( new FacadeMethod<>( "IndexHits<T> get( String key, Object value )", self -> self.get( "foo", "bar" ) ) ),
private static final FacadeMethod<Index<Node>> QUERY_BY_KEY = new FacadeMethod<>( "IndexHits<T> query( String key, Object queryOrQueryObject )", self -> self.query( "foo", "bar" ) ); QUERY_BY_KEY( new FacadeMethod<>( "IndexHits<T> query( String key, Object queryOrQueryObject )", self -> self.query( "foo", "bar" ) ) ),
private static final FacadeMethod<Index<Node>> QUERY = new FacadeMethod<>( "IndexHits<T> query( Object queryOrQueryObject )", self -> self.query( "foo" ) ); QUERY( new FacadeMethod<>( "IndexHits<T> query( Object queryOrQueryObject )", self -> self.query( "foo" ) ) ),
private static final FacadeMethod<Index<Node>> ADD = new FacadeMethod<>( "void add( T entity, String key, Object value )", self -> self.add( null, "foo", 42 ) ); ADD( new FacadeMethod<>( "void add( T entity, String key, Object value )", self -> self.add( null, "foo", 42 ) ) ),
private static final FacadeMethod<Index<Node>> REMOVE_BY_KEY_AND_VALUE = new FacadeMethod<>( "void remove( T entity, String key, Object value )", self -> self.remove( null, "foo", 42 ) ); REMOVE_BY_KEY_AND_VALUE( new FacadeMethod<>( "void remove( T entity, String key, Object value )", self -> self.remove( null, "foo", 42 ) ) ),
private static final FacadeMethod<Index<Node>> REMOVE_BY_KEY = new FacadeMethod<>( "void remove( T entity, String key )", self -> self.remove( null, "foo" ) ); REMOVE_BY_KEY( new FacadeMethod<>( "void remove( T entity, String key )", self -> self.remove( null, "foo" ) ) ),
private static final FacadeMethod<Index<Node>> REMOVE = new FacadeMethod<>( "void remove( T entity )", self -> self.remove( null ) ); REMOVE( new FacadeMethod<>( "void remove( T entity )", self -> self.remove( null ) ) ),
private static final FacadeMethod<Index<Node>> DELETE = new FacadeMethod<>( "void delete()", Index::delete ); DELETE( new FacadeMethod<>( "void delete()", Index::delete ) ),
private static final FacadeMethod<Index<Node>> PUT_IF_ABSENT = new FacadeMethod<>( "T putIfAbsent( T entity, String key, Object value )", self -> self.putIfAbsent( null, "foo", 42 ) ); PUT_IF_ABSENT( new FacadeMethod<>( "T putIfAbsent( T entity, String key, Object value )", self -> self.putIfAbsent( null, "foo", 42 ) ) );

private final FacadeMethod<Index<Node>> facadeMethod;

NodeIndexFacadeMethods( FacadeMethod<Index<Node>> facadeMethod )
{
this.facadeMethod = facadeMethod;
}


static final Iterable<FacadeMethod<Index<Node>>> ALL_NODE_INDEX_FACADE_METHODS = unmodifiableCollection( @Override
asList( public void accept( Index<Node> nodeIndex )
GET, {
QUERY_BY_KEY, facadeMethod.accept( nodeIndex );
QUERY, }
ADD,
REMOVE_BY_KEY_AND_VALUE,
REMOVE_BY_KEY,
REMOVE,
DELETE,
PUT_IF_ABSENT
) );


private NodeIndexFacadeMethods() @Override
public String toString()
{ {
return facadeMethod.toString();
} }
} }
Expand Up @@ -19,44 +19,45 @@
*/ */
package org.neo4j.graphdb; package org.neo4j.graphdb;


import org.neo4j.graphdb.index.RelationshipIndex; import java.util.function.Consumer;


import static java.util.Arrays.asList; import org.neo4j.graphdb.index.RelationshipIndex;
import static java.util.Collections.unmodifiableCollection;


public class RelationshipIndexFacadeMethods public enum RelationshipIndexFacadeMethods implements Consumer<RelationshipIndex>
{ {
private static final FacadeMethod<RelationshipIndex> GET_WITH_FILTER = new FacadeMethod<>( "IndexHits<Relationship> get( String key, Object valueOrNull, Node startNodeOrNull, Node endNodeOrNull )", ri -> ri.get( "foo", 42, null, null ) ); GET_WITH_FILTER( new FacadeMethod<>( "IndexHits<Relationship> get( String key, Object valueOrNull, Node startNodeOrNull, Node endNodeOrNull )",
private static final FacadeMethod<RelationshipIndex> QUERY_BY_KEY_WITH_FILTER = new FacadeMethod<>( "IndexHits<Relationship> query( String key, Object queryOrQueryObjectOrNull, Node startNodeOrNull, Node endNodeOrNull )", ri -> ri.query( "foo", 42, null, null ) ); ri -> ri.get( "foo", 42, null, null ) ) ),
private static final FacadeMethod<RelationshipIndex> QUERY_WITH_FILTER = new FacadeMethod<>( "IndexHits<Relationship> query( Object queryOrQueryObjectOrNull, Node startNodeOrNull, Node endNodeOrNull )", ri -> ri.query( 42, null, null ) ); QUERY_BY_KEY_WITH_FILTER(
private static final FacadeMethod<RelationshipIndex> GET = new FacadeMethod<>( "IndexHits<T> get( String key, Object value )", ri -> ri.get( "foo", "bar" ) ); new FacadeMethod<>( "IndexHits<Relationship> query( String key, Object queryOrQueryObjectOrNull, Node startNodeOrNull, Node endNodeOrNull )",
private static final FacadeMethod<RelationshipIndex> QUERY_BY_KEY = new FacadeMethod<>( "IndexHits<T> query( String key, Object queryOrQueryObject )", ri -> ri.query( "foo", "bar" ) ); ri -> ri.query( "foo", 42, null, null ) ) ),
private static final FacadeMethod<RelationshipIndex> QUERY = new FacadeMethod<>( "IndexHits<T> query( Object queryOrQueryObject )", ri -> ri.query( "foo" ) ); QUERY_WITH_FILTER( new FacadeMethod<>( "IndexHits<Relationship> query( Object queryOrQueryObjectOrNull, Node startNodeOrNull, Node endNodeOrNull )",
private static final FacadeMethod<RelationshipIndex> ADD = new FacadeMethod<>( "void add( T entity, String key, Object value )", ri -> ri.add( null, "foo", 42 ) ); ri -> ri.query( 42, null, null ) ) ),
private static final FacadeMethod<RelationshipIndex> REMOVE_BY_KEY_AND_VALUE = new FacadeMethod<>( "void remove( T entity, String key, Object value )", ri -> ri.remove( null, "foo", 42 ) ); GET( new FacadeMethod<>( "IndexHits<T> get( String key, Object value )", ri -> ri.get( "foo", "bar" ) ) ),
private static final FacadeMethod<RelationshipIndex> REMOVE_BY_KEY = new FacadeMethod<>( "void remove( T entity, String key )", ri -> ri.remove( null, "foo" ) ); QUERY_BY_KEY( new FacadeMethod<>( "IndexHits<T> query( String key, Object queryOrQueryObject )", ri -> ri.query( "foo", "bar" ) ) ),
private static final FacadeMethod<RelationshipIndex> REMOVE = new FacadeMethod<>( "void remove( T entity )", ri -> ri.remove( null ) ); QUERY( new FacadeMethod<>( "IndexHits<T> query( Object queryOrQueryObject )", ri -> ri.query( "foo" ) ) ),
private static final FacadeMethod<RelationshipIndex> DELETE = new FacadeMethod<>( "void delete()", RelationshipIndex::delete ); ADD( new FacadeMethod<>( "void add( T entity, String key, Object value )", ri -> ri.add( null, "foo", 42 ) ) ),
private static final FacadeMethod<RelationshipIndex> PUT_IF_ABSENT = new FacadeMethod<>( "T putIfAbsent( T entity, String key, Object value )", ri -> ri.putIfAbsent( null, "foo", 42 ) ); REMOVE_BY_KEY_AND_VALUE( new FacadeMethod<>( "void remove( T entity, String key, Object value )", ri -> ri.remove( null, "foo", 42 ) ) ),
REMOVE_BY_KEY( new FacadeMethod<>( "void remove( T entity, String key )", ri -> ri.remove( null, "foo" ) ) ),
REMOVE( new FacadeMethod<>( "void remove( T entity )", ri -> ri.remove( null ) ) ),
DELETE( new FacadeMethod<>( "void delete()", RelationshipIndex::delete ) ),
PUT_IF_ABSENT( new FacadeMethod<>( "T putIfAbsent( T entity, String key, Object value )", ri -> ri.putIfAbsent( null, "foo", 42 ) ) );

private final FacadeMethod<RelationshipIndex> facadeMethod;

RelationshipIndexFacadeMethods( FacadeMethod<RelationshipIndex> facadeMethod )
{
this.facadeMethod = facadeMethod;
}


static final Iterable<FacadeMethod<RelationshipIndex>> ALL_RELATIONSHIP_INDEX_FACADE_METHODS = @Override
unmodifiableCollection( public void accept( RelationshipIndex relationshipIndex )
asList( {
GET_WITH_FILTER, facadeMethod.accept( relationshipIndex );
QUERY_BY_KEY_WITH_FILTER, }
QUERY_WITH_FILTER,
GET,
QUERY_BY_KEY,
QUERY,
ADD,
REMOVE_BY_KEY_AND_VALUE,
REMOVE_BY_KEY,
REMOVE,
DELETE,
PUT_IF_ABSENT
) );


private RelationshipIndexFacadeMethods() @Override
public String toString()
{ {
return facadeMethod.toString();
} }
} }
Expand Up @@ -60,13 +60,13 @@ public void obtainEntityInTerminatedTransaction( Consumer<T> f )


protected abstract T obtainEntityInTransaction( GraphDatabaseService graphDatabaseService ); protected abstract T obtainEntityInTransaction( GraphDatabaseService graphDatabaseService );


public static <T> void assertFacadeMethodsThrowNotInTransaction( T entity, Iterable<FacadeMethod<T>> methods ) public static <T> void assertFacadeMethodsThrowNotInTransaction( T entity, Consumer<T>[] methods )
{ {
for ( FacadeMethod<T> method : methods ) for ( Consumer<T> method : methods )
{ {
try try
{ {
method.call( entity ); method.accept( entity );


fail( "Transactions are mandatory, also for reads: " + method ); fail( "Transactions are mandatory, also for reads: " + method );
} }
Expand All @@ -77,15 +77,15 @@ public static <T> void assertFacadeMethodsThrowNotInTransaction( T entity, Itera
} }
} }


public void assertFacadeMethodsThrowAfterTerminate( Iterable<FacadeMethod<T>> methods ) public void assertFacadeMethodsThrowAfterTerminate( Consumer<T>[] methods )
{ {
for ( final FacadeMethod<T> method : methods ) for ( final Consumer<T> method : methods )
{ {
obtainEntityInTerminatedTransaction( entity -> obtainEntityInTerminatedTransaction( entity ->
{ {
try try
{ {
method.call( entity ); method.accept( entity );


fail( "Transaction was terminated, yet not exception thrown in: " + method ); fail( "Transaction was terminated, yet not exception thrown in: " + method );
} }
Expand Down
Expand Up @@ -19,23 +19,31 @@
*/ */
package org.neo4j.graphdb; package org.neo4j.graphdb;


import org.neo4j.graphdb.schema.ConstraintCreator; import java.util.function.Consumer;


import static java.util.Arrays.asList; import org.neo4j.graphdb.schema.ConstraintCreator;
import static java.util.Collections.unmodifiableCollection;


public class ConstraintCreatorFacadeMethods public enum ConstraintCreatorFacadeMethods implements Consumer<ConstraintCreator>
{ {
private static final FacadeMethod<ConstraintCreator> UNIQUE = new FacadeMethod<>( "ConstraintCreator assertPropertyIsUnique()", self -> self.assertPropertyIsUnique( "property" ) ); UNIQUE( new FacadeMethod<>( "ConstraintCreator assertPropertyIsUnique()", self -> self.assertPropertyIsUnique( "property" ) ) ),
private static final FacadeMethod<ConstraintCreator> CREATE = new FacadeMethod<>( "ConstraintDefinition create()", ConstraintCreator::create ); CREATE( new FacadeMethod<>( "ConstraintDefinition create()", ConstraintCreator::create ) );

private final FacadeMethod<ConstraintCreator> facadeMethod;

ConstraintCreatorFacadeMethods( FacadeMethod<ConstraintCreator> facadeMethod )
{
this.facadeMethod = facadeMethod;
}


static final Iterable<FacadeMethod<ConstraintCreator>> ALL_CONSTRAINT_CREATOR_FACADE_METHODS = @Override
unmodifiableCollection( asList( public void accept( ConstraintCreator constraintCreator )
UNIQUE, {
CREATE facadeMethod.accept( constraintCreator );
) ); }


private ConstraintCreatorFacadeMethods() @Override
public String toString()
{ {
return facadeMethod.toString();
} }
} }
Expand Up @@ -19,30 +19,35 @@
*/ */
package org.neo4j.graphdb; package org.neo4j.graphdb;


import java.util.function.Consumer;

import org.neo4j.graphdb.schema.ConstraintDefinition; import org.neo4j.graphdb.schema.ConstraintDefinition;
import org.neo4j.graphdb.schema.ConstraintType; import org.neo4j.graphdb.schema.ConstraintType;


import static java.util.Arrays.asList; public enum ConstraintDefinitionFacadeMethods implements Consumer<ConstraintDefinition>
import static java.util.Collections.unmodifiableCollection;

public class ConstraintDefinitionFacadeMethods
{ {
private static final FacadeMethod<ConstraintDefinition> GET_LABEL = new FacadeMethod<>( "Label getLabel()", ConstraintDefinition::getLabel ); GET_LABEL( new FacadeMethod<>( "Label getLabel()", ConstraintDefinition::getLabel ) ),
private static final FacadeMethod<ConstraintDefinition> GET_RELATIONSHIP_TYPE = new FacadeMethod<>( "RelationshipType getRelationshipType()", ConstraintDefinition::getRelationshipType ); GET_RELATIONSHIP_TYPE( new FacadeMethod<>( "RelationshipType getRelationshipType()", ConstraintDefinition::getRelationshipType ) ),
private static final FacadeMethod<ConstraintDefinition> DROP = new FacadeMethod<>( "void drop()", ConstraintDefinition::drop ); DROP( new FacadeMethod<>( "void drop()", ConstraintDefinition::drop ) ),
private static final FacadeMethod<ConstraintDefinition> IS_CONSTRAINT_TYPE = new FacadeMethod<>( "boolean isConstraintType( ConstraintType type )", self -> self.isConstraintType( ConstraintType.UNIQUENESS ) ); IS_CONSTRAINT_TYPE( new FacadeMethod<>( "boolean isConstraintType( ConstraintType type )", self -> self.isConstraintType( ConstraintType.UNIQUENESS ) ) ),
private static final FacadeMethod<ConstraintDefinition> GET_PROPERTY_KEYS = new FacadeMethod<>( "Iterable<String> getPropertyKeys()", ConstraintDefinition::getPropertyKeys ); GET_PROPERTY_KEYS( new FacadeMethod<>( "Iterable<String> getPropertyKeys()", ConstraintDefinition::getPropertyKeys ) );

private final FacadeMethod<ConstraintDefinition> facadeMethod;

ConstraintDefinitionFacadeMethods( FacadeMethod<ConstraintDefinition> facadeMethod )
{
this.facadeMethod = facadeMethod;
}


static final Iterable<FacadeMethod<ConstraintDefinition>> ALL_CONSTRAINT_DEFINITION_FACADE_METHODS = @Override
unmodifiableCollection( asList( public void accept( ConstraintDefinition constraintDefinition )
GET_LABEL, {
GET_RELATIONSHIP_TYPE, facadeMethod.accept( constraintDefinition );
GET_PROPERTY_KEYS, }
DROP,
IS_CONSTRAINT_TYPE
) );


private ConstraintDefinitionFacadeMethods() @Override
public String toString()
{ {
return facadeMethod.toString();
} }
} }

0 comments on commit 4004cc2

Please sign in to comment.