2
2
3
3
import static org .hamcrest .MatcherAssert .assertThat ;
4
4
import static org .hamcrest .Matchers .empty ;
5
- import static org .junit .Assert .fail ;
5
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
6
+ import static org .junit .jupiter .api .Assertions .fail ;
6
7
7
8
import com .cloudbees .jenkins .support .api .Component ;
8
9
import com .cloudbees .jenkins .support .configfiles .AgentsConfigFile ;
38
39
import java .net .InetAddress ;
39
40
import java .net .NetworkInterface ;
40
41
import java .net .SocketException ;
41
- import java .net .UnknownHostException ;
42
42
import java .nio .charset .StandardCharsets ;
43
43
import java .nio .file .Files ;
44
44
import java .util .Arrays ;
55
55
import java .util .zip .ZipEntry ;
56
56
import java .util .zip .ZipFile ;
57
57
import jenkins .model .Jenkins ;
58
- import org .junit .Assert ;
59
- import org .junit .Rule ;
60
- import org .junit .Test ;
61
- import org .junit .rules .TemporaryFolder ;
58
+ import org .junit .jupiter .api .Test ;
59
+ import org .junit .jupiter .api .io .TempDir ;
62
60
import org .jvnet .hudson .test .JenkinsRule ;
63
- import org .jvnet .hudson .test .LoggerRule ;
61
+ import org .jvnet .hudson .test .junit . jupiter . WithJenkins ;
64
62
65
- public class CheckFilterTest {
63
+ @ WithJenkins
64
+ class CheckFilterTest {
66
65
private static final Logger LOGGER = Logger .getLogger (CheckFilterTest .class .getName ());
67
66
68
67
private static final String JOB_NAME = "thejob" ;
69
68
private static final String AGENT_NAME = "agent0" ; // it's the name used by createOnlineSlave
70
69
private static final String VIEW_ALL_NEW_NAME = "all-view" ;
71
70
private static final String ENV_VAR = getFirstEnvVar ();
72
71
73
- @ Rule
74
- public TemporaryFolder temp = new TemporaryFolder ();
75
-
76
- @ Rule
77
- public JenkinsRule j = new JenkinsRule ();
78
-
79
- @ Rule
80
- public LoggerRule logging = new LoggerRule ().record (AsyncResultCache .class , Level .FINER );
72
+ @ TempDir
73
+ private File temp ;
81
74
82
75
@ Test
83
- public void checkFilterTest () throws Exception {
76
+ void checkFilterTest (JenkinsRule j ) throws Exception {
84
77
// Create the files to check
85
78
FileChecker checker = new FileChecker (j .jenkins );
86
79
// Create the objects needed for some contents to be included
87
- QueueTaskFuture <FreeStyleBuild > build = createObjectsWithNames ();
80
+ QueueTaskFuture <FreeStyleBuild > build = createObjectsWithNames (j );
88
81
89
82
// Reload the mappings, after the objects were created
90
83
ContentFilters .get ().setEnabled (true );
@@ -123,7 +116,7 @@ public void checkFilterTest() throws Exception {
123
116
j .waitUntilNoActivity ();
124
117
}
125
118
126
- private QueueTaskFuture <FreeStyleBuild > createObjectsWithNames () throws Exception {
119
+ private static QueueTaskFuture <FreeStyleBuild > createObjectsWithNames (JenkinsRule j ) throws Exception {
127
120
// For an environment variable
128
121
if (ENV_VAR != null ) {
129
122
User .getOrCreateByIdOrFullName (ENV_VAR );
@@ -157,7 +150,7 @@ private QueueTaskFuture<FreeStyleBuild> createObjectsWithNames() throws Exceptio
157
150
158
151
private void assertComponent (List <Class <? extends Component >> componentClasses , FileChecker checker )
159
152
throws IOException {
160
- File fileZip = new File (temp . getRoot () , "filteredBundle.zip" );
153
+ File fileZip = new File (temp , "filteredBundle.zip" );
161
154
Files .deleteIfExists (fileZip .toPath ());
162
155
163
156
try (FileOutputStream zipOutputStream = new FileOutputStream (fileZip )) {
@@ -191,7 +184,7 @@ private void assertComponent(List<Class<? extends Component>> componentClasses,
191
184
}
192
185
}
193
186
194
- private String getContentFromEntry (ZipFile zip , ZipEntry entry ) throws IOException {
187
+ private static String getContentFromEntry (ZipFile zip , ZipEntry entry ) throws IOException {
195
188
if (entry .getSize () == 0 ) {
196
189
return "" ;
197
190
}
@@ -209,7 +202,7 @@ private String getContentFromEntry(ZipFile zip, ZipEntry entry) throws IOExcepti
209
202
out .write (data , 0 , currentByte );
210
203
}
211
204
out .flush ();
212
- content = new String ( out .toByteArray (), StandardCharsets .UTF_8 );
205
+ content = out .toString ( StandardCharsets .UTF_8 );
213
206
}
214
207
return content ;
215
208
}
@@ -228,11 +221,11 @@ private static String getFirstEnvVar() {
228
221
}
229
222
230
223
private static class FileChecker {
231
- private Set <FileToCheck > fileSet = new HashSet <>();
232
- private Set <FileToCheck > unchecked = new HashSet <>();
233
- private Set <String > words = new HashSet <>();
224
+ private final Set <FileToCheck > fileSet = new HashSet <>();
225
+ private final Set <FileToCheck > unchecked = new HashSet <>();
226
+ private final Set <String > words = new HashSet <>();
234
227
235
- private FileChecker (Jenkins jenkins ) throws UnknownHostException {
228
+ private FileChecker (Jenkins jenkins ) {
236
229
fileSet .add (of ("manifest.md" , "about" , false ));
237
230
fileSet .add (of ("about.md" , "b" , false ));
238
231
// fileSet.add(of("items.md", "jobs", false));
@@ -347,22 +340,22 @@ private FileChecker(Jenkins jenkins) throws UnknownHostException {
347
340
unchecked .addAll (fileSet );
348
341
}
349
342
350
- private String getUpdateCenterURL (Jenkins jenkins ) {
343
+ private static String getUpdateCenterURL (Jenkins jenkins ) {
351
344
if (jenkins .getUpdateCenter ().getSiteList () != null
352
345
&& jenkins .getUpdateCenter ().getSiteList ().get (0 ) != null ) {
353
346
return jenkins .getUpdateCenter ().getSiteList ().get (0 ).getUrl ();
354
347
}
355
348
return null ;
356
349
}
357
350
358
- private String getInetAddress () {
351
+ private static String getInetAddress () {
359
352
try {
360
353
Enumeration <NetworkInterface > networkInterfaces = null ;
361
354
networkInterfaces = NetworkInterface .getNetworkInterfaces ();
362
355
if (networkInterfaces .hasMoreElements ()) {
363
356
NetworkInterface ni = networkInterfaces .nextElement ();
364
357
Enumeration <InetAddress > inetAddresses = ni .getInetAddresses ();
365
- if (inetAddresses != null && inetAddresses .hasMoreElements ()) {
358
+ if (inetAddresses .hasMoreElements ()) {
366
359
return inetAddresses .nextElement ().toString ();
367
360
}
368
361
}
@@ -409,15 +402,15 @@ private void check(String file, String content) {
409
402
if (content == null ) {
410
403
fail (String .format ("Error checking the file %s because its content was null" , file ));
411
404
} else {
412
- Assert .assertTrue (
405
+ assertTrue (
406
+ content .toLowerCase (Locale .ENGLISH )
407
+ .contains (value .wordFiltered .toLowerCase (Locale .ENGLISH )),
413
408
String .format (
414
409
"The file '%s' should have the word '%s'. File content:\n \n ----------\n %s\n %s----------\n \n " ,
415
410
file ,
416
411
value .wordFiltered ,
417
412
content .substring (0 , Math .min (MAX_CONTENT_LENGTH , content .length ())),
418
- content .length () > MAX_CONTENT_LENGTH ? "...\n (content cut off)\n " : "" ),
419
- content .toLowerCase (Locale .ENGLISH )
420
- .contains (value .wordFiltered .toLowerCase (Locale .ENGLISH )));
413
+ content .length () > MAX_CONTENT_LENGTH ? "...\n (content cut off)\n " : "" ));
421
414
}
422
415
return ;
423
416
}
@@ -427,9 +420,9 @@ private void check(String file, String content) {
427
420
}
428
421
429
422
private static class FileToCheck {
430
- private String filePattern ;
431
- private String word ;
432
- private boolean fileIsFiltered ;
423
+ private final String filePattern ;
424
+ private final String word ;
425
+ private final boolean fileIsFiltered ;
433
426
private String wordFiltered ;
434
427
435
428
private FileToCheck (String filePattern , String word , boolean fileIsFiltered ) {
0 commit comments