diff --git a/community/io/src/test/java/org/neo4j/test/extension/DefaultFileSystemExtension.java b/community/io/src/test/java/org/neo4j/test/extension/DefaultFileSystemExtension.java index e3871a91ca8e2..2cc4d653d1d49 100644 --- a/community/io/src/test/java/org/neo4j/test/extension/DefaultFileSystemExtension.java +++ b/community/io/src/test/java/org/neo4j/test/extension/DefaultFileSystemExtension.java @@ -19,28 +19,12 @@ */ package org.neo4j.test.extension; -import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.ExtensionContext.Namespace; import org.neo4j.io.fs.DefaultFileSystemAbstraction; -public class DefaultFileSystemExtension extends StatefullFieldExtension implements AfterEachCallback +public class DefaultFileSystemExtension extends FileSystemExtension { - static final String FILE_SYSTEM = "fileSystem"; - static final Namespace FILE_SYSTEM_NAMESPACE = Namespace.create( FILE_SYSTEM ); - - @Override - public void afterEach( ExtensionContext context ) throws Exception - { - getStoredValue( context ).close(); - } - - @Override - protected String getFieldKey() - { - return FILE_SYSTEM; - } @Override protected Class getFieldType() @@ -53,10 +37,4 @@ protected DefaultFileSystemAbstraction createField( ExtensionContext extensionCo { return new DefaultFileSystemAbstraction(); } - - @Override - protected Namespace getNameSpace() - { - return FILE_SYSTEM_NAMESPACE; - } } diff --git a/community/io/src/test/java/org/neo4j/test/extension/EphemeralFileSystemExtension.java b/community/io/src/test/java/org/neo4j/test/extension/EphemeralFileSystemExtension.java new file mode 100644 index 0000000000000..1f2dc1ffe3156 --- /dev/null +++ b/community/io/src/test/java/org/neo4j/test/extension/EphemeralFileSystemExtension.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.test.extension; + +import org.junit.jupiter.api.extension.ExtensionContext; + +import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction; + +public class EphemeralFileSystemExtension extends FileSystemExtension +{ + @Override + protected Class getFieldType() + { + return EphemeralFileSystemAbstraction.class; + } + + @Override + protected EphemeralFileSystemAbstraction createField( ExtensionContext extensionContext ) + { + return new EphemeralFileSystemAbstraction(); + } +} diff --git a/community/io/src/test/java/org/neo4j/test/extension/EphemeralFileSystemExtensionTest.java b/community/io/src/test/java/org/neo4j/test/extension/EphemeralFileSystemExtensionTest.java new file mode 100644 index 0000000000000..d0beabadc1b1c --- /dev/null +++ b/community/io/src/test/java/org/neo4j/test/extension/EphemeralFileSystemExtensionTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.test.extension; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +@ExtendWith( EphemeralFileSystemExtension.class ) +class EphemeralFileSystemExtensionTest +{ + @Inject + EphemeralFileSystemAbstraction rootFileSystem; + + @Test + void fileSystemInjectionCreateFileSystem() + { + assertNotNull( rootFileSystem ); + } + + @Nested + class NestedFileSystemTest + { + @Inject + EphemeralFileSystemAbstraction nestedFileSystem; + + @Test + void nestedFileSystemInjection() + { + assertNotNull( nestedFileSystem ); + } + + @Test + void rootFileSystemAvailable() + { + assertNotNull( rootFileSystem ); + } + + @Test + void nestedAndRootFileSystemsAreTheSame() + { + assertSame( nestedFileSystem, rootFileSystem ); + } + } +} diff --git a/community/io/src/test/java/org/neo4j/test/extension/FileSystemExtension.java b/community/io/src/test/java/org/neo4j/test/extension/FileSystemExtension.java new file mode 100644 index 0000000000000..84e6ce2ed9d78 --- /dev/null +++ b/community/io/src/test/java/org/neo4j/test/extension/FileSystemExtension.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.test.extension; + +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; + +import org.neo4j.io.fs.FileSystemAbstraction; + +abstract class FileSystemExtension extends StatefullFieldExtension implements AfterEachCallback +{ + static final String FILE_SYSTEM = "fileSystem"; + static final Namespace FILE_SYSTEM_NAMESPACE = Namespace.create( FILE_SYSTEM ); + + @Override + public void afterEach( ExtensionContext context ) throws Exception + { + getStoredValue( context ).close(); + } + + @Override + protected String getFieldKey() + { + return FILE_SYSTEM; + } + + @Override + protected Namespace getNameSpace() + { + return FILE_SYSTEM_NAMESPACE; + } +} diff --git a/community/logging/pom.xml b/community/logging/pom.xml index 5237194a14bea..7d5d5f5db6676 100644 --- a/community/logging/pom.xml +++ b/community/logging/pom.xml @@ -89,6 +89,10 @@ the relevant Commercial Agreement. org.junit.jupiter junit-jupiter-api + + org.junit.jupiter + junit-jupiter-params + org.apache.maven.surefire surefire-logger-api diff --git a/community/logging/src/test/java/org/neo4j/logging/DuplicatingLogProviderTest.java b/community/logging/src/test/java/org/neo4j/logging/DuplicatingLogProviderTest.java index 48eba45dfe54c..bf2e2f7ab2e70 100644 --- a/community/logging/src/test/java/org/neo4j/logging/DuplicatingLogProviderTest.java +++ b/community/logging/src/test/java/org/neo4j/logging/DuplicatingLogProviderTest.java @@ -19,16 +19,16 @@ */ package org.neo4j.logging; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; -public class DuplicatingLogProviderTest +class DuplicatingLogProviderTest { @Test - public void shouldReturnSameLoggerForSameClass() + void shouldReturnSameLoggerForSameClass() { // Given DuplicatingLogProvider logProvider = new DuplicatingLogProvider(); @@ -39,7 +39,7 @@ public void shouldReturnSameLoggerForSameClass() } @Test - public void shouldReturnSameLoggerForSameContext() + void shouldReturnSameLoggerForSameContext() { // Given DuplicatingLogProvider logProvider = new DuplicatingLogProvider(); @@ -50,7 +50,7 @@ public void shouldReturnSameLoggerForSameContext() } @Test - public void shouldRemoveLogProviderFromDuplication() + void shouldRemoveLogProviderFromDuplication() { // Given AssertableLogProvider logProvider1 = new AssertableLogProvider(); @@ -61,7 +61,7 @@ public void shouldRemoveLogProviderFromDuplication() // When Log log = logProvider.getLog( getClass() ); log.info( "When the going gets weird" ); - assertThat( logProvider.remove( logProvider1 ), is( true ) ); + assertTrue( logProvider.remove( logProvider1 ) ); log.info( "The weird turn pro" ); // Then diff --git a/community/logging/src/test/java/org/neo4j/logging/DuplicatingLogTest.java b/community/logging/src/test/java/org/neo4j/logging/DuplicatingLogTest.java index 1ab8c996b0686..01834eda98f57 100644 --- a/community/logging/src/test/java/org/neo4j/logging/DuplicatingLogTest.java +++ b/community/logging/src/test/java/org/neo4j/logging/DuplicatingLogTest.java @@ -19,12 +19,12 @@ */ package org.neo4j.logging; -import org.junit.Test; +import org.junit.jupiter.api.Test; -public class DuplicatingLogTest +class DuplicatingLogTest { @Test - public void shouldOutputToMultipleLogs() + void shouldOutputToMultipleLogs() { // Given AssertableLogProvider logProvider = new AssertableLogProvider(); @@ -44,7 +44,7 @@ public void shouldOutputToMultipleLogs() } @Test - public void shouldBulkOutputToMultipleLogs() + void shouldBulkOutputToMultipleLogs() { // Given AssertableLogProvider logProvider = new AssertableLogProvider(); @@ -64,7 +64,7 @@ public void shouldBulkOutputToMultipleLogs() } @Test - public void shouldRemoveLogFromDuplication() + void shouldRemoveLogFromDuplication() { // Given AssertableLogProvider logProvider = new AssertableLogProvider(); @@ -87,7 +87,7 @@ public void shouldRemoveLogFromDuplication() } @Test - public void shouldRemoveLoggersFromDuplication() + void shouldRemoveLoggersFromDuplication() { // Given AssertableLogProvider logProvider = new AssertableLogProvider(); diff --git a/community/logging/src/test/java/org/neo4j/logging/FormattedLogProviderTest.java b/community/logging/src/test/java/org/neo4j/logging/FormattedLogProviderTest.java index d7646990de484..f39156d59d7cf 100644 --- a/community/logging/src/test/java/org/neo4j/logging/FormattedLogProviderTest.java +++ b/community/logging/src/test/java/org/neo4j/logging/FormattedLogProviderTest.java @@ -19,6 +19,8 @@ */ package org.neo4j.logging; +import org.junit.jupiter.api.Test; + import java.io.ByteArrayOutputStream; import java.io.PrintWriter; import java.io.StringWriter; @@ -27,21 +29,18 @@ import java.util.Date; import java.util.Map; -import org.junit.Test; - import org.neo4j.function.Suppliers; import static java.lang.String.format; - import static org.hamcrest.CoreMatchers.endsWith; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; -public class FormattedLogProviderTest +class FormattedLogProviderTest { @Test - public void shouldReturnSameLoggerForSameClass() + void shouldReturnSameLoggerForSameClass() { // Given FormattedLogProvider logProvider = FormattedLogProvider.toOutputStream( new ByteArrayOutputStream() ); @@ -52,7 +51,7 @@ public void shouldReturnSameLoggerForSameClass() } @Test - public void shouldReturnSameLoggerForSameContext() + void shouldReturnSameLoggerForSameContext() { // Given FormattedLogProvider logProvider = FormattedLogProvider.toOutputStream( new ByteArrayOutputStream() ); @@ -63,7 +62,7 @@ public void shouldReturnSameLoggerForSameContext() } @Test - public void shouldLogWithAbbreviatedClassNameAsContext() + void shouldLogWithAbbreviatedClassNameAsContext() { // Given StringWriter writer = new StringWriter(); @@ -78,7 +77,7 @@ public void shouldLogWithAbbreviatedClassNameAsContext() } @Test - public void shouldSetLevelForLogWithMatchingContext() + void shouldSetLevelForLogWithMatchingContext() { // Given StringWriter writer = new StringWriter(); @@ -98,7 +97,7 @@ public void shouldSetLevelForLogWithMatchingContext() } @Test - public void shouldSetLevelForLogWithPartiallyMatchingContext() + void shouldSetLevelForLogWithPartiallyMatchingContext() { // Given StringWriter writer = new StringWriter(); diff --git a/community/logging/src/test/java/org/neo4j/logging/FormattedLogTest.java b/community/logging/src/test/java/org/neo4j/logging/FormattedLogTest.java index 1dedcde0bb57c..36fced8fd901b 100644 --- a/community/logging/src/test/java/org/neo4j/logging/FormattedLogTest.java +++ b/community/logging/src/test/java/org/neo4j/logging/FormattedLogTest.java @@ -19,6 +19,8 @@ */ package org.neo4j.logging; +import org.junit.jupiter.api.Test; + import java.io.PrintWriter; import java.io.StringWriter; import java.time.ZoneOffset; @@ -26,23 +28,20 @@ import java.util.IllegalFormatException; import java.util.function.Supplier; -import org.junit.Test; - import org.neo4j.function.Suppliers; import static java.lang.String.format; - import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; -public class FormattedLogTest +class FormattedLogTest { private static final Supplier DATE_TIME_SUPPLIER = () -> ZonedDateTime.of( 1984, 10, 26, 4, 23, 24, 343000000, ZoneOffset.UTC ); @Test - public void logShouldWriteMessage() + void logShouldWriteMessage() { // Given StringWriter writer = new StringWriter(); @@ -57,7 +56,7 @@ public void logShouldWriteMessage() } @Test - public void logShouldWriteMessageAndThrowable() + void logShouldWriteMessageAndThrowable() { // Given StringWriter writer = new StringWriter(); @@ -76,7 +75,7 @@ public void logShouldWriteMessageAndThrowable() } @Test - public void logShouldWriteMessageAndThrowableWithNullMessage() + void logShouldWriteMessageAndThrowableWithNullMessage() { // Given StringWriter writer = new StringWriter(); @@ -93,7 +92,7 @@ public void logShouldWriteMessageAndThrowableWithNullMessage() } @Test - public void logShouldWriteMessageWithFormat() + void logShouldWriteMessageWithFormat() { // Given StringWriter writer = new StringWriter(); @@ -112,7 +111,7 @@ public void logShouldWriteMessageWithFormat() } @Test - public void logShouldWriteNotFormattedMessageWhenNoParametersGiven() + void logShouldWriteNotFormattedMessageWhenNoParametersGiven() { // Given StringWriter writer = new StringWriter(); @@ -129,27 +128,18 @@ public void logShouldWriteNotFormattedMessageWhenNoParametersGiven() } @Test - public void logShouldFailAndWriteNothingForInvalidParametersArray() + void logShouldFailAndWriteNothingForInvalidParametersArray() { // Given StringWriter writer = new StringWriter(); Log log = newFormattedLog( writer ); - try - { - // When - log.info( "%s like me. A T-%d, advanced prototype.", "Not", "1000", 1000 ); - fail( "Should have thrown " + IllegalFormatException.class ); - } - catch ( IllegalFormatException ife ) - { - // Then - assertThat( writer.toString(), equalTo( "" ) ); - } + assertThrows( IllegalFormatException.class, () -> log.info( "%s like me. A T-%d, advanced prototype.", "Not", "1000", 1000 ) ); + assertThat( writer.toString(), equalTo( "" ) ); } @Test - public void shouldNotWriteLogIfLevelIsHigherThanWritten() + void shouldNotWriteLogIfLevelIsHigherThanWritten() { // Given StringWriter writer = new StringWriter(); @@ -163,7 +153,7 @@ public void shouldNotWriteLogIfLevelIsHigherThanWritten() } @Test - public void shouldAllowLevelToBeChanged() + void shouldAllowLevelToBeChanged() { // Given StringWriter writer = new StringWriter(); diff --git a/community/logging/src/test/java/org/neo4j/logging/RotatingFileOutputStreamSupplierTest.java b/community/logging/src/test/java/org/neo4j/logging/RotatingFileOutputStreamSupplierTest.java index 84606b23ec0bb..f9273d990d6dc 100644 --- a/community/logging/src/test/java/org/neo4j/logging/RotatingFileOutputStreamSupplierTest.java +++ b/community/logging/src/test/java/org/neo4j/logging/RotatingFileOutputStreamSupplierTest.java @@ -19,9 +19,9 @@ */ package org.neo4j.logging; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import java.io.FileNotFoundException; @@ -54,17 +54,23 @@ import org.neo4j.io.fs.DefaultFileSystemAbstraction; import org.neo4j.io.fs.FileSystemAbstraction; import org.neo4j.logging.RotatingFileOutputStreamSupplier.RotationListener; +import org.neo4j.test.extension.EphemeralFileSystemExtension; +import org.neo4j.test.extension.Inject; +import org.neo4j.test.extension.SuppressOutputExtension; +import org.neo4j.test.extension.TestDirectoryExtension; import org.neo4j.test.rule.SuppressOutput; import org.neo4j.test.rule.TestDirectory; +import static java.time.Duration.ofMillis; import static java.util.concurrent.TimeUnit.SECONDS; import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.CoreMatchers.sameInstance; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTimeout; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doThrow; @@ -75,16 +81,17 @@ import static org.neo4j.logging.FormattedLog.OUTPUT_STREAM_CONVERTER; import static org.neo4j.logging.RotatingFileOutputStreamSupplier.getAllArchives; -public class RotatingFileOutputStreamSupplierTest +@ExtendWith( {SuppressOutputExtension.class, EphemeralFileSystemExtension.class, TestDirectoryExtension.class} ) +class RotatingFileOutputStreamSupplierTest { private static final long TEST_TIMEOUT_MILLIS = 10_000; private static final java.util.concurrent.Executor DIRECT_EXECUTOR = Runnable::run; - private FileSystemAbstraction fileSystem = new EphemeralFileSystemAbstraction(); - - @Rule - public final TestDirectory testDirectory = TestDirectory.testDirectory( getClass(), fileSystem ); - @Rule - public final SuppressOutput suppressOutput = SuppressOutput.suppressAll(); + @Inject + private EphemeralFileSystemAbstraction fileSystem; + @Inject + private TestDirectory testDirectory; + @Inject + private SuppressOutput suppressOutput; private File logFile; private File archiveLogFile1; @@ -97,8 +104,8 @@ public class RotatingFileOutputStreamSupplierTest private File archiveLogFile8; private File archiveLogFile9; - @Before - public void setup() + @BeforeEach + void setup() { File logDir = testDirectory.directory(); logFile = new File( logDir, "logfile.log" ); @@ -114,14 +121,14 @@ public void setup() } @Test - public void createsLogOnConstruction() throws Exception + void createsLogOnConstruction() throws Exception { new RotatingFileOutputStreamSupplier( fileSystem, logFile, 250000, 0, 10, DIRECT_EXECUTOR ); assertThat( fileSystem.fileExists( logFile ), is( true ) ); } @Test - public void rotatesLogWhenSizeExceeded() throws Exception + void rotatesLogWhenSizeExceeded() throws Exception { RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier( fileSystem, logFile, 10, 0, 10, DIRECT_EXECUTOR ); @@ -146,7 +153,7 @@ public void rotatesLogWhenSizeExceeded() throws Exception } @Test - public void limitsNumberOfArchivedLogs() throws Exception + void limitsNumberOfArchivedLogs() throws Exception { RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier( fileSystem, logFile, 10, 0, 2, DIRECT_EXECUTOR ); @@ -177,67 +184,70 @@ public void limitsNumberOfArchivedLogs() throws Exception assertThat( fileSystem.fileExists( archiveLogFile3 ), is( false ) ); } - @Test( timeout = TEST_TIMEOUT_MILLIS ) - public void rotationShouldNotDeadlockOnListener() throws Exception + @Test + void rotationShouldNotDeadlockOnListener() { - String logContent = "Output file created"; - final AtomicReference listenerException = new AtomicReference<>( null ); - CountDownLatch latch = new CountDownLatch( 1 ); - RotationListener listener = new RotationListener() + assertTimeout( ofMillis( TEST_TIMEOUT_MILLIS ), () -> { - @Override - public void outputFileCreated( OutputStream out ) + String logContent = "Output file created"; + final AtomicReference listenerException = new AtomicReference<>( null ); + CountDownLatch latch = new CountDownLatch( 1 ); + RotationListener listener = new RotationListener() { - try + @Override + public void outputFileCreated( OutputStream out ) { - Thread thread = new Thread( () -> + try { - try - { - out.write( logContent.getBytes() ); - out.flush(); - } - catch ( IOException e ) + Thread thread = new Thread( () -> { - listenerException.set( e ); - } - } ); - thread.start(); - thread.join(); + try + { + out.write( logContent.getBytes() ); + out.flush(); + } + catch ( IOException e ) + { + listenerException.set( e ); + } + } ); + thread.start(); + thread.join(); + } + catch ( Exception e ) + { + listenerException.set( e ); + } + super.outputFileCreated( out ); } - catch ( Exception e ) + + @Override + public void rotationCompleted( OutputStream out ) { - listenerException.set( e ); + latch.countDown(); } - super.outputFileCreated( out ); - } + }; + ExecutorService executor = Executors.newSingleThreadExecutor(); + DefaultFileSystemAbstraction defaultFileSystemAbstraction = new DefaultFileSystemAbstraction(); + RotatingFileOutputStreamSupplier supplier = + new RotatingFileOutputStreamSupplier( defaultFileSystemAbstraction, logFile, 0, 0, 10, executor, listener ); - @Override - public void rotationCompleted( OutputStream out ) + OutputStream outputStream = supplier.get(); + LockingPrintWriter lockingPrintWriter = new LockingPrintWriter( outputStream ); + lockingPrintWriter.withLock( () -> { - latch.countDown(); - } - }; - ExecutorService executor = Executors.newSingleThreadExecutor(); - DefaultFileSystemAbstraction defaultFileSystemAbstraction = new DefaultFileSystemAbstraction(); - RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier( defaultFileSystemAbstraction, - logFile, 0, 0, 10, executor, listener ); + supplier.rotate(); + latch.await(); + return Void.TYPE; + } ); - OutputStream outputStream = supplier.get(); - LockingPrintWriter lockingPrintWriter = new LockingPrintWriter( outputStream ); - lockingPrintWriter.withLock( () -> - { - supplier.rotate(); - latch.await(); - return Void.TYPE; - } ); - - shutDownExecutor( executor ); + shutDownExecutor( executor ); - List strings = Files.readAllLines( logFile.toPath() ); - String actual = String.join( "", strings ); - assertEquals( logContent, actual ); - assertNull( listenerException.get() ); + List strings = Files.readAllLines( logFile.toPath() ); + String actual = String.join( "", strings ); + assertEquals( logContent, actual ); + assertNull( listenerException.get() ); + } ); } private void shutDownExecutor( ExecutorService executor ) throws InterruptedException @@ -251,7 +261,7 @@ private void shutDownExecutor( ExecutorService executor ) throws InterruptedExce } @Test - public void shouldNotRotateLogWhenSizeExceededButNotDelay() throws Exception + void shouldNotRotateLogWhenSizeExceededButNotDelay() throws Exception { UpdatableLongSupplier clock = new UpdatableLongSupplier( System.currentTimeMillis() ); RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier( clock, fileSystem, logFile, @@ -283,7 +293,7 @@ public void shouldNotRotateLogWhenSizeExceededButNotDelay() throws Exception } @Test - public void shouldFindAllArchives() throws Exception + void shouldFindAllArchives() throws Exception { RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier( fileSystem, logFile, 10, 0, 2, DIRECT_EXECUTOR ); @@ -301,7 +311,7 @@ public void shouldFindAllArchives() throws Exception } @Test - public void shouldNotifyListenerWhenNewLogIsCreated() throws Exception + void shouldNotifyListenerWhenNewLogIsCreated() throws Exception { final CountDownLatch allowRotationComplete = new CountDownLatch( 1 ); final CountDownLatch rotationComplete = new CountDownLatch( 1 ); @@ -361,7 +371,7 @@ public void rotationCompleted( OutputStream out ) } @Test - public void shouldNotifyListenerOnRotationErrorDuringJobExecution() throws Exception + void shouldNotifyListenerOnRotationErrorDuringJobExecution() throws Exception { RotationListener rotationListener = mock( RotationListener.class ); Executor executor = mock( Executor.class ); @@ -379,7 +389,7 @@ public void shouldNotifyListenerOnRotationErrorDuringJobExecution() throws Excep } @Test - public void shouldReattemptRotationAfterExceptionDuringJobExecution() throws Exception + void shouldReattemptRotationAfterExceptionDuringJobExecution() throws Exception { RotationListener rotationListener = mock( RotationListener.class ); Executor executor = mock( Executor.class ); @@ -398,7 +408,7 @@ public void shouldReattemptRotationAfterExceptionDuringJobExecution() throws Exc } @Test - public void shouldNotifyListenerOnRotationErrorDuringRotationIO() throws Exception + void shouldNotifyListenerOnRotationErrorDuringRotationIO() throws Exception { RotationListener rotationListener = mock( RotationListener.class ); FileSystemAbstraction fs = spy( fileSystem ); @@ -416,7 +426,7 @@ public void shouldNotifyListenerOnRotationErrorDuringRotationIO() throws Excepti } @Test - public void shouldNotUpdateOutputStreamWhenClosedDuringRotation() throws Exception + void shouldNotUpdateOutputStreamWhenClosedDuringRotation() throws Exception { final CountDownLatch allowRotationComplete = new CountDownLatch( 1 ); @@ -470,7 +480,7 @@ public OutputStream openAsOutputStream( File fileName, boolean append ) throws I } @Test - public void shouldCloseAllOutputStreams() throws Exception + void shouldCloseAllOutputStreams() throws Exception { final List mockStreams = new ArrayList<>(); FileSystemAbstraction fs = new DelegatingFileSystemAbstraction( fileSystem ) @@ -495,7 +505,7 @@ public OutputStream openAsOutputStream( File fileName, boolean append ) throws I } @Test - public void shouldCloseAllStreamsDespiteError() throws Exception + void shouldCloseAllStreamsDespiteError() throws Exception { final List mockStreams = new ArrayList<>(); FileSystemAbstraction fs = new DelegatingFileSystemAbstraction( fileSystem ) @@ -519,20 +529,13 @@ public OutputStream openAsOutputStream( File fileName, boolean append ) throws I OutputStream mockStream = mockStreams.get( 1 ); doThrow( exception ).when( mockStream ).close(); - try - { - supplier.close(); - fail(); - } - catch ( IOException e ) - { - assertThat( e, sameInstance( exception ) ); - } + IOException ioException = assertThrows( IOException.class, supplier::close ); + assertThat( ioException, sameInstance( exception ) ); verify( mockStream ).close(); } @Test - public void shouldSurviveFilesystemErrors() throws Exception + void shouldSurviveFilesystemErrors() throws Exception { final RandomAdversary adversary = new RandomAdversary( 0.1, 0.1, 0 ); adversary.setProbabilityFactor( 0 ); @@ -579,17 +582,9 @@ private void writeLines( Supplier outputStreamSupplier, int count } } - private void assertStreamClosed( OutputStream stream ) throws IOException + private void assertStreamClosed( OutputStream stream ) { - try - { - stream.write( 0 ); - fail( "Expected ClosedChannelException" ); - } - catch ( ClosedChannelException e ) - { - // expected - } + assertThrows( ClosedChannelException.class, () -> stream.write( 0 ) ); } private class LockingPrintWriter extends PrintWriter diff --git a/community/logging/src/test/java/org/neo4j/logging/async/AsyncLogTest.java b/community/logging/src/test/java/org/neo4j/logging/async/AsyncLogTest.java index f125d2f8d8097..8dad00f1e8777 100644 --- a/community/logging/src/test/java/org/neo4j/logging/async/AsyncLogTest.java +++ b/community/logging/src/test/java/org/neo4j/logging/async/AsyncLogTest.java @@ -20,13 +20,14 @@ package org.neo4j.logging.async; import org.hamcrest.Matcher; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; +import java.util.stream.Stream; import javax.annotation.Nonnull; import org.neo4j.logging.AbstractLog; @@ -42,41 +43,28 @@ import static org.hamcrest.CoreMatchers.startsWith; import static org.neo4j.logging.AssertableLogProvider.inLog; -@RunWith( Parameterized.class ) public class AsyncLogTest { - @Parameterized.Parameters( name = "{0} {1}.log({2})" ) - public static Iterable parameters() + private static final Throwable exception = new Exception(); + + public static Stream parameters() { - List parameters = new ArrayList<>(); - for ( Invocation invocation : Invocation.values() ) + List parameters = new ArrayList<>(); + for ( Invocation invocation: Invocation.values() ) { - for ( Level level : Level.values() ) + for ( Level level: Level.values() ) { - for ( Style style : Style.values() ) + for ( Style style: Style.values() ) { - parameters.add( new Object[]{invocation, level, style} ); + parameters.add( Arguments.of( invocation, level, style ) ); } } - } - return parameters; - } - - @SuppressWarnings( "ThrowableInstanceNeverThrown" ) - private final Throwable exception = new Exception(); - private final Invocation invocation; - private final Level level; - private final Style style; - - public AsyncLogTest( Invocation invocation, Level level, Style style ) - { - this.invocation = invocation; - this.level = level; - this.style = style; + } return parameters.stream(); } - @Test - public void shouldLogAsynchronously() + @ParameterizedTest + @MethodSource( "parameters" ) + void shouldLogAsynchronously( Invocation invocation, Level level, Style style ) { // given AssertableLogProvider logging = new AssertableLogProvider(); @@ -85,7 +73,7 @@ public void shouldLogAsynchronously() AsyncLog asyncLog = new AsyncLog( events, log ); // when - log( invocation.decorate( asyncLog ) ); + log( invocation.decorate( asyncLog ), level, style ); // then logging.assertNoLoggingOccurred(); @@ -93,11 +81,11 @@ public void shouldLogAsynchronously() events.process(); // then MatcherBuilder matcherBuilder = new MatcherBuilder( inLog( getClass() ) ); - log( matcherBuilder ); + log( matcherBuilder, level, style ); logging.assertExactly( matcherBuilder.matcher() ); } - private void log( Log log ) + private void log( Log log, Level level, Style style ) { style.invoke( this, level.logger( log ) ); } @@ -183,7 +171,7 @@ public String toString() @Override void invoke( AsyncLogTest state, Logger logger ) { - logger.log( "an exception", state.exception ); + logger.log( "an exception", exception ); } @Override