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
packageorg.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-
importjava.io.IOException;
203
importjava.util.HashMap;
21-
importjava.util.Map;
224

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

31-
publicclassEnvarBasedValueSourceTest{
13+
classEnvarBasedValueSourceTest{
3214

3315
@BeforeEach
34-
publicvoidsetUp(){
16+
voidsetUp(){
3517
EnvarBasedValueSource.resetStatics();
3618
}
3719

3820
@Test
39-
voidtestNoArgConstructorIsCaseSensitive() throwsIOException{
40-
OperatingSystemUtils.setEnvVarSource(newOperatingSystemUtils.EnvVarSource(){
41-
publicMap<String, String> getEnvMap(){
42-
HashMap<String, String> map = newHashMap<String, String>();
43-
map.put("aVariable", "variable");
44-
returnmap;
45-
}
21+
voidnoArgConstructorIsCaseSensitive() throwsException{
22+
OperatingSystemUtils.setEnvVarSource(() ->{
23+
HashMap<String, String> map = newHashMap<>();
24+
map.put("aVariable", "variable");
25+
returnmap;
4626
});
4727

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

5636
@Test
57-
voidtestCaseInsensitive() throwsIOException{
58-
OperatingSystemUtils.setEnvVarSource(newOperatingSystemUtils.EnvVarSource(){
59-
publicMap<String, String> getEnvMap(){
60-
HashMap<String, String> map = newHashMap<String, String>();
61-
map.put("aVariable", "variable");
62-
returnmap;
63-
}
37+
voidcaseInsensitive() throwsException{
38+
OperatingSystemUtils.setEnvVarSource(() ->{
39+
HashMap<String, String> map = newHashMap<>();
40+
map.put("aVariable", "variable");
41+
returnmap;
6442
});
6543

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

7452
@Test
75-
voidtestGetRealEnvironmentVariable() throwsIOException{
53+
voidgetRealEnvironmentVariable() throwsException{
7654
OperatingSystemUtils.setEnvVarSource(newOperatingSystemUtils.DefaultEnvVarSource());
7755

7856
EnvarBasedValueSourcesource = newEnvarBasedValueSource();

‎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
importstaticorg.junit.jupiter.api.Assertions.assertNull;
2424
importstaticorg.junit.jupiter.api.Assertions.assertTrue;
2525

26-
publicclassFeedbackingValueSourceTest{
26+
classFeedbackingValueSourceTest{
2727

2828
@Test
29-
publicvoidtestStandalone(){
29+
voidstandalone(){
3030
ValueSourcevalueSource = newFeedbackingValueSource();
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-
publicvoidtestAfterResolvedExpression() throwsInterpolationException{
37+
voidafterResolvedExpression() throwsException{
3838
StringSearchInterpolatorinterpolator = newStringSearchInterpolator();
3939
interpolator.addValueSource(newMapBasedValueSource(singletonMap("key", "val")));
4040
interpolator.addValueSource(newFeedbackingValueSource());
@@ -43,7 +43,7 @@ public void testAfterResolvedExpression() throws InterpolationException{
4343
}
4444

4545
@Test
46-
publicvoidtestBeforeResolvedExpression() throwsInterpolationException{
46+
voidbeforeResolvedExpression() throwsException{
4747
StringSearchInterpolatorinterpolator = newStringSearchInterpolator();
4848
interpolator.addValueSource(newFeedbackingValueSource("Resolving ${expression}"));
4949
interpolator.addValueSource(newMapBasedValueSource(singletonMap("key", "val")));
@@ -53,7 +53,7 @@ public void testBeforeResolvedExpression() throws InterpolationException{
5353
}
5454

5555
@Test
56-
publicvoidtestAfterNotResolvedExpression() throwsInterpolationException{
56+
voidafterNotResolvedExpression() throwsException{
5757
StringSearchInterpolatorinterpolator = newStringSearchInterpolator();
5858
interpolator.addValueSource(newMapBasedValueSource(singletonMap("key", "val")));
5959
interpolator.addValueSource(newFeedbackingValueSource());

‎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-
publicclassInterpolatorFilterReaderTest{
44+
classInterpolatorFilterReaderTest{
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-
publicvoidtestShouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken() throwsException{
51-
Mapm = newHashMap();
50+
voidshouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken() throwsException{
51+
Map<String, String> m = newHashMap<>();
5252
m.put("test", "TestValue");
5353

5454
StringtestStr = "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-
publicvoidtestShouldNotInterpolateExpressionWithMissingEndToken() throwsException{
64-
Mapm = newHashMap();
63+
voidshouldNotInterpolateExpressionWithMissingEndToken() throwsException{
64+
Map<String, String> m = newHashMap<>();
6565
m.put("test", "TestValue");
6666

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

7272
@Test
73-
publicvoidtestShouldNotInterpolateWithMalformedStartToken() throwsException{
74-
Mapm = newHashMap();
73+
voidshouldNotInterpolateWithMalformedStartToken() throwsException{
74+
Map<String, String> m = newHashMap<>();
7575
m.put("test", "testValue");
7676

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

8282
@Test
83-
publicvoidtestShouldNotInterpolateWithMalformedEndToken() throwsException{
84-
Mapm = newHashMap();
83+
voidshouldNotInterpolateWithMalformedEndToken() throwsException{
84+
Map<String, String> m = newHashMap<>();
8585
m.put("test", "testValue");
8686

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

9292
@Test
93-
publicvoidtestDefaultInterpolationWithNonInterpolatedValueAtEnd() throwsException{
94-
Mapm = newHashMap();
93+
voiddefaultInterpolationWithNonInterpolatedValueAtEnd() throwsException{
94+
Map<String, String> m = newHashMap<>();
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-
publicvoidtestDefaultInterpolationWithInterpolatedValueAtEnd() throwsException{
105-
Mapm = newHashMap();
104+
voiddefaultInterpolationWithInterpolatedValueAtEnd() throwsException{
105+
Map<String, String> m = newHashMap<>();
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-
publicvoidtestInterpolationWithInterpolatedValueAtEndWithCustomToken() throwsException{
116-
Mapm = newHashMap();
115+
voidinterpolationWithInterpolatedValueAtEndWithCustomToken() throwsException{
116+
Map<String, String> m = newHashMap<>();
117117
m.put("name", "jason");
118118
m.put("noun", "asshole");
119119

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

125125
@Test
126-
publicvoidtestInterpolationWithInterpolatedValueAtEndWithCustomTokenAndCustomString() throwsException{
127-
Mapm = newHashMap();
126+
voidinterpolationWithInterpolatedValueAtEndWithCustomTokenAndCustomString() throwsException{
127+
Map<String, String> m = newHashMap<>();
128128
m.put("name", "jason");
129129
m.put("noun", "asshole");
130130

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

136136
@Test
137-
publicvoidtestEscape() throwsException{
138-
Mapm = newHashMap();
137+
voidescape() throwsException{
138+
Map<String, String> m = newHashMap<>();
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-
publicvoidtestEscapeAtStart() throwsException{
149-
Mapm = newHashMap();
148+
voidescapeAtStart() throwsException{
149+
Map<String, String> m = newHashMap<>();
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-
publicvoidtestEscapeOnlyAtStart() throwsException{
160-
Mapm = newHashMap();
159+
voidescapeOnlyAtStart() throwsException{
160+
Map<String, String> m = newHashMap<>();
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-
publicvoidtestEscapeOnlyAtStartDefaultToken() throwsException{
172-
Mapm = newHashMap();
171+
voidescapeOnlyAtStartDefaultToken() throwsException{
172+
Map<String, String> m = newHashMap<>();
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-
publicvoidtestShouldDetectRecursiveExpressionPassingThroughTwoPrefixes() throwsException{
184-
Listprefixes = newArrayList();
183+
voidshouldDetectRecursiveExpressionPassingThroughTwoPrefixes() throwsException{
184+
List<String>prefixes = newArrayList<>();
185185

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

189189
RecursionInterceptorri = newPrefixAwareRecursionInterceptor(prefixes, false);
190190

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

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

213213
@Test
214-
publicvoidtestShouldDetectRecursiveExpressionWithPrefixAndWithout() throwsException{
215-
Listprefixes = newArrayList();
214+
voidshouldDetectRecursiveExpressionWithPrefixAndWithout() throwsException{
215+
List<String>prefixes = newArrayList<>();
216216

217217
prefixes.add("prefix1");
218218

219219
RecursionInterceptorri = newPrefixAwareRecursionInterceptor(prefixes, false);
220220

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

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

247-
privateStringinterpolate(Stringinput, Mapcontext) throwsException{
247+
privateStringinterpolate(Stringinput, Map<String, String>context) throwsException{
248248
returninterpolate(input, context, null);
249249
}
250250

251-
privateStringinterpolate(Stringinput, Mapcontext, StringescapeStr) throwsException{
251+
privateStringinterpolate(Stringinput, Map<String, String>context, StringescapeStr) throwsException{
252252
Interpolatorinterpolator = newStringSearchInterpolator();
253253

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

271-
privateStringinterpolate(Stringinput, Mapcontext, StringbeginToken, StringendToken) throwsException{
271+
privateStringinterpolate(Stringinput, Map<String, String> context, StringbeginToken, StringendToken)
272+
throwsException{
272273
StringSearchInterpolatorinterpolator = newStringSearchInterpolator(beginToken, endToken);
273274

274275
interpolator.addValueSource(newMapBasedValueSource(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
importstaticorg.junit.jupiter.api.Assertions.assertFalse;
2626
importstaticorg.junit.jupiter.api.Assertions.assertTrue;
2727

28-
publicclassPrefixAwareRecursionInterceptorTest{
28+
classPrefixAwareRecursionInterceptorTest{
2929

3030
@Test
31-
publicvoidtestFindExpression(){
31+
voidfindExpression(){
3232
PrefixAwareRecursionInterceptorreceptor =
3333
newPrefixAwareRecursionInterceptor(Collections.singleton("prefix."));
3434

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

4747
@Test
48-
publicvoidtestFindExpressionWithDifferentPrefix(){
48+
voidfindExpressionWithDifferentPrefix(){
4949
PrefixAwareRecursionInterceptorreceptor =
50-
newPrefixAwareRecursionInterceptor(Arrays.asList(newString[]{"prefix.", "other."}));
50+
newPrefixAwareRecursionInterceptor(Arrays.asList("prefix.", "other."));
5151

5252
Stringexpr = "prefix.first";
5353

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

6363
@Test
64-
publicvoidtestFindExpressionWithoutPrefix(){
64+
voidfindExpressionWithoutPrefix(){
6565
PrefixAwareRecursionInterceptorreceptor =
66-
newPrefixAwareRecursionInterceptor(Arrays.asList(newString[]{"prefix.", "other."}));
66+
newPrefixAwareRecursionInterceptor(Arrays.asList("prefix.", "other."));
6767

6868
StringprefixedExpr = "prefix.first";
6969
Stringexpr = "first";

0 commit comments

Comments
(0)