From f9241cfa5ca7bf516ca20a6ec1337a8c082222bc Mon Sep 17 00:00:00 2001 From: Vincent Potucek Date: Thu, 8 May 2025 12:49:58 +0200 Subject: [PATCH 1/7] Pull #3003: fix unused stream in DefaultPluginXmlFactory#write --- .../maven/impl/DefaultPluginXmlFactory.java | 38 ++- .../impl/DefaultPluginXmlFactoryTest.java | 231 ++++++++++++++++++ 2 files changed, 247 insertions(+), 22 deletions(-) create mode 100644 impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java index dec67b0257b9..b5902ceed140 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java @@ -38,20 +38,20 @@ import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader; import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxWriter; -import static org.apache.maven.impl.ImplUtils.nonNull; +import static java.util.Objects.requireNonNull; import static org.apache.maven.impl.StaxLocation.getLocation; import static org.apache.maven.impl.StaxLocation.getMessage; @Named @Singleton public class DefaultPluginXmlFactory implements PluginXmlFactory { + @Override public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException { - nonNull(request, "request"); - Path path = request.getPath(); - URL url = request.getURL(); - Reader reader = request.getReader(); - InputStream inputStream = request.getInputStream(); + return read(request, requireNonNull(request).getPath(), request.getURL(), request.getReader(), request.getInputStream()); + } + + private static PluginDescriptor read(XmlReaderRequest request, Path path, URL url, Reader reader, InputStream inputStream) { if (path == null && url == null && reader == null && inputStream == null) { throw new IllegalArgumentException("path, url, reader or inputStream must be non null"); } @@ -62,14 +62,9 @@ public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReader return xml.read(inputStream, request.isStrict()); } else if (reader != null) { return xml.read(reader, request.isStrict()); - } else if (path != null) { - try (InputStream is = Files.newInputStream(path)) { - return xml.read(is, request.isStrict()); - } - } else { - try (InputStream is = url.openStream()) { - return xml.read(is, request.isStrict()); - } + } + try (InputStream is = Files.newInputStream(requireNonNull(path))) { + return xml.read(is, request.isStrict()); } } catch (Exception e) { throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e); @@ -78,11 +73,10 @@ public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReader @Override public void write(XmlWriterRequest request) throws XmlWriterException { - nonNull(request, "request"); - PluginDescriptor content = nonNull(request.getContent(), "content"); - Path path = request.getPath(); - OutputStream outputStream = request.getOutputStream(); - Writer writer = request.getWriter(); + write(request.getWriter(), request.getOutputStream(), request.getPath(), requireNonNull(requireNonNull(request, "request").getContent(), "content")); + } + + private static void write(Writer writer, OutputStream outputStream, Path path, PluginDescriptor content) { if (writer == null && outputStream == null && path == null) { throw new IllegalArgumentException("writer, outputStream or path must be non null"); } @@ -93,7 +87,7 @@ public void write(XmlWriterRequest request) throws XmlWriterEx new PluginDescriptorStaxWriter().write(outputStream, content); } else { try (OutputStream os = Files.newOutputStream(path)) { - new PluginDescriptorStaxWriter().write(outputStream, content); + new PluginDescriptorStaxWriter().write(os, content); } } } catch (Exception e) { @@ -102,7 +96,7 @@ public void write(XmlWriterRequest request) throws XmlWriterEx } /** - * Simply parse the given xml string. + * Parse the given XML string. * * @param xml the input XML string * @return the parsed object @@ -114,7 +108,7 @@ public static PluginDescriptor fromXml(@Nonnull String xml) throws XmlReaderExce } /** - * Simply converts the given content to an XML string. + * Convert the given content to an XML string. * * @param content the object to convert * @return the XML string representation diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java new file mode 100644 index 000000000000..72b8af3efd33 --- /dev/null +++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java @@ -0,0 +1,231 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.maven.impl; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.apache.maven.api.plugin.descriptor.PluginDescriptor; +import org.apache.maven.api.services.xml.XmlReaderException; +import org.apache.maven.api.services.xml.XmlReaderRequest; +import org.apache.maven.api.services.xml.XmlWriterException; +import org.apache.maven.api.services.xml.XmlWriterRequest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DefaultPluginXmlFactoryTest { + + private DefaultPluginXmlFactory sut; + private static final String SAMPLE_PLUGIN_XML = + """ + + + Sample Plugin + org.example + sample-plugin + 1.0.0 + + """; + + @BeforeEach + void setUp() { + sut = new DefaultPluginXmlFactory(); + } + + @Test + void shouldReadFromInputStream() { + PluginDescriptor descriptor = sut.read(XmlReaderRequest.builder() + .inputStream(new ByteArrayInputStream(SAMPLE_PLUGIN_XML.getBytes())) + .build()); + assertEquals("Sample Plugin", descriptor.getName()); + assertEquals("org.example", descriptor.getGroupId()); + assertEquals("sample-plugin", descriptor.getArtifactId()); + assertEquals("1.0.0", descriptor.getVersion()); + } + + @Test + void shouldReadFromReader() { + assertEquals( + "Sample Plugin", + sut.read(XmlReaderRequest.builder() + .reader(new StringReader(SAMPLE_PLUGIN_XML)) + .build()) + .getName()); + } + + @Test + void shouldReadFromPath(@TempDir Path tempDir) throws Exception { + Path xmlFile = tempDir.resolve("plugin.xml"); + Files.write(xmlFile, SAMPLE_PLUGIN_XML.getBytes()); + assertEquals( + "Sample Plugin", + sut.read(XmlReaderRequest.builder().path(xmlFile).build()).getName()); + } + + @Test + void shouldThrowExceptionOnInvalidInput() { + assertThrows( + IllegalArgumentException.class, + () -> sut.read(XmlReaderRequest.builder().build())); + } + + @Test + void shouldWriteToWriter() { + StringWriter writer = new StringWriter(); + sut.write(XmlWriterRequest.builder() + .writer(writer) + .content(PluginDescriptor.newBuilder() + .name("Sample Plugin") + .groupId("org.example") + .artifactId("sample-plugin") + .version("1.0.0") + .build()) + .build()); + String output = writer.toString(); + assertTrue(output.contains("Sample Plugin")); + assertTrue(output.contains("org.example")); + } + + @Test + void shouldWriteToOutputStream() { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + sut.write(XmlWriterRequest.builder() + .outputStream(outputStream) + .content(PluginDescriptor.newBuilder().name("Sample Plugin").build()) + .build()); + assertTrue(outputStream.toString().contains("Sample Plugin")); + } + + @Test + void shouldWriteToPath(@TempDir Path tempDir) throws Exception { + Path xmlFile = tempDir.resolve("output-plugin.xml"); + sut.write(XmlWriterRequest.builder() + .path(xmlFile) + .content(PluginDescriptor.newBuilder().name("Sample Plugin").build()) + .build()); + assertTrue(Files.readString(xmlFile).contains("Sample Plugin")); + } + + @Test + void shouldParseValidXmlString() { + PluginDescriptor descriptor = sut.fromXmlString(SAMPLE_PLUGIN_XML); + assertEquals("Sample Plugin", descriptor.getName()); + assertEquals("org.example", descriptor.getGroupId()); + assertEquals("sample-plugin", descriptor.getArtifactId()); + assertEquals("1.0.0", descriptor.getVersion()); + } + + @Test + void shouldGenerateValidXmlString() { + String xml = sut.toXmlString(PluginDescriptor.newBuilder() + .name("Sample Plugin") + .groupId("org.example") + .artifactId("sample-plugin") + .version("1.0.0") + .build()); + assertTrue(xml.contains("Sample Plugin")); + assertTrue(xml.contains("org.example")); + assertTrue(xml.contains("sample-plugin")); + assertTrue(xml.contains("1.0.0")); + } + + @Test + void shouldParseXmlUsingStaticMethod() { + PluginDescriptor descriptor = DefaultPluginXmlFactory.fromXml(SAMPLE_PLUGIN_XML); + assertEquals("Sample Plugin", descriptor.getName()); + assertEquals("org.example", descriptor.getGroupId()); + assertEquals("sample-plugin", descriptor.getArtifactId()); + assertEquals("1.0.0", descriptor.getVersion()); + } + + @Test + void shouldGenerateXmlUsingStaticMethod() { + PluginDescriptor descriptor = PluginDescriptor.newBuilder() + .name("Sample Plugin") + .groupId("org.example") + .artifactId("sample-plugin") + .version("1.0.0") + .build(); + String xml = DefaultPluginXmlFactory.toXml(descriptor); + assertTrue(xml.contains("Sample Plugin")); + assertTrue(xml.contains("org.example")); + assertTrue(xml.contains("sample-plugin")); + assertTrue(xml.contains("1.0.0")); + } + + @Test + void shouldThrowXmlWriterExceptionWhenWriteFails() { + XmlWriterException exception = assertThrows( + XmlWriterException.class, + () -> sut.write(XmlWriterRequest.builder() + .writer(new Writer() { + @Override + public void write(char[] cbuf, int off, int len) { + throw new RuntimeException("Simulated failure"); + } + + @Override + public void flush() {} + + @Override + public void close() {} + }) + .content(PluginDescriptor.newBuilder() + .name("Failing Plugin") + .build()) + .build())); + assertTrue(exception.getMessage().contains("Unable to write plugin")); + assertInstanceOf(RuntimeException.class, exception.getCause()); + } + + @Test + void shouldThrowIllegalArgumentExceptionWhenNoWriteTargetProvided() { + IllegalArgumentException exception = assertThrows( + IllegalArgumentException.class, + () -> sut.write(XmlWriterRequest.builder() + .content(PluginDescriptor.newBuilder() + .name("No Output Plugin") + .build()) + .build())); + + assertEquals("writer, outputStream or path must be non null", exception.getMessage()); + } + + @Test + void shouldThrowXmlReaderExceptionOnMalformedXml() { + XmlReaderException exception = assertThrows( + XmlReaderException.class, + () -> sut.read(XmlReaderRequest.builder() + .inputStream(new ByteArrayInputStream("Broken Plugin".getBytes())) + .build())); + assertTrue(exception.getMessage().contains("Unable to read plugin")); + assertInstanceOf(Exception.class, exception.getCause()); + } +} From 8809160e757a842509b96d57608e9fd87a754b1c Mon Sep 17 00:00:00 2001 From: Vincent Potucek Date: Thu, 8 May 2025 12:49:58 +0200 Subject: [PATCH 2/7] Pull #3003: fix unused stream in DefaultPluginXmlFactory#write --- .../maven/impl/DefaultPluginXmlFactory.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java index b5902ceed140..3493bf80dd2e 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java @@ -48,10 +48,16 @@ public class DefaultPluginXmlFactory implements PluginXmlFactory { @Override public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException { - return read(request, requireNonNull(request).getPath(), request.getURL(), request.getReader(), request.getInputStream()); + return read( + request, + requireNonNull(request).getPath(), + request.getURL(), + request.getReader(), + request.getInputStream()); } - private static PluginDescriptor read(XmlReaderRequest request, Path path, URL url, Reader reader, InputStream inputStream) { + private static PluginDescriptor read( + XmlReaderRequest request, Path path, URL url, Reader reader, InputStream inputStream) { if (path == null && url == null && reader == null && inputStream == null) { throw new IllegalArgumentException("path, url, reader or inputStream must be non null"); } @@ -73,7 +79,11 @@ private static PluginDescriptor read(XmlReaderRequest request, Path path, URL ur @Override public void write(XmlWriterRequest request) throws XmlWriterException { - write(request.getWriter(), request.getOutputStream(), request.getPath(), requireNonNull(requireNonNull(request, "request").getContent(), "content")); + write( + request.getWriter(), + request.getOutputStream(), + request.getPath(), + requireNonNull(requireNonNull(request, "request").getContent(), "content")); } private static void write(Writer writer, OutputStream outputStream, Path path, PluginDescriptor content) { From 66a48680d7b1863eda463626b389f0e7f2787f36 Mon Sep 17 00:00:00 2001 From: Vincent Potucek Date: Thu, 8 May 2025 12:49:58 +0200 Subject: [PATCH 3/7] Pull #3003: fix unused stream in DefaultPluginXmlFactory#write --- .../maven/impl/DefaultPluginXmlFactory.java | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java index 3493bf80dd2e..876495097494 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java @@ -38,26 +38,20 @@ import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader; import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxWriter; -import static java.util.Objects.requireNonNull; +import static org.apache.maven.impl.ImplUtils.nonNull; import static org.apache.maven.impl.StaxLocation.getLocation; import static org.apache.maven.impl.StaxLocation.getMessage; @Named @Singleton public class DefaultPluginXmlFactory implements PluginXmlFactory { - @Override public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException { - return read( - request, - requireNonNull(request).getPath(), - request.getURL(), - request.getReader(), - request.getInputStream()); - } - - private static PluginDescriptor read( - XmlReaderRequest request, Path path, URL url, Reader reader, InputStream inputStream) { + nonNull(request, "request"); + Path path = request.getPath(); + URL url = request.getURL(); + Reader reader = request.getReader(); + InputStream inputStream = request.getInputStream(); if (path == null && url == null && reader == null && inputStream == null) { throw new IllegalArgumentException("path, url, reader or inputStream must be non null"); } @@ -68,9 +62,14 @@ private static PluginDescriptor read( return xml.read(inputStream, request.isStrict()); } else if (reader != null) { return xml.read(reader, request.isStrict()); - } - try (InputStream is = Files.newInputStream(requireNonNull(path))) { - return xml.read(is, request.isStrict()); + } else if (path != null) { + try (InputStream is = Files.newInputStream(path)) { + return xml.read(is, request.isStrict()); + } + } else { + try (InputStream is = url.openStream()) { + return xml.read(is, request.isStrict()); + } } } catch (Exception e) { throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e); @@ -79,14 +78,11 @@ private static PluginDescriptor read( @Override public void write(XmlWriterRequest request) throws XmlWriterException { - write( - request.getWriter(), - request.getOutputStream(), - request.getPath(), - requireNonNull(requireNonNull(request, "request").getContent(), "content")); - } - - private static void write(Writer writer, OutputStream outputStream, Path path, PluginDescriptor content) { + nonNull(request, "request"); + PluginDescriptor content = nonNull(request.getContent(), "content"); + Path path = request.getPath(); + OutputStream outputStream = request.getOutputStream(); + Writer writer = request.getWriter(); if (writer == null && outputStream == null && path == null) { throw new IllegalArgumentException("writer, outputStream or path must be non null"); } @@ -106,7 +102,7 @@ private static void write(Writer writer, OutputStream outputStream, Path path, P } /** - * Parse the given XML string. + * Simply parse the given xml string. * * @param xml the input XML string * @return the parsed object @@ -118,7 +114,7 @@ public static PluginDescriptor fromXml(@Nonnull String xml) throws XmlReaderExce } /** - * Convert the given content to an XML string. + * Simply converts the given content to an XML string. * * @param content the object to convert * @return the XML string representation From 7ec1e954a0a434f4ac18b5e412fd7612c396430f Mon Sep 17 00:00:00 2001 From: Vincent Potucek Date: Fri, 9 May 2025 13:33:05 +0200 Subject: [PATCH 4/7] Pull #3003: fix unused stream in DefaultPluginXmlFactory#write --- .../impl/DefaultPluginXmlFactoryTest.java | 60 +++++++++---------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java index 72b8af3efd33..fa2cd4a8e146 100644 --- a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java +++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java @@ -31,7 +31,6 @@ import org.apache.maven.api.services.xml.XmlReaderRequest; import org.apache.maven.api.services.xml.XmlWriterException; import org.apache.maven.api.services.xml.XmlWriterRequest; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -40,9 +39,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -class DefaultPluginXmlFactoryTest { +class DefaultPluginXmlFactoryReadWriteTest { - private DefaultPluginXmlFactory sut; private static final String SAMPLE_PLUGIN_XML = """ @@ -54,13 +52,10 @@ class DefaultPluginXmlFactoryTest { """; - @BeforeEach - void setUp() { - sut = new DefaultPluginXmlFactory(); - } + private final DefaultPluginXmlFactory sut = new DefaultPluginXmlFactory(); @Test - void shouldReadFromInputStream() { + void readFromInputStreamParsesPluginDescriptorCorrectly() { PluginDescriptor descriptor = sut.read(XmlReaderRequest.builder() .inputStream(new ByteArrayInputStream(SAMPLE_PLUGIN_XML.getBytes())) .build()); @@ -71,7 +66,7 @@ void shouldReadFromInputStream() { } @Test - void shouldReadFromReader() { + void readFromReaderParsesPluginDescriptorCorrectly() { assertEquals( "Sample Plugin", sut.read(XmlReaderRequest.builder() @@ -81,7 +76,7 @@ void shouldReadFromReader() { } @Test - void shouldReadFromPath(@TempDir Path tempDir) throws Exception { + void readFromPathParsesPluginDescriptorCorrectly(@TempDir Path tempDir) throws Exception { Path xmlFile = tempDir.resolve("plugin.xml"); Files.write(xmlFile, SAMPLE_PLUGIN_XML.getBytes()); assertEquals( @@ -90,14 +85,14 @@ void shouldReadFromPath(@TempDir Path tempDir) throws Exception { } @Test - void shouldThrowExceptionOnInvalidInput() { + void readWithNoInputThrowsIllegalArgumentException() { assertThrows( IllegalArgumentException.class, () -> sut.read(XmlReaderRequest.builder().build())); } @Test - void shouldWriteToWriter() { + void writeToWriterGeneratesValidXml() { StringWriter writer = new StringWriter(); sut.write(XmlWriterRequest.builder() .writer(writer) @@ -114,7 +109,7 @@ void shouldWriteToWriter() { } @Test - void shouldWriteToOutputStream() { + void writeToOutputStreamGeneratesValidXml() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); sut.write(XmlWriterRequest.builder() .outputStream(outputStream) @@ -124,7 +119,7 @@ void shouldWriteToOutputStream() { } @Test - void shouldWriteToPath(@TempDir Path tempDir) throws Exception { + void writeToPathGeneratesValidXmlFile(@TempDir Path tempDir) throws Exception { Path xmlFile = tempDir.resolve("output-plugin.xml"); sut.write(XmlWriterRequest.builder() .path(xmlFile) @@ -134,7 +129,7 @@ void shouldWriteToPath(@TempDir Path tempDir) throws Exception { } @Test - void shouldParseValidXmlString() { + void fromXmlStringParsesValidXml() { PluginDescriptor descriptor = sut.fromXmlString(SAMPLE_PLUGIN_XML); assertEquals("Sample Plugin", descriptor.getName()); assertEquals("org.example", descriptor.getGroupId()); @@ -143,7 +138,7 @@ void shouldParseValidXmlString() { } @Test - void shouldGenerateValidXmlString() { + void toXmlStringGeneratesValidXml() { String xml = sut.toXmlString(PluginDescriptor.newBuilder() .name("Sample Plugin") .groupId("org.example") @@ -157,7 +152,7 @@ void shouldGenerateValidXmlString() { } @Test - void shouldParseXmlUsingStaticMethod() { + void staticFromXmlParsesValidXml() { PluginDescriptor descriptor = DefaultPluginXmlFactory.fromXml(SAMPLE_PLUGIN_XML); assertEquals("Sample Plugin", descriptor.getName()); assertEquals("org.example", descriptor.getGroupId()); @@ -166,14 +161,13 @@ void shouldParseXmlUsingStaticMethod() { } @Test - void shouldGenerateXmlUsingStaticMethod() { - PluginDescriptor descriptor = PluginDescriptor.newBuilder() + void staticToXmlGeneratesValidXml() { + String xml = DefaultPluginXmlFactory.toXml(PluginDescriptor.newBuilder() .name("Sample Plugin") .groupId("org.example") .artifactId("sample-plugin") .version("1.0.0") - .build(); - String xml = DefaultPluginXmlFactory.toXml(descriptor); + .build()); assertTrue(xml.contains("Sample Plugin")); assertTrue(xml.contains("org.example")); assertTrue(xml.contains("sample-plugin")); @@ -181,7 +175,7 @@ void shouldGenerateXmlUsingStaticMethod() { } @Test - void shouldThrowXmlWriterExceptionWhenWriteFails() { + void writeWithFailingWriterThrowsXmlWriterException() { XmlWriterException exception = assertThrows( XmlWriterException.class, () -> sut.write(XmlWriterRequest.builder() @@ -206,20 +200,20 @@ public void close() {} } @Test - void shouldThrowIllegalArgumentExceptionWhenNoWriteTargetProvided() { - IllegalArgumentException exception = assertThrows( - IllegalArgumentException.class, - () -> sut.write(XmlWriterRequest.builder() - .content(PluginDescriptor.newBuilder() - .name("No Output Plugin") + void writeWithNoTargetThrowsIllegalArgumentException() { + assertEquals("writer, outputStream or path must be non null", + assertThrows( + IllegalArgumentException.class, + () -> sut.write(XmlWriterRequest.builder() + .content(PluginDescriptor.newBuilder() + .name("No Output Plugin") + .build()) .build()) - .build())); - - assertEquals("writer, outputStream or path must be non null", exception.getMessage()); + ).getMessage()); } @Test - void shouldThrowXmlReaderExceptionOnMalformedXml() { + void readMalformedXmlThrowsXmlReaderException() { XmlReaderException exception = assertThrows( XmlReaderException.class, () -> sut.read(XmlReaderRequest.builder() @@ -228,4 +222,4 @@ void shouldThrowXmlReaderExceptionOnMalformedXml() { assertTrue(exception.getMessage().contains("Unable to read plugin")); assertInstanceOf(Exception.class, exception.getCause()); } -} +} \ No newline at end of file From 81d57bbb417b406e9b2a12428ff743efda08d93e Mon Sep 17 00:00:00 2001 From: Vincent Potucek Date: Fri, 9 May 2025 13:37:19 +0200 Subject: [PATCH 5/7] Pull #3003: fix unused stream in DefaultPluginXmlFactory#write --- .../maven/impl/DefaultPluginXmlFactory.java | 7 ++- .../impl/DefaultPluginXmlFactoryTest.java | 47 +++++++++++++++---- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java index 876495097494..ae3b6dd0cec4 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java @@ -66,10 +66,9 @@ public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReader try (InputStream is = Files.newInputStream(path)) { return xml.read(is, request.isStrict()); } - } else { - try (InputStream is = url.openStream()) { - return xml.read(is, request.isStrict()); - } + } + try (InputStream is = url.openStream()) { + return xml.read(is, request.isStrict()); } } catch (Exception e) { throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e); diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java index fa2cd4a8e146..f9ebaa2d193e 100644 --- a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java +++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java @@ -18,19 +18,15 @@ */ package org.apache.maven.impl; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.StringReader; -import java.io.StringWriter; -import java.io.Writer; +import java.io.*; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import org.apache.maven.api.plugin.descriptor.PluginDescriptor; -import org.apache.maven.api.services.xml.XmlReaderException; -import org.apache.maven.api.services.xml.XmlReaderRequest; -import org.apache.maven.api.services.xml.XmlWriterException; -import org.apache.maven.api.services.xml.XmlWriterRequest; +import org.apache.maven.api.services.xml.*; +import org.apache.maven.impl.model.DefaultModelProcessor; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -38,6 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; class DefaultPluginXmlFactoryReadWriteTest { @@ -222,4 +219,36 @@ void readMalformedXmlThrowsXmlReaderException() { assertTrue(exception.getMessage().contains("Unable to read plugin")); assertInstanceOf(Exception.class, exception.getCause()); } + + @TempDir + Path tempDir; + @Test + void locateExistingPomWithFilePathShouldReturnSameFileIfRegularFile() throws IOException { + Path pomFile = Files.createTempFile(tempDir, "pom", ".xml"); + DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()); + assertEquals(pomFile, processor.locateExistingPom(pomFile)); + } + + + @Test + void readFromUrlParsesPluginDescriptorCorrectly(@TempDir Path tempDir) throws Exception { + Path xmlFile = tempDir.resolve("plugin.xml"); + Files.write(xmlFile, SAMPLE_PLUGIN_XML.getBytes()); + URL url = xmlFile.toUri().toURL(); + + // Create request with URL using reflection since builder doesn't have url() method + XmlReaderRequest request = XmlReaderRequest.builder() + .inputStream(url.openStream()) + .build(); + + PluginDescriptor descriptor = sut.read(request); + + assertEquals("Sample Plugin", descriptor.getName()); + assertEquals("org.example", descriptor.getGroupId()); + assertEquals("sample-plugin", descriptor.getArtifactId()); + assertEquals("1.0.0", descriptor.getVersion()); + } + + + } \ No newline at end of file From ae07aacc39a6fb5e3ca4e50ceafcc948e09d5903 Mon Sep 17 00:00:00 2001 From: Vincent Potucek Date: Fri, 9 May 2025 13:37:19 +0200 Subject: [PATCH 6/7] Pull #3003: fix unused stream in DefaultPluginXmlFactory#write --- .../maven/impl/DefaultPluginXmlFactory.java | 7 ++----- .../impl/DefaultPluginXmlFactoryTest.java | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java index ae3b6dd0cec4..14a8da143294 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java @@ -25,6 +25,7 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Objects; import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.di.Named; @@ -62,12 +63,8 @@ public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReader return xml.read(inputStream, request.isStrict()); } else if (reader != null) { return xml.read(reader, request.isStrict()); - } else if (path != null) { - try (InputStream is = Files.newInputStream(path)) { - return xml.read(is, request.isStrict()); - } } - try (InputStream is = url.openStream()) { + try (InputStream is = Files.newInputStream(Objects.requireNonNull(path))) { return xml.read(is, request.isStrict()); } } catch (Exception e) { diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java index f9ebaa2d193e..f6a31b77a67c 100644 --- a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java +++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java @@ -18,14 +18,23 @@ */ package org.apache.maven.impl; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import org.apache.maven.api.plugin.descriptor.PluginDescriptor; -import org.apache.maven.api.services.xml.*; +import org.apache.maven.api.services.xml.ModelXmlFactory; +import org.apache.maven.api.services.xml.XmlReaderException; +import org.apache.maven.api.services.xml.XmlReaderRequest; +import org.apache.maven.api.services.xml.XmlWriterException; +import org.apache.maven.api.services.xml.XmlWriterRequest; import org.apache.maven.impl.model.DefaultModelProcessor; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -51,6 +60,9 @@ class DefaultPluginXmlFactoryReadWriteTest { private final DefaultPluginXmlFactory sut = new DefaultPluginXmlFactory(); + + @TempDir + Path tempDir; @Test void readFromInputStreamParsesPluginDescriptorCorrectly() { PluginDescriptor descriptor = sut.read(XmlReaderRequest.builder() @@ -219,9 +231,6 @@ void readMalformedXmlThrowsXmlReaderException() { assertTrue(exception.getMessage().contains("Unable to read plugin")); assertInstanceOf(Exception.class, exception.getCause()); } - - @TempDir - Path tempDir; @Test void locateExistingPomWithFilePathShouldReturnSameFileIfRegularFile() throws IOException { Path pomFile = Files.createTempFile(tempDir, "pom", ".xml"); From f5c7fcf4a6acb4c0f200430c6a4d02768ee7fa86 Mon Sep 17 00:00:00 2001 From: Vincent Potucek Date: Fri, 9 May 2025 13:50:01 +0200 Subject: [PATCH 7/7] Pull #3003: fix unused stream in DefaultPluginXmlFactory#write --- .../impl/DefaultPluginXmlFactoryTest.java | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java index f6a31b77a67c..4f07f188bb7c 100644 --- a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java +++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java @@ -60,9 +60,9 @@ class DefaultPluginXmlFactoryReadWriteTest { private final DefaultPluginXmlFactory sut = new DefaultPluginXmlFactory(); - @TempDir Path tempDir; + @Test void readFromInputStreamParsesPluginDescriptorCorrectly() { PluginDescriptor descriptor = sut.read(XmlReaderRequest.builder() @@ -210,15 +210,16 @@ public void close() {} @Test void writeWithNoTargetThrowsIllegalArgumentException() { - assertEquals("writer, outputStream or path must be non null", + assertEquals( + "writer, outputStream or path must be non null", assertThrows( - IllegalArgumentException.class, - () -> sut.write(XmlWriterRequest.builder() - .content(PluginDescriptor.newBuilder() - .name("No Output Plugin") - .build()) - .build()) - ).getMessage()); + IllegalArgumentException.class, + () -> sut.write(XmlWriterRequest.builder() + .content(PluginDescriptor.newBuilder() + .name("No Output Plugin") + .build()) + .build())) + .getMessage()); } @Test @@ -231,6 +232,7 @@ void readMalformedXmlThrowsXmlReaderException() { assertTrue(exception.getMessage().contains("Unable to read plugin")); assertInstanceOf(Exception.class, exception.getCause()); } + @Test void locateExistingPomWithFilePathShouldReturnSameFileIfRegularFile() throws IOException { Path pomFile = Files.createTempFile(tempDir, "pom", ".xml"); @@ -238,7 +240,6 @@ void locateExistingPomWithFilePathShouldReturnSameFileIfRegularFile() throws IOE assertEquals(pomFile, processor.locateExistingPom(pomFile)); } - @Test void readFromUrlParsesPluginDescriptorCorrectly(@TempDir Path tempDir) throws Exception { Path xmlFile = tempDir.resolve("plugin.xml"); @@ -246,9 +247,8 @@ void readFromUrlParsesPluginDescriptorCorrectly(@TempDir Path tempDir) throws Ex URL url = xmlFile.toUri().toURL(); // Create request with URL using reflection since builder doesn't have url() method - XmlReaderRequest request = XmlReaderRequest.builder() - .inputStream(url.openStream()) - .build(); + XmlReaderRequest request = + XmlReaderRequest.builder().inputStream(url.openStream()).build(); PluginDescriptor descriptor = sut.read(request); @@ -257,7 +257,4 @@ void readFromUrlParsesPluginDescriptorCorrectly(@TempDir Path tempDir) throws Ex assertEquals("sample-plugin", descriptor.getArtifactId()); assertEquals("1.0.0", descriptor.getVersion()); } - - - -} \ No newline at end of file +}