uowfile: fix tests for parallel execution
Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/cb2c52fb Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/cb2c52fb Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/cb2c52fb Branch: refs/heads/develop Commit: cb2c52fb3d44b0e95bdb913d69106168848c6f90 Parents: 5a5408c Author: Paul Merlin <[email protected]> Authored: Sat Nov 19 19:57:31 2016 +0100 Committer: Paul Merlin <[email protected]> Committed: Sat Nov 19 19:57:31 2016 +0100 ---------------------------------------------------------------------- .../library/uowfile/AbstractUoWFileTest.java | 92 -------------------- .../zest/library/uowfile/HasUoWFileTest.java | 58 +++++++++--- .../zest/library/uowfile/HasUoWFilesTest.java | 51 +++++++++-- 3 files changed, 91 insertions(+), 110 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/cb2c52fb/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/AbstractUoWFileTest.java ---------------------------------------------------------------------- diff --git a/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/AbstractUoWFileTest.java b/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/AbstractUoWFileTest.java deleted file mode 100644 index 928a02c..0000000 --- a/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/AbstractUoWFileTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.uowfile; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.apache.zest.io.Inputs; -import org.apache.zest.io.Outputs; -import org.apache.zest.test.AbstractZestTest; - -public abstract class AbstractUoWFileTest - extends AbstractZestTest -{ - protected static File baseTestDir; - - @BeforeClass - public static void beforeClass() - throws IOException - { - File testDir = new File( "build/uowfiletest" ); - if( !testDir.exists() ) - { - if( !testDir.mkdirs() ) - { - throw new IOException( "Unable to create directory: " + testDir ); - } - } - baseTestDir = testDir; - } - - @AfterClass - public static void afterClass() - { - // Delete test data - Stack<File> stack = new Stack<>(); - stack.push( baseTestDir ); - while( !stack.empty() ) - { - File each = stack.peek(); - if( each.isDirectory() ) - { - File[] children = each.listFiles(); - if( children.length > 0 ) - { - for( File child : children ) - { - stack.push( child ); - } - } - else - { - stack.pop().delete(); - } - } - else - { - stack.pop().delete(); - } - } - } - - protected final boolean isFileFirstLineEqualsTo( File file, String start ) - throws IOException - { - List<String> lines = new ArrayList<>(); - // This load the full file but used test resources are single line files - Inputs.text( file ).transferTo( Outputs.collection( lines ) ); - return lines.get( 0 ).trim().startsWith( start ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/cb2c52fb/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFileTest.java ---------------------------------------------------------------------- diff --git a/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFileTest.java b/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFileTest.java index 11cf220..8a53c0a 100644 --- a/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFileTest.java +++ b/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFileTest.java @@ -22,21 +22,21 @@ package org.apache.zest.library.uowfile; import java.io.File; import java.io.IOException; import java.net.URL; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; -import org.apache.zest.api.identity.HasIdentity; -import org.apache.zest.api.identity.Identity; -import org.apache.zest.api.unitofwork.UnitOfWorkFactory; -import org.junit.Before; -import org.junit.Test; +import java.util.stream.Stream; import org.apache.zest.api.concern.Concerns; import org.apache.zest.api.entity.EntityBuilder; +import org.apache.zest.api.identity.HasIdentity; +import org.apache.zest.api.identity.Identity; import org.apache.zest.api.injection.scope.Structure; import org.apache.zest.api.injection.scope.This; import org.apache.zest.api.mixin.Mixins; import org.apache.zest.api.property.Property; import org.apache.zest.api.unitofwork.UnitOfWork; import org.apache.zest.api.unitofwork.UnitOfWorkCompletionException; +import org.apache.zest.api.unitofwork.UnitOfWorkFactory; import org.apache.zest.api.unitofwork.concern.UnitOfWorkConcern; import org.apache.zest.api.unitofwork.concern.UnitOfWorkPropagation; import org.apache.zest.api.unitofwork.concern.UnitOfWorkRetry; @@ -49,21 +49,32 @@ import org.apache.zest.library.uowfile.bootstrap.UoWFileAssembler; import org.apache.zest.library.uowfile.internal.ConcurrentUoWFileModificationException; import org.apache.zest.library.uowfile.singular.HasUoWFileLifecycle; import org.apache.zest.library.uowfile.singular.UoWFileLocator; +import org.apache.zest.spi.ZestSPI; +import org.apache.zest.test.AbstractZestTest; import org.apache.zest.test.EntityTestAssembler; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; public class HasUoWFileTest - extends AbstractUoWFileTest + extends AbstractZestTest { private static final Logger LOGGER = LoggerFactory.getLogger( HasUoWFileTest.class ); private static final URL CREATION_CONTENT_URL = HasUoWFileTest.class.getResource( "creation.txt" ); private static final URL MODIFICATION_CONTENT_URL = HasUoWFileTest.class.getResource( "modification.txt" ); + @Rule + public final TemporaryFolder tmpDir = new TemporaryFolder(); + // START SNIPPET: entity // START SNIPPET: uowfile public interface TestedEntity @@ -83,10 +94,14 @@ public class HasUoWFileTest @This private HasIdentity meAsIdentity; + @Structure + private ZestSPI spi; + @Override public File locateAttachedFile() { - return new File( baseTestDir, meAsIdentity.identity().get().toString() ); + File baseDir = spi.entityDescriptorFor( meAsIdentity ).metaInfo( File.class ); + return new File( baseDir, meAsIdentity.identity().get().toString() ); } } // END SNIPPET: locator @@ -170,6 +185,7 @@ public class HasUoWFileTest module.entities( TestedEntity.class ).withMixins( TestedFileLocatorMixin.class ); // END SNIPPET: assembly + module.entities( TestedEntity.class ).setMetaInfo( tmpDir.getRoot() ); module.services( TestService.class ); new EntityTestAssembler().assemble( module ); new FileConfigurationAssembler().assemble( module ); @@ -207,7 +223,12 @@ public class HasUoWFileTest attachedFile = entity.attachedFile(); uow.complete(); } - assertTrue( "File content was not the good one", isFileFirstLineEqualsTo( attachedFile, "Creation" ) ); + try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) + { + assertThat("File content was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Creation" ) ); + } } @Test @@ -232,7 +253,12 @@ public class HasUoWFileTest { testService.modifyFile( entityId ); } - assertTrue( "File content after discarded modification was not the good one", isFileFirstLineEqualsTo( attachedFile, "Creation" ) ); + try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) + { + assertThat("File content after discarded modification was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Creation" ) ); + } // Testing completed modification try( UnitOfWork uow = unitOfWorkFactory.newUnitOfWork() ) @@ -240,7 +266,12 @@ public class HasUoWFileTest testService.modifyFile( entityId ); uow.complete(); } - assertTrue( "Modified file content was not the good one", isFileFirstLineEqualsTo( attachedFile, "Modification" ) ); + try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) + { + assertThat("Modified file content was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Modification" ) ); + } } @Test @@ -370,7 +401,12 @@ public class HasUoWFileTest } assertTrue( "There were errors during TestRetry", ex.isEmpty() ); - assertTrue( "Modified file content was not the good one", isFileFirstLineEqualsTo( attachedFile, "Modification" ) ); + try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) + { + assertThat("Modified file content was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Modification" ) ); + } } private TestedEntity createTestedEntity( UnitOfWork uow, String name ) http://git-wip-us.apache.org/repos/asf/zest-java/blob/cb2c52fb/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFilesTest.java ---------------------------------------------------------------------- diff --git a/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFilesTest.java b/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFilesTest.java index ce0ece2..4cdb275 100644 --- a/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFilesTest.java +++ b/libraries/uowfile/src/test/java/org/apache/zest/library/uowfile/HasUoWFilesTest.java @@ -22,12 +22,17 @@ package org.apache.zest.library.uowfile; import java.io.File; import java.io.IOException; import java.net.URL; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; +import java.util.stream.Stream; import org.apache.zest.api.identity.HasIdentity; import org.apache.zest.api.identity.Identity; import org.apache.zest.api.unitofwork.UnitOfWorkFactory; +import org.apache.zest.spi.ZestSPI; +import org.apache.zest.test.AbstractZestTest; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.apache.zest.api.concern.Concerns; import org.apache.zest.api.entity.EntityBuilder; @@ -50,20 +55,26 @@ import org.apache.zest.library.uowfile.internal.ConcurrentUoWFileModificationExc import org.apache.zest.library.uowfile.plural.HasUoWFilesLifecycle; import org.apache.zest.library.uowfile.plural.UoWFilesLocator; import org.apache.zest.test.EntityTestAssembler; +import org.junit.rules.TemporaryFolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; public class HasUoWFilesTest - extends AbstractUoWFileTest + extends AbstractZestTest { private static final Logger LOGGER = LoggerFactory.getLogger( HasUoWFilesTest.class ); private static final URL CREATION_CONTENT_URL = HasUoWFilesTest.class.getResource( "creation.txt" ); private static final URL MODIFICATION_CONTENT_URL = HasUoWFilesTest.class.getResource( "modification.txt" ); + @Rule + public final TemporaryFolder tmpDir = new TemporaryFolder(); + // START SNIPPET: uowfile public enum MyEnum { @@ -88,13 +99,17 @@ public class HasUoWFilesTest @This private HasIdentity meAsIdentity; + @Structure + private ZestSPI spi; + @Override public Iterable<File> locateAttachedFiles() { + File baseDir = spi.entityDescriptorFor( meAsIdentity ).metaInfo( File.class ); List<File> list = new ArrayList<>(); for( MyEnum eachValue : MyEnum.values() ) { - list.add( new File( baseTestDir, meAsIdentity.identity().get() + "." + eachValue.name() ) ); + list.add( new File( baseDir, meAsIdentity.identity().get() + "." + eachValue.name() ) ); } return list; } @@ -102,7 +117,8 @@ public class HasUoWFilesTest @Override public File locateAttachedFile( MyEnum key ) { - return new File( baseTestDir, meAsIdentity.identity().get() + "." + key.name() ); + File baseDir = spi.entityDescriptorFor( meAsIdentity ).metaInfo( File.class ); + return new File( baseDir, meAsIdentity.identity().get() + "." + key.name() ); } } // END SNIPPET: locator @@ -185,6 +201,7 @@ public class HasUoWFilesTest module.entities( TestedEntity.class ).withMixins( TestedFilesLocatorMixin.class ); // END SNIPPET: assembly + module.entities( TestedEntity.class ).setMetaInfo( tmpDir.getRoot() ); module.services( TestService.class ); new EntityTestAssembler().assemble( module ); new FileConfigurationAssembler().assemble( module ); @@ -222,7 +239,12 @@ public class HasUoWFilesTest attachedFile = entity.attachedFile( MyEnum.fileOne ); uow.complete(); } - assertTrue( "File content was not the good one", isFileFirstLineEqualsTo( attachedFile, "Creation" ) ); + try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) + { + assertThat("File content was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Creation" ) ); + } } @Test @@ -247,7 +269,12 @@ public class HasUoWFilesTest { testService.modifyFile( entityId ); } - assertTrue( "File content after discarded modification was not the good one", isFileFirstLineEqualsTo( attachedFile, "Creation" ) ); + try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) + { + assertThat("File content after discarded modification was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Creation" ) ); + } // Testing completed modification try( UnitOfWork uow = unitOfWorkFactory.newUnitOfWork() ) @@ -255,7 +282,12 @@ public class HasUoWFilesTest testService.modifyFile( entityId ); uow.complete(); } - assertTrue( "Modified file content was not the good one", isFileFirstLineEqualsTo( attachedFile, "Modification" ) ); + try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) + { + assertThat("Modified file content was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Modification" ) ); + } } @Test @@ -385,7 +417,12 @@ public class HasUoWFilesTest } assertTrue( "There were errors during TestRetry", ex.isEmpty() ); - assertTrue( "Modified file content was not the good one", isFileFirstLineEqualsTo( attachedFile, "Modification" ) ); + try( Stream<String> lines = Files.lines( attachedFile.toPath() ) ) + { + assertThat("Modified file content was not the good one", + lines.limit( 1 ).findFirst().get(), + equalTo( "Modification" ) ); + } } private TestedEntity createTestedOneEntityTwoFilesEntity( UnitOfWork uow, String name )
