Skip to content

Commit 5363cf9

Browse files
plamentotevslachiewicz
authored andcommitted
Migrate to JUnit 5
* Migrate org.junit.* to org.junit.jupiter.api.* * Fix the assertions. In JUnit 5 the message is the last argument, not the first. Move all messages to the end of the arguments list. * Replace JUnit4 rules with JUnit 5 build-in extensions. * Remove dependency to hamcrest as it is used only in single test. Use the JUnit5 assertions instead. * Sisu's InjectedTest setup and tearDown methods are not annotated with the JUnit5 annotations - @beforeeach and @AfterEach. Override those methods in TestSupport and place the respective annotations so those methods are invoked before/after each test. * @beforeeach and @AfterEach are not inherited when the method is overridden. Place the appropriate annotation where there are such methods, so they are executed before/after each test. Closes #81
1 parent bf33d58 commit 5363cf9

40 files changed

+264
-273
lines changed

pom.xml

+7-12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<javaVersion>8</javaVersion>
3535
<sisuVersion>0.3.5</sisuVersion>
3636
<slf4jVersion>1.7.36</slf4jVersion>
37+
<junitVersion>5.8.2</junitVersion>
3738
<project.build.outputTimestamp>2022-06-23T09:57:49Z</project.build.outputTimestamp>
3839
</properties>
3940

@@ -104,21 +105,15 @@
104105
</dependency>
105106
<!-- Test dependencies -->
106107
<dependency>
107-
<groupId>junit</groupId>
108-
<artifactId>junit</artifactId>
109-
<version>4.13.2</version>
108+
<groupId>org.junit.jupiter</groupId>
109+
<artifactId>junit-jupiter-api</artifactId>
110+
<version>${junitVersion}</version>
110111
<scope>test</scope>
111-
<exclusions>
112-
<exclusion>
113-
<groupId>org.hamcrest</groupId>
114-
<artifactId>hamcrest-core</artifactId>
115-
</exclusion>
116-
</exclusions>
117112
</dependency>
118113
<dependency>
119-
<groupId>org.hamcrest</groupId>
120-
<artifactId>hamcrest</artifactId>
121-
<version>2.2</version>
114+
<groupId>org.junit.jupiter</groupId>
115+
<artifactId>junit-jupiter-engine</artifactId>
116+
<version>${junitVersion}</version>
122117
<scope>test</scope>
123118
</dependency>
124119
<dependency>

src/test/java/org/codehaus/plexus/archiver/AbstractArchiverTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
import java.io.File;
44
import java.io.IOException;
55

6-
import org.junit.Before;
7-
import org.junit.Test;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
88

9-
import static org.junit.Assert.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
1010

