From 000da89d4d6c884b950393f88cdcf4699644a223 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Thu, 21 Sep 2023 23:02:39 +0200 Subject: [PATCH] Code cleanup --- .../io/attributes/AttributeConstants.java | 1 + .../io/attributes/AttributeUtils.java | 2 +- .../io/attributes/FileAttributes.java | 3 +- .../PlexusIoResourceAttributes.java | 3 -- .../io/attributes/SymlinkUtils.java | 5 ++- .../PlexusIoProxyResourceAttributes.java | 7 +--- .../io/filemappers/AbstractFileMapper.java | 2 +- .../io/filemappers/FileExtensionMapper.java | 2 +- .../io/filemappers/IdentityMapper.java | 2 +- .../io/filemappers/MergeFileMapper.java | 2 +- .../io/filemappers/PrefixFileMapper.java | 2 +- .../io/functions/InputStreamTransformer.java | 2 +- ...ractPlexusIoArchiveResourceCollection.java | 23 +++++------- .../AbstractPlexusIoResourceCollection.java | 5 ++- .../components/io/resources/Deferred.java | 7 +++- ...xusIoCompressedFileResourceCollection.java | 2 +- .../io/resources/PlexusIoFileResource.java | 12 +++---- .../io/resources/PlexusIoResource.java | 2 +- .../io/resources/PlexusIoSymlinkResource.java | 6 ---- .../PlexusIoProxyResourceCollection.java | 6 ++-- .../proxy/ResourceInvocationHandler.java | 2 +- .../io/attributes/AttributeUtilsTest.java | 9 ++--- .../io/attributes/FileAttributesTest.java | 10 ++---- .../PlexusIoResourceAttributeUtilsTest.java | 36 +++++++++---------- .../io/attributes/SymlinkUtilsTest.java | 14 ++++---- .../io/filemappers/FileSelectorTest.java | 6 ++-- ...bstractPlexusIoResourceCollectionTest.java | 6 ++-- .../PlexusIoPlexusIoFileResourceTest.java | 9 ++--- .../PlexusIoProxyResourceCollectionTest.java | 8 ++--- .../io/resources/proxy/ProxyFactoryTest.java | 8 ++--- 30 files changed, 89 insertions(+), 115 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/components/io/attributes/AttributeConstants.java b/src/main/java/org/codehaus/plexus/components/io/attributes/AttributeConstants.java index 5ff5ba1..fb87b56 100644 --- a/src/main/java/org/codehaus/plexus/components/io/attributes/AttributeConstants.java +++ b/src/main/java/org/codehaus/plexus/components/io/attributes/AttributeConstants.java @@ -16,6 +16,7 @@ * limitations under the License. */ +@SuppressWarnings("OctalInteger") public final class AttributeConstants { public static final int OCTAL_OWNER_READ = 0400; diff --git a/src/main/java/org/codehaus/plexus/components/io/attributes/AttributeUtils.java b/src/main/java/org/codehaus/plexus/components/io/attributes/AttributeUtils.java index 37462dc..be00c0a 100644 --- a/src/main/java/org/codehaus/plexus/components/io/attributes/AttributeUtils.java +++ b/src/main/java/org/codehaus/plexus/components/io/attributes/AttributeUtils.java @@ -33,6 +33,7 @@ /** * @author Kristian Rosenvold */ +@SuppressWarnings("OctalInteger") public class AttributeUtils { /* Reads last-modified with proper failure handling if something goes wrong. @@ -101,7 +102,6 @@ public static BasicFileAttributes getFileAttributes(@Nonnull File file) throws I public static BasicFileAttributes getFileAttributes(Path path) throws IOException { if (isUnix(path)) { - try { return Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS); } catch (UnsupportedOperationException ignore) { diff --git a/src/main/java/org/codehaus/plexus/components/io/attributes/FileAttributes.java b/src/main/java/org/codehaus/plexus/components/io/attributes/FileAttributes.java index 20c8396..94c0d3c 100644 --- a/src/main/java/org/codehaus/plexus/components/io/attributes/FileAttributes.java +++ b/src/main/java/org/codehaus/plexus/components/io/attributes/FileAttributes.java @@ -112,9 +112,10 @@ public FileAttributes(@Nonnull File file, boolean followLinks) throws IOExceptio this.octalMode = attrs.containsKey("mode") ? (Integer) attrs.get("mode") & 0xfff : PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE; + //noinspection unchecked this.permissions = attrs.containsKey("permissions") ? (Set) attrs.get("permissions") - : Collections.emptySet(); + : Collections.emptySet(); this.size = (Long) attrs.get("size"); this.lastModifiedTime = (FileTime) attrs.get("lastModifiedTime"); } diff --git a/src/main/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributes.java b/src/main/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributes.java index 0b1dcb3..2aa0ef6 100644 --- a/src/main/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributes.java +++ b/src/main/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributes.java @@ -72,9 +72,6 @@ public interface PlexusIoResourceAttributes { */ int getOctalMode(); - // @Nonnull - // String getOctalModeString(); - /** * Indicates if this is a symbolic link element. * For file-based resource attributes this value may be always "false" for versions prior to java7. diff --git a/src/main/java/org/codehaus/plexus/components/io/attributes/SymlinkUtils.java b/src/main/java/org/codehaus/plexus/components/io/attributes/SymlinkUtils.java index 00b6d4f..6c6ea68 100644 --- a/src/main/java/org/codehaus/plexus/components/io/attributes/SymlinkUtils.java +++ b/src/main/java/org/codehaus/plexus/components/io/attributes/SymlinkUtils.java @@ -35,14 +35,13 @@ public class SymlinkUtils { * @throws java.io.IOException */ public static @Nonnull File readSymbolicLink(@Nonnull File symlink) throws IOException { - final java.nio.file.Path path = java.nio.file.Files.readSymbolicLink(symlink.toPath()); - return path.toFile(); + return Files.readSymbolicLink(symlink.toPath()).toFile(); } public static @Nonnull File createSymbolicLink(@Nonnull File symlink, File target) throws IOException { Path link = symlink.toPath(); if (!Files.exists(link, LinkOption.NOFOLLOW_LINKS)) { - link = java.nio.file.Files.createSymbolicLink(link, target.toPath()); + link = Files.createSymbolicLink(link, target.toPath()); } return link.toFile(); } diff --git a/src/main/java/org/codehaus/plexus/components/io/attributes/proxy/PlexusIoProxyResourceAttributes.java b/src/main/java/org/codehaus/plexus/components/io/attributes/proxy/PlexusIoProxyResourceAttributes.java index a751b36..15194fe 100644 --- a/src/main/java/org/codehaus/plexus/components/io/attributes/proxy/PlexusIoProxyResourceAttributes.java +++ b/src/main/java/org/codehaus/plexus/components/io/attributes/proxy/PlexusIoProxyResourceAttributes.java @@ -6,7 +6,7 @@ public class PlexusIoProxyResourceAttributes implements PlexusIoResourceAttributes { - PlexusIoResourceAttributes target; + final PlexusIoResourceAttributes target; public PlexusIoProxyResourceAttributes(PlexusIoResourceAttributes thisAttr) { this.target = thisAttr; @@ -45,11 +45,6 @@ public Integer getUserId() { return target.getUserId(); } - /* public String getOctalModeString() - { - return target.getOctalModeString(); - } - */ public boolean isOwnerWritable() { return target.isOwnerWritable(); } diff --git a/src/main/java/org/codehaus/plexus/components/io/filemappers/AbstractFileMapper.java b/src/main/java/org/codehaus/plexus/components/io/filemappers/AbstractFileMapper.java index a0540ad..141244f 100644 --- a/src/main/java/org/codehaus/plexus/components/io/filemappers/AbstractFileMapper.java +++ b/src/main/java/org/codehaus/plexus/components/io/filemappers/AbstractFileMapper.java @@ -29,7 +29,7 @@ public abstract class AbstractFileMapper implements FileMapper { * Checks the input and returns it without modifications. */ public @Nonnull String getMappedFileName(@Nonnull String pName) { - if (pName == null || pName.length() == 0) { + if (pName == null || pName.isEmpty()) { throw new IllegalArgumentException("The source name must not be null."); } return pName; diff --git a/src/main/java/org/codehaus/plexus/components/io/filemappers/FileExtensionMapper.java b/src/main/java/org/codehaus/plexus/components/io/filemappers/FileExtensionMapper.java index 14e0a4c..5302338 100644 --- a/src/main/java/org/codehaus/plexus/components/io/filemappers/FileExtensionMapper.java +++ b/src/main/java/org/codehaus/plexus/components/io/filemappers/FileExtensionMapper.java @@ -42,7 +42,7 @@ public void setTargetExtension(String pTargetExtension) { if (pTargetExtension == null) { throw new IllegalArgumentException("The target extension is null."); } - if (pTargetExtension.length() == 0) { + if (pTargetExtension.isEmpty()) { throw new IllegalArgumentException("The target extension is empty."); } if (pTargetExtension.charAt(0) == '.') { diff --git a/src/main/java/org/codehaus/plexus/components/io/filemappers/IdentityMapper.java b/src/main/java/org/codehaus/plexus/components/io/filemappers/IdentityMapper.java index c45cbb3..789f7f3 100644 --- a/src/main/java/org/codehaus/plexus/components/io/filemappers/IdentityMapper.java +++ b/src/main/java/org/codehaus/plexus/components/io/filemappers/IdentityMapper.java @@ -31,7 +31,7 @@ public class IdentityMapper extends AbstractFileMapper { @Nonnull public String getMappedFileName(@Nonnull String pName) { - if (pName == null || pName.length() == 0) { + if (pName == null || pName.isEmpty()) { throw new IllegalArgumentException("The source name must not be null."); } return pName; diff --git a/src/main/java/org/codehaus/plexus/components/io/filemappers/MergeFileMapper.java b/src/main/java/org/codehaus/plexus/components/io/filemappers/MergeFileMapper.java index 2a503af..796a428 100644 --- a/src/main/java/org/codehaus/plexus/components/io/filemappers/MergeFileMapper.java +++ b/src/main/java/org/codehaus/plexus/components/io/filemappers/MergeFileMapper.java @@ -41,7 +41,7 @@ public void setTargetName(String pName) { if (pName == null) { throw new IllegalArgumentException("The target name is null."); } - if (pName.length() == 0) { + if (pName.isEmpty()) { throw new IllegalArgumentException("The target name is empty."); } targetName = pName; diff --git a/src/main/java/org/codehaus/plexus/components/io/filemappers/PrefixFileMapper.java b/src/main/java/org/codehaus/plexus/components/io/filemappers/PrefixFileMapper.java index 1bb5ca2..d3be2d3 100644 --- a/src/main/java/org/codehaus/plexus/components/io/filemappers/PrefixFileMapper.java +++ b/src/main/java/org/codehaus/plexus/components/io/filemappers/PrefixFileMapper.java @@ -55,7 +55,7 @@ public void setPrefix(String prefix) { * Performs the mapping of a file name by adding a prefix. */ public static String getMappedFileName(String prefix, String name) { - if (prefix == null || prefix.length() == 0) { + if (prefix == null || prefix.isEmpty()) { return name; } return prefix + name; diff --git a/src/main/java/org/codehaus/plexus/components/io/functions/InputStreamTransformer.java b/src/main/java/org/codehaus/plexus/components/io/functions/InputStreamTransformer.java index a63ca7f..5907e10 100644 --- a/src/main/java/org/codehaus/plexus/components/io/functions/InputStreamTransformer.java +++ b/src/main/java/org/codehaus/plexus/components/io/functions/InputStreamTransformer.java @@ -29,7 +29,7 @@ public interface InputStreamTransformer { /** * Transform the supplied input stream into another input stream. - * + *

* The close method will be delegated through the entire call chain * * @param resource The p-io resource the stream is for diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoArchiveResourceCollection.java b/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoArchiveResourceCollection.java index da2bef0..03d73cf 100644 --- a/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoArchiveResourceCollection.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoArchiveResourceCollection.java @@ -21,8 +21,6 @@ import java.io.IOException; import java.util.Iterator; -import org.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer; - /** * Default implementation of {@link PlexusIoFileResourceCollection} for * zip files, tar files, etc. @@ -106,20 +104,17 @@ public void close() throws IOException { } public Stream stream() { - return new Stream() { - public void forEach(PlexusIoResourceConsumer resourceConsumer) throws IOException { - - final Iterator it = getEntries(); - while (it.hasNext()) { - final PlexusIoResource res = it.next(); - if (isSelected(res)) { - resourceConsumer.accept(res); - } - } - if (it instanceof Closeable) { - ((Closeable) it).close(); + return resourceConsumer -> { + Iterator it = getEntries(); + while (it.hasNext()) { + final PlexusIoResource res = it.next(); + if (isSelected(res)) { + resourceConsumer.accept(res); } } + if (it instanceof Closeable) { + ((Closeable) it).close(); + } }; } diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollection.java b/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollection.java index cafae1d..f0dfe91 100644 --- a/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollection.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollection.java @@ -249,9 +249,8 @@ public PlexusIoResource resolve(final PlexusIoResource resource) throws IOExcept public long getLastModified() throws IOException { long lastModified = PlexusIoResource.UNKNOWN_MODIFICATION_DATE; - for (final Iterator iter = getResources(); iter.hasNext(); ) { - final PlexusIoResource res = (PlexusIoResource) iter.next(); - long l = res.getLastModified(); + for (Iterator iter = getResources(); iter.hasNext(); ) { + long l = iter.next().getLastModified(); if (l == PlexusIoResource.UNKNOWN_MODIFICATION_DATE) { return PlexusIoResource.UNKNOWN_MODIFICATION_DATE; } diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/Deferred.java b/src/main/java/org/codehaus/plexus/components/io/resources/Deferred.java index c7461f3..7db54f3 100644 --- a/src/main/java/org/codehaus/plexus/components/io/resources/Deferred.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/Deferred.java @@ -40,7 +40,12 @@ public Deferred(final PlexusIoResource resource, PlexusIoResourceCollection owne throws IOException { this.resource = resource; this.owner = owner; - dfos = hasTransformer ? new DeferredFileOutputStream(5000000, "p-archiver", null, null) : null; + dfos = hasTransformer + ? DeferredFileOutputStream.builder() + .setThreshold(5000000) + .setPrefix("p-archiver") + .get() + : null; if (dfos != null) { InputStream inputStream = owner.getInputStream(resource); IOUtils.copy(inputStream, dfos); diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoCompressedFileResourceCollection.java b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoCompressedFileResourceCollection.java index e103803..64ba973 100644 --- a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoCompressedFileResourceCollection.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoCompressedFileResourceCollection.java @@ -108,7 +108,7 @@ public InputStream getContents() throws IOException { protected String getName(File file) throws IOException { final String name = file.getPath(); final String ext = getDefaultExtension(); - if (ext != null && ext.length() > 0 && name.endsWith(ext)) { + if (ext != null && !ext.isEmpty() && name.endsWith(ext)) { return name.substring(0, name.length() - ext.length()); } return name; diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResource.java b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResource.java index a97334c..dd262cb 100755 --- a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResource.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResource.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.nio.file.Files; import java.nio.file.attribute.FileTime; import org.apache.commons.io.IOUtils; @@ -103,7 +104,10 @@ protected PlexusIoFileResource(@Nonnull File file, @Nonnull String name, @Nonnul private static DeferredFileOutputStream asDeferredStream( @Nonnull ContentSupplier supplier, @Nonnull InputStreamTransformer transToUse, PlexusIoResource resource) throws IOException { - DeferredFileOutputStream dfos = new DeferredFileOutputStream(5000000, "p-archiver", null, null); + DeferredFileOutputStream dfos = DeferredFileOutputStream.builder() + .setThreshold(5000000) + .setPrefix("p-archiver") + .get(); InputStream inputStream = supplier.getContents(); InputStream transformed = transToUse.transform(resource, inputStream); IOUtils.copy(transformed, dfos); @@ -113,11 +117,7 @@ private static DeferredFileOutputStream asDeferredStream( } private static ContentSupplier getRootContentSupplier(final File file) { - return new ContentSupplier() { - public InputStream getContents() throws IOException { - return new FileInputStream(file); - } - }; + return () -> Files.newInputStream(file.toPath()); } public static String getName(File file) { diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoResource.java b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoResource.java index 27791a0..7653744 100644 --- a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoResource.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoResource.java @@ -73,7 +73,7 @@ public interface PlexusIoResource extends FileInfo, SizeSupplier, ContentSupplie * Creates an {@link java.io.InputStream}, which may be used to read * the files contents. This is useful, if the file selector * comes to a decision based on the files contents. - * + *

* Please note that this InputStream is unbuffered. Clients should wrap this in a * BufferedInputStream or attempt reading reasonably large chunks (8K+). */ diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoSymlinkResource.java b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoSymlinkResource.java index 441fabf..1c6b867 100644 --- a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoSymlinkResource.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoSymlinkResource.java @@ -87,10 +87,4 @@ public boolean isFile() { public long getLastModified() { return targetResource.getLastModified(); } - - @Nonnull - @Override - public PlexusIoResourceAttributes getAttributes() { - return super.getAttributes(); - } } diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/proxy/PlexusIoProxyResourceCollection.java b/src/main/java/org/codehaus/plexus/components/io/resources/proxy/PlexusIoProxyResourceCollection.java index b540ee4..ffa9430 100644 --- a/src/main/java/org/codehaus/plexus/components/io/resources/proxy/PlexusIoProxyResourceCollection.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/proxy/PlexusIoProxyResourceCollection.java @@ -42,7 +42,7 @@ */ public class PlexusIoProxyResourceCollection extends AbstractPlexusIoResourceCollectionWithAttributes implements EncodingSupported { - private PlexusIoResourceCollection src; + private final PlexusIoResourceCollection src; public PlexusIoProxyResourceCollection(@Nonnull PlexusIoResourceCollection src) { this.src = src; @@ -98,14 +98,14 @@ protected FileSelector getDefaultFileSelector() { private String getNonEmptyPrfix() { String prefix = getPrefix(); - if (prefix != null && prefix.length() == 0) { + if (prefix != null && prefix.isEmpty()) { return null; } return prefix; } class FwdIterator extends ForwardingIterator { - Iterator iter; + final Iterator iter; private final FileSelector fileSelector = getDefaultFileSelector(); diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/proxy/ResourceInvocationHandler.java b/src/main/java/org/codehaus/plexus/components/io/resources/proxy/ResourceInvocationHandler.java index 503317b..7ac62ce 100644 --- a/src/main/java/org/codehaus/plexus/components/io/resources/proxy/ResourceInvocationHandler.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/proxy/ResourceInvocationHandler.java @@ -28,7 +28,7 @@ import org.codehaus.plexus.components.io.resources.PlexusIoResource; class ResourceInvocationHandler implements InvocationHandler { - private PlexusIoResource testImpl; + private final PlexusIoResource testImpl; private final ContentSupplier contentSupplier; private final NameSupplier nameSupplier; diff --git a/src/test/java/org/codehaus/plexus/components/io/attributes/AttributeUtilsTest.java b/src/test/java/org/codehaus/plexus/components/io/attributes/AttributeUtilsTest.java index 38f1d01..8a56959 100644 --- a/src/test/java/org/codehaus/plexus/components/io/attributes/AttributeUtilsTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/attributes/AttributeUtilsTest.java @@ -5,8 +5,9 @@ import java.nio.file.attribute.PosixFilePermission; import java.util.Set; -import org.codehaus.plexus.util.Os; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -34,7 +35,7 @@ void testMorePatterns() { } @Test - void testEvenMorePatterns() throws Exception { + void testEvenMorePatterns() { final Set permissions = AttributeUtils.getPermissions(0412); assertTrue(permissions.contains(PosixFilePermission.OWNER_READ)); assertTrue(permissions.contains(PosixFilePermission.GROUP_EXECUTE)); @@ -42,14 +43,14 @@ void testEvenMorePatterns() throws Exception { } @Test - void test777() throws Exception { + void test777() { final Set permissions = AttributeUtils.getPermissions(0777); assertEquals(9, permissions.size()); } @Test + @DisabledOnOs(OS.WINDOWS) void testChmodBackAndForth() throws IOException { - if (Os.isFamily(Os.FAMILY_WINDOWS)) return; final File bxx = File.createTempFile("bxx", "ff"); AttributeUtils.chmod(bxx, 0422); PlexusIoResourceAttributes firstAttrs = new FileAttributes(bxx); diff --git a/src/test/java/org/codehaus/plexus/components/io/attributes/FileAttributesTest.java b/src/test/java/org/codehaus/plexus/components/io/attributes/FileAttributesTest.java index 0e3025c..3449f76 100644 --- a/src/test/java/org/codehaus/plexus/components/io/attributes/FileAttributesTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/attributes/FileAttributesTest.java @@ -18,8 +18,9 @@ import java.io.File; -import org.codehaus.plexus.util.Os; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -28,14 +29,9 @@ */ public class FileAttributesTest { @Test + @DisabledOnOs(OS.WINDOWS) void testGetPosixFileAttributes() throws Exception { - - if (Os.isFamily(Os.FAMILY_WINDOWS)) { - return; - } - File file = new File("."); - PlexusIoResourceAttributes fa = new FileAttributes(file); assertNotNull(fa); } diff --git a/src/test/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributeUtilsTest.java b/src/test/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributeUtilsTest.java index fe88770..4712e82 100644 --- a/src/test/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributeUtilsTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributeUtilsTest.java @@ -22,12 +22,13 @@ import java.nio.file.NoSuchFileException; import java.util.Map; -import org.codehaus.plexus.util.Os; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.cli.CommandLineException; import org.codehaus.plexus.util.cli.CommandLineUtils; import org.codehaus.plexus.util.cli.Commandline; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; import static org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributeUtils.getFileAttributes; import static org.junit.jupiter.api.Assertions.*; @@ -36,12 +37,8 @@ public class PlexusIoResourceAttributeUtilsTest { @Test + @DisabledOnOs(OS.WINDOWS) void testGetAttributesForThisTestClass() throws IOException { - if (Os.isFamily(Os.FAMILY_WINDOWS)) { - System.out.println("WARNING: Unsupported OS, skipping test"); - return; - } - URL resource = Thread.currentThread() .getContextClassLoader() .getResource(getClass().getName().replace('.', '/') + ".class"); @@ -64,12 +61,8 @@ void testGetAttributesForThisTestClass() throws IOException { } @Test + @DisabledOnOs(OS.WINDOWS) void testDirectory() throws IOException, CommandLineException { - - if (Os.isFamily(Os.FAMILY_WINDOWS)) { - return; // Nothing to do here. - } - URL resource = Thread.currentThread() .getContextClassLoader() .getResource(getClass().getName().replace('.', '/') + ".class"); @@ -106,11 +99,8 @@ void testDirectory() throws IOException, CommandLineException { } @Test + @DisabledOnOs(OS.WINDOWS) void testSrcResource() throws IOException { - if (Os.isFamily(Os.FAMILY_WINDOWS)) { - return; // Nothing to do here. - } - File dir = new File("src/test/resources/symlinks"); final Map fileAttributesByPathScreenScrape = PlexusIoResourceAttributeUtils.getFileAttributesByPath(dir, true); @@ -260,15 +250,21 @@ void testMergeAttributesOverride() { } @Test + void testFileAttributesGeneric() throws IOException { + PlexusIoResourceAttributes attrs = getFileAttributes(new File("src/test/resources/symlinks/src/fileW.txt")); + assertFalse(attrs.isSymbolicLink()); + assertTrue(StringUtils.isNotEmpty(attrs.getUserName())); + } + + @Test + @DisabledOnOs(OS.WINDOWS) void testFileAttributes() throws IOException { PlexusIoResourceAttributes attrs = getFileAttributes(new File("src/test/resources/symlinks/src/fileW.txt")); assertFalse(attrs.isSymbolicLink()); assertTrue(StringUtils.isNotEmpty(attrs.getUserName())); - if (!Os.isFamily(Os.FAMILY_WINDOWS)) { - assertTrue(StringUtils.isNotEmpty(attrs.getGroupName())); - assertNotNull(attrs.getGroupId()); - assertNotNull(attrs.getUserId()); - } + assertTrue(StringUtils.isNotEmpty(attrs.getGroupName())); + assertNotNull(attrs.getGroupId()); + assertNotNull(attrs.getUserId()); } @Test diff --git a/src/test/java/org/codehaus/plexus/components/io/attributes/SymlinkUtilsTest.java b/src/test/java/org/codehaus/plexus/components/io/attributes/SymlinkUtilsTest.java index b3f97aa..7b216bb 100644 --- a/src/test/java/org/codehaus/plexus/components/io/attributes/SymlinkUtilsTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/attributes/SymlinkUtilsTest.java @@ -24,12 +24,13 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.jupiter.api.Assertions.*; public class SymlinkUtilsTest { - File target = new File("target/symlinkCapabilities"); + final File target = new File("target/symlinkCapabilities"); - String expected = "This is a filed that we'll be symlinking to\n"; + final String expected = "This is a filed that we'll be symlinking to\n"; @BeforeEach public void setup() throws IOException { @@ -37,15 +38,12 @@ public void setup() throws IOException { Files.createDirectories(target.toPath()); } - @Test - void testName() throws Exception {} - @Test void create_read_symbolic_link_to_file() throws Exception { File symlink = new File(target, "symlinkToTarget"); File relativePath = createTargetFile(target); SymlinkUtils.createSymbolicLink(symlink, relativePath); - assertEquals(expected, FileUtils.readFileToString(symlink)); + assertEquals(expected, FileUtils.readFileToString(symlink, UTF_8)); assertEquals(new File("actualFile"), SymlinkUtils.readSymbolicLink(new File(target, "symlinkToTarget"))); } @@ -55,14 +53,14 @@ void create_read_symbolic_link_to_directory() throws Exception { createTargetFile(subDir); File symlink = new File(target, "symlinkToDir"); SymlinkUtils.createSymbolicLink(symlink, new File("aSubDir")); - assertEquals(expected, FileUtils.readFileToString(new File(symlink, "actualFile"))); + assertEquals(expected, FileUtils.readFileToString(new File(symlink, "actualFile"), UTF_8)); assertEquals(new File("aSubDir"), SymlinkUtils.readSymbolicLink(new File(target, "symlinkToDir"))); } private File createTargetFile(File target) throws IOException { File relativePath = new File("actualFile"); File actualFile = new File(target, relativePath.getPath()); - FileUtils.write(actualFile, expected); + FileUtils.write(actualFile, expected, UTF_8); return relativePath; } } diff --git a/src/test/java/org/codehaus/plexus/components/io/filemappers/FileSelectorTest.java b/src/test/java/org/codehaus/plexus/components/io/filemappers/FileSelectorTest.java index c32b03c..6879c7f 100644 --- a/src/test/java/org/codehaus/plexus/components/io/filemappers/FileSelectorTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/filemappers/FileSelectorTest.java @@ -49,11 +49,11 @@ protected void testFileSelector(FileSelector pSelector, String[] pInput, boolean final String name = pInput[i]; AbstractPlexusIoResource resource = new AbstractPlexusIoResource(name, 0, 0, true, false, true) { @Nonnull - public InputStream getContents() throws IOException { + public InputStream getContents() { throw new IllegalStateException("Not implemented"); } - public URL getURL() throws IOException { + public URL getURL() { throw new IllegalStateException("Not implemented"); } }; @@ -121,7 +121,7 @@ void testIncludeExcludeFileSelector() throws Exception { } @Test - void testIncludeExcludeFileSelector_SetExcludes() throws Exception { + void testIncludeExcludeFileSelector_SetExcludes() { IncludeExcludeFileSelector selector = new IncludeExcludeFileSelector(); // Test that the setExcludes method does not modify the excludes. diff --git a/src/test/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollectionTest.java b/src/test/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollectionTest.java index 21c3714..4e068ba 100644 --- a/src/test/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollectionTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollectionTest.java @@ -22,7 +22,7 @@ public class AbstractPlexusIoResourceCollectionTest { @Test void testGetIncludes() throws Exception { AbstractPlexusIoResourceCollection sut = new AbstractPlexusIoResourceCollection() { - public Iterator getResources() throws IOException { + public Iterator getResources() { return Arrays.asList(getResource("r1"), getResource("r2")).iterator(); } @@ -57,11 +57,11 @@ public InputStream transform(@Nonnull PlexusIoResource resource, @Nonnull final private static PlexusIoResource getResource(final String r1) { return new AbstractPlexusIoResource(r1, 0, 0, true, false, true) { @Nonnull - public InputStream getContents() throws IOException { + public InputStream getContents() { return new ByteArrayInputStream((r1 + "Payload").getBytes()); } - public URL getURL() throws IOException { + public URL getURL() { throw new IllegalStateException("Not implemented"); } }; diff --git a/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoPlexusIoFileResourceTest.java b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoPlexusIoFileResourceTest.java index a4733f9..d61cf54 100644 --- a/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoPlexusIoFileResourceTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoPlexusIoFileResourceTest.java @@ -7,16 +7,17 @@ import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes; import org.codehaus.plexus.components.io.attributes.SymlinkUtils; import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier; -import org.codehaus.plexus.util.Os; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; import static org.junit.jupiter.api.Assertions.*; public class PlexusIoPlexusIoFileResourceTest { @Test + @DisabledOnOs(OS.WINDOWS) void testRealSymlink() throws IOException { - if (Os.isFamily(Os.FAMILY_WINDOWS)) return; final File file = new File("src/test/resources/symlinks/src/symDir"); PlexusIoResourceAttributes attrs = FileAttributes.uncached(file); assertTrue(attrs.isSymbolicLink()); @@ -30,8 +31,8 @@ void testRealSymlink() throws IOException { } @Test + @DisabledOnOs(OS.WINDOWS) void testSymSymlinkFile() throws IOException { - if (Os.isFamily(Os.FAMILY_WINDOWS)) return; final File file = new File("src/test/resources/symlinks/src/symSymR"); PlexusIoResource r = ResourceFactory.createResource(file); assertTrue(r.isSymbolicLink()); @@ -45,8 +46,8 @@ void testSymSymlinkFile() throws IOException { } @Test + @DisabledOnOs(OS.WINDOWS) void testSymlinkFile() throws IOException { - if (Os.isFamily(Os.FAMILY_WINDOWS)) return; final File file = new File("src/test/resources/symlinks/src/symR"); PlexusIoResource r = ResourceFactory.createResource(file); assertTrue(r.isSymbolicLink()); diff --git a/src/test/java/org/codehaus/plexus/components/io/resources/proxy/PlexusIoProxyResourceCollectionTest.java b/src/test/java/org/codehaus/plexus/components/io/resources/proxy/PlexusIoProxyResourceCollectionTest.java index 426ff90..1ce7f92 100644 --- a/src/test/java/org/codehaus/plexus/components/io/resources/proxy/PlexusIoProxyResourceCollectionTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/resources/proxy/PlexusIoProxyResourceCollectionTest.java @@ -42,7 +42,7 @@ public class PlexusIoProxyResourceCollectionTest { private final String[] SAMPLE_EXCLUDES = {"*.junk", "somwhere/**"}; @Test - void testGetDefaultFileSelector() throws Exception { + void testGetDefaultFileSelector() { PlexusIoProxyResourceCollection resCol = new PlexusIoProxyResourceCollection(null); // This will throw an exception if there is a bug @@ -75,11 +75,11 @@ public boolean hasNext() { public PlexusIoResource next() { return new AbstractPlexusIoResource("fud", 123, 22, true, false, false) { @Nonnull - public InputStream getContents() throws IOException { + public InputStream getContents() { return null; } - public URL getURL() throws IOException { + public URL getURL() { return null; } }; @@ -95,7 +95,7 @@ void testClosing() throws IOException { final CloseableIterator closeableIterator = new CloseableIterator(); PlexusIoProxyResourceCollection resCol = new PlexusIoProxyResourceCollection(new AbstractPlexusIoResourceCollection() { - public Iterator getResources() throws IOException { + public Iterator getResources() { return closeableIterator; } diff --git a/src/test/java/org/codehaus/plexus/components/io/resources/proxy/ProxyFactoryTest.java b/src/test/java/org/codehaus/plexus/components/io/resources/proxy/ProxyFactoryTest.java index 158c363..152b633 100644 --- a/src/test/java/org/codehaus/plexus/components/io/resources/proxy/ProxyFactoryTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/resources/proxy/ProxyFactoryTest.java @@ -58,11 +58,7 @@ void testCreateProxyWithSizeSupplierOverride() throws Exception { @Test void testCreateProxyWithContentSupplierOverride() throws Exception { final InputStream s = new ByteArrayInputStream(new byte[10]); - ContentSupplier ns = new ContentSupplier() { - public InputStream getContents() throws IOException { - return s; - } - }; + ContentSupplier ns = () -> s; final PlexusIoResource proxy = ProxyFactory.createProxy(getPomResource(), ns); assertEquals(s, proxy.getContents()); } @@ -86,7 +82,7 @@ public Dummy(@Nonnull File file, @Nonnull PlexusIoResourceAttributes attrs) thro super(file, file.getName(), attrs); } - public String getSymlinkDestination() throws IOException { + public String getSymlinkDestination() { throw new IllegalStateException("Unsupported"); } }