Skip to content

Commit c6d4709

Browse files
authored
Merge pull request #71 from hazendaz/release
[tests] Rewrite tests to junit 5
2 parents 23d7f32 + 90f45ee commit c6d4709

File tree

6 files changed

+88
-68
lines changed

6 files changed

+88
-68
lines changed

.gitattributes

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
*.dbproj merge=union
1111

1212
# Standard to msysgit
13-
*.doc diff=astextplain
14-
*.DOC diff=astextplain
13+
*.doc diff=astextplain
14+
*.DOC diff=astextplain
1515
*.docx diff=astextplain
1616
*.DOCX diff=astextplain
1717
*.dot diff=astextplain
1818
*.DOT diff=astextplain
1919
*.pdf diff=astextplain
20-
*.PDF diff=astextplain
21-
*.rtf diff=astextplain
22-
*.RTF diff=astextplain
20+
*.PDF diff=astextplain
21+
*.rtf diff=astextplain
22+
*.RTF diff=astextplain

pom.xml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@
3535
<checkstyle.excludeGeneratedSources>true</checkstyle.excludeGeneratedSources>
3636
</properties>
3737
<dependencies>
38-
<dependency>
39-
<groupId>junit</groupId>
40-
<artifactId>junit</artifactId>
41-
<scope>test</scope>
42-
</dependency>
4338
<dependency>
4439
<groupId>org.apache.commons</groupId>
4540
<artifactId>commons-lang3</artifactId>
@@ -89,6 +84,12 @@
8984
<artifactId>hamcrest</artifactId>
9085
<scope>test</scope>
9186
</dependency>
87+
<dependency>
88+
<groupId>org.junit.jupiter</groupId>
89+
<artifactId>junit-jupiter-api</artifactId>
90+
<version>5.11.0</version>
91+
<scope>test</scope>
92+
</dependency>
9293
<dependency>
9394
<groupId>org.mockito</groupId>
9495
<artifactId>mockito-core</artifactId>

src/test/java/au/com/acegi/xmlformat/FormatUtilTest.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,33 @@
2828
import static java.nio.charset.StandardCharsets.UTF_8;
2929
import static org.hamcrest.CoreMatchers.is;
3030
import static org.hamcrest.MatcherAssert.assertThat;
31+
import static org.junit.jupiter.api.Assertions.assertThrows;
3132

3233
import java.io.ByteArrayOutputStream;
3334
import java.io.File;
3435
import java.io.IOException;
3536
import java.io.InputStream;
3637

3738
import org.dom4j.DocumentException;
38-
import org.junit.Rule;
39-
import org.junit.Test;
40-
import org.junit.rules.TemporaryFolder;
39+
import org.junit.jupiter.api.Test;
40+
import org.junit.jupiter.api.io.TempDir;
4141

4242
/** Tests {@link FormatUtil}. */
43-
public final class FormatUtilTest {
43+
class FormatUtilTest {
4444

4545
private static final String FORMATTED_XML = "<xml><hello/></xml>";
4646
private static final String UNFORMATTED_XML = "<xml> <hello/> </xml>";
4747

48-
@Rule public TemporaryFolder tmp = new TemporaryFolder();
48+
@TempDir
49+
private File tmp;
4950

5051
@Test
51-
public void formattedWillNotChange() throws DocumentException, IOException {
52+
void formattedWillNotChange() throws DocumentException, IOException {
5253
inPlaceChange(FORMATTED_XML, false);
5354
}
5455

5556
@Test
56-
public void test1() throws DocumentException, IOException {
57+
void test1() throws DocumentException, IOException {
5758
final XmlOutputFormat fmt = new XmlOutputFormat();
5859
fmt.setIndentSize(4);
5960
fmt.setNewLineAfterDeclaration(false);
@@ -62,7 +63,7 @@ public void test1() throws DocumentException, IOException {
6263
}
6364

6465
@Test
65-
public void test1KeepBlankLines() throws DocumentException, IOException {
66+
void test1KeepBlankLines() throws DocumentException, IOException {
6667
final XmlOutputFormat fmt = new XmlOutputFormat();
6768
fmt.setIndentSize(4);
6869
fmt.setNewLineAfterDeclaration(false);
@@ -72,7 +73,7 @@ public void test1KeepBlankLines() throws DocumentException, IOException {
7273
}
7374

7475
@Test
75-
public void test2() throws DocumentException, IOException {
76+
void test2() throws DocumentException, IOException {
7677
final XmlOutputFormat fmt = new XmlOutputFormat();
7778
fmt.setIndentSize(2);
7879
fmt.setNewLineAfterDeclaration(false);
@@ -81,7 +82,7 @@ public void test2() throws DocumentException, IOException {
8182
}
8283

8384
@Test
84-
public void test2KeepBlankLines() throws DocumentException, IOException {
85+
void test2KeepBlankLines() throws DocumentException, IOException {
8586
final XmlOutputFormat fmt = new XmlOutputFormat();
8687
fmt.setIndentSize(2);
8788
fmt.setNewLineAfterDeclaration(false);
@@ -91,7 +92,7 @@ public void test2KeepBlankLines() throws DocumentException, IOException {
9192
}
9293

9394
@Test
94-
public void test3() throws DocumentException, IOException {
95+
void test3() throws DocumentException, IOException {
9596
final XmlOutputFormat fmt = new XmlOutputFormat();
9697
fmt.setIndentSize(2);
9798
fmt.setNewLineAfterDeclaration(false);
@@ -100,7 +101,7 @@ public void test3() throws DocumentException, IOException {
100101
}
101102

102103
@Test
103-
public void test4() throws DocumentException, IOException {
104+
void test4() throws DocumentException, IOException {
104105
final XmlOutputFormat fmt = new XmlOutputFormat();
105106
fmt.setIndent("\t");
106107
fmt.setNewLineAfterDeclaration(false);
@@ -109,7 +110,7 @@ public void test4() throws DocumentException, IOException {
109110
}
110111

111112
@Test
112-
public void test4KeepBlankLines() throws DocumentException, IOException {
113+
void test4KeepBlankLines() throws DocumentException, IOException {
113114
final XmlOutputFormat fmt = new XmlOutputFormat();
114115
fmt.setIndent("\t");
115116
fmt.setNewLineAfterDeclaration(false);
@@ -119,7 +120,7 @@ public void test4KeepBlankLines() throws DocumentException, IOException {
119120
}
120121

121122
@Test
122-
public void test5() throws DocumentException, IOException {
123+
void test5() throws DocumentException, IOException {
123124
final XmlOutputFormat fmt = new XmlOutputFormat();
124125
fmt.setIndent(" ");
125126
fmt.setNewLineAfterDeclaration(false);
@@ -133,7 +134,7 @@ public void test5() throws DocumentException, IOException {
133134
* seems, they don't reach the XMLWriter. Not ideal, but believe we can leave with this exception
134135
*/
135136
@Test
136-
public void test5KeepBlankLines() throws DocumentException, IOException {
137+
void test5KeepBlankLines() throws DocumentException, IOException {
137138
final XmlOutputFormat fmt = new XmlOutputFormat();
138139
fmt.setIndent(" ");
139140
// Set to true to keep the new line given keep blank lines will not
@@ -149,7 +150,7 @@ public void test5KeepBlankLines() throws DocumentException, IOException {
149150
* seems, they don't reach the XMLWriter. Not ideal, but believe we can leave with this exception
150151
*/
151152
@Test
152-
public void test6() throws DocumentException, IOException {
153+
void test6() throws DocumentException, IOException {
153154
final XmlOutputFormat fmt = new XmlOutputFormat();
154155
fmt.setIndent(" ");
155156
// Set to true to keep the new line given keep blank lines will not
@@ -160,22 +161,24 @@ public void test6() throws DocumentException, IOException {
160161
testInOut(6, fmt);
161162
}
162163

163-
@Test(expected = DocumentException.class)
164-
public void testInvalid() throws DocumentException, IOException {
165-
try (InputStream in = getResource("/invalid.xml")) {
166-
final ByteArrayOutputStream out = new ByteArrayOutputStream();
167-
format(in, out, new XmlOutputFormat());
168-
}
164+
@Test
165+
void testInvalid() throws DocumentException, IOException {
166+
assertThrows(DocumentException.class, () -> {
167+
try (InputStream in = getResource("/invalid.xml")) {
168+
final ByteArrayOutputStream out = new ByteArrayOutputStream();
169+
format(in, out, new XmlOutputFormat());
170+
}
171+
});
169172
}
170173

171174
@Test
172-
public void unformattedWillChange() throws DocumentException, IOException {
175+
void unformattedWillChange() throws DocumentException, IOException {
173176
inPlaceChange(UNFORMATTED_XML, true);
174177
}
175178

176179
private void inPlaceChange(final String txt, final boolean shouldChange)
177180
throws DocumentException, IOException {
178-
final File file = tmp.newFile();
181+
final File file = File.createTempFile("junit", null, tmp);
179182
stringToFile(txt, file);
180183

181184
final XmlOutputFormat fmt = new XmlOutputFormat();

src/test/java/au/com/acegi/xmlformat/IOTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@
2828
import java.io.IOException;
2929
import java.io.InputStream;
3030

31-
import org.junit.Test;
31+
import org.junit.jupiter.api.Test;
3232

3333
/**
3434
* Tests {@link IOUtil}.
3535
*/
36-
public final class IOTest {
36+
class IOTest {
3737

3838
@Test
39-
public void hash1() throws IOException {
39+
void hash1() throws IOException {
4040
testHash("/test1-in.xml", 459_402_491L);
4141
}
4242

4343
@Test
44-
public void hash2() throws IOException {
44+
void hash2() throws IOException {
4545
testHash("/test2-in.xml", 1_687_393_391L);
4646
}
4747

4848
@Test
49-
public void hashInvalid() throws IOException {
49+
void hashInvalid() throws IOException {
5050
testHash("/invalid.xml", 2_274_913_643L);
5151
}
5252

src/test/java/au/com/acegi/xmlformat/XmlCheckPluginTest.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import static au.com.acegi.xmlformat.TestUtil.stringToFile;
2525
import static org.hamcrest.CoreMatchers.is;
2626
import static org.hamcrest.MatcherAssert.assertThat;
27-
import static org.junit.Assert.fail;
27+
import static org.junit.jupiter.api.Assertions.fail;
2828
import static org.mockito.ArgumentMatchers.anyString;
2929
import static org.mockito.Mockito.atLeastOnce;
3030
import static org.mockito.Mockito.mock;
@@ -38,15 +38,14 @@
3838
import org.apache.maven.plugin.MojoExecutionException;
3939
import org.apache.maven.plugin.MojoFailureException;
4040
import org.apache.maven.plugin.logging.Log;
41-
import org.junit.Before;
42-
import org.junit.Rule;
43-
import org.junit.Test;
44-
import org.junit.rules.TemporaryFolder;
41+
import org.junit.jupiter.api.BeforeEach;
42+
import org.junit.jupiter.api.Test;
43+
import org.junit.jupiter.api.io.TempDir;
4544

4645
/**
4746
* Tests {@link XmlCheckPlugin}.
4847
*/
49-
public class XmlCheckPluginTest {
48+
class XmlCheckPluginTest {
5049

5150
private static final String EMPTY_FILE_NAME = "empty.xml";
5251
private static final String EMPTY_TXT = "";
@@ -56,18 +55,18 @@ public class XmlCheckPluginTest {
5655
private static final String TO_CHG_FILE_NAME = "my.xml";
5756
private static final String TO_CHG_TXT = "<xml> <hello/> </xml>";
5857

59-
@Rule
60-
public TemporaryFolder tmp = new TemporaryFolder();
58+
@TempDir
59+
private File tmp;
6160

6261
@SuppressWarnings("PMD.ProperLogger")
6362
private Log log;
6463

6564
private File proj;
6665
private File target;
6766

68-
@Before
69-
public void before() throws IOException {
70-
proj = tmp.newFolder();
67+
@BeforeEach
68+
void before() throws IOException {
69+
proj = newFolder(tmp, "junit");
7170
target = new File(proj, "target");
7271
assertThat(target.mkdir(), is(true));
7372

@@ -93,7 +92,7 @@ public void before() throws IOException {
9392

9493
@Test
9594
@SuppressWarnings("PMD.JUnitUseExpected")
96-
public void pluginReportsError() throws MojoExecutionException {
95+
void pluginReportsError() throws MojoExecutionException {
9796
final XmlCheckPlugin plugin = new XmlCheckPlugin();
9897
plugin.setLog(log);
9998
when(log.isDebugEnabled()).thenReturn(true);
@@ -116,7 +115,7 @@ public void pluginReportsError() throws MojoExecutionException {
116115

117116
@Test
118117
@SuppressWarnings("PMD.JUnitUseExpected")
119-
public void pluginReportsFormattingNeeded() throws MojoFailureException {
118+
void pluginReportsFormattingNeeded() throws MojoFailureException {
120119
final XmlCheckPlugin plugin = new XmlCheckPlugin();
121120
plugin.setLog(log);
122121
when(log.isDebugEnabled()).thenReturn(true);
@@ -139,7 +138,7 @@ public void pluginReportsFormattingNeeded() throws MojoFailureException {
139138
}
140139

141140
@Test
142-
public void pluginSucceedsWhenAllFormatted() throws MojoExecutionException, MojoFailureException {
141+
void pluginSucceedsWhenAllFormatted() throws MojoExecutionException, MojoFailureException {
143142
final XmlCheckPlugin plugin = new XmlCheckPlugin();
144143
plugin.setLog(log);
145144
when(log.isDebugEnabled()).thenReturn(true);
@@ -156,4 +155,13 @@ public void pluginSucceedsWhenAllFormatted() throws MojoExecutionException, Mojo
156155
verify(log, atLeastOnce()).isDebugEnabled();
157156
verify(log, atLeastOnce()).debug(anyString());
158157
}
158+
159+
private static File newFolder(final File root, final String... subDirs) throws IOException {
160+
final String subFolder = String.join("/", subDirs);
161+
final File result = new File(root, subFolder);
162+
if (!result.mkdirs()) {
163+
throw new IOException("Couldn't create folders " + root);
164+
}
165+
return result;
166+
}
159167
}

0 commit comments

Comments
 (0)