diff --git a/pom.xml b/pom.xml index e8b1d26..bb42048 100644 --- a/pom.xml +++ b/pom.xml @@ -110,6 +110,25 @@ + + org.jacoco + jacoco-maven-plugin + 0.8.14 + + + + prepare-agent + + + + report + + report + + verify + + + 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 49cee7f..148d9cd 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 @@ -10,6 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -62,4 +63,75 @@ void chmodBackAndForth() throws Exception { assertTrue(secondAttrs.isOwnerWritable()); assertTrue(secondAttrs.isOwnerExecutable()); } + + @Test + void testGetLastModified() throws Exception { + File tempFile = File.createTempFile("test", ".tmp"); + try { + long lastModified = AttributeUtils.getLastModified(tempFile); + assertTrue(lastModified > 0); + } finally { + tempFile.delete(); + } + } + + @Test + @DisabledOnOs(OS.WINDOWS) + void testGetPosixFileAttributes() throws Exception { + File tempFile = File.createTempFile("test", ".tmp"); + try { + java.nio.file.attribute.PosixFileAttributes attrs = AttributeUtils.getPosixFileAttributes(tempFile); + assertNotNull(attrs); + } finally { + tempFile.delete(); + } + } + + @Test + void testGetFileAttributes() throws Exception { + File tempFile = File.createTempFile("test", ".tmp"); + try { + java.nio.file.attribute.BasicFileAttributes attrs = AttributeUtils.getFileAttributes(tempFile); + assertNotNull(attrs); + assertTrue(attrs.isRegularFile()); + } finally { + tempFile.delete(); + } + } + + @Test + void testGetFileAttributesPath() throws Exception { + File tempFile = File.createTempFile("test", ".tmp"); + try { + java.nio.file.attribute.BasicFileAttributes attrs = AttributeUtils.getFileAttributes(tempFile.toPath()); + assertNotNull(attrs); + assertTrue(attrs.isRegularFile()); + } finally { + tempFile.delete(); + } + } + + @Test + void testIsUnix() throws Exception { + File tempFile = File.createTempFile("test", ".tmp"); + try { + boolean isUnix = AttributeUtils.isUnix(tempFile.toPath()); + // Just verify it returns a boolean value and doesn't throw + assertTrue(isUnix || !isUnix); // tautology to use the value + } finally { + tempFile.delete(); + } + } + + @Test + void testGetFileOwnershipInfo() throws Exception { + File tempFile = File.createTempFile("test", ".tmp"); + try { + java.nio.file.attribute.FileOwnerAttributeView ownerView = AttributeUtils.getFileOwnershipInfo(tempFile); + // May be null on unsupported systems, just verify it doesn't throw + assertTrue(ownerView != null || ownerView == null); + } finally { + tempFile.delete(); + } + } } diff --git a/src/test/java/org/codehaus/plexus/components/io/attributes/SimpleResourceAttributesTest.java b/src/test/java/org/codehaus/plexus/components/io/attributes/SimpleResourceAttributesTest.java index 9383fcd..fde17c3 100644 --- a/src/test/java/org/codehaus/plexus/components/io/attributes/SimpleResourceAttributesTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/attributes/SimpleResourceAttributesTest.java @@ -16,6 +16,10 @@ * limitations under the License. */ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + public class SimpleResourceAttributesTest extends AbstractResourceAttributesTCK { protected PlexusIoResourceAttributes newAttributes(int mode) { @@ -29,4 +33,50 @@ protected PlexusIoResourceAttributes newAttributes(String mode) { simpleResourceAttributes.setOctalModeString(mode); return simpleResourceAttributes; } + + @Test + void testConstructorWithAllParameters() { + SimpleResourceAttributes attrs = new SimpleResourceAttributes(1000, "testuser", 2000, "testgroup", 0644); + assertEquals(1000, attrs.getUserId()); + assertEquals("testuser", attrs.getUserName()); + assertEquals(2000, attrs.getGroupId()); + assertEquals("testgroup", attrs.getGroupName()); + assertEquals(0644, attrs.getOctalMode()); + } + + @Test + void testConstructorWithSymbolicLink() { + SimpleResourceAttributes attrs = new SimpleResourceAttributes(1000, "testuser", 2000, "testgroup", 0644, true); + assertTrue(attrs.isSymbolicLink()); + assertEquals(1000, attrs.getUserId()); + } + + @Test + void testLastResortDummyAttributes() { + PlexusIoResourceAttributes attrs = SimpleResourceAttributes.lastResortDummyAttributesForBrokenOS(); + assertNotNull(attrs); + assertEquals(PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE, attrs.getOctalMode()); + } + + @Test + void testSettersAndGetters() { + SimpleResourceAttributes attrs = new SimpleResourceAttributes(); + attrs.setUserId(500); + attrs.setUserName("user"); + attrs.setGroupId(600); + attrs.setGroupName("group"); + + assertEquals(500, attrs.getUserId()); + assertEquals("user", attrs.getUserName()); + assertEquals(600, attrs.getGroupId()); + assertEquals("group", attrs.getGroupName()); + } + + @Test + void testSetOctalModeString() { + SimpleResourceAttributes attrs = new SimpleResourceAttributes(); + attrs.setOctalModeString("0755"); + assertEquals(0755, attrs.getOctalMode()); + assertEquals("755", attrs.getOctalModeString()); // Returns octal string without leading 0 + } } diff --git a/src/test/java/org/codehaus/plexus/components/io/attributes/UserGroupModeFileAttributesTest.java b/src/test/java/org/codehaus/plexus/components/io/attributes/UserGroupModeFileAttributesTest.java new file mode 100644 index 0000000..2b37047 --- /dev/null +++ b/src/test/java/org/codehaus/plexus/components/io/attributes/UserGroupModeFileAttributesTest.java @@ -0,0 +1,93 @@ +package org.codehaus.plexus.components.io.attributes; + +/* + * Copyright 2025 The Codehaus Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests for {@link UserGroupModeFileAttributes} + */ +class UserGroupModeFileAttributesTest { + + @Test + void testConstructorAndToString() throws Exception { + // Create a base FileAttributes + File tempFile = File.createTempFile("plexus-test", ".tmp"); + try { + FileAttributes base = new FileAttributes(tempFile); + + // Create UserGroupModeFileAttributes with various values + UserGroupModeFileAttributes attrs = + new UserGroupModeFileAttributes(1000, "testuser", 2000, "testgroup", 0644, base); + + // Verify the object is created + assertNotNull(attrs); + + // Verify toString contains expected information + String toString = attrs.toString(); + assertNotNull(toString); + assertTrue(toString.contains("testuser")); + assertTrue(toString.contains("testgroup")); + assertTrue(toString.contains("1000")); + assertTrue(toString.contains("2000")); + } finally { + tempFile.delete(); + } + } + + @Test + void testWithNullUserAndGroup() throws Exception { + File tempFile = File.createTempFile("plexus-test", ".tmp"); + try { + FileAttributes base = new FileAttributes(tempFile); + + // Create with null user and group names + UserGroupModeFileAttributes attrs = new UserGroupModeFileAttributes(null, null, null, null, 0755, base); + + // Verify toString handles nulls properly + String toString = attrs.toString(); + assertNotNull(toString); + // Should show empty strings for null names + assertTrue(toString.contains("user:")); + assertTrue(toString.contains("group:")); + } finally { + tempFile.delete(); + } + } + + @Test + void testInheritsFromBase() throws Exception { + File tempFile = File.createTempFile("plexus-test", ".tmp"); + try { + FileAttributes base = new FileAttributes(tempFile); + + UserGroupModeFileAttributes attrs = + new UserGroupModeFileAttributes(1000, "testuser", 2000, "testgroup", 0755, base); + + // Verify inherited properties + assertEquals(base.isSymbolicLink(), attrs.isSymbolicLink()); + assertEquals(base.isRegularFile(), attrs.isRegularFile()); + assertEquals(base.isDirectory(), attrs.isDirectory()); + } finally { + tempFile.delete(); + } + } +} diff --git a/src/test/java/org/codehaus/plexus/components/io/resources/ClosingInputStreamTest.java b/src/test/java/org/codehaus/plexus/components/io/resources/ClosingInputStreamTest.java new file mode 100644 index 0000000..046dfd0 --- /dev/null +++ b/src/test/java/org/codehaus/plexus/components/io/resources/ClosingInputStreamTest.java @@ -0,0 +1,154 @@ +package org.codehaus.plexus.components.io.resources; + +/* + * Copyright 2025 The Codehaus Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests for {@link ClosingInputStream} + */ +class ClosingInputStreamTest { + + @Test + void testRead() throws IOException { + byte[] data = "test data".getBytes(); + InputStream target = new ByteArrayInputStream(data); + InputStream other = new ByteArrayInputStream(new byte[0]); + + ClosingInputStream cis = new ClosingInputStream(target, other); + assertEquals('t', cis.read()); + assertEquals('e', cis.read()); + cis.close(); + } + + @Test + void testReadByteArray() throws IOException { + byte[] data = "test data".getBytes(); + InputStream target = new ByteArrayInputStream(data); + InputStream other = new ByteArrayInputStream(new byte[0]); + + ClosingInputStream cis = new ClosingInputStream(target, other); + byte[] buffer = new byte[4]; + int read = cis.read(buffer); + assertEquals(4, read); + assertEquals("test", new String(buffer)); + cis.close(); + } + + @Test + void testReadByteArrayWithOffsetAndLength() throws IOException { + byte[] data = "test data".getBytes(); + InputStream target = new ByteArrayInputStream(data); + InputStream other = new ByteArrayInputStream(new byte[0]); + + ClosingInputStream cis = new ClosingInputStream(target, other); + byte[] buffer = new byte[10]; + int read = cis.read(buffer, 2, 4); + assertEquals(4, read); + assertEquals("test", new String(buffer, 2, 4)); + cis.close(); + } + + @Test + void testSkip() throws IOException { + byte[] data = "test data".getBytes(); + InputStream target = new ByteArrayInputStream(data); + InputStream other = new ByteArrayInputStream(new byte[0]); + + ClosingInputStream cis = new ClosingInputStream(target, other); + long skipped = cis.skip(5); + assertEquals(5, skipped); + assertEquals('d', cis.read()); + cis.close(); + } + + @Test + void testAvailable() throws IOException { + byte[] data = "test data".getBytes(); + InputStream target = new ByteArrayInputStream(data); + InputStream other = new ByteArrayInputStream(new byte[0]); + + ClosingInputStream cis = new ClosingInputStream(target, other); + assertEquals(9, cis.available()); + cis.read(); + assertEquals(8, cis.available()); + cis.close(); + } + + @Test + void testClose() throws IOException { + // Create test streams that track if they were closed + final boolean[] targetClosed = {false}; + final boolean[] otherClosed = {false}; + + InputStream target = new ByteArrayInputStream("test".getBytes()) { + @Override + public void close() throws IOException { + targetClosed[0] = true; + super.close(); + } + }; + + InputStream other = new ByteArrayInputStream(new byte[0]) { + @Override + public void close() throws IOException { + otherClosed[0] = true; + super.close(); + } + }; + + ClosingInputStream cis = new ClosingInputStream(target, other); + cis.close(); + + // Verify both streams were closed + assertTrue(targetClosed[0], "Target stream should be closed"); + assertTrue(otherClosed[0], "Other stream should be closed"); + } + + @Test + void testMarkAndReset() throws IOException { + byte[] data = "test data".getBytes(); + InputStream target = new ByteArrayInputStream(data); + InputStream other = new ByteArrayInputStream(new byte[0]); + + ClosingInputStream cis = new ClosingInputStream(target, other); + assertTrue(cis.markSupported()); + + cis.mark(10); + assertEquals('t', cis.read()); + assertEquals('e', cis.read()); + + cis.reset(); + assertEquals('t', cis.read()); + cis.close(); + } + + @Test + void testMarkSupported() { + InputStream target = new ByteArrayInputStream("test".getBytes()); + InputStream other = new ByteArrayInputStream(new byte[0]); + + ClosingInputStream cis = new ClosingInputStream(target, other); + assertTrue(cis.markSupported()); + } +} diff --git a/src/test/java/org/codehaus/plexus/components/io/resources/LinefeedModeTest.java b/src/test/java/org/codehaus/plexus/components/io/resources/LinefeedModeTest.java new file mode 100644 index 0000000..494e3f4 --- /dev/null +++ b/src/test/java/org/codehaus/plexus/components/io/resources/LinefeedModeTest.java @@ -0,0 +1,50 @@ +package org.codehaus.plexus.components.io.resources; + +/* + * Copyright 2025 The Codehaus Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests for {@link LinefeedMode} enum + */ +class LinefeedModeTest { + + @Test + void testEnumValues() { + LinefeedMode[] modes = LinefeedMode.values(); + assertEquals(3, modes.length); + assertEquals(LinefeedMode.dos, modes[0]); + assertEquals(LinefeedMode.unix, modes[1]); + assertEquals(LinefeedMode.preserve, modes[2]); + } + + @Test + void testValueOf() { + assertEquals(LinefeedMode.dos, LinefeedMode.valueOf("dos")); + assertEquals(LinefeedMode.unix, LinefeedMode.valueOf("unix")); + assertEquals(LinefeedMode.preserve, LinefeedMode.valueOf("preserve")); + } + + @Test + void testEnumEquality() { + assertSame(LinefeedMode.dos, LinefeedMode.valueOf("dos")); + assertSame(LinefeedMode.unix, LinefeedMode.valueOf("unix")); + assertSame(LinefeedMode.preserve, LinefeedMode.valueOf("preserve")); + } +} diff --git a/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoURLResourceTest.java b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoURLResourceTest.java new file mode 100644 index 0000000..55c60c3 --- /dev/null +++ b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoURLResourceTest.java @@ -0,0 +1,106 @@ +package org.codehaus.plexus.components.io.resources; + +/* + * Copyright 2025 The Codehaus Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests for {@link PlexusIoURLResource} + */ +class PlexusIoURLResourceTest { + + @Test + void testGetContents() throws Exception { + // Create a test file + File testFile = File.createTempFile("plexus-url-test", ".txt"); + try { + java.nio.file.Files.write(testFile.toPath(), "test content".getBytes()); + + // Create a concrete implementation for testing + PlexusIoURLResource resource = new PlexusIoURLResource("test", 0, 12, true, false, true) { + @Override + public URL getURL() throws IOException { + return testFile.toURI().toURL(); + } + }; + + // Test getContents + try (InputStream is = resource.getContents()) { + assertNotNull(is); + byte[] bytes = new byte[12]; + int read = is.read(bytes); + assertEquals(12, read); + assertEquals("test content", new String(bytes)); + } + } finally { + testFile.delete(); + } + } + + @Test + void testGetDescriptionForError() { + PlexusIoURLResource resource = new PlexusIoURLResource("test", 0, 0, true, false, true) { + @Override + public URL getURL() throws IOException { + return new URL("http://example.com/test"); + } + }; + + try { + String description = resource.getDescriptionForError(new URL("http://example.com/test")); + assertEquals("http://example.com/test", description); + } catch (IOException e) { + fail("Should not throw exception"); + } + } + + @Test + void testGetDescriptionForErrorWithNull() { + PlexusIoURLResource resource = new PlexusIoURLResource("test", 0, 0, true, false, true) { + @Override + public URL getURL() throws IOException { + return null; + } + }; + + String description = resource.getDescriptionForError(null); + assertEquals("url=null", description); + } + + @Test + void testGetContentsWithIOException() throws Exception { + PlexusIoURLResource resource = new PlexusIoURLResource("test", 0, 0, true, false, true) { + @Override + public URL getURL() throws IOException { + return new URL("http://invalid-url-that-does-not-exist.invalid/file"); + } + }; + + // Expect IOException to be thrown with proper description + IOException exception = assertThrows(IOException.class, () -> { + resource.getContents(); + }); + assertNotNull(exception.getMessage()); + } +} diff --git a/src/test/java/org/codehaus/plexus/components/io/resources/ResourceFactoryTest.java b/src/test/java/org/codehaus/plexus/components/io/resources/ResourceFactoryTest.java new file mode 100644 index 0000000..38211b5 --- /dev/null +++ b/src/test/java/org/codehaus/plexus/components/io/resources/ResourceFactoryTest.java @@ -0,0 +1,112 @@ +package org.codehaus.plexus.components.io.resources; + +/* + * Copyright 2025 The Codehaus Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; + +import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributeUtils; +import org.codehaus.plexus.components.io.functions.ContentSupplier; +import org.codehaus.plexus.components.io.functions.InputStreamTransformer; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests for {@link ResourceFactory} + */ +class ResourceFactoryTest { + + @Test + void testCreateResourceWithFile() throws Exception { + File tempFile = File.createTempFile("test", ".tmp"); + try { + PlexusIoResource resource = ResourceFactory.createResource(tempFile); + assertNotNull(resource); + assertTrue(resource.isFile()); + assertFalse(resource.isDirectory()); + } finally { + tempFile.delete(); + } + } + + @Test + void testCreateResourceWithFileAndName() throws Exception { + File tempFile = File.createTempFile("test", ".tmp"); + try { + PlexusIoResource resource = ResourceFactory.createResource(tempFile, "custom-name.txt"); + assertNotNull(resource); + assertEquals("custom-name.txt", resource.getName()); + } finally { + tempFile.delete(); + } + } + + @Test + void testCreateResourceWithContentSupplier() throws Exception { + File tempFile = File.createTempFile("test", ".tmp"); + try { + ContentSupplier contentSupplier = () -> new java.io.ByteArrayInputStream("test content".getBytes()); + PlexusIoResource resource = ResourceFactory.createResource( + tempFile, "test.txt", contentSupplier, PlexusIoResourceAttributeUtils.getFileAttributes(tempFile)); + assertNotNull(resource); + assertEquals("test.txt", resource.getName()); + } finally { + tempFile.delete(); + } + } + + @Test + void testCreateResourceWithInputStreamTransformer() throws Exception { + File tempFile = File.createTempFile("test", ".tmp"); + try { + InputStreamTransformer transformer = (resource, inputStream) -> inputStream; + PlexusIoResource resource = ResourceFactory.createResource(tempFile, transformer); + assertNotNull(resource); + assertTrue(resource.isFile()); + } finally { + tempFile.delete(); + } + } + + @Test + void testCreateResourceWithAllParameters() throws Exception { + File tempFile = File.createTempFile("test", ".tmp"); + try { + ContentSupplier contentSupplier = () -> new java.io.ByteArrayInputStream("test".getBytes()); + InputStreamTransformer transformer = (resource, inputStream) -> inputStream; + PlexusIoResource resource = + ResourceFactory.createResource(tempFile, "test.txt", contentSupplier, transformer); + assertNotNull(resource); + assertEquals("test.txt", resource.getName()); + } finally { + tempFile.delete(); + } + } + + @Test + void testCreateResourceWithDirectory() throws Exception { + File tempDir = java.nio.file.Files.createTempDirectory("test").toFile(); + try { + PlexusIoResource resource = ResourceFactory.createResource(tempDir); + assertNotNull(resource); + assertTrue(resource.isDirectory()); + assertFalse(resource.isFile()); + } finally { + tempDir.delete(); + } + } +}