From 2812e38296bb2c5e3faf3a4e50ce6123a34a3e50 Mon Sep 17 00:00:00 2001 From: Mattias Persson Date: Tue, 13 Sep 2016 16:27:10 +0200 Subject: [PATCH] Completes a previous change on InputEntityDecorators where everything should be Decorator, not Function there --- .../input/InputEntityDecorators.java | 25 ++++-- .../input/InputEntityDecoratorsTest.java | 79 +++++++++++++++---- 2 files changed, 82 insertions(+), 22 deletions(-) diff --git a/community/kernel/src/main/java/org/neo4j/unsafe/impl/batchimport/input/InputEntityDecorators.java b/community/kernel/src/main/java/org/neo4j/unsafe/impl/batchimport/input/InputEntityDecorators.java index 2512b9e0dc362..7a0254ae4a3d0 100644 --- a/community/kernel/src/main/java/org/neo4j/unsafe/impl/batchimport/input/InputEntityDecorators.java +++ b/community/kernel/src/main/java/org/neo4j/unsafe/impl/batchimport/input/InputEntityDecorators.java @@ -19,7 +19,7 @@ */ package org.neo4j.unsafe.impl.batchimport.input; -import java.util.function.Function; +import java.util.stream.Stream; import org.neo4j.helpers.ArrayUtil; import org.neo4j.unsafe.impl.batchimport.input.csv.Decorator; @@ -75,15 +75,26 @@ public static Decorator defaultRelationshipType( final String }; } - public static Function decorators( - final Function... decorators ) + public static Decorator decorators( + final Decorator... decorators ) { - return from -> { - for ( Function decorator : decorators ) + return new Decorator() + { + @Override + public ENTITY apply( ENTITY from ) + { + for ( Decorator decorator : decorators ) + { + from = decorator.apply( from ); + } + return from; + } + + @Override + public boolean isMutable() { - from = decorator.apply( from ); + return Stream.of( decorators ).anyMatch( Decorator::isMutable ); } - return from; }; } diff --git a/community/kernel/src/test/java/org/neo4j/unsafe/impl/batchimport/input/InputEntityDecoratorsTest.java b/community/kernel/src/test/java/org/neo4j/unsafe/impl/batchimport/input/InputEntityDecoratorsTest.java index 7df356d3637a3..24da884d3ff09 100644 --- a/community/kernel/src/test/java/org/neo4j/unsafe/impl/batchimport/input/InputEntityDecoratorsTest.java +++ b/community/kernel/src/test/java/org/neo4j/unsafe/impl/batchimport/input/InputEntityDecoratorsTest.java @@ -22,18 +22,22 @@ import org.junit.Test; import org.mockito.InOrder; -import java.util.function.Function; - import org.neo4j.helpers.ArrayUtil; +import org.neo4j.unsafe.impl.batchimport.input.csv.Decorator; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.neo4j.helpers.collection.Iterators.asSet; +import static org.neo4j.unsafe.impl.batchimport.input.InputEntityDecorators.additiveLabels; +import static org.neo4j.unsafe.impl.batchimport.input.InputEntityDecorators.decorators; +import static org.neo4j.unsafe.impl.batchimport.input.InputEntityDecorators.defaultRelationshipType; public class InputEntityDecoratorsTest { @@ -42,8 +46,7 @@ public void shouldProvideDefaultRelationshipType() throws Exception { // GIVEN String defaultType = "TYPE"; - Function decorator = - InputEntityDecorators.defaultRelationshipType( defaultType ); + Decorator decorator = defaultRelationshipType( defaultType ); // WHEN InputRelationship relationship = new InputRelationship( "source", 1, 0, InputEntity.NO_PROPERTIES, null, @@ -59,8 +62,7 @@ public void shouldNotOverrideAlreadySetRelationshipType() throws Exception { // GIVEN String defaultType = "TYPE"; - Function decorator = - InputEntityDecorators.defaultRelationshipType( defaultType ); + Decorator decorator = defaultRelationshipType( defaultType ); // WHEN String customType = "CUSTOM_TYPE"; @@ -77,8 +79,7 @@ public void shouldNotOverrideAlreadySetRelationshipTypeId() throws Exception { // GIVEN String defaultType = "TYPE"; - Function decorator = - InputEntityDecorators.defaultRelationshipType( defaultType ); + Decorator decorator = defaultRelationshipType( defaultType ); // WHEN Integer typeId = 5; @@ -96,7 +97,7 @@ public void shouldAddLabelsToNodeWithoutLabels() throws Exception { // GIVEN String[] toAdd = new String[] {"Add1", "Add2"}; - Function decorator = InputEntityDecorators.additiveLabels( toAdd ); + Decorator decorator = additiveLabels( toAdd ); // WHEN InputNode node = new InputNode( "source", 1, 0, "id", InputEntity.NO_PROPERTIES, null, null, null ); @@ -111,7 +112,7 @@ public void shouldAddMissingLabels() throws Exception { // GIVEN String[] toAdd = new String[] {"Add1", "Add2"}; - Function decorator = InputEntityDecorators.additiveLabels( toAdd ); + Decorator decorator = additiveLabels( toAdd ); // WHEN String[] nodeLabels = new String[] {"SomeOther"}; @@ -127,7 +128,7 @@ public void shouldNotTouchLabelsIfNodeHasLabelFieldSet() throws Exception { // GIVEN String[] toAdd = new String[] {"Add1", "Add2"}; - Function decorator = InputEntityDecorators.additiveLabels( toAdd ); + Decorator decorator = additiveLabels( toAdd ); // WHEN long labelField = 123L; @@ -143,9 +144,9 @@ public void shouldNotTouchLabelsIfNodeHasLabelFieldSet() throws Exception public void shouldCramMultipleDecoratorsIntoOne() throws Exception { // GIVEN - Function decorator1 = spy( new IdentityDecorator() ); - Function decorator2 = spy( new IdentityDecorator() ); - Function multi = InputEntityDecorators.decorators( decorator1, decorator2 ); + Decorator decorator1 = spy( new IdentityDecorator() ); + Decorator decorator2 = spy( new IdentityDecorator() ); + Decorator multi = decorators( decorator1, decorator2 ); // WHEN InputNode node = mock( InputNode.class ); @@ -158,12 +159,60 @@ public void shouldCramMultipleDecoratorsIntoOne() throws Exception order.verifyNoMoreInteractions(); } - private static class IdentityDecorator implements Function + @Test + public void shouldThinkMultiDecoratorIsntMutableIfNooneIs() throws Exception + { + // GIVEN + Decorator decorator1 = spy( new IdentityDecorator() ); + Decorator decorator2 = spy( new IdentityDecorator() ); + Decorator multi = decorators( decorator1, decorator2 ); + + // WHEN + boolean mutable = multi.isMutable(); + + // THEN + assertFalse( mutable ); + } + + @Test + public void shouldThinkMultiDecoratorIsMutableIfAnyIs() throws Exception + { + // GIVEN + Decorator decorator1 = spy( new IdentityDecorator() ); + Decorator decorator2 = spy( new IdentityDecorator( true ) ); + Decorator multi = decorators( decorator1, decorator2 ); + + // WHEN + boolean mutable = multi.isMutable(); + + // THEN + assertTrue( mutable ); + } + + private static class IdentityDecorator implements Decorator { + private final boolean mutable; + + public IdentityDecorator() + { + this( false ); + } + + public IdentityDecorator( boolean mutable ) + { + this.mutable = mutable; + } + @Override public InputNode apply( InputNode from ) throws RuntimeException { return from; } + + @Override + public boolean isMutable() + { + return mutable; + } } }