Skip to content

Commit

Permalink
Test cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Dec 15, 2024
1 parent 1d4f135 commit 350603d
Show file tree
Hide file tree
Showing 28 changed files with 271 additions and 334 deletions.
28 changes: 14 additions & 14 deletions src/test/java/org/codehaus/plexus/util/CollectionUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class CollectionUtilsTest {
*/
@Test
void mergeMaps() {
Map<String, String> dominantMap = new HashMap<String, String>();
Map<String, String> dominantMap = new HashMap<>();
dominantMap.put("a", "a");
dominantMap.put("b", "b");
dominantMap.put("c", "c");
dominantMap.put("d", "d");
dominantMap.put("e", "e");
dominantMap.put("f", "f");

Map<String, String> recessiveMap = new HashMap<String, String>();
Map<String, String> recessiveMap = new HashMap<>();
recessiveMap.put("a", "invalid");
recessiveMap.put("b", "invalid");
recessiveMap.put("c", "invalid");
Expand All @@ -60,7 +60,7 @@ void mergeMaps() {
Map<String, String> result = CollectionUtils.mergeMaps(dominantMap, recessiveMap);

// We should have 9 elements
assertEquals(9, result.keySet().size());
assertEquals(9, result.size());

// Check the elements.
assertEquals("a", result.get("a"));
Expand All @@ -86,15 +86,15 @@ void mergeMapArray() {
assertNull(result0);

// Test with an array with a single element.
Map<String, String> map1 = new HashMap<String, String>();
Map<String, String> map1 = new HashMap<>();
map1.put("a", "a");

Map<String, String> result1 = CollectionUtils.mergeMaps(new Map[] {map1});

assertEquals("a", result1.get("a"));

// Test with an array with two elements.
Map<String, String> map2 = new HashMap<String, String>();
Map<String, String> map2 = new HashMap<>();
map2.put("a", "aa");
map2.put("b", "bb");

Expand All @@ -110,7 +110,7 @@ void mergeMapArray() {
assertEquals("bb", result3.get("b"));

// Test with an array with three elements.
Map<String, String> map3 = new HashMap<String, String>();
Map<String, String> map3 = new HashMap<>();
map3.put("a", "aaa");
map3.put("b", "bbb");
map3.put("c", "ccc");
Expand Down Expand Up @@ -175,26 +175,26 @@ void mavenPropertiesLoading() {
});

// Values that should be taken from systemProperties.
assertEquals("/projects/maven", (String) result.get("maven.home"));
assertEquals("/projects/maven", result.get("maven.home"));

// Values that should be taken from userBuildProperties.
assertEquals("/opt/maven/artifact", (String) result.get("maven.repo.local"));
assertEquals("false", (String) result.get("maven.repo.remote.enabled"));
assertEquals("jvanzyl", (String) result.get("maven.username"));
assertEquals("/opt/maven/artifact", result.get("maven.repo.local"));
assertEquals("false", result.get("maven.repo.remote.enabled"));
assertEquals("jvanzyl", result.get("maven.username"));

// Values take from projectBuildProperties.
assertEquals("maven", (String) result.get("maven.final.name"));
assertEquals("maven", result.get("maven.final.name"));

// Values take from projectProperties.
assertEquals(mavenRepoRemote, (String) result.get("maven.repo.remote"));
assertEquals(mavenRepoRemote, result.get("maven.repo.remote"));
}

/**
* <p>testIteratorToListWithAPopulatedList.</p>
*/
@Test
void iteratorToListWithAPopulatedList() {
List<String> original = new ArrayList<String>();
List<String> original = new ArrayList<>();

original.add("en");
original.add("to");
Expand All @@ -216,7 +216,7 @@ void iteratorToListWithAPopulatedList() {
*/
@Test
void iteratorToListWithAEmptyList() {
List<String> original = new ArrayList<String>();
List<String> original = new ArrayList<>();

List<String> copy = CollectionUtils.iteratorToList(original.iterator());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ void doNotScanUnnecesaryDirectories() throws IOException {
"directoryTest" + File.separator + "test-dir-123" + File.separator + "file1.dat"
};

final Set<String> scannedDirSet = new HashSet<String>();
final Set<String> scannedDirSet = new HashSet<>();

DirectoryScanner ds = new DirectoryScanner() {
@Override
Expand All @@ -570,7 +570,7 @@ protected void scandir(File dir, String vpath, boolean fast) {
assertInclusionsAndExclusions(ds.getIncludedFiles(), excludedPaths, includedPaths);

Set<String> expectedScannedDirSet =
new HashSet<String>(Arrays.asList("io", "directoryTest", "testDir123", "test_dir_123", "test-dir-123"));
new HashSet<>(Arrays.asList("io", "directoryTest", "testDir123", "test_dir_123", "test-dir-123"));

assertEquals(expectedScannedDirSet, scannedDirSet);
}
Expand Down Expand Up @@ -626,7 +626,7 @@ private void assertInclusionsAndExclusions(String[] files, String[] excludedPath
System.out.println(file);
}

List<String> failedToExclude = new ArrayList<String>();
List<String> failedToExclude = new ArrayList<>();
for (String excludedPath : excludedPaths) {
String alt = excludedPath.replace('/', '\\');
System.out.println("Searching for exclusion as: " + excludedPath + "\nor: " + alt);
Expand All @@ -635,7 +635,7 @@ private void assertInclusionsAndExclusions(String[] files, String[] excludedPath
}
}

List<String> failedToInclude = new ArrayList<String>();
List<String> failedToInclude = new ArrayList<>();
for (String includedPath : includedPaths) {
String alt = includedPath.replace('/', '\\');
System.out.println("Searching for inclusion as: " + includedPath + "\nor: " + alt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,9 @@ protected byte[] createFile(final File file, final long size) throws IOException

byte[] data = generateTestData(size);

final BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(file.toPath()));

try {
try (BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
output.write(data);

return data;
} finally {
output.close();
}
}

Expand Down
18 changes: 8 additions & 10 deletions src/test/java/org/codehaus/plexus/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.Writer;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;
Expand Down Expand Up @@ -227,7 +228,7 @@ void mkdir() {
File winFile = new File(getTestDirectory(), "bla*bla");
winFile.deleteOnExit();
FileUtils.mkdir(winFile.getAbsolutePath());
assertTrue(false);
fail();
} catch (IllegalArgumentException e) {
assertTrue(true);
}
Expand Down Expand Up @@ -307,12 +308,9 @@ void copyURLToFile() throws Exception {
FileUtils.copyURLToFile(getClass().getResource(resourceName), file);

// Tests that resource was copied correctly
final InputStream fis = Files.newInputStream(file.toPath());
try {
try (InputStream fis = Files.newInputStream(file.toPath())) {
assertTrue(
IOUtil.contentEquals(getClass().getResourceAsStream(resourceName), fis), "Content is not equal.");
} finally {
fis.close();
}
}

Expand Down Expand Up @@ -367,7 +365,7 @@ void forceMkdir() throws Exception {
File winFile = new File(getTestDirectory(), "bla*bla");
winFile.deleteOnExit();
FileUtils.forceMkdir(winFile);
assertTrue(false);
fail();
} catch (IllegalArgumentException e) {
assertTrue(true);
}
Expand Down Expand Up @@ -883,9 +881,9 @@ void getExtensionWithPaths() {
final String[][] testsWithPaths = {
{sep + "tmp" + sep + "foo" + sep + "filename.ext", "ext"},
{"C:" + sep + "temp" + sep + "foo" + sep + "filename.ext", "ext"},
{"" + sep + "tmp" + sep + "foo.bar" + sep + "filename.ext", "ext"},
{sep + "tmp" + sep + "foo.bar" + sep + "filename.ext", "ext"},
{"C:" + sep + "temp" + sep + "foo.bar" + sep + "filename.ext", "ext"},
{"" + sep + "tmp" + sep + "foo.bar" + sep + "README", ""},
{sep + "tmp" + sep + "foo.bar" + sep + "README", ""},
{"C:" + sep + "temp" + sep + "foo.bar" + sep + "README", ""},
{".." + sep + "filename.ext", "ext"},
{"blabla", ""}
Expand Down Expand Up @@ -1410,14 +1408,14 @@ void deleteLongPathOnWindows() throws Exception {
File a1 = new File(a, "a");
a1.mkdir();

StringBuilder path = new StringBuilder("");
StringBuilder path = new StringBuilder();
for (int i = 0; i < 100; i++) {
path.append("../a/");
}

File f = new File(a1, path.toString() + "test.txt");

InputStream is = new ByteArrayInputStream("Blabla".getBytes("UTF-8"));
InputStream is = new ByteArrayInputStream("Blabla".getBytes(StandardCharsets.UTF_8));
OutputStream os = Files.newOutputStream(f.getCanonicalFile().toPath());
IOUtil.copy(is, os);
IOUtil.close(is);
Expand Down
19 changes: 5 additions & 14 deletions src/test/java/org/codehaus/plexus/util/IOUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.util.Arrays;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;

/**
* This is used to test IOUtil for correctness. The following checks are performed:
Expand Down Expand Up @@ -104,19 +99,18 @@ private void createFile(File file, long size) throws IOException {

/** Assert that the contents of two byte arrays are the same. */
private void assertEqualContent(byte[] b0, byte[] b1) {
assertTrue(Arrays.equals(b0, b1), "Content not equal according to java.util.Arrays#equals()");
assertArrayEquals(b0, b1, "Content not equal according to java.util.Arrays#equals()");
}

/** Assert that the content of two files is the same. */
private void assertEqualContent(File f0, File f1) throws IOException {
InputStream is0 = Files.newInputStream(f0.toPath());
InputStream is1 = Files.newInputStream(f1.toPath());
byte[] buf0 = new byte[FILE_SIZE];
byte[] buf1 = new byte[FILE_SIZE];
int n0 = 0;
int n1 = 0;

try {
try (InputStream is0 = Files.newInputStream(f0.toPath());
InputStream is1 = Files.newInputStream(f1.toPath())) {
while (0 <= n0) {
n0 = is0.read(buf0);
n1 = is1.read(buf1);
Expand All @@ -125,11 +119,8 @@ private void assertEqualContent(File f0, File f1) throws IOException {
"The files " + f0 + " and " + f1 + " have differing number of bytes available (" + n0 + " vs "
+ n1 + ")");

assertTrue(Arrays.equals(buf0, buf1), "The files " + f0 + " and " + f1 + " have different content");
assertArrayEquals(buf0, buf1, "The files " + f0 + " and " + f1 + " have different content");
}
} finally {
is0.close();
is1.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class InterpolationFilterReaderTest {
*/
@Test
void shouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken() throws Exception {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("test", "TestValue");

String testStr = "This is a ${test";
Expand All @@ -61,7 +61,7 @@ void shouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken() throws Excep
*/
@Test
void shouldNotInterpolateExpressionWithMissingEndToken() throws Exception {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("test", "TestValue");

String testStr = "This is a ${test, really";
Expand All @@ -76,7 +76,7 @@ void shouldNotInterpolateExpressionWithMissingEndToken() throws Exception {
*/
@Test
void shouldNotInterpolateWithMalformedStartToken() throws Exception {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("test", "testValue");

String foo = "This is a $!test} again";
Expand All @@ -91,7 +91,7 @@ void shouldNotInterpolateWithMalformedStartToken() throws Exception {
*/
@Test
void shouldNotInterpolateWithMalformedEndToken() throws Exception {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("test", "testValue");

String foo = "This is a ${test!} again";
Expand All @@ -106,7 +106,7 @@ void shouldNotInterpolateWithMalformedEndToken() throws Exception {
*/
@Test
void interpolationWithMulticharDelimiters() throws Exception {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("test", "testValue");

String foo = "This is a ${test$} again";
Expand All @@ -121,7 +121,7 @@ void interpolationWithMulticharDelimiters() throws Exception {
*/
@Test
void defaultInterpolationWithNonInterpolatedValueAtEnd() throws Exception {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("name", "jason");
m.put("noun", "asshole");

Expand All @@ -137,7 +137,7 @@ void defaultInterpolationWithNonInterpolatedValueAtEnd() throws Exception {
*/
@Test
void defaultInterpolationWithInterpolatedValueAtEnd() throws Exception {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("name", "jason");
m.put("noun", "asshole");

Expand All @@ -153,7 +153,7 @@ void defaultInterpolationWithInterpolatedValueAtEnd() throws Exception {
*/
@Test
void interpolationWithSpecifiedBoundaryTokens() throws Exception {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("name", "jason");
m.put("noun", "asshole");

Expand All @@ -169,7 +169,7 @@ void interpolationWithSpecifiedBoundaryTokens() throws Exception {
*/
@Test
void interpolationWithSpecifiedBoundaryTokensWithNonInterpolatedValueAtEnd() throws Exception {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("name", "jason");
m.put("noun", "asshole");

Expand All @@ -185,7 +185,7 @@ void interpolationWithSpecifiedBoundaryTokensWithNonInterpolatedValueAtEnd() thr
*/
@Test
void interpolationWithSpecifiedBoundaryTokensWithInterpolatedValueAtEnd() throws Exception {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("name", "jason");
m.put("noun", "asshole");

Expand All @@ -201,7 +201,7 @@ void interpolationWithSpecifiedBoundaryTokensWithInterpolatedValueAtEnd() throws
*/
@Test
void interpolationWithSpecifiedBoundaryTokensAndAdditionalTokenCharacter() throws Exception {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("name", "jason");
m.put("noun", "asshole");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void defaultInterpolationWithNonInterpolatedValueAtEnd() throws Exception {
}

private Map<String, String> getStandardMap() {
Map<String, String> m = new HashMap<String, String>();
Map<String, String> m = new HashMap<>();
m.put("name", "jason");
m.put("noun", "asshole");
return m;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/codehaus/plexus/util/PerfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void subString() {
int len = src.length();
for (int cnt = 0; cnt < oops; cnt++) {
for (int i = 0; i < len - 5; i++) {
res.append(src.substring(i, i + 4));
res.append(src, i, i + 4);
}
}
int i = res.length();
Expand Down
Loading

0 comments on commit 350603d

Please sign in to comment.