Skip to content

Commit e9bd2f3

Browse files
Introduce helper method for icon file creation
1 parent ed80793 commit e9bd2f3

File tree

6 files changed

+89
-204
lines changed

6 files changed

+89
-204
lines changed

src/test/java/jenkins/plugins/foldericon/CustomFolderIconConfigurationTest.java

Lines changed: 18 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package jenkins.plugins.foldericon;
2626

27+
import static jenkins.plugins.foldericon.utils.TestUtils.createCustomIconFile;
2728
import static jenkins.plugins.foldericon.utils.TestUtils.mockStaplerRequest;
2829
import static jenkins.plugins.foldericon.utils.TestUtils.validateResponse;
2930
import static org.junit.jupiter.api.Assertions.*;
@@ -90,15 +91,7 @@ void testGetRequiredGlobalConfigPagePermission(@SuppressWarnings("unused") Jenki
9091
void testGetDiskUsage(JenkinsRule r) throws Exception {
9192
CustomFolderIconConfiguration descriptor = new CustomFolderIconConfiguration();
9293

93-
FilePath parent = r.jenkins
94-
.getRootPath()
95-
.child(CustomFolderIconConfiguration.USER_CONTENT_PATH)
96-
.child(CustomFolderIconConfiguration.PLUGIN_PATH);
97-
parent.mkdirs();
98-
99-
FilePath file = parent.child(System.currentTimeMillis() + ".png");
100-
file.touch(System.currentTimeMillis());
101-
assertTrue(file.exists());
94+
FilePath file = createCustomIconFile(r);
10295

10396
String usage = descriptor.getDiskUsage();
10497
assertEquals(FileUtils.byteCountToDisplaySize(file.length()), usage);
@@ -142,12 +135,7 @@ void testGetDiskUsageWithException(JenkinsRule r) throws Exception {
142135
CustomFolderIconConfiguration descriptor = new CustomFolderIconConfiguration();
143136

144137
FilePath userContent = r.jenkins.getRootPath().child(CustomFolderIconConfiguration.USER_CONTENT_PATH);
145-
FilePath iconDir = userContent.child(CustomFolderIconConfiguration.PLUGIN_PATH);
146-
iconDir.mkdirs();
147-
148-
String filename = System.currentTimeMillis() + ".png";
149-
FilePath file = iconDir.child(filename);
150-
file.touch(System.currentTimeMillis());
138+
FilePath file = createCustomIconFile(r);
151139

152140
try (@SuppressWarnings("unused")
153141
MockedConstruction<FilePath> mocked = mockConstructionWithAnswer(FilePath.class, invocation -> {
@@ -158,7 +146,8 @@ void testGetDiskUsageWithException(JenkinsRule r) throws Exception {
158146
return true;
159147
} else if (StringUtils.equals(call, "filePath.list();")) {
160148
return List.of(file);
161-
} else if (StringUtils.equals(call, "filePath.child(\"" + filename + "\");")) {
149+
} else if (StringUtils.equals(
150+
call, "filePath.child(\n" + " \"" + file.getName() + "\"\n" + ");")) {
162151
throw new IOException("Mocked Exception!");
163152
}
164153
return fail("Unexpected invocation '" + call + "' - Test is broken!");
@@ -180,15 +169,7 @@ void testDoCleanupNoItems(JenkinsRule r) throws Exception {
180169
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
181170
StaplerRequest mockReq = mockStaplerRequest(stapler);
182171

183-
FilePath parent = r.jenkins
184-
.getRootPath()
185-
.child(CustomFolderIconConfiguration.USER_CONTENT_PATH)
186-
.child(CustomFolderIconConfiguration.PLUGIN_PATH);
187-
parent.mkdirs();
188-
189-
FilePath file = parent.child(System.currentTimeMillis() + ".png");
190-
file.touch(System.currentTimeMillis());
191-
assertTrue(file.exists());
172+
FilePath file = createCustomIconFile(r);
192173
HttpResponse response = descriptor.doCleanup(mockReq);
193174

194175
validateResponse(response, HttpServletResponse.SC_OK, null, null);
@@ -240,15 +221,7 @@ void testDoCleanupOnlyUsedIcons(JenkinsRule r) throws Exception {
240221
project1.setIcon(customIcon);
241222
project2.setIcon(customIcon);
242223

243-
FilePath parent = r.jenkins
244-
.getRootPath()
245-
.child(CustomFolderIconConfiguration.USER_CONTENT_PATH)
246-
.child(CustomFolderIconConfiguration.PLUGIN_PATH);
247-
parent.mkdirs();
248-
249-
FilePath dummy = parent.child(DUMMY_PNG);
250-
dummy.touch(System.currentTimeMillis());
251-
assertTrue(dummy.exists());
224+
FilePath dummy = createCustomIconFile(r);
252225

253226
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
254227
StaplerRequest mockReq = mockStaplerRequest(stapler);
@@ -266,29 +239,18 @@ void testDoCleanupOnlyUsedIcons(JenkinsRule r) throws Exception {
266239
*/
267240
@Test
268241
void testDoCleanupUsedAndUnusedIcons(JenkinsRule r) throws Exception {
242+
FilePath dummy = createCustomIconFile(r);
243+
FilePath unused = createCustomIconFile(r);
244+
269245
CustomFolderIconConfiguration descriptor = new CustomFolderIconConfiguration();
270-
CustomFolderIcon customIcon = new CustomFolderIcon(DUMMY_PNG);
246+
CustomFolderIcon customIcon = new CustomFolderIcon(dummy.getName());
271247

272248
Folder project1 = r.jenkins.createProject(Folder.class, "folder");
273249
OrganizationFolder project2 = r.jenkins.createProject(OrganizationFolder.class, "org");
274250

275251
project1.setIcon(customIcon);
276252
project2.setIcon(customIcon);
277253

278-
FilePath parent = r.jenkins
279-
.getRootPath()
280-
.child(CustomFolderIconConfiguration.USER_CONTENT_PATH)
281-
.child(CustomFolderIconConfiguration.PLUGIN_PATH);
282-
parent.mkdirs();
283-
284-
FilePath dummy = parent.child(DUMMY_PNG);
285-
dummy.touch(System.currentTimeMillis());
286-
assertTrue(dummy.exists());
287-
288-
FilePath unused = parent.child("unused.png");
289-
unused.touch(System.currentTimeMillis());
290-
assertTrue(unused.exists());
291-
292254
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
293255
StaplerRequest mockReq = mockStaplerRequest(stapler);
294256
HttpResponse response = descriptor.doCleanup(mockReq);
@@ -336,13 +298,8 @@ void testDoCleanupFileNotDeleted(JenkinsRule r) throws Exception {
336298
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
337299
StaplerRequest mockReq = mockStaplerRequest(stapler);
338300

301+
FilePath file = createCustomIconFile(r);
339302
FilePath userContent = r.jenkins.getRootPath().child(CustomFolderIconConfiguration.USER_CONTENT_PATH);
340-
FilePath iconDir = userContent.child(CustomFolderIconConfiguration.PLUGIN_PATH);
341-
iconDir.mkdirs();
342-
343-
String filename = System.currentTimeMillis() + ".png";
344-
FilePath file = iconDir.child(filename);
345-
file.touch(System.currentTimeMillis());
346303

347304
try (@SuppressWarnings("unused")
348305
MockedConstruction<FilePath> mocked = mockConstructionWithAnswer(FilePath.class, invocation -> {
@@ -353,7 +310,8 @@ void testDoCleanupFileNotDeleted(JenkinsRule r) throws Exception {
353310
return true;
354311
} else if (StringUtils.equals(call, "filePath.list();")) {
355312
return List.of(file);
356-
} else if (StringUtils.equals(call, "filePath.child(\"" + filename + "\");")) {
313+
} else if (StringUtils.equals(
314+
call, "filePath.child(\n" + " \"" + file.getName() + "\"\n" + ");")) {
357315
FilePath mock = mock(FilePath.class);
358316
when(mock.delete()).thenReturn(false);
359317
return mock;
@@ -382,14 +340,7 @@ void testDoCleanupFileNotDeletedWithException(JenkinsRule r) throws Exception {
382340
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
383341
StaplerRequest mockReq = mockStaplerRequest(stapler);
384342

385-
FilePath parent = r.jenkins
386-
.getRootPath()
387-
.child(CustomFolderIconConfiguration.USER_CONTENT_PATH)
388-
.child(CustomFolderIconConfiguration.PLUGIN_PATH);
389-
parent.mkdirs();
390-
391-
FilePath file = parent.child(System.currentTimeMillis() + ".png");
392-
file.touch(System.currentTimeMillis());
343+
FilePath file = createCustomIconFile(r);
393344
File remoteFile = new File(file.getRemote());
394345

395346
// jenkins is pretty brutal when deleting files...
@@ -432,12 +383,7 @@ void testDoCleanupFileNotDeletedWithMockedException(JenkinsRule r) throws Except
432383
StaplerRequest mockReq = mockStaplerRequest(stapler);
433384

434385
FilePath userContent = r.jenkins.getRootPath().child(CustomFolderIconConfiguration.USER_CONTENT_PATH);
435-
FilePath iconDir = userContent.child(CustomFolderIconConfiguration.PLUGIN_PATH);
436-
iconDir.mkdirs();
437-
438-
String filename = System.currentTimeMillis() + ".png";
439-
FilePath file = iconDir.child(filename);
440-
file.touch(System.currentTimeMillis());
386+
FilePath file = createCustomIconFile(r);
441387

442388
try (@SuppressWarnings("unused")
443389
MockedConstruction<FilePath> mocked = mockConstructionWithAnswer(FilePath.class, invocation -> {
@@ -448,7 +394,8 @@ void testDoCleanupFileNotDeletedWithMockedException(JenkinsRule r) throws Except
448394
return true;
449395
} else if (StringUtils.equals(call, "filePath.list();")) {
450396
return List.of(file);
451-
} else if (StringUtils.equals(call, "filePath.child(\"" + filename + "\");")) {
397+
} else if (StringUtils.equals(
398+
call, "filePath.child(\n" + " \"" + file.getName() + "\"\n" + ");")) {
452399
throw new IOException("Mocked Exception!");
453400
}
454401
return fail("Unexpected invocation '" + call + "' - Test is broken!");

0 commit comments

Comments
 (0)