1111
public class AbstractArchiverTest
1212
{
1313

1414
private AbstractArchiver archiver;
1515

16-
@Before
16+
@BeforeEach
1717
public void setUp() throws Exception
1818
{
1919
this.archiver = new AbstractArchiver()

src/test/java/org/codehaus/plexus/archiver/AbstractUnArchiverTest.java

+48-64
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,15 @@
2121
import java.util.Date;
2222

2323
import org.codehaus.plexus.components.io.filemappers.FileMapper;
24-
import org.junit.After;
25-
import org.junit.Before;
26-
import org.junit.Rule;
27-
import org.junit.Test;
28-
import org.junit.rules.ExpectedException;
29-
import org.junit.rules.TemporaryFolder;
30-
31-
import static org.hamcrest.CoreMatchers.equalTo;
32-
import static org.hamcrest.CoreMatchers.is;
33-
import static org.hamcrest.MatcherAssert.assertThat;
34-
import static org.hamcrest.Matchers.greaterThan;
24+
import org.junit.jupiter.api.AfterEach;
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.io.TempDir;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertFalse;
31+
import static org.junit.jupiter.api.Assertions.assertThrows;
32+
import static org.junit.jupiter.api.Assertions.assertTrue;
3533

3634
/**
3735
* Unit test for {@link AbstractUnArchiver}
@@ -40,15 +38,9 @@
4038
*/
4139
public class AbstractUnArchiverTest
4240
{
43-
@Rule
44-
public ExpectedException thrown = ExpectedException.none();
45-
46-
@Rule
47-
public TemporaryFolder temporaryFolder = new TemporaryFolder();
48-
4941
private AbstractUnArchiver abstractUnArchiver;
5042

51-
@Before
43+
@BeforeEach
5244
public void setUp()
5345
{
5446
this.abstractUnArchiver = new AbstractUnArchiver()
@@ -69,129 +61,121 @@ protected void execute()
6961
};
7062
}
7163

72-
@After
64+
@AfterEach
7365
public void tearDown()
7466
{
7567
this.abstractUnArchiver = null;
7668
}
7769

7870
@Test
79-
public void shouldThrowExceptionBecauseRewrittenPathIsOutOfDirectory()
80-
throws ArchiverException, IOException
71+
public void shouldThrowExceptionBecauseRewrittenPathIsOutOfDirectory( @TempDir File targetFolder )
72+
throws ArchiverException
8173
{
8274
// given
83-
this.thrown.expectMessage( "Entry is outside of the target directory (../PREFIX/ENTRYNAME.SUFFIX)" );
84-
final File targetFolder = temporaryFolder.newFolder();
85-
final FileMapper[] fileMappers = new FileMapper[] { new FileMapper()
86-
{
87-
@Override
88-
public String getMappedFileName( String pName )
89-
{
90-
return "../PREFIX/" + pName;
91-
}
92-
}, new FileMapper()
93-
{
94-
@Override
95-
public String getMappedFileName( String pName )
96-
{
97-
return pName + ".SUFFIX";
98-
}
99-
} };
75+
final FileMapper[] fileMappers = new FileMapper[] { pName -> "../PREFIX/" + pName, pName -> pName + ".SUFFIX"};
10076

10177
// when
102-
this.abstractUnArchiver.extractFile( null, targetFolder, null, "ENTRYNAME", null, false, null, null,
103-
fileMappers );
104-
78+
Exception exception = assertThrows( ArchiverException.class, () ->
79+
abstractUnArchiver.extractFile( null, targetFolder, null, "ENTRYNAME", null, false, null, null, fileMappers ) );
10580
// then
10681
// ArchiverException is thrown providing the rewritten path
82+
assertEquals( "Entry is outside of the target directory (../PREFIX/ENTRYNAME.SUFFIX)", exception.getMessage() );
10783
}
10884

10985
@Test
110-
public void shouldExtractWhenFileOnDiskDoesNotExist() throws IOException
86+
public void shouldExtractWhenFileOnDiskDoesNotExist( @TempDir File temporaryFolder ) throws IOException
11187
{
11288
// given
113-
File file = new File( temporaryFolder.getRoot(), "whatever.txt" ); // does not create the file!
89+
File file = new File( temporaryFolder, "whatever.txt" ); // does not create the file!
11490
String entryname = file.getName();
11591
Date entryDate = new Date();
11692

11793
// when & then
118-
assertThat( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder.getRoot(), file, entryname, entryDate ), is ( true ) );
94+
assertTrue( abstractUnArchiver.shouldExtractEntry( temporaryFolder, file, entryname, entryDate ) );
11995
}
12096

12197
@Test
122-
public void shouldNotExtractWhenFileOnDiskIsNewerThanEntryInArchive() throws IOException
98+
public void shouldNotExtractWhenFileOnDiskIsNewerThanEntryInArchive( @TempDir File temporaryFolder ) throws IOException
12399
{
124100
// given
125-
File file = temporaryFolder.newFile();
101+
File file = new File( temporaryFolder, "whatever.txt" );
102+
file.createNewFile();
126103
file.setLastModified( System.currentTimeMillis() );
127104
String entryname = file.getName();
128105
Date entryDate = new Date( 0 );
129106

130107
// when & then
131-
assertThat( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder.getRoot(), file, entryname, entryDate ), is ( false ) );
108+
assertFalse( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder, file, entryname, entryDate ) );
132109
}
133110

134111
@Test
135-
public void shouldNotExtractWhenFileOnDiskIsNewerThanEntryInArchive_andWarnAboutDifferentCasing() throws IOException
112+
public void shouldNotExtractWhenFileOnDiskIsNewerThanEntryInArchive_andWarnAboutDifferentCasing( @TempDir File temporaryFolder )
113+
throws IOException
136114
{
137115
// given
138-
File file = temporaryFolder.newFile();
116+
File file = new File( temporaryFolder, "whatever.txt" );
117+
file.createNewFile();
139118
file.setLastModified( System.currentTimeMillis() );
140119
String entryname = file.getName().toUpperCase();
141120
Date entryDate = new Date( 0 );
142121

143122
// when & then
144-
assertThat( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder.getRoot(), file, entryname, entryDate ), is ( false ) );
145-
assertThat( this.abstractUnArchiver.casingMessageEmitted.get(), greaterThan(0) );
123+
assertFalse( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder, file, entryname, entryDate ) );
124+
assertTrue( this.abstractUnArchiver.casingMessageEmitted.get() > 0 );
146125
}
147126

