Skip to content

Commit e250003

Browse files
author
Vincent Potucek
committed
Pull #2303: Fix unused stream os in DefaultPluginXmlFactory#write
1 parent 6be7a12 commit e250003

File tree

2 files changed

+255
-1
lines changed

2 files changed

+255
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
9393
new PluginDescriptorStaxWriter().write(outputStream, content);
9494
} else {
9595
try (OutputStream os = Files.newOutputStream(path)) {
96-
new PluginDescriptorStaxWriter().write(outputStream, content);
96+
new PluginDescriptorStaxWriter().write(os, content);
9797
}
9898
}
9999
} catch (Exception e) {
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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.IOException;
24+
import java.io.StringReader;
25+
import java.io.StringWriter;
26+
import java.io.Writer;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
29+
import java.util.List;
30+
31+
import org.apache.maven.api.plugin.descriptor.PluginDescriptor;
32+
import org.apache.maven.api.services.xml.ModelXmlFactory;
33+
import org.apache.maven.api.services.xml.XmlReaderException;
34+
import org.apache.maven.api.services.xml.XmlReaderRequest;
35+
import org.apache.maven.api.services.xml.XmlWriterException;
36+
import org.apache.maven.api.services.xml.XmlWriterRequest;
37+
import org.apache.maven.impl.model.DefaultModelProcessor;
38+
import org.junit.jupiter.api.Test;
39+
import org.junit.jupiter.api.io.TempDir;
40+
41+
import static org.junit.jupiter.api.Assertions.assertEquals;
42+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
43+
import static org.junit.jupiter.api.Assertions.assertThrows;
44+
import static org.junit.jupiter.api.Assertions.assertTrue;
45+
import static org.mockito.Mockito.mock;
46+
47+
class DefaultPluginXmlFactoryReadWriteTest {
48+
49+
private static final String SAMPLE_PLUGIN_XML =
50+
"""
51+
<?xml version="1.0" encoding="UTF-8"?>
52+
<plugin>
53+
<name>Sample Plugin</name>
54+
<groupId>org.example</groupId>
55+
<artifactId>sample-plugin</artifactId>
56+
<version>1.0.0</version>
57+
</plugin>
58+
""";
59+
60+
private final DefaultPluginXmlFactory sut = new DefaultPluginXmlFactory();
61+
62+
@TempDir
63+
Path tempDir;
64+
65+
@Test
66+
void readFromInputStreamParsesPluginDescriptorCorrectly() {
67+
PluginDescriptor descriptor = sut.read(XmlReaderRequest.builder()
68+
.inputStream(new ByteArrayInputStream(SAMPLE_PLUGIN_XML.getBytes()))
69+
.build());
70+
assertEquals("Sample Plugin", descriptor.getName());
71+
assertEquals("org.example", descriptor.getGroupId());
72+
assertEquals("sample-plugin", descriptor.getArtifactId());
73+
assertEquals("1.0.0", descriptor.getVersion());
74+
}
75+
76+
@Test
77+
void readFromReaderParsesPluginDescriptorCorrectly() {
78+
assertEquals(
79+
"Sample Plugin",
80+
sut.read(XmlReaderRequest.builder()
81+
.reader(new StringReader(SAMPLE_PLUGIN_XML))
82+
.build())
83+
.getName());
84+
}
85+
86+
@Test
87+
void readFromPathParsesPluginDescriptorCorrectly() throws Exception {
88+
Path xmlFile = tempDir.resolve("plugin.xml");
89+
Files.write(xmlFile, SAMPLE_PLUGIN_XML.getBytes());
90+
assertEquals(
91+
"Sample Plugin",
92+
sut.read(XmlReaderRequest.builder().path(xmlFile).build()).getName());
93+
}
94+
95+
@Test
96+
void readWithNoInputThrowsIllegalArgumentException() {
97+
assertThrows(
98+
IllegalArgumentException.class,
99+
() -> sut.read(XmlReaderRequest.builder().build()));
100+
}
101+
102+
@Test
103+
void writeToWriterGeneratesValidXml() {
104+
StringWriter writer = new StringWriter();
105+
sut.write(XmlWriterRequest.<PluginDescriptor>builder()
106+
.writer(writer)
107+
.content(PluginDescriptor.newBuilder()
108+
.name("Sample Plugin")
109+
.groupId("org.example")
110+
.artifactId("sample-plugin")
111+
.version("1.0.0")
112+
.build())
113+
.build());
114+
String output = writer.toString();
115+
assertTrue(output.contains("<name>Sample Plugin</name>"));
116+
assertTrue(output.contains("<groupId>org.example</groupId>"));
117+
}
118+
119+
@Test
120+
void writeToOutputStreamGeneratesValidXml() {
121+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
122+
sut.write(XmlWriterRequest.<PluginDescriptor>builder()
123+
.outputStream(outputStream)
124+
.content(PluginDescriptor.newBuilder().name("Sample Plugin").build())
125+
.build());
126+
assertTrue(outputStream.toString().contains("<name>Sample Plugin</name>"));
127+
}
128+
129+
@Test
130+
void writeToPathGeneratesValidXmlFile() throws Exception {
131+
Path xmlFile = tempDir.resolve("output-plugin.xml");
132+
sut.write(XmlWriterRequest.<PluginDescriptor>builder()
133+
.path(xmlFile)
134+
.content(PluginDescriptor.newBuilder().name("Sample Plugin").build())
135+
.build());
136+
assertTrue(Files.readString(xmlFile).contains("<name>Sample Plugin</name>"));
137+
}
138+
139+
@Test
140+
void fromXmlStringParsesValidXml() {
141+
PluginDescriptor descriptor = sut.fromXmlString(SAMPLE_PLUGIN_XML);
142+
assertEquals("Sample Plugin", descriptor.getName());
143+
assertEquals("org.example", descriptor.getGroupId());
144+
assertEquals("sample-plugin", descriptor.getArtifactId());
145+
assertEquals("1.0.0", descriptor.getVersion());
146+
}
147+
148+
@Test
149+
void toXmlStringGeneratesValidXml() {
150+
String xml = sut.toXmlString(PluginDescriptor.newBuilder()
151+
.name("Sample Plugin")
152+
.groupId("org.example")
153+
.artifactId("sample-plugin")
154+
.version("1.0.0")
155+
.build());
156+
assertTrue(xml.contains("<name>Sample Plugin</name>"));
157+
assertTrue(xml.contains("<groupId>org.example</groupId>"));
158+
assertTrue(xml.contains("<artifactId>sample-plugin</artifactId>"));
159+
assertTrue(xml.contains("<version>1.0.0</version>"));
160+
}
161+
162+
@Test
163+
void staticFromXmlParsesValidXml() {
164+
PluginDescriptor descriptor = DefaultPluginXmlFactory.fromXml(SAMPLE_PLUGIN_XML);
165+
assertEquals("Sample Plugin", descriptor.getName());
166+
assertEquals("org.example", descriptor.getGroupId());
167+
assertEquals("sample-plugin", descriptor.getArtifactId());
168+
assertEquals("1.0.0", descriptor.getVersion());
169+
}
170+
171+
@Test
172+
void staticToXmlGeneratesValidXml() {
173+
String xml = DefaultPluginXmlFactory.toXml(PluginDescriptor.newBuilder()
174+
.name("Sample Plugin")
175+
.groupId("org.example")
176+
.artifactId("sample-plugin")
177+
.version("1.0.0")
178+
.build());
179+
assertTrue(xml.contains("<name>Sample Plugin</name>"));
180+
assertTrue(xml.contains("<groupId>org.example</groupId>"));
181+
assertTrue(xml.contains("<artifactId>sample-plugin</artifactId>"));
182+
assertTrue(xml.contains("<version>1.0.0</version>"));
183+
}
184+
185+
@Test
186+
void writeWithFailingWriterThrowsXmlWriterException() {
187+
XmlWriterException exception = assertThrows(
188+
XmlWriterException.class,
189+
() -> sut.write(XmlWriterRequest.<PluginDescriptor>builder()
190+
.writer(new Writer() {
191+
@Override
192+
public void write(char[] cbuf, int off, int len) {
193+
throw new RuntimeException("Simulated failure");
194+
}
195+
196+
@Override
197+
public void flush() {}
198+
199+
@Override
200+
public void close() {}
201+
})
202+
.content(PluginDescriptor.newBuilder()
203+
.name("Failing Plugin")
204+
.build())
205+
.build()));
206+
assertTrue(exception.getMessage().contains("Unable to write plugin"));
207+
assertInstanceOf(RuntimeException.class, exception.getCause());
208+
}
209+
210+
@Test
211+
void writeWithNoTargetThrowsIllegalArgumentException() {
212+
assertEquals(
213+
"writer, outputStream or path must be non null",
214+
assertThrows(
215+
IllegalArgumentException.class,
216+
() -> sut.write(XmlWriterRequest.<PluginDescriptor>builder()
217+
.content(PluginDescriptor.newBuilder()
218+
.name("No Output Plugin")
219+
.build())
220+
.build()))
221+
.getMessage());
222+
}
223+
224+
@Test
225+
void readMalformedXmlThrowsXmlReaderException() {
226+
XmlReaderException exception = assertThrows(
227+
XmlReaderException.class,
228+
() -> sut.read(XmlReaderRequest.builder()
229+
.inputStream(new ByteArrayInputStream("<plugin><name>Broken Plugin".getBytes()))
230+
.build()));
231+
assertTrue(exception.getMessage().contains("Unable to read plugin"));
232+
assertInstanceOf(Exception.class, exception.getCause());
233+
}
234+
235+
@Test
236+
void locateExistingPomWithFilePathShouldReturnSameFileIfRegularFile() throws IOException {
237+
Path pomFile = Files.createTempFile(tempDir, "pom", ".xml");
238+
DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of());
239+
assertEquals(pomFile, processor.locateExistingPom(pomFile));
240+
}
241+
242+
@Test
243+
void readFromUrlParsesPluginDescriptorCorrectly() throws Exception {
244+
Path xmlFile = tempDir.resolve("plugin.xml");
245+
Files.write(xmlFile, SAMPLE_PLUGIN_XML.getBytes());
246+
PluginDescriptor descriptor = sut.read(XmlReaderRequest.builder()
247+
.inputStream(xmlFile.toUri().toURL().openStream())
248+
.build());
249+
assertEquals("Sample Plugin", descriptor.getName());
250+
assertEquals("org.example", descriptor.getGroupId());
251+
assertEquals("sample-plugin", descriptor.getArtifactId());
252+
assertEquals("1.0.0", descriptor.getVersion());
253+
}
254+
}

0 commit comments

Comments
 (0)