Skip to content

Commit 7ee22e2

Browse files
Implement UI tests (#388)
1 parent 1df2a3e commit 7ee22e2

File tree

6 files changed

+219
-190
lines changed

6 files changed

+219
-190
lines changed

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

Lines changed: 15 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,7 @@ 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(call, "filePath.child(\n \"" + file.getName() + "\"\n);")) {
162150
throw new IOException("Mocked Exception!");
163151
}
164152
return fail("Unexpected invocation '" + call + "' - Test is broken!");
@@ -180,15 +168,7 @@ void testDoCleanupNoItems(JenkinsRule r) throws Exception {
180168
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
181169
StaplerRequest mockReq = mockStaplerRequest(stapler);
182170

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());
171+
FilePath file = createCustomIconFile(r);
192172
HttpResponse response = descriptor.doCleanup(mockReq);
193173

194174
validateResponse(response, HttpServletResponse.SC_OK, null, null);
@@ -240,15 +220,7 @@ void testDoCleanupOnlyUsedIcons(JenkinsRule r) throws Exception {
240220
project1.setIcon(customIcon);
241221
project2.setIcon(customIcon);
242222

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());
223+
FilePath dummy = createCustomIconFile(r);
252224

253225
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
254226
StaplerRequest mockReq = mockStaplerRequest(stapler);
@@ -266,29 +238,18 @@ void testDoCleanupOnlyUsedIcons(JenkinsRule r) throws Exception {
266238
*/
267239
@Test
268240
void testDoCleanupUsedAndUnusedIcons(JenkinsRule r) throws Exception {
241+
FilePath dummy = createCustomIconFile(r);
242+
FilePath unused = createCustomIconFile(r);
243+
269244
CustomFolderIconConfiguration descriptor = new CustomFolderIconConfiguration();
270-
CustomFolderIcon customIcon = new CustomFolderIcon(DUMMY_PNG);
245+
CustomFolderIcon customIcon = new CustomFolderIcon(dummy.getName());
271246

272247
Folder project1 = r.jenkins.createProject(Folder.class, "folder");
273248
OrganizationFolder project2 = r.jenkins.createProject(OrganizationFolder.class, "org");
274249

275250
project1.setIcon(customIcon);
276251
project2.setIcon(customIcon);
277252

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-
292253
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
293254
StaplerRequest mockReq = mockStaplerRequest(stapler);
294255
HttpResponse response = descriptor.doCleanup(mockReq);
@@ -336,13 +297,8 @@ void testDoCleanupFileNotDeleted(JenkinsRule r) throws Exception {
336297
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
337298
StaplerRequest mockReq = mockStaplerRequest(stapler);
338299

300+
FilePath file = createCustomIconFile(r);
339301
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());
346302

347303
try (@SuppressWarnings("unused")
348304
MockedConstruction<FilePath> mocked = mockConstructionWithAnswer(FilePath.class, invocation -> {
@@ -353,7 +309,7 @@ void testDoCleanupFileNotDeleted(JenkinsRule r) throws Exception {
353309
return true;
354310
} else if (StringUtils.equals(call, "filePath.list();")) {
355311
return List.of(file);
356-
} else if (StringUtils.equals(call, "filePath.child(\"" + filename + "\");")) {
312+
} else if (StringUtils.equals(call, "filePath.child(\n \"" + file.getName() + "\"\n);")) {
357313
FilePath mock = mock(FilePath.class);
358314
when(mock.delete()).thenReturn(false);
359315
return mock;
@@ -382,14 +338,7 @@ void testDoCleanupFileNotDeletedWithException(JenkinsRule r) throws Exception {
382338
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
383339
StaplerRequest mockReq = mockStaplerRequest(stapler);
384340

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());
341+
FilePath file = createCustomIconFile(r);
393342
File remoteFile = new File(file.getRemote());
394343

395344
// jenkins is pretty brutal when deleting files...
@@ -432,12 +381,7 @@ void testDoCleanupFileNotDeletedWithMockedException(JenkinsRule r) throws Except
432381
StaplerRequest mockReq = mockStaplerRequest(stapler);
433382

434383
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());
384+
FilePath file = createCustomIconFile(r);
441385

442386
try (@SuppressWarnings("unused")
443387
MockedConstruction<FilePath> mocked = mockConstructionWithAnswer(FilePath.class, invocation -> {
@@ -448,7 +392,7 @@ void testDoCleanupFileNotDeletedWithMockedException(JenkinsRule r) throws Except
448392
return true;
449393
} else if (StringUtils.equals(call, "filePath.list();")) {
450394
return List.of(file);
451-
} else if (StringUtils.equals(call, "filePath.child(\"" + filename + "\");")) {
395+
} else if (StringUtils.equals(call, "filePath.child(\n \"" + file.getName() + "\"\n);")) {
452396
throw new IOException("Mocked Exception!");
453397
}
454398
return fail("Unexpected invocation '" + call + "' - Test is broken!");

0 commit comments

Comments
 (0)