Skip to content

Commit 00c0d7d

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

File tree

3 files changed

+326
-15
lines changed

3 files changed

+326
-15
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

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

21+
import javax.xml.stream.XMLStreamException;
22+
23+
import java.io.IOException;
2124
import java.io.InputStream;
2225
import java.io.OutputStream;
2326
import java.io.Reader;
@@ -45,58 +48,71 @@
4548
@Named
4649
@Singleton
4750
public class DefaultPluginXmlFactory implements PluginXmlFactory {
51+
4852
@Override
4953
public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
5054
nonNull(request, "request");
5155
Path path = request.getPath();
5256
URL url = request.getURL();
5357
Reader reader = request.getReader();
5458
InputStream inputStream = request.getInputStream();
55-
if (path == null && url == null && reader == null && inputStream == null) {
59+
if (inputStream == null && reader == null && path == null && url == null) {
5660
throw new IllegalArgumentException("path, url, reader or inputStream must be non null");
5761
}
62+
return read(request.isAddDefaultEntities(), request.isStrict(), inputStream, reader, path, url);
63+
}
64+
65+
private static PluginDescriptor read(
66+
boolean addDefaultEntities, boolean strict, InputStream inputStream, Reader reader, Path path, URL url) {
5867
try {
5968
PluginDescriptorStaxReader xml = new PluginDescriptorStaxReader();
60-
xml.setAddDefaultEntities(request.isAddDefaultEntities());
69+
xml.setAddDefaultEntities(addDefaultEntities);
6170
if (inputStream != null) {
62-
return xml.read(inputStream, request.isStrict());
71+
return read(xml, inputStream, strict);
6372
} else if (reader != null) {
64-
return xml.read(reader, request.isStrict());
73+
return xml.read(reader, strict);
6574
} else if (path != null) {
6675
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());
76+
return read(xml, is, strict);
7277
}
7378
}
74-
} catch (Exception e) {
79+
try (InputStream is = url.openStream()) {
80+
return read(xml, is, strict);
81+
}
82+
} catch (IOException | XMLStreamException e) {
7583
throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e);
7684
}
7785
}
7886

87+
private static PluginDescriptor read(PluginDescriptorStaxReader xml, InputStream is, boolean strict)
88+
throws XMLStreamException {
89+
return xml.read(is, strict);
90+
}
91+
7992
@Override
8093
public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException {
8194
nonNull(request, "request");
82-
PluginDescriptor content = nonNull(request.getContent(), "content");
8395
Path path = request.getPath();
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, request.getContent(), 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
}
99-
} catch (Exception e) {
115+
} catch (IOException | XMLStreamException e) {
100116
throw new XmlWriterException("Unable to write plugin: " + getMessage(e), getLocation(e), e);
101117
}
102118
}

0 commit comments

Comments
 (0)