Skip to content

Commit f9241cf

Browse files
author
Vincent Potucek
committed
Pull #3003: fix unused stream in DefaultPluginXmlFactory#write
1 parent 6be7a12 commit f9241cf

File tree

2 files changed

+247
-22
lines changed

2 files changed

+247
-22
lines changed

impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@
3838
import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader;
3939
import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxWriter;
4040

41-
import static org.apache.maven.impl.ImplUtils.nonNull;
41+
import static java.util.Objects.requireNonNull;
4242
import static org.apache.maven.impl.StaxLocation.getLocation;
4343
import static org.apache.maven.impl.StaxLocation.getMessage;
4444

4545
@Named
4646
@Singleton
4747
public class DefaultPluginXmlFactory implements PluginXmlFactory {
48+
4849
@Override
4950
public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
50-
nonNull(request, "request");
51-
Path path = request.getPath();
52-
URL url = request.getURL();
53-
Reader reader = request.getReader();
54-
InputStream inputStream = request.getInputStream();
51+
return read(request, requireNonNull(request).getPath(), request.getURL(), request.getReader(), request.getInputStream());
52+
}
53+
54+
private static PluginDescriptor read(XmlReaderRequest request, Path path, URL url, Reader reader, InputStream inputStream) {
5555
if (path == null && url == null && reader == null && inputStream == null) {
5656
throw new IllegalArgumentException("path, url, reader or inputStream must be non null");
5757
}
@@ -62,14 +62,9 @@ public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReader
6262
return xml.read(inputStream, request.isStrict());
6363
} else if (reader != null) {
6464
return xml.read(reader, request.isStrict());
65-
} else if (path != null) {
66-
try (InputStream is = Files.newInputStream(path)) {
67-
return xml.read(is, request.isStrict());
68-
}
69-
} else {
70-
try (InputStream is = url.openStream()) {
71-
return xml.read(is, request.isStrict());
72-
}
65+
}
66+
try (InputStream is = Files.newInputStream(requireNonNull(path))) {
67+
return xml.read(is, request.isStrict());
7368
}
7469
} catch (Exception e) {
7570
throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e);
@@ -78,11 +73,10 @@ public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReader
7873

7974
@Override
8075
public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException {
81-
nonNull(request, "request");
82-
PluginDescriptor content = nonNull(request.getContent(), "content");
83-
Path path = request.getPath();
84-
OutputStream outputStream = request.getOutputStream();
85-
Writer writer = request.getWriter();
76+
write(request.getWriter(), request.getOutputStream(), request.getPath(), requireNonNull(requireNonNull(request, "request").getContent(), "content"));
77+
}
78+
79+
private static void write(Writer writer, OutputStream outputStream, Path path, PluginDescriptor content) {
8680
if (writer == null && outputStream == null && path == null) {
8781
throw new IllegalArgumentException("writer, outputStream or path must be non null");
8882
}
@@ -93,7 +87,7 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
9387
new PluginDescriptorStaxWriter().write(outputStream, content);
9488
} else {
9589
try (OutputStream os = Files.newOutputStream(path)) {
96-
new PluginDescriptorStaxWriter().write(outputStream, content);
90+
new PluginDescriptorStaxWriter().write(os, content);
9791
}
9892
}
9993
} catch (Exception e) {
@@ -102,7 +96,7 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
10296
}
10397

