|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.plugins.clean; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.AccessDeniedException; |
| 23 | +import java.nio.file.DirectoryNotEmptyException; |
| 24 | +import java.nio.file.FileSystems; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.attribute.PosixFilePermission; |
| 27 | +import java.nio.file.attribute.PosixFilePermissions; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +import org.apache.maven.plugin.logging.Log; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.io.TempDir; |
| 33 | +import org.mockito.ArgumentCaptor; |
| 34 | +import org.mockito.InOrder; |
| 35 | + |
| 36 | +import static java.nio.file.Files.createDirectory; |
| 37 | +import static java.nio.file.Files.createFile; |
| 38 | +import static java.nio.file.Files.exists; |
| 39 | +import static java.nio.file.Files.setPosixFilePermissions; |
| 40 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 45 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 46 | +import static org.mockito.ArgumentMatchers.any; |
| 47 | +import static org.mockito.ArgumentMatchers.eq; |
| 48 | +import static org.mockito.Mockito.inOrder; |
| 49 | +import static org.mockito.Mockito.mock; |
| 50 | +import static org.mockito.Mockito.never; |
| 51 | +import static org.mockito.Mockito.times; |
| 52 | +import static org.mockito.Mockito.verify; |
| 53 | +import static org.mockito.Mockito.when; |
| 54 | + |
| 55 | +class CleanerTest { |
| 56 | + |
| 57 | + private static final boolean POSIX_COMPLIANT = |
| 58 | + FileSystems.getDefault().supportedFileAttributeViews().contains("posix"); |
| 59 | + |
| 60 | + private final Log log = mock(); |
| 61 | + |
| 62 | + @Test |
| 63 | + void deleteSucceedsDeeply(@TempDir Path tempDir) throws Exception { |
| 64 | + assumeTrue(POSIX_COMPLIANT); |
| 65 | + final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath(); |
| 66 | + final Path file = createFile(basedir.resolve("file")); |
| 67 | + final Cleaner cleaner = new Cleaner(null, log, false, null, null); |
| 68 | + cleaner.delete(basedir.toFile(), null, false, true, false); |
| 69 | + assertFalse(exists(basedir)); |
| 70 | + assertFalse(exists(file)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void deleteFailsWithoutRetryWhenNoPermission(@TempDir Path tempDir) throws Exception { |
| 75 | + assumeTrue(POSIX_COMPLIANT); |
| 76 | + when(log.isWarnEnabled()).thenReturn(true); |
| 77 | + final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath(); |
| 78 | + createFile(basedir.resolve("file")); |
| 79 | + // Remove the executable flag to prevent directory listing, which will result in a DirectoryNotEmptyException. |
| 80 | + final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rw-rw-r--"); |
| 81 | + setPosixFilePermissions(basedir, permissions); |
| 82 | + final Cleaner cleaner = new Cleaner(null, log, false, null, null); |
| 83 | + final IOException exception = |
| 84 | + assertThrows(IOException.class, () -> cleaner.delete(basedir.toFile(), null, false, true, false)); |
| 85 | + verify(log, never()).warn(any(CharSequence.class), any(Throwable.class)); |
| 86 | + assertEquals("Failed to delete " + basedir, exception.getMessage()); |
| 87 | + final DirectoryNotEmptyException cause = |
| 88 | + assertInstanceOf(DirectoryNotEmptyException.class, exception.getCause()); |
| 89 | + assertEquals(basedir.toString(), cause.getMessage()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception { |
| 94 | + assumeTrue(POSIX_COMPLIANT); |
| 95 | + final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath(); |
| 96 | + createFile(basedir.resolve("file")); |
| 97 | + // Remove the executable flag to prevent directory listing, which will result in a DirectoryNotEmptyException. |
| 98 | + final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rw-rw-r--"); |
| 99 | + setPosixFilePermissions(basedir, permissions); |
| 100 | + final Cleaner cleaner = new Cleaner(null, log, false, null, null); |
| 101 | + final IOException exception = |
| 102 | + assertThrows(IOException.class, () -> cleaner.delete(basedir.toFile(), null, false, true, true)); |
| 103 | + assertEquals("Failed to delete " + basedir, exception.getMessage()); |
| 104 | + final DirectoryNotEmptyException cause = |
| 105 | + assertInstanceOf(DirectoryNotEmptyException.class, exception.getCause()); |
| 106 | + assertEquals(basedir.toString(), cause.getMessage()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void deleteLogsWarningWithoutRetryWhenNoPermission(@TempDir Path tempDir) throws Exception { |
| 111 | + assumeTrue(POSIX_COMPLIANT); |
| 112 | + when(log.isWarnEnabled()).thenReturn(true); |
| 113 | + final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath(); |
| 114 | + final Path file = createFile(basedir.resolve("file")); |
| 115 | + // Remove the writable flag to prevent deletion of the file, which will result in an AccessDeniedException. |
| 116 | + final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("r-xr-xr-x"); |
| 117 | + setPosixFilePermissions(basedir, permissions); |
| 118 | + final Cleaner cleaner = new Cleaner(null, log, false, null, null); |
| 119 | + assertDoesNotThrow(() -> cleaner.delete(basedir.toFile(), null, false, false, false)); |
| 120 | + verify(log, times(2)).warn(any(CharSequence.class), any(Throwable.class)); |
| 121 | + InOrder inOrder = inOrder(log); |
| 122 | + ArgumentCaptor<AccessDeniedException> cause1 = ArgumentCaptor.forClass(AccessDeniedException.class); |
| 123 | + inOrder.verify(log).warn(eq("Failed to delete " + file), cause1.capture()); |
| 124 | + assertEquals(file.toString(), cause1.getValue().getMessage()); |
| 125 | + ArgumentCaptor<DirectoryNotEmptyException> cause2 = ArgumentCaptor.forClass(DirectoryNotEmptyException.class); |
| 126 | + inOrder.verify(log).warn(eq("Failed to delete " + basedir), cause2.capture()); |
| 127 | + assertEquals(basedir.toString(), cause2.getValue().getMessage()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void deleteDoesNotLogAnythingWhenNoPermissionAndWarnDisabled(@TempDir Path tempDir) throws Exception { |
| 132 | + assumeTrue(POSIX_COMPLIANT); |
| 133 | + when(log.isWarnEnabled()).thenReturn(false); |
| 134 | + final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath(); |
| 135 | + createFile(basedir.resolve("file")); |
| 136 | + // Remove the writable flag to prevent deletion of the file, which will result in an AccessDeniedException. |
| 137 | + final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("r-xr-xr-x"); |
| 138 | + setPosixFilePermissions(basedir, permissions); |
| 139 | + final Cleaner cleaner = new Cleaner(null, log, false, null, null); |
| 140 | + assertDoesNotThrow(() -> cleaner.delete(basedir.toFile(), null, false, false, false)); |
| 141 | + verify(log, never()).warn(any(CharSequence.class), any(Throwable.class)); |
| 142 | + } |
| 143 | +} |
0 commit comments