Skip to content

Commit 0b37d3b

Browse files
author
Vincent Potucek
committed
Test unused stream in DefaultPluginXmlFactory#write
1 parent d777133 commit 0b37d3b

File tree

3 files changed

+315
-10
lines changed

3 files changed

+315
-10
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
121121
Writer writer = request.getWriter();
122122
Function<Object, String> inputLocationFormatter = request.getInputLocationFormatter();
123123
if (writer == null && outputStream == null && path == null) {
124-
throw new IllegalArgumentException("writer, outputStream or path must be non null");
124+
throw new IllegalArgumentException("writer, output stream, or path must be non null");
125125
}
126126
try {
127127
MavenStaxWriter w = new MavenStaxWriter();

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

+25-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.apache.maven.impl;
2020

21+
import javax.xml.stream.XMLStreamException;
22+
2123
import java.io.InputStream;
2224
import java.io.OutputStream;
2325
import java.io.Reader;
@@ -45,37 +47,47 @@
4547
@Named
4648
@Singleton
4749
public class DefaultPluginXmlFactory implements PluginXmlFactory {
50+
4851
@Override
4952
public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
5053
nonNull(request, "request");
5154
Path path = request.getPath();
5255
URL url = request.getURL();
5356
Reader reader = request.getReader();
5457
InputStream inputStream = request.getInputStream();
55-
if (path == null && url == null && reader == null && inputStream == null) {
58+
if (inputStream == null && reader == null && path == null && url == null) {
5659
throw new IllegalArgumentException("path, url, reader or inputStream must be non null");
5760
}
61+
return read(request, inputStream, reader, path, url);
62+
}
63+
64+
private static PluginDescriptor read(
65+
XmlReaderRequest request, InputStream inputStream, Reader reader, Path path, URL url) {
5866
try {
5967
PluginDescriptorStaxReader xml = new PluginDescriptorStaxReader();
6068
xml.setAddDefaultEntities(request.isAddDefaultEntities());
6169
if (inputStream != null) {
62-
return xml.read(inputStream, request.isStrict());
70+
return read(request, xml, inputStream);
6371
} else if (reader != null) {
6472
return xml.read(reader, request.isStrict());
6573
} else if (path != null) {
6674
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());
75+
return read(request, xml, is);
7276
}
7377
}
78+
try (InputStream is = url.openStream()) {
79+
return read(request, xml, is);
80+
}
7481
} catch (Exception e) {
7582
throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e);
7683
}
7784
}
7885

86+
private static PluginDescriptor read(XmlReaderRequest request, PluginDescriptorStaxReader xml, InputStream is)
87+
throws XMLStreamException {
88+
return xml.read(is, request.isStrict());
89+
}
90+
7991
@Override
8092
public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException {
8193
nonNull(request, "request");
@@ -84,16 +96,20 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
8496
OutputStream outputStream = request.getOutputStream();
8597
Writer writer = request.getWriter();
8698
if (writer == null && outputStream == null && path == null) {
87-
throw new IllegalArgumentException("writer, outputStream or path must be non null");
99+
throw new IllegalArgumentException("writer, output stream, or path must be non null");
88100
}
101+
write(writer, content, outputStream, path);
102+
}
103+
104+
private static void write(Writer writer, PluginDescriptor content, OutputStream outputStream, Path path) {
89105
try {
90106
if (writer != null) {
91107
new PluginDescriptorStaxWriter().write(writer, content);
92108
} else if (outputStream != null) {
93109
new PluginDescriptorStaxWriter().write(outputStream, content);
94110
} else {
95111
try (OutputStream os = Files.newOutputStream(path)) {
96-
new PluginDescriptorStaxWriter().write(outputStream, content);
112+
new PluginDescriptorStaxWriter().write(os, content);
97113
}
98114
}
99115
} catch (Exception e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
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 com.ctc.wstx.exc.WstxEOFException;
32+
import org.apache.maven.api.plugin.descriptor.PluginDescriptor;
33+
import org.apache.maven.api.services.xml.ModelXmlFactory;
34+
import org.apache.maven.api.services.xml.XmlReaderException;
35+
import org.apache.maven.api.services.xml.XmlReaderRequest;
36+
import org.apache.maven.api.services.xml.XmlWriterException;
37+
import org.apache.maven.api.services.xml.XmlWriterRequest;
38+
import org.apache.maven.impl.model.DefaultModelProcessor;
39+
import org.junit.jupiter.api.Disabled;
40+
import org.junit.jupiter.api.Test;
41+
import org.junit.jupiter.api.io.TempDir;
42+
43+
import static org.assertj.core.api.Assertions.assertThat;
44+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
45+
import static org.junit.jupiter.api.Assertions.assertNotNull;
46+
import static org.junit.jupiter.api.Assertions.assertThrows;
47+
import static org.mockito.Mockito.mock;
48+
import static org.mockito.Mockito.when;
49+
50+
class DefaultPluginXmlFactoryReadWriteTest {
51+
52+
private static final String SAMPLE_PLUGIN_XML =
53+
"""
54+
<?xml version="1.0" encoding="UTF-8"?>
55+
<plugin>
56+
<name>Sample Plugin</name>
57+
<groupId>org.example</groupId>
58+
<artifactId>sample-plugin</artifactId>
59+
<version>1.0.0</version>
60+
</plugin>
61+
""";
62+
63+
private final DefaultPluginXmlFactory defaultPluginXmlFactory = new DefaultPluginXmlFactory();
64+
65+
@TempDir
66+
Path tempDir;
67+
68+
@Test
69+
void readFromInputStreamParsesPluginDescriptorCorrectly() {
70+
PluginDescriptor descriptor = defaultPluginXmlFactory.read(XmlReaderRequest.builder()
71+
.inputStream(new ByteArrayInputStream(SAMPLE_PLUGIN_XML.getBytes()))
72+
.build());
73+
assertThat(descriptor.getName()).isEqualTo("Sample Plugin");
74+
assertThat(descriptor.getGroupId()).isEqualTo("org.example");
75+
assertThat(descriptor.getArtifactId()).isEqualTo("sample-plugin");
76+
assertThat(descriptor.getVersion()).isEqualTo("1.0.0");
77+
}
78+
79+
@Test
80+
void parsePlugin() {
81+
assertThat(defaultPluginXmlFactory
82+
.read(XmlReaderRequest.builder()
83+
.reader(new StringReader(SAMPLE_PLUGIN_XML))
84+
.build())
85+
.getName())
86+
.isEqualTo("Sample Plugin");
87+
}
88+
89+
@Test
90+
@Disabled
91+
void readFromPathParsesPluginDescriptorCorrectly() throws Exception {
92+
Path xmlFile = tempDir.resolve("plugin.xml");
93+
Files.write(xmlFile, SAMPLE_PLUGIN_XML.getBytes());
94+
assertThat(defaultPluginXmlFactory
95+
.read(XmlReaderRequest.builder().path(xmlFile).build())
96+
.getName())
97+
.isEqualTo("Sample Plugin");
98+
}
99+
100+
@Test
101+
void readWithNoInputThrowsIllegalArgumentException() {
102+
assertThatExceptionOfType(IllegalArgumentException.class)
103+
.isThrownBy(() ->
104+
defaultPluginXmlFactory.read(XmlReaderRequest.builder().build()));
105+
}
106+
107+
@Test
108+
void writeToWriterGeneratesValidXml() {
109+
StringWriter writer = new StringWriter();
110+
defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder()
111+
.writer(writer)
112+
.content(PluginDescriptor.newBuilder()
113+
.name("Sample Plugin")
114+
.groupId("org.example")
115+
.artifactId("sample-plugin")
116+
.version("1.0.0")
117+
.build())
118+
.build());
119+
String output = writer.toString();
120+
assertThat(output).contains("<name>Sample Plugin</name>");
121+
assertThat(output).contains("<groupId>org.example</groupId>");
122+
}
123+
124+
@Test
125+
void writeToOutputStreamGeneratesValidXml() {
126+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
127+
defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder()
128+
.outputStream(outputStream)
129+
.content(PluginDescriptor.newBuilder().name("Sample Plugin").build())
130+
.build());
131+
assertThat(outputStream.toString()).contains("<name>Sample Plugin</name>");
132+
}
133+
134+
@Test
135+
void writeToPathGeneratesValidXmlFile() throws Exception {
136+
Path xmlFile = tempDir.resolve("output-plugin.xml");
137+
defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder()
138+
.path(xmlFile)
139+
.content(PluginDescriptor.newBuilder().name("Sample Plugin").build())
140+
.build());
141+
assertThat(Files.readString(xmlFile)).contains("<name>Sample Plugin</name>");
142+
}
143+
144+
@Test
145+
void fromXmlStringParsesValidXml() {
146+
PluginDescriptor descriptor = defaultPluginXmlFactory.fromXmlString(SAMPLE_PLUGIN_XML);
147+
assertThat(descriptor.getName()).isEqualTo("Sample Plugin");
148+
assertThat(descriptor.getGroupId()).isEqualTo("org.example");
149+
assertThat(descriptor.getArtifactId()).isEqualTo("sample-plugin");
150+
assertThat(descriptor.getVersion()).isEqualTo("1.0.0");
151+
}
152+
153+
@Test
154+
void toXmlStringGeneratesValidXml() {
155+
String xml = defaultPluginXmlFactory.toXmlString(PluginDescriptor.newBuilder()
156+
.name("Sample Plugin")
157+
.groupId("org.example")
158+
.artifactId("sample-plugin")
159+
.version("1.0.0")
160+
.build());
161+
assertThat(xml).contains("<name>Sample Plugin</name>");
162+
assertThat(xml).contains("<groupId>org.example</groupId>");
163+
assertThat(xml).contains("<artifactId>sample-plugin</artifactId>");
164+
assertThat(xml).contains("<version>1.0.0</version>");
165+
}
166+
167+
@Test
168+
void staticFromXmlParsesValidXml() {
169+
PluginDescriptor descriptor = DefaultPluginXmlFactory.fromXml(SAMPLE_PLUGIN_XML);
170+
assertThat(descriptor.getName()).isEqualTo("Sample Plugin");
171+
assertThat(descriptor.getGroupId()).isEqualTo("org.example");
172+
assertThat(descriptor.getArtifactId()).isEqualTo("sample-plugin");
173+
assertThat(descriptor.getVersion()).isEqualTo("1.0.0");
174+
}
175+
176+
@Test
177+
void staticToXmlGeneratesValidXml() {
178+
String xml = DefaultPluginXmlFactory.toXml(PluginDescriptor.newBuilder()
179+
.name("Sample Plugin")
180+
.groupId("org.example")
181+
.artifactId("sample-plugin")
182+
.version("1.0.0")
183+
.build());
184+
assertThat(xml).contains("<name>Sample Plugin</name>");
185+
assertThat(xml).contains("<name>Sample Plugin</name>");
186+
assertThat(xml).contains("<groupId>org.example</groupId>");
187+
assertThat(xml).contains("<artifactId>sample-plugin</artifactId>");
188+
assertThat(xml).contains("<version>1.0.0</version>");
189+
}
190+
191+
@Test
192+
void writeWithFailingWriterThrowsXmlWriterException() {
193+
XmlWriterException exception = assertThatExceptionOfType(XmlWriterException.class)
194+
.isThrownBy(() -> defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder()
195+
.writer(new Writer() {
196+
@Override
197+
public void write(char[] cbuf, int off, int len) {
198+
throw new RuntimeException("Simulated failure");
199+
}
200+
201+
@Override
202+
public void flush() {}
203+
204+
@Override
205+
public void close() {}
206+
})
207+
.content(PluginDescriptor.newBuilder()
208+
.name("Failing Plugin")
209+
.build())
210+
.build()))
211+
.actual();
212+
assertThat(exception.getMessage()).contains("Unable to write plugin");
213+
assertThat(exception.getCause()).isInstanceOf(RuntimeException.class);
214+
}
215+
216+
@Test
217+
void writeWithNoTargetThrowsIllegalArgumentException() {
218+
assertThat(assertThrows(
219+
IllegalArgumentException.class,
220+
() -> defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder()
221+
.content(PluginDescriptor.newBuilder()
222+
.name("No Output Plugin")
223+
.build())
224+
.build()))
225+
.getMessage())
226+
.isEqualTo("writer, output stream, or path must be non null");
227+
}
228+
229+
@Test
230+
void readMalformedXmlThrowsXmlReaderException() {
231+
XmlReaderException exception = assertThatExceptionOfType(XmlReaderException.class)
232+
.isThrownBy(() -> defaultPluginXmlFactory.read(XmlReaderRequest.builder()
233+
.inputStream(new ByteArrayInputStream("<plugin><name>Broken Plugin".getBytes()))
234+
.build()))
235+
.actual();
236+
assertThat(exception.getMessage()).contains("Unable to read plugin");
237+
assertThat(exception.getCause()).isInstanceOf(WstxEOFException.class);
238+
}
239+
240+
@Test
241+
void locateExistingPomWithFilePathShouldReturnSameFileIfRegularFile() throws IOException {
242+
Path pomFile = Files.createTempFile(tempDir, "pom", ".xml");
243+
DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of());
244+
assertThat(processor.locateExistingPom(pomFile)).isEqualTo(pomFile);
245+
}
246+
247+
@Test
248+
void readFromUrlParsesPluginDescriptorCorrectly() throws Exception {
249+
Path xmlFile = tempDir.resolve("plugin.xml");
250+
Files.write(xmlFile, SAMPLE_PLUGIN_XML.getBytes());
251+
PluginDescriptor descriptor = defaultPluginXmlFactory.read(XmlReaderRequest.builder()
252+
.inputStream(xmlFile.toUri().toURL().openStream())
253+
.build());
254+
assertThat(descriptor.getName()).isEqualTo("Sample Plugin");
255+
assertThat(descriptor.getGroupId()).isEqualTo("org.example");
256+
assertThat(descriptor.getArtifactId()).isEqualTo("sample-plugin");
257+
assertThat(descriptor.getVersion()).isEqualTo("1.0.0");
258+
}
259+
260+
@Test
261+
void testReadWithPath() throws Exception {
262+
Path tempPath = Files.createTempFile("plugin", ".xml");
263+
Files.writeString(tempPath, "<plugin/>");
264+
XmlReaderRequest request = mock(XmlReaderRequest.class);
265+
when(request.getPath()).thenReturn(tempPath);
266+
when(request.getInputStream()).thenReturn(null);
267+
when(request.getReader()).thenReturn(null);
268+
when(request.getURL()).thenReturn(null);
269+
when(request.isAddDefaultEntities()).thenReturn(false);
270+
when(request.isStrict()).thenReturn(false);
271+
assertNotNull(new DefaultPluginXmlFactory().read(request));
272+
Files.deleteIfExists(tempPath);
273+
}
274+
275+
@Test
276+
void testReadWithPathUrlDefault() throws Exception {
277+
Path tempPath = Files.createTempFile("plugin", ".xml");
278+
Files.writeString(tempPath, "<plugin/>");
279+
XmlReaderRequest request = mock(XmlReaderRequest.class);
280+
when(request.getPath()).thenReturn(null);
281+
when(request.getInputStream()).thenReturn(null);
282+
when(request.getReader()).thenReturn(null);
283+
when(request.getURL()).thenReturn(tempPath.toUri().toURL());
284+
when(request.isAddDefaultEntities()).thenReturn(false);
285+
when(request.isStrict()).thenReturn(false);
286+
assertNotNull(new DefaultPluginXmlFactory().read(request));
287+
Files.deleteIfExists(tempPath);
288+
}
289+
}

0 commit comments

Comments
 (0)