148127
@Test
149-
public void shouldExtractWhenEntryInArchiveIsNewerThanFileOnDisk() throws IOException
128+
public void shouldExtractWhenEntryInArchiveIsNewerThanFileOnDisk( @TempDir File temporaryFolder ) throws IOException
150129
{
151130
// given
152-
File file = temporaryFolder.newFile();
131+
File file = new File( temporaryFolder, "whatever.txt" );
132+
file.createNewFile();
153133
file.setLastModified( 0 );
154134
String entryname = file.getName().toUpperCase();
155135
Date entryDate = new Date( System.currentTimeMillis() );
156136

157137
// when & then
158138
this.abstractUnArchiver.setOverwrite( true );
159-
assertThat( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder.getRoot(), file, entryname, entryDate ), is( true ) );
139+
assertTrue( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder, file, entryname, entryDate ) );
160140

161141
// when & then
162142
this.abstractUnArchiver.setOverwrite( false );
163-
assertThat( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder.getRoot(), file, entryname, entryDate ), is( false ) );
143+
assertFalse( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder, file, entryname, entryDate ) );
164144
}
165145

166146
@Test
167-
public void shouldExtractWhenEntryInArchiveIsNewerThanFileOnDiskAndWarnAboutDifferentCasing() throws IOException
147+
public void shouldExtractWhenEntryInArchiveIsNewerThanFileOnDiskAndWarnAboutDifferentCasing( @TempDir File temporaryFolder )
148+
throws IOException
168149
{
169150
// given
170-
File file = temporaryFolder.newFile();
151+
File file = new File( temporaryFolder, "whatever.txt" );
152+
file.createNewFile();
171153
file.setLastModified( 0 );
172154
String entryname = file.getName().toUpperCase();
173155
Date entryDate = new Date( System.currentTimeMillis() );
174156

175157
// when & then
176158
this.abstractUnArchiver.setOverwrite( true );
177-
assertThat( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder.getRoot(), file, entryname, entryDate ), is( true ) );
159+
assertTrue( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder, file, entryname, entryDate ) );
178160
this.abstractUnArchiver.setOverwrite( false );
179-
assertThat( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder.getRoot(), file, entryname, entryDate ), is( false ) );
180-
assertThat( this.abstractUnArchiver.casingMessageEmitted.get(), greaterThan(0) );
161+
assertFalse( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder, file, entryname, entryDate ) );
162+
assertTrue( this.abstractUnArchiver.casingMessageEmitted.get() > 0 );
181163
}
182164

183165
@Test
184-
public void shouldNotWarnAboutDifferentCasingForDirectoryEntries() throws IOException
166+
public void shouldNotWarnAboutDifferentCasingForDirectoryEntries( @TempDir File temporaryFolder )
167+
throws IOException
185168
{
186169
// given
187-
File file = temporaryFolder.newFolder();
170+
File file = new File( temporaryFolder, "whatever.txt" );
171+
file.createNewFile();
188172
file.setLastModified( 0 );
189173
String entryname = file.getName() + '/'; // archive entries for directories end with a '/'
190174
Date entryDate = new Date();
191175

192176
// when & then
193177
this.abstractUnArchiver.setOverwrite( true );
194-
assertThat( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder.getRoot(), file, entryname, entryDate ), is( true ) );
195-
assertThat( this.abstractUnArchiver.casingMessageEmitted.get(), equalTo( 0 ) );
178+
assertTrue( this.abstractUnArchiver.shouldExtractEntry( temporaryFolder, file, entryname, entryDate ) );
179+
assertEquals( 0, this.abstractUnArchiver.casingMessageEmitted.get() );
196180
}
197181
}

src/test/java/org/codehaus/plexus/archiver/DotDirectiveArchiveFinalizerTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import java.io.File;
44
import java.util.jar.JarFile;
55
import org.codehaus.plexus.archiver.jar.JarArchiver;
6-
import org.junit.Test;
6+
import org.junit.jupiter.api.Test;
77

8-
import static org.junit.Assert.assertNotNull;
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
99

1010
/**
1111
* @author Jason van Zyl

src/test/java/org/codehaus/plexus/archiver/DuplicateFilesTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
import org.codehaus.plexus.archiver.tar.TarArchiver;
1313
import org.codehaus.plexus.archiver.tar.TarLongFileMode;
1414
import org.codehaus.plexus.util.FileUtils;
15-
import org.junit.Test;
15+
import org.junit.jupiter.api.Test;
1616

17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertTrue;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
1919

2020
/**
2121
* @author Erik Engstrom

src/test/java/org/codehaus/plexus/archiver/EmptyDirectoryTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import java.io.File;
2727
import org.codehaus.plexus.archiver.tar.TarArchiver;
2828
import org.codehaus.plexus.archiver.tar.TarLongFileMode;
29-
import org.junit.Test;
29+
import org.junit.jupiter.api.Test;
3030

31-
import static org.junit.Assert.assertTrue;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
3232

3333
/**
3434
* @author Daniel Krisher

src/test/java/org/codehaus/plexus/archiver/SymlinkTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import org.codehaus.plexus.archiver.zip.ZipArchiver;
1111
import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
1212
import org.codehaus.plexus.util.Os;
13-
import org.junit.Test;
13+
import org.junit.jupiter.api.Test;
1414

15-
import static org.junit.Assert.assertFalse;
16-
import static org.junit.Assert.assertTrue;
15+
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
1717

1818
/**
1919
* @author Kristian Rosenvold

src/test/java/org/codehaus/plexus/archiver/TestSupport.java

+14
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,27 @@
55
import java.nio.file.Paths;
66

77
import org.eclipse.sisu.launch.InjectedTest;
8+
import org.junit.jupiter.api.AfterEach;
9+
import org.junit.jupiter.api.BeforeEach;
810

911
/**
1012
* Test support class.
1113
*/
1214
public abstract class TestSupport
1315
extends InjectedTest
1416
{
17+
@Override
18+
@BeforeEach
19+
public void setUp() throws Exception {
20+
super.setUp();
21+
}
22+
23+
@Override
24+
@AfterEach
25+
public void tearDown() throws Exception {
26+
super.tearDown();
27+
}
28+
1529
private static Path basedir()
1630
{
1731
return Paths.get( System.getProperty( "basedir", ( new File( "" ) ).getAbsolutePath() ) );

src/test/java/org/codehaus/plexus/archiver/bzip2/BZip2ArchiverTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
import org.codehaus.plexus.archiver.zip.ZipArchiver;
3737
import org.codehaus.plexus.util.FileUtils;
3838
import org.codehaus.plexus.util.IOUtil;
39-
import org.junit.Test;
39+
import org.junit.jupiter.api.Test;
4040

41-
import static org.junit.Assert.assertEquals;
42-
import static org.junit.Assert.assertFalse;
43-
import static org.junit.Assert.assertTrue;
44-
import static org.junit.Assert.fail;
41+
import static org.junit.jupiter.api.Assertions.assertEquals;
42+
import static org.junit.jupiter.api.Assertions.assertFalse;
43+
import static org.junit.jupiter.api.Assertions.assertTrue;
44+
import static org.junit.jupiter.api.Assertions.fail;
4545

4646
/**
4747
* @author Emmanuel Venisse

src/test/java/org/codehaus/plexus/archiver/gzip/GZipArchiverTest.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@
3636
import org.codehaus.plexus.archiver.zip.ZipArchiver;
3737
import org.codehaus.plexus.util.FileUtils;
3838
import org.codehaus.plexus.util.IOUtil;
39-
import org.junit.Test;
39+
import org.junit.jupiter.api.Test;
4040

41-
import static org.junit.Assert.assertEquals;
42-
import static org.junit.Assert.assertFalse;
43-
import static org.junit.Assert.assertNotEquals;
44-
import static org.junit.Assert.assertTrue;
45-
import static org.junit.Assert.fail;
41+
import static org.junit.jupiter.api.Assertions.assertEquals;
42+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
43+
import static org.junit.jupiter.api.Assertions.assertTrue;
44+
import static org.junit.jupiter.api.Assertions.fail;
4645

4746
/**
4847
* @author Emmanuel Venisse

0 commit comments

Comments
 (0)