Skip to content

Commit 80e5f3c

Browse files
JUnit Jupiter best practices & tests cleanup (#91)
Co-authored-by: Moderne <[email protected]>
1 parent a414fed commit 80e5f3c

15 files changed

+286
-390
lines changed

src/test/java/org/codehaus/plexus/interpolation/EnvarBasedValueSourceTest.java

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
package org.codehaus.plexus.interpolation;
22

3-
/*
4-
* Copyright 2007 The Codehaus Foundation.
5-
*
6-
* Licensed under the Apache License, Version 2.0 (the "License");
7-
* you may not use this file except in compliance with the License.
8-
* You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
12-
* Unless required by applicable law or agreed to in writing, software
13-
* distributed under the License is distributed on an "AS IS" BASIS,
14-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
* See the License for the specific language governing permissions and
16-
* limitations under the License.
17-
*/
18-
19-
import java.io.IOException;
203
import java.util.HashMap;
21-
import java.util.Map;
224

235
import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
246
import org.junit.jupiter.api.BeforeEach;
@@ -28,21 +10,19 @@
2810
import static org.junit.jupiter.api.Assertions.assertNotNull;
2911
import static org.junit.jupiter.api.Assertions.assertNull;
3012

31-
public class EnvarBasedValueSourceTest {
13+
class EnvarBasedValueSourceTest {
3214

3315
@BeforeEach
34-
public void setUp() {
16+
void setUp() {
3517
EnvarBasedValueSource.resetStatics();
3618
}
3719

3820
@Test
39-
void testNoArgConstructorIsCaseSensitive() throws IOException {
40-
OperatingSystemUtils.setEnvVarSource(new OperatingSystemUtils.EnvVarSource() {
41-
public Map<String, String> getEnvMap() {
42-
HashMap<String, String> map = new HashMap<String, String>();
43-
map.put("aVariable", "variable");
44-
return map;
45-
}
21+
void noArgConstructorIsCaseSensitive() throws Exception {
22+
OperatingSystemUtils.setEnvVarSource(() -> {
23+
HashMap<String, String> map = new HashMap<>();
24+
map.put("aVariable", "variable");
25+
return map;
4626
});
4727

4828
EnvarBasedValueSource source = new EnvarBasedValueSource();
@@ -54,13 +34,11 @@ public Map<String, String> getEnvMap() {
5434
}
5535

5636
@Test
57-
void testCaseInsensitive() throws IOException {
58-
OperatingSystemUtils.setEnvVarSource(new OperatingSystemUtils.EnvVarSource() {
59-
public Map<String, String> getEnvMap() {
60-
HashMap<String, String> map = new HashMap<String, String>();
61-
map.put("aVariable", "variable");
62-
return map;
63-
}
37+
void caseInsensitive() throws Exception {
38+
OperatingSystemUtils.setEnvVarSource(() -> {
39+
HashMap<String, String> map = new HashMap<>();
40+
map.put("aVariable", "variable");
41+
return map;
6442
});
6543

6644
EnvarBasedValueSource source = new EnvarBasedValueSource(false);
@@ -72,7 +50,7 @@ public Map<String, String> getEnvMap() {
7250
}
7351

7452
@Test
75-
void testGetRealEnvironmentVariable() throws IOException {
53+
void getRealEnvironmentVariable() throws Exception {
7654
OperatingSystemUtils.setEnvVarSource(new OperatingSystemUtils.DefaultEnvVarSource());
7755

7856
EnvarBasedValueSource source = new EnvarBasedValueSource();

src/test/java/org/codehaus/plexus/interpolation/FeedbackingValueSourceTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
import static org.junit.jupiter.api.Assertions.assertNull;
2424
import static org.junit.jupiter.api.Assertions.assertTrue;
2525

26-
public class FeedbackingValueSourceTest {
26+
class FeedbackingValueSourceTest {
2727

2828
@Test
29-
public void testStandalone() {
29+
void standalone() {
3030
ValueSource valueSource = new FeedbackingValueSource();
3131
assertNull(valueSource.getValue("test"));
3232
assertEquals(1, valueSource.getFeedback().size());
3333
assertEquals("'test' not resolved", valueSource.getFeedback().iterator().next());
3434
}
3535

3636
@Test
37-
public void testAfterResolvedExpression() throws InterpolationException {
37+
void afterResolvedExpression() throws Exception {
3838
StringSearchInterpolator interpolator = new StringSearchInterpolator();
3939
interpolator.addValueSource(new MapBasedValueSource(singletonMap("key", "val")));
4040
interpolator.addValueSource(new FeedbackingValueSource());
@@ -43,7 +43,7 @@ public void testAfterResolvedExpression() throws InterpolationException {
4343
}
4444

4545
@Test
46-
public void testBeforeResolvedExpression() throws InterpolationException {
46+
void beforeResolvedExpression() throws Exception {
4747
StringSearchInterpolator interpolator = new StringSearchInterpolator();
4848
interpolator.addValueSource(new FeedbackingValueSource("Resolving ${expression}"));
4949
interpolator.addValueSource(new MapBasedValueSource(singletonMap("key", "val")));
@@ -53,7 +53,7 @@ public void testBeforeResolvedExpression() throws InterpolationException {
5353
}
5454

5555
@Test
56-
public void testAfterNotResolvedExpression() throws InterpolationException {
56+
void afterNotResolvedExpression() throws Exception {
5757
StringSearchInterpolator interpolator = new StringSearchInterpolator();
5858
interpolator.addValueSource(new MapBasedValueSource(singletonMap("key", "val")));
5959
interpolator.addValueSource(new FeedbackingValueSource());

src/test/java/org/codehaus/plexus/interpolation/InterpolatorFilterReaderTest.java

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@
4141
* @author cstamas
4242
*
4343
*/
44-
public class InterpolatorFilterReaderTest {
44+
class InterpolatorFilterReaderTest {
4545
/*
4646
* Added and commented by jdcasey@03-Feb-2005 because it is a bug in the InterpolationFilterReader.
4747
* kenneyw@15-04-2005 fixed the bug.
4848
*/
4949
@Test
50-
public void testShouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken() throws Exception {
51-
Map m = new HashMap();
50+
void shouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken() throws Exception {
51+
Map<String, String> m = new HashMap<>();
5252
m.put("test", "TestValue");
5353

5454
String testStr = "This is a ${test";
@@ -60,8 +60,8 @@ public void testShouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken() t
6060
* kenneyw@14-04-2005 Added test to check above fix.
6161
*/
6262
@Test
63-
public void testShouldNotInterpolateExpressionWithMissingEndToken() throws Exception {
64-
Map m = new HashMap();
63+
void shouldNotInterpolateExpressionWithMissingEndToken() throws Exception {
64+
Map<String, String> m = new HashMap<>();
6565
m.put("test", "TestValue");
6666

6767
String testStr = "This is a ${test, really";
@@ -70,8 +70,8 @@ public void testShouldNotInterpolateExpressionWithMissingEndToken() throws Excep
7070
}
7171

7272
@Test
73-
public void testShouldNotInterpolateWithMalformedStartToken() throws Exception {
74-
Map m = new HashMap();
73+
void shouldNotInterpolateWithMalformedStartToken() throws Exception {
74+
Map<String, String> m = new HashMap<>();
7575
m.put("test", "testValue");
7676

7777
String foo = "This is a $!test} again";
@@ -80,8 +80,8 @@ public void testShouldNotInterpolateWithMalformedStartToken() throws Exception {
8080
}
8181

8282
@Test
83-
public void testShouldNotInterpolateWithMalformedEndToken() throws Exception {
84-
Map m = new HashMap();
83+
void shouldNotInterpolateWithMalformedEndToken() throws Exception {
84+
Map<String, String> m = new HashMap<>();
8585
m.put("test", "testValue");
8686

8787
String foo = "This is a ${test!} again";
@@ -90,8 +90,8 @@ public void testShouldNotInterpolateWithMalformedEndToken() throws Exception {
9090
}
9191

9292
@Test
93-
public void testDefaultInterpolationWithNonInterpolatedValueAtEnd() throws Exception {
94-
Map m = new HashMap();
93+
void defaultInterpolationWithNonInterpolatedValueAtEnd() throws Exception {
94+
Map<String, String> m = new HashMap<>();
9595
m.put("name", "jason");
9696
m.put("noun", "asshole");
9797

@@ -101,8 +101,8 @@ public void testDefaultInterpolationWithNonInterpolatedValueAtEnd() throws Excep
101101
}
102102

103103
@Test
104-
public void testDefaultInterpolationWithInterpolatedValueAtEnd() throws Exception {
105-
Map m = new HashMap();
104+
void defaultInterpolationWithInterpolatedValueAtEnd() throws Exception {
105+
Map<String, String> m = new HashMap<>();
106106
m.put("name", "jason");
107107
m.put("noun", "asshole");
108108

@@ -112,8 +112,8 @@ public void testDefaultInterpolationWithInterpolatedValueAtEnd() throws Exceptio
112112
}
113113

114114
@Test
115-
public void testInterpolationWithInterpolatedValueAtEndWithCustomToken() throws Exception {
116-
Map m = new HashMap();
115+
void interpolationWithInterpolatedValueAtEndWithCustomToken() throws Exception {
116+
Map<String, String> m = new HashMap<>();
117117
m.put("name", "jason");
118118
m.put("noun", "asshole");
119119

@@ -123,8 +123,8 @@ public void testInterpolationWithInterpolatedValueAtEndWithCustomToken() throws
123123
}
124124

125125
@Test
126-
public void testInterpolationWithInterpolatedValueAtEndWithCustomTokenAndCustomString() throws Exception {
127-
Map m = new HashMap();
126+
void interpolationWithInterpolatedValueAtEndWithCustomTokenAndCustomString() throws Exception {
127+
Map<String, String> m = new HashMap<>();
128128
m.put("name", "jason");
129129
m.put("noun", "asshole");
130130

@@ -134,8 +134,8 @@ public void testInterpolationWithInterpolatedValueAtEndWithCustomTokenAndCustomS
134134
}
135135

136136
@Test
137-
public void testEscape() throws Exception {
138-
Map m = new HashMap();
137+
void escape() throws Exception {
138+
Map<String, String> m = new HashMap<>();
139139
m.put("name", "jason");
140140
m.put("noun", "asshole");
141141

@@ -145,8 +145,8 @@ public void testEscape() throws Exception {
145145
}
146146

147147
@Test
148-
public void testEscapeAtStart() throws Exception {
149-
Map m = new HashMap();
148+
void escapeAtStart() throws Exception {
149+
Map<String, String> m = new HashMap<>();
150150
m.put("name", "jason");
151151
m.put("noun", "asshole");
152152

@@ -156,8 +156,8 @@ public void testEscapeAtStart() throws Exception {
156156
}
157157

158158
@Test
159-
public void testEscapeOnlyAtStart() throws Exception {
160-
Map m = new HashMap();
159+
void escapeOnlyAtStart() throws Exception {
160+
Map<String, String> m = new HashMap<>();
161161
m.put("name", "jason");
162162
m.put("noun", "asshole");
163163

@@ -168,8 +168,8 @@ public void testEscapeOnlyAtStart() throws Exception {
168168
}
169169

170170
@Test
171-
public void testEscapeOnlyAtStartDefaultToken() throws Exception {
172-
Map m = new HashMap();
171+
void escapeOnlyAtStartDefaultToken() throws Exception {
172+
Map<String, String> m = new HashMap<>();
173173
m.put("name", "jason");
174174
m.put("noun", "asshole");
175175

@@ -180,15 +180,15 @@ public void testEscapeOnlyAtStartDefaultToken() throws Exception {
180180
}
181181

182182
@Test
183-
public void testShouldDetectRecursiveExpressionPassingThroughTwoPrefixes() throws Exception {
184-
List prefixes = new ArrayList();
183+
void shouldDetectRecursiveExpressionPassingThroughTwoPrefixes() throws Exception {
184+
List<String> prefixes = new ArrayList<>();
185185

186186
prefixes.add("prefix1");
187187
prefixes.add("prefix2");
188188

189189
RecursionInterceptor ri = new PrefixAwareRecursionInterceptor(prefixes, false);
190190

191-
Map context = new HashMap();
191+
Map<String, String> context = new HashMap<>();
192192
context.put("name", "${prefix2.name}");
193193

194194
String input = "${prefix1.name}";
@@ -211,14 +211,14 @@ public void testShouldDetectRecursiveExpressionPassingThroughTwoPrefixes() throw
211211
}
212212

213213
@Test
214-
public void testShouldDetectRecursiveExpressionWithPrefixAndWithout() throws Exception {
215-
List prefixes = new ArrayList();
214+
void shouldDetectRecursiveExpressionWithPrefixAndWithout() throws Exception {
215+
List<String> prefixes = new ArrayList<>();
216216

217217
prefixes.add("prefix1");
218218

219219
RecursionInterceptor ri = new PrefixAwareRecursionInterceptor(prefixes, false);
220220

221-
Map context = new HashMap();
221+
Map<String, String> context = new HashMap<>();
222222
context.put("name", "${prefix1.name}");
223223

224224
String input = "${name}";
@@ -244,11 +244,11 @@ public void testShouldDetectRecursiveExpressionWithPrefixAndWithout() throws Exc
244244
//
245245
// ----------------------------------------------------------------------
246246

247-
private String interpolate(String input, Map context) throws Exception {
247+
private String interpolate(String input, Map<String, String> context) throws Exception {
248248
return interpolate(input, context, null);
249249
}
250250

251-
private String interpolate(String input, Map context, String escapeStr) throws Exception {
251+
private String interpolate(String input, Map<String, String> context, String escapeStr) throws Exception {
252252
Interpolator interpolator = new StringSearchInterpolator();
253253

254254
interpolator.addValueSource(new MapBasedValueSource(context));
@@ -268,7 +268,8 @@ private String interpolate(String input, Map context, String escapeStr) throws E
268268
return buf.toString();
269269
}
270270

271-
private String interpolate(String input, Map context, String beginToken, String endToken) throws Exception {
271+
private String interpolate(String input, Map<String, String> context, String beginToken, String endToken)
272+
throws Exception {
272273
StringSearchInterpolator interpolator = new StringSearchInterpolator(beginToken, endToken);
273274

274275
interpolator.addValueSource(new MapBasedValueSource(context));

src/test/java/org/codehaus/plexus/interpolation/PrefixAwareRecursionInterceptorTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
import static org.junit.jupiter.api.Assertions.assertFalse;
2626
import static org.junit.jupiter.api.Assertions.assertTrue;
2727

28-
public class PrefixAwareRecursionInterceptorTest {
28+
class PrefixAwareRecursionInterceptorTest {
2929

3030
@Test
31-
public void testFindExpression() {
31+
void findExpression() {
3232
PrefixAwareRecursionInterceptor receptor =
3333
new PrefixAwareRecursionInterceptor(Collections.singleton("prefix."));
3434

@@ -45,9 +45,9 @@ public void testFindExpression() {
4545
}
4646

4747
@Test
48-
public void testFindExpressionWithDifferentPrefix() {
48+
void findExpressionWithDifferentPrefix() {
4949
PrefixAwareRecursionInterceptor receptor =
50-
new PrefixAwareRecursionInterceptor(Arrays.asList(new String[] {"prefix.", "other."}));
50+
new PrefixAwareRecursionInterceptor(Arrays.asList("prefix.", "other."));
5151

5252
String expr = "prefix.first";
5353

@@ -61,9 +61,9 @@ public void testFindExpressionWithDifferentPrefix() {
6161
}
6262

6363
@Test
64-
public void testFindExpressionWithoutPrefix() {
64+
void findExpressionWithoutPrefix() {
6565
PrefixAwareRecursionInterceptor receptor =
66-
new PrefixAwareRecursionInterceptor(Arrays.asList(new String[] {"prefix.", "other."}));
66+
new PrefixAwareRecursionInterceptor(Arrays.asList("prefix.", "other."));
6767

6868
String prefixedExpr = "prefix.first";
6969
String expr = "first";

0 commit comments

Comments
 (0)