Skip to content

Commit

Permalink
Merge pull request #71 from hazendaz/release
Browse files Browse the repository at this point in the history
[tests] Rewrite tests to junit 5
  • Loading branch information
hazendaz authored Sep 22, 2024
2 parents 23d7f32 + 90f45ee commit c6d4709
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 68 deletions.
10 changes: 5 additions & 5 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
*.dbproj merge=union

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
11 changes: 6 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@
<checkstyle.excludeGeneratedSources>true</checkstyle.excludeGeneratedSources>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down Expand Up @@ -89,6 +84,12 @@
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
51 changes: 27 additions & 24 deletions src/test/java/au/com/acegi/xmlformat/FormatUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,33 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.dom4j.DocumentException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/** Tests {@link FormatUtil}. */
public final class FormatUtilTest {
class FormatUtilTest {

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

@Rule public TemporaryFolder tmp = new TemporaryFolder();
@TempDir
private File tmp;

@Test
public void formattedWillNotChange() throws DocumentException, IOException {
void formattedWillNotChange() throws DocumentException, IOException {
inPlaceChange(FORMATTED_XML, false);
}

@Test
public void test1() throws DocumentException, IOException {
void test1() throws DocumentException, IOException {
final XmlOutputFormat fmt = new XmlOutputFormat();
fmt.setIndentSize(4);
fmt.setNewLineAfterDeclaration(false);
Expand All @@ -62,7 +63,7 @@ public void test1() throws DocumentException, IOException {
}

@Test
public void test1KeepBlankLines() throws DocumentException, IOException {
void test1KeepBlankLines() throws DocumentException, IOException {
final XmlOutputFormat fmt = new XmlOutputFormat();
fmt.setIndentSize(4);
fmt.setNewLineAfterDeclaration(false);
Expand All @@ -72,7 +73,7 @@ public void test1KeepBlankLines() throws DocumentException, IOException {
}

@Test
public void test2() throws DocumentException, IOException {
void test2() throws DocumentException, IOException {
final XmlOutputFormat fmt = new XmlOutputFormat();
fmt.setIndentSize(2);
fmt.setNewLineAfterDeclaration(false);
Expand All @@ -81,7 +82,7 @@ public void test2() throws DocumentException, IOException {
}

@Test
public void test2KeepBlankLines() throws DocumentException, IOException {
void test2KeepBlankLines() throws DocumentException, IOException {
final XmlOutputFormat fmt = new XmlOutputFormat();
fmt.setIndentSize(2);
fmt.setNewLineAfterDeclaration(false);
Expand All @@ -91,7 +92,7 @@ public void test2KeepBlankLines() throws DocumentException, IOException {
}

@Test
public void test3() throws DocumentException, IOException {
void test3() throws DocumentException, IOException {
final XmlOutputFormat fmt = new XmlOutputFormat();
fmt.setIndentSize(2);
fmt.setNewLineAfterDeclaration(false);
Expand All @@ -100,7 +101,7 @@ public void test3() throws DocumentException, IOException {
}

@Test
public void test4() throws DocumentException, IOException {
void test4() throws DocumentException, IOException {
final XmlOutputFormat fmt = new XmlOutputFormat();
fmt.setIndent("\t");
fmt.setNewLineAfterDeclaration(false);
Expand All @@ -109,7 +110,7 @@ public void test4() throws DocumentException, IOException {
}

@Test
public void test4KeepBlankLines() throws DocumentException, IOException {
void test4KeepBlankLines() throws DocumentException, IOException {
final XmlOutputFormat fmt = new XmlOutputFormat();
fmt.setIndent("\t");
fmt.setNewLineAfterDeclaration(false);
Expand All @@ -119,7 +120,7 @@ public void test4KeepBlankLines() throws DocumentException, IOException {
}

@Test
public void test5() throws DocumentException, IOException {
void test5() throws DocumentException, IOException {
final XmlOutputFormat fmt = new XmlOutputFormat();
fmt.setIndent(" ");
fmt.setNewLineAfterDeclaration(false);
Expand All @@ -133,7 +134,7 @@ public void test5() throws DocumentException, IOException {
* seems, they don't reach the XMLWriter. Not ideal, but believe we can leave with this exception
*/
@Test
public void test5KeepBlankLines() throws DocumentException, IOException {
void test5KeepBlankLines() throws DocumentException, IOException {
final XmlOutputFormat fmt = new XmlOutputFormat();
fmt.setIndent(" ");
// Set to true to keep the new line given keep blank lines will not
Expand All @@ -149,7 +150,7 @@ public void test5KeepBlankLines() throws DocumentException, IOException {
* seems, they don't reach the XMLWriter. Not ideal, but believe we can leave with this exception
*/
@Test
public void test6() throws DocumentException, IOException {
void test6() throws DocumentException, IOException {
final XmlOutputFormat fmt = new XmlOutputFormat();
fmt.setIndent(" ");
// Set to true to keep the new line given keep blank lines will not
Expand All @@ -160,22 +161,24 @@ public void test6() throws DocumentException, IOException {
testInOut(6, fmt);
}

@Test(expected = DocumentException.class)
public void testInvalid() throws DocumentException, IOException {
try (InputStream in = getResource("/invalid.xml")) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
format(in, out, new XmlOutputFormat());
}
@Test
void testInvalid() throws DocumentException, IOException {
assertThrows(DocumentException.class, () -> {
try (InputStream in = getResource("/invalid.xml")) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
format(in, out, new XmlOutputFormat());
}
});
}

@Test
public void unformattedWillChange() throws DocumentException, IOException {
void unformattedWillChange() throws DocumentException, IOException {
inPlaceChange(UNFORMATTED_XML, true);
}

private void inPlaceChange(final String txt, final boolean shouldChange)
throws DocumentException, IOException {
final File file = tmp.newFile();
final File file = File.createTempFile("junit", null, tmp);
stringToFile(txt, file);

final XmlOutputFormat fmt = new XmlOutputFormat();
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/au/com/acegi/xmlformat/IOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@
import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Tests {@link IOUtil}.
*/
public final class IOTest {
class IOTest {

@Test
public void hash1() throws IOException {
void hash1() throws IOException {
testHash("/test1-in.xml", 459_402_491L);
}

@Test
public void hash2() throws IOException {
void hash2() throws IOException {
testHash("/test2-in.xml", 1_687_393_391L);
}

@Test
public void hashInvalid() throws IOException {
void hashInvalid() throws IOException {
testHash("/invalid.xml", 2_274_913_643L);
}

Expand Down
36 changes: 22 additions & 14 deletions src/test/java/au/com/acegi/xmlformat/XmlCheckPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static au.com.acegi.xmlformat.TestUtil.stringToFile;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
Expand All @@ -38,15 +38,14 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* Tests {@link XmlCheckPlugin}.
*/
public class XmlCheckPluginTest {
class XmlCheckPluginTest {

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

@Rule
public TemporaryFolder tmp = new TemporaryFolder();
@TempDir
private File tmp;

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

private File proj;
private File target;

@Before
public void before() throws IOException {
proj = tmp.newFolder();
@BeforeEach
void before() throws IOException {
proj = newFolder(tmp, "junit");
target = new File(proj, "target");
assertThat(target.mkdir(), is(true));

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

@Test
@SuppressWarnings("PMD.JUnitUseExpected")
public void pluginReportsError() throws MojoExecutionException {
void pluginReportsError() throws MojoExecutionException {
final XmlCheckPlugin plugin = new XmlCheckPlugin();
plugin.setLog(log);
when(log.isDebugEnabled()).thenReturn(true);
Expand All @@ -116,7 +115,7 @@ public void pluginReportsError() throws MojoExecutionException {

@Test
@SuppressWarnings("PMD.JUnitUseExpected")
public void pluginReportsFormattingNeeded() throws MojoFailureException {
void pluginReportsFormattingNeeded() throws MojoFailureException {
final XmlCheckPlugin plugin = new XmlCheckPlugin();
plugin.setLog(log);
when(log.isDebugEnabled()).thenReturn(true);
Expand All @@ -139,7 +138,7 @@ public void pluginReportsFormattingNeeded() throws MojoFailureException {
}

@Test
public void pluginSucceedsWhenAllFormatted() throws MojoExecutionException, MojoFailureException {
void pluginSucceedsWhenAllFormatted() throws MojoExecutionException, MojoFailureException {
final XmlCheckPlugin plugin = new XmlCheckPlugin();
plugin.setLog(log);
when(log.isDebugEnabled()).thenReturn(true);
Expand All @@ -156,4 +155,13 @@ public void pluginSucceedsWhenAllFormatted() throws MojoExecutionException, Mojo
verify(log, atLeastOnce()).isDebugEnabled();
verify(log, atLeastOnce()).debug(anyString());
}

private static File newFolder(final File root, final String... subDirs) throws IOException {
final String subFolder = String.join("/", subDirs);
final File result = new File(root, subFolder);
if (!result.mkdirs()) {
throw new IOException("Couldn't create folders " + root);
}
return result;
}
}
Loading

0 comments on commit c6d4709

Please sign in to comment.