Skip to content

Commit 350603d

Browse files
committed
Test cleanups
1 parent 1d4f135 commit 350603d

28 files changed

+271
-334
lines changed

src/test/java/org/codehaus/plexus/util/CollectionUtilsTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ class CollectionUtilsTest {
4141
*/
4242
@Test
4343
void mergeMaps() {
44-
Map<String, String> dominantMap = new HashMap<String, String>();
44+
Map<String, String> dominantMap = new HashMap<>();
4545
dominantMap.put("a", "a");
4646
dominantMap.put("b", "b");
4747
dominantMap.put("c", "c");
4848
dominantMap.put("d", "d");
4949
dominantMap.put("e", "e");
5050
dominantMap.put("f", "f");
5151

52-
Map<String, String> recessiveMap = new HashMap<String, String>();
52+
Map<String, String> recessiveMap = new HashMap<>();
5353
recessiveMap.put("a", "invalid");
5454
recessiveMap.put("b", "invalid");
5555
recessiveMap.put("c", "invalid");
@@ -60,7 +60,7 @@ void mergeMaps() {
6060
Map<String, String> result = CollectionUtils.mergeMaps(dominantMap, recessiveMap);
6161

6262
// We should have 9 elements
63-
assertEquals(9, result.keySet().size());
63+
assertEquals(9, result.size());
6464

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

8888
// Test with an array with a single element.
89-
Map<String, String> map1 = new HashMap<String, String>();
89+
Map<String, String> map1 = new HashMap<>();
9090
map1.put("a", "a");
9191

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

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

9696
// Test with an array with two elements.
97-
Map<String, String> map2 = new HashMap<String, String>();
97+
Map<String, String> map2 = new HashMap<>();
9898
map2.put("a", "aa");
9999
map2.put("b", "bb");
100100

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

112112
// Test with an array with three elements.
113-
Map<String, String> map3 = new HashMap<String, String>();
113+
Map<String, String> map3 = new HashMap<>();
114114
map3.put("a", "aaa");
115115
map3.put("b", "bbb");
116116
map3.put("c", "ccc");
@@ -175,26 +175,26 @@ void mavenPropertiesLoading() {
175175
});
176176

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

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

185185
// Values take from projectBuildProperties.
186-
assertEquals("maven", (String) result.get("maven.final.name"));
186+
assertEquals("maven", result.get("maven.final.name"));
187187

188188
// Values take from projectProperties.
189-
assertEquals(mavenRepoRemote, (String) result.get("maven.repo.remote"));
189+
assertEquals(mavenRepoRemote, result.get("maven.repo.remote"));
190190
}
191191

192192
/**
193193
* <p>testIteratorToListWithAPopulatedList.</p>
194194
*/
195195
@Test
196196
void iteratorToListWithAPopulatedList() {
197-
List<String> original = new ArrayList<String>();
197+
List<String> original = new ArrayList<>();
198198

199199
original.add("en");
200200
original.add("to");
@@ -216,7 +216,7 @@ void iteratorToListWithAPopulatedList() {
216216
*/
217217
@Test
218218
void iteratorToListWithAEmptyList() {
219-
List<String> original = new ArrayList<String>();
219+
List<String> original = new ArrayList<>();
220220

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

src/test/java/org/codehaus/plexus/util/DirectoryScannerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ void doNotScanUnnecesaryDirectories() throws IOException {
551551
"directoryTest" + File.separator + "test-dir-123" + File.separator + "file1.dat"
552552
};
553553

554-
final Set<String> scannedDirSet = new HashSet<String>();
554+
final Set<String> scannedDirSet = new HashSet<>();
555555

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

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

575575
assertEquals(expectedScannedDirSet, scannedDirSet);
576576
}
@@ -626,7 +626,7 @@ private void assertInclusionsAndExclusions(String[] files, String[] excludedPath
626626
System.out.println(file);
627627
}
628628

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

638-
List<String> failedToInclude = new ArrayList<String>();
638+
List<String> failedToInclude = new ArrayList<>();
639639
for (String includedPath : includedPaths) {
640640
String alt = includedPath.replace('/', '\\');
641641
System.out.println("Searching for inclusion as: " + includedPath + "\nor: " + alt);

src/test/java/org/codehaus/plexus/util/FileBasedTestCase.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,9 @@ protected byte[] createFile(final File file, final long size) throws IOException
6767

6868
byte[] data = generateTestData(size);
6969

70-
final BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(file.toPath()));
71-
72-
try {
70+
try (BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
7371
output.write(data);
74-
7572
return data;
76-
} finally {
77-
output.close();
7873
}
7974
}
8075

src/test/java/org/codehaus/plexus/util/FileUtilsTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.Writer;
2727
import java.lang.reflect.Method;
2828
import java.net.URL;
29+
import java.nio.charset.StandardCharsets;
2930
import java.nio.file.Files;
3031
import java.nio.file.Paths;
3132
import java.util.Optional;
@@ -227,7 +228,7 @@ void mkdir() {
227228
File winFile = new File(getTestDirectory(), "bla*bla");
228229
winFile.deleteOnExit();
229230
FileUtils.mkdir(winFile.getAbsolutePath());
230-
assertTrue(false);
231+
fail();
231232
} catch (IllegalArgumentException e) {
232233
assertTrue(true);
233234
}
@@ -307,12 +308,9 @@ void copyURLToFile() throws Exception {
307308
FileUtils.copyURLToFile(getClass().getResource(resourceName), file);
308309

309310
// Tests that resource was copied correctly
310-
final InputStream fis = Files.newInputStream(file.toPath());
311-
try {
311+
try (InputStream fis = Files.newInputStream(file.toPath())) {
312312
assertTrue(
313313
IOUtil.contentEquals(getClass().getResourceAsStream(resourceName), fis), "Content is not equal.");
314-
} finally {
315-
fis.close();
316314
}
317315
}
318316

@@ -367,7 +365,7 @@ void forceMkdir() throws Exception {
367365
File winFile = new File(getTestDirectory(), "bla*bla");
368366
winFile.deleteOnExit();
369367
FileUtils.forceMkdir(winFile);
370-
assertTrue(false);
368+
fail();
371369
} catch (IllegalArgumentException e) {
372370
assertTrue(true);
373371
}
@@ -883,9 +881,9 @@ void getExtensionWithPaths() {
883881
final String[][] testsWithPaths = {
884882
{sep + "tmp" + sep + "foo" + sep + "filename.ext", "ext"},
885883
{"C:" + sep + "temp" + sep + "foo" + sep + "filename.ext", "ext"},
886-
{"" + sep + "tmp" + sep + "foo.bar" + sep + "filename.ext", "ext"},
884+
{sep + "tmp" + sep + "foo.bar" + sep + "filename.ext", "ext"},
887885
{"C:" + sep + "temp" + sep + "foo.bar" + sep + "filename.ext", "ext"},
888-
{"" + sep + "tmp" + sep + "foo.bar" + sep + "README", ""},
886+
{sep + "tmp" + sep + "foo.bar" + sep + "README", ""},
889887
{"C:" + sep + "temp" + sep + "foo.bar" + sep + "README", ""},
890888
{".." + sep + "filename.ext", "ext"},
891889
{"blabla", ""}
@@ -1410,14 +1408,14 @@ void deleteLongPathOnWindows() throws Exception {
14101408
File a1 = new File(a, "a");
14111409
a1.mkdir();
14121410

1413-
StringBuilder path = new StringBuilder("");
1411+
StringBuilder path = new StringBuilder();
14141412
for (int i = 0; i < 100; i++) {
14151413
path.append("../a/");
14161414
}
14171415

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

1420-
InputStream is = new ByteArrayInputStream("Blabla".getBytes("UTF-8"));
1418+
InputStream is = new ByteArrayInputStream("Blabla".getBytes(StandardCharsets.UTF_8));
14211419
OutputStream os = Files.newOutputStream(f.getCanonicalFile().toPath());
14221420
IOUtil.copy(is, os);
14231421
IOUtil.close(is);

src/test/java/org/codehaus/plexus/util/IOUtilTest.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,11 @@
2626
import java.io.Reader;
2727
import java.io.Writer;
2828
import java.nio.file.Files;
29-
import java.util.Arrays;
3029

3130
import org.junit.jupiter.api.BeforeEach;
3231
import org.junit.jupiter.api.Test;
3332

34-
import static org.junit.jupiter.api.Assertions.assertEquals;
35-
import static org.junit.jupiter.api.Assertions.assertFalse;
36-
import static org.junit.jupiter.api.Assertions.assertNotNull;
37-
import static org.junit.jupiter.api.Assertions.assertTrue;
38-
import static org.junit.jupiter.api.Assertions.fail;
33+
import static org.junit.jupiter.api.Assertions.*;
3934

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

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

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

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

128-
assertTrue(Arrays.equals(buf0, buf1), "The files " + f0 + " and " + f1 + " have different content");
122+
assertArrayEquals(buf0, buf1, "The files " + f0 + " and " + f1 + " have different content");
129123
}
130-
} finally {
131-
is0.close();
132-
is1.close();
133124
}
134125
}
135126

src/test/java/org/codehaus/plexus/util/InterpolationFilterReaderTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class InterpolationFilterReaderTest {
4343
*/
4444
@Test
4545
void shouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken() throws Exception {
46-
Map<String, String> m = new HashMap<String, String>();
46+
Map<String, String> m = new HashMap<>();
4747
m.put("test", "TestValue");
4848

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

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

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

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

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

@@ -137,7 +137,7 @@ void defaultInterpolationWithNonInterpolatedValueAtEnd() throws Exception {
137137
*/
138138
@Test
139139
void defaultInterpolationWithInterpolatedValueAtEnd() throws Exception {
140-
Map<String, String> m = new HashMap<String, String>();
140+
Map<String, String> m = new HashMap<>();
141141
m.put("name", "jason");
142142
m.put("noun", "asshole");
143143

@@ -153,7 +153,7 @@ void defaultInterpolationWithInterpolatedValueAtEnd() throws Exception {
153153
*/
154154
@Test
155155
void interpolationWithSpecifiedBoundaryTokens() throws Exception {
156-
Map<String, String> m = new HashMap<String, String>();
156+
Map<String, String> m = new HashMap<>();
157157
m.put("name", "jason");
158158
m.put("noun", "asshole");
159159

@@ -169,7 +169,7 @@ void interpolationWithSpecifiedBoundaryTokens() throws Exception {
169169
*/
170170
@Test
171171
void interpolationWithSpecifiedBoundaryTokensWithNonInterpolatedValueAtEnd() throws Exception {
172-
Map<String, String> m = new HashMap<String, String>();
172+
Map<String, String> m = new HashMap<>();
173173
m.put("name", "jason");
174174
m.put("noun", "asshole");
175175

@@ -185,7 +185,7 @@ void interpolationWithSpecifiedBoundaryTokensWithNonInterpolatedValueAtEnd() thr
185185
*/
186186
@Test
187187
void interpolationWithSpecifiedBoundaryTokensWithInterpolatedValueAtEnd() throws Exception {
188-
Map<String, String> m = new HashMap<String, String>();
188+
Map<String, String> m = new HashMap<>();
189189
m.put("name", "jason");
190190
m.put("noun", "asshole");
191191

@@ -201,7 +201,7 @@ void interpolationWithSpecifiedBoundaryTokensWithInterpolatedValueAtEnd() throws
201201
*/
202202
@Test
203203
void interpolationWithSpecifiedBoundaryTokensAndAdditionalTokenCharacter() throws Exception {
204-
Map<String, String> m = new HashMap<String, String>();
204+
Map<String, String> m = new HashMap<>();
205205
m.put("name", "jason");
206206
m.put("noun", "asshole");
207207

src/test/java/org/codehaus/plexus/util/LineOrientedInterpolatingReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void defaultInterpolationWithNonInterpolatedValueAtEnd() throws Exception {
7878
}
7979

8080
private Map<String, String> getStandardMap() {
81-
Map<String, String> m = new HashMap<String, String>();
81+
Map<String, String> m = new HashMap<>();
8282
m.put("name", "jason");
8383
m.put("noun", "asshole");
8484
return m;

src/test/java/org/codehaus/plexus/util/PerfTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void subString() {
2323
int len = src.length();
2424
for (int cnt = 0; cnt < oops; cnt++) {
2525
for (int i = 0; i < len - 5; i++) {
26-
res.append(src.substring(i, i + 4));
26+
res.append(src, i, i + 4);
2727
}
2828
}
2929
int i = res.length();

0 commit comments

Comments
 (0)