From ef391f6498ae557d4f8dacbb1adf7494a6046e1f Mon Sep 17 00:00:00 2001 From: Vincent Potucek Date: Fri, 9 May 2025 22:07:21 +0200 Subject: [PATCH] Fix unused stream in DefaultPluginXmlFactory#write --- .../maven/impl/DefaultPluginXmlFactory.java | 2 +- .../impl/DefaultPluginXmlFactoryTest.java | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) 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..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 @@ -93,7 +93,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) { 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..824a2631eab9 --- /dev/null +++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java @@ -0,0 +1,47 @@ +/* + * 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.nio.file.Files; +import java.nio.file.Path; + +import org.apache.maven.api.plugin.descriptor.PluginDescriptor; +import org.apache.maven.api.services.xml.XmlWriterRequest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.assertj.core.api.Assertions.assertThat; + +class DefaultPluginXmlFactoryReadWriteTest { + + private final DefaultPluginXmlFactory defaultPluginXmlFactory = new DefaultPluginXmlFactory(); + + @TempDir + Path tempDir; + + @Test + void writeToPathGeneratesValidXmlFile() throws Exception { + Path xmlFile = tempDir.resolve("output-plugin.xml"); + defaultPluginXmlFactory.write(XmlWriterRequest.builder() + .path(xmlFile) + .content(PluginDescriptor.newBuilder().name("Sample Plugin").build()) + .build()); + assertThat(Files.readString(xmlFile)).contains("Sample Plugin"); + } +}