10498
/**
105-
* Simply parse the given xml string.
99+
* Parse the given XML string.
106100
*
107101
* @param xml the input XML string
108102
* @return the parsed object
@@ -114,7 +108,7 @@ public static PluginDescriptor fromXml(@Nonnull String xml) throws XmlReaderExce
114108
}
115109

116110
/**
117-
* Simply converts the given content to an XML string.
111+
* Convert the given content to an XML string.
118112
*
119113
* @param content the object to convert
120114
* @return the XML string representation
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.impl;
20+
21+
import java.io.ByteArrayInputStream;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.StringReader;
24+
import java.io.StringWriter;
25+
import java.io.Writer;
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
29+
import org.apache.maven.api.plugin.descriptor.PluginDescriptor;
30+
import org.apache.maven.api.services.xml.XmlReaderException;
31+
import org.apache.maven.api.services.xml.XmlReaderRequest;
32+
import org.apache.maven.api.services.xml.XmlWriterException;
33+
import org.apache.maven.api.services.xml.XmlWriterRequest;
34+
import org.junit.jupiter.api.BeforeEach;
35+
import org.junit.jupiter.api.Test;
36+
import org.junit.jupiter.api.io.TempDir;
37+
38+
import static org.junit.jupiter.api.Assertions.assertEquals;
39+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
40+
import static org.junit.jupiter.api.Assertions.assertThrows;
41+
import static org.junit.jupiter.api.Assertions.assertTrue;
42+
43+
class DefaultPluginXmlFactoryTest {
44+
45+
private DefaultPluginXmlFactory sut;
46+
private static final String SAMPLE_PLUGIN_XML =
47+
"""
48+
<?xml version="1.0" encoding="UTF-8"?>
49+
<plugin>
50+
<name>Sample Plugin</name>
51+
<groupId>org.example</groupId>
52+
<artifactId>sample-plugin</artifactId>
53+
<version>1.0.0</version>
54+
</plugin>
55+
""";
56+
57+
@BeforeEach
58+
void setUp() {
59+
sut = new DefaultPluginXmlFactory();
60+
}
61+
62+
@Test
63+
void shouldReadFromInputStream() {
64+
PluginDescriptor descriptor = sut.read(XmlReaderRequest.builder()
65+
.inputStream(new ByteArrayInputStream(SAMPLE_PLUGIN_XML.getBytes()))
66+
.build());
67+
assertEquals("Sample Plugin", descriptor.getName());
68+
assertEquals("org.example", descriptor.getGroupId());
69+
assertEquals("sample-plugin", descriptor.getArtifactId());
70+
assertEquals("1.0.0", descriptor.getVersion());
71+
}
72+
73+
@Test
74+
void shouldReadFromReader() {
75+
assertEquals(
76+
"Sample Plugin",
77+
sut.read(XmlReaderRequest.builder()
78+
.reader(new StringReader(SAMPLE_PLUGIN_XML))
79+
.build())
80+
.getName());
81+
}
82+
83+
@Test
84+
void shouldReadFromPath(@TempDir Path tempDir) throws Exception {
85+
Path xmlFile = tempDir.resolve("plugin.xml");
86+
Files.write(xmlFile, SAMPLE_PLUGIN_XML.getBytes());
87+
assertEquals(
88+
"Sample Plugin",
89+
sut.read(XmlReaderRequest.builder().path(xmlFile).build()).getName());
90+
}
91+
92+
@Test
93+
void shouldThrowExceptionOnInvalidInput() {
94+
assertThrows(
95+
IllegalArgumentException.class,
96+
() -> sut.read(XmlReaderRequest.builder().build()));
97+
}
98+
99+
@Test
100+
void shouldWriteToWriter() {
101+
StringWriter writer = new StringWriter();
102+
sut.write(XmlWriterRequest.<PluginDescriptor>builder()
103+
.writer(writer)
104+
.content(PluginDescriptor.newBuilder()
105+
.name("Sample Plugin")
106+
.groupId("org.example")
107+
.artifactId("sample-plugin")
108+
.version("1.0.0")
109+
.build())
110+
.build());
111+
String output = writer.toString();
112+
assertTrue(output.contains("<name>Sample Plugin</name>"));
113+
assertTrue(output.contains("<groupId>org.example</groupId>"));
114+
}
115+
116+
@Test
117+
void shouldWriteToOutputStream() {
118+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
119+
sut.write(XmlWriterRequest.<PluginDescriptor>builder()
120+
.outputStream(outputStream)
121+
.content(PluginDescriptor.newBuilder().name("Sample Plugin").build())
122+
.build());
123+
assertTrue(outputStream.toString().contains("<name>Sample Plugin</name>"));
124+
}
125+
126+
@Test
127+
void shouldWriteToPath(@TempDir Path tempDir) throws Exception {
128+
Path xmlFile = tempDir.resolve("output-plugin.xml");
129+
sut.write(XmlWriterRequest.<PluginDescriptor>builder()
130+
.path(xmlFile)
131+
.content(PluginDescriptor.newBuilder().name("Sample Plugin").build())
132+
.build());
133+
assertTrue(Files.readString(xmlFile).contains("<name>Sample Plugin</name>"));
134+
}
135+
136+
@Test
137+
void shouldParseValidXmlString() {
138+
PluginDescriptor descriptor = sut.fromXmlString(SAMPLE_PLUGIN_XML);
139+
assertEquals("Sample Plugin", descriptor.getName());
140+
assertEquals("org.example", descriptor.getGroupId());
141+
assertEquals("sample-plugin", descriptor.getArtifactId());
142+
assertEquals("1.0.0", descriptor.getVersion());
143+
}
144+
145+
@Test
146+
void shouldGenerateValidXmlString() {
147+
String xml = sut.toXmlString(PluginDescriptor.newBuilder()
148+
.name("Sample Plugin")
149+
.groupId("org.example")
150+
.artifactId("sample-plugin")
151+
.version("1.0.0")
152+
.build());
153+
assertTrue(xml.contains("<name>Sample Plugin</name>"));
154+
assertTrue(xml.contains("<groupId>org.example</groupId>"));
155+
assertTrue(xml.contains("<artifactId>sample-plugin</artifactId>"));
156+
assertTrue(xml.contains("<version>1.0.0</version>"));
157+
}
158+
159+
@Test
160+
void shouldParseXmlUsingStaticMethod() {
161+
PluginDescriptor descriptor = DefaultPluginXmlFactory.fromXml(SAMPLE_PLUGIN_XML);
162+
assertEquals("Sample Plugin", descriptor.getName());
163+
assertEquals("org.example", descriptor.getGroupId());
164+
assertEquals("sample-plugin", descriptor.getArtifactId());
165+
assertEquals("1.0.0", descriptor.getVersion());
166+
}
167+
168+
@Test
169+
void shouldGenerateXmlUsingStaticMethod() {
170+
PluginDescriptor descriptor = PluginDescriptor.newBuilder()
171+
.name("Sample Plugin")
172+
.groupId("org.example")
173+
.artifactId("sample-plugin")
174+
.version("1.0.0")
175+
.build();
176+
String xml = DefaultPluginXmlFactory.toXml(descriptor);
177+
assertTrue(xml.contains("<name>Sample Plugin</name>"));
178+
assertTrue(xml.contains("<groupId>org.example</groupId>"));
179+
assertTrue(xml.contains("<artifactId>sample-plugin</artifactId>"));
180+
assertTrue(xml.contains("<version>1.0.0</version>"));
181+
}
182+
183+
@Test
184+
void shouldThrowXmlWriterExceptionWhenWriteFails() {
185+
XmlWriterException exception = assertThrows(
186+
XmlWriterException.class,
187+
() -> sut.write(XmlWriterRequest.<PluginDescriptor>builder()
188+
.writer(new Writer() {
189+
@Override
190+
public void write(char[] cbuf, int off, int len) {
191+
throw new RuntimeException("Simulated failure");
192+
}
193+
194+
@Override
195+
public void flush() {}
196+
197+
@Override
198+
public void close() {}
199+
})
200+
.content(PluginDescriptor.newBuilder()
201+
.name("Failing Plugin")
202+
.build())
203+
.build()));
204+
assertTrue(exception.getMessage().contains("Unable to write plugin"));
205+
assertInstanceOf(RuntimeException.class, exception.getCause());
206+
}
207+
208+
@Test
209+
void shouldThrowIllegalArgumentExceptionWhenNoWriteTargetProvided() {
210+
IllegalArgumentException exception = assertThrows(
211+
IllegalArgumentException.class,
212+
() -> sut.write(XmlWriterRequest.<PluginDescriptor>builder()
213+
.content(PluginDescriptor.newBuilder()
214+
.name("No Output Plugin")
215+
.build())
216+
.build()));
217+
218+
assertEquals("writer, outputStream or path must be non null", exception.getMessage());
219+
}
220+
221+
@Test
222+
void shouldThrowXmlReaderExceptionOnMalformedXml() {
223+
XmlReaderException exception = assertThrows(
224+
XmlReaderException.class,
225+
() -> sut.read(XmlReaderRequest.builder()
226+
.inputStream(new ByteArrayInputStream("<plugin><name>Broken Plugin".getBytes()))
227+
.build()));
228+
assertTrue(exception.getMessage().contains("Unable to read plugin"));
229+
assertInstanceOf(Exception.class, exception.getCause());
230+
}
231+
}

0 commit comments

Comments
 (0)