Skip to content

Commit 0022e03

Browse files
author
Vincent Potucek
committed
Test unused stream in DefaultPluginXmlFactory#write
1 parent f7e0024 commit 0022e03

File tree

4 files changed

+112
-167
lines changed

4 files changed

+112
-167
lines changed

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

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
*/
1919
package org.apache.maven.impl;
2020

21+
import java.io.InputStream;
22+
import java.io.OutputStream;
23+
import java.io.Reader;
24+
import java.io.Writer;
25+
import java.net.URL;
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
2129
import org.apache.maven.api.annotations.Nonnull;
2230
import org.apache.maven.api.di.Named;
2331
import org.apache.maven.api.di.Singleton;
@@ -27,18 +35,93 @@
2735
import org.apache.maven.api.services.xml.XmlReaderRequest;
2836
import org.apache.maven.api.services.xml.XmlWriterException;
2937
import org.apache.maven.api.services.xml.XmlWriterRequest;
38+
import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader;
39+
import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxWriter;
40+
41+
import static org.apache.maven.impl.ImplUtils.nonNull;
42+
import static org.apache.maven.impl.StaxLocation.getLocation;
43+
import static org.apache.maven.impl.StaxLocation.getMessage;
3044

3145
@Named
3246
@Singleton
3347
public class DefaultPluginXmlFactory implements PluginXmlFactory {
34-
3548
@Override
3649
public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
37-
return new ReadRequest(request).read();
50+
nonNull(request, "request");
51+
Path path = request.getPath();
52+
URL url = request.getURL();
53+
Reader reader = request.getReader();
54+
InputStream inputStream = request.getInputStream();
55+
if (path == null && url == null && reader == null && inputStream == null) {
56+
throw new IllegalArgumentException("path, url, reader or inputStream must be non null");
57+
}
58+
try {
59+
PluginDescriptorStaxReader xml = new PluginDescriptorStaxReader();
60+
xml.setAddDefaultEntities(request.isAddDefaultEntities());
61+
if (inputStream != null) {
62+
return xml.read(inputStream, request.isStrict());
63+
} else if (reader != null) {
64+
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+
}
73+
}
74+
} catch (Exception e) {
75+
throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e);
76+
}
3877
}
3978

4079
@Override
4180
public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException {
42-
new WriteRequest(request).write();
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();
86+
if (writer == null && outputStream == null && path == null) {
87+
throw new IllegalArgumentException("writer, outputStream or path must be non null");
88+
}
89+
try {
90+
if (writer != null) {
91+
new PluginDescriptorStaxWriter().write(writer, content);
92+
} else if (outputStream != null) {
93+
new PluginDescriptorStaxWriter().write(outputStream, content);
94+
} else {
95+
try (OutputStream os = Files.newOutputStream(path)) {
96+
new PluginDescriptorStaxWriter().write(os, content);
97+
}
98+
}
99+
} catch (Exception e) {
100+
throw new XmlWriterException("Unable to write plugin: " + getMessage(e), getLocation(e), e);
101+
}
102+
}
103+
104+
/**
105+
* Simply parse the given xml string.
106+
*
107+
* @param xml the input XML string
108+
* @return the parsed object
109+
* @throws XmlReaderException if an error occurs during the parsing
110+
* @see #toXmlString(Object)
111+
*/
112+
public static PluginDescriptor fromXml(@Nonnull String xml) throws XmlReaderException {
113+
return new DefaultPluginXmlFactory().fromXmlString(xml);
114+
}
115+
116+
/**
117+
* Simply converts the given content to an XML string.
118+
*
119+
* @param content the object to convert
120+
* @return the XML string representation
121+
* @throws XmlWriterException if an error occurs during the transformation
122+
* @see #fromXmlString(String)
123+
*/
124+
public static String toXml(@Nonnull PluginDescriptor content) throws XmlWriterException {
125+
return new DefaultPluginXmlFactory().toXmlString(content);
43126
}
44127
}

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

Lines changed: 0 additions & 76 deletions
This file was deleted.

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

Lines changed: 0 additions & 63 deletions
This file was deleted.

impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultPluginXmlFactoryTest.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -167,29 +167,29 @@ void toXmlStringGeneratesValidXml() {
167167
assertThat(xml).contains("<version>1.0.0</version>");
168168
}
169169

170-
// @Test
171-
// void staticFromXmlParsesValidXml() {
172-
// PluginDescriptor descriptor = DefaultPluginXmlFactory.fromXml(SAMPLE_PLUGIN_XML);
173-
// assertThat(descriptor.getName()).isEqualTo(NAME);
174-
// assertThat(descriptor.getGroupId()).isEqualTo("org.example");
175-
// assertThat(descriptor.getArtifactId()).isEqualTo("sample-plugin");
176-
// assertThat(descriptor.getVersion()).isEqualTo("1.0.0");
177-
// }
170+
@Test
171+
void staticFromXmlParsesValidXml() {
172+
PluginDescriptor descriptor = DefaultPluginXmlFactory.fromXml(SAMPLE_PLUGIN_XML);
173+
assertThat(descriptor.getName()).isEqualTo(NAME);
174+
assertThat(descriptor.getGroupId()).isEqualTo("org.example");
175+
assertThat(descriptor.getArtifactId()).isEqualTo("sample-plugin");
176+
assertThat(descriptor.getVersion()).isEqualTo("1.0.0");
177+
}
178178

179-
// @Test
180-
// void staticToXmlGeneratesValidXml() {
181-
// String xml = DefaultPluginXmlFactory.toXml(PluginDescriptor.newBuilder()
182-
// .name(NAME)
183-
// .groupId("org.example")
184-
// .artifactId("sample-plugin")
185-
// .version("1.0.0")
186-
// .build());
187-
// assertThat(xml).contains("<name>" + NAME + "</name>");
188-
// assertThat(xml).contains("<name>" + NAME + "</name>");
189-
// assertThat(xml).contains("<groupId>org.example</groupId>");
190-
// assertThat(xml).contains("<artifactId>sample-plugin</artifactId>");
191-
// assertThat(xml).contains("<version>1.0.0</version>");
192-
// }
179+
@Test
180+
void staticToXmlGeneratesValidXml() {
181+
String xml = DefaultPluginXmlFactory.toXml(PluginDescriptor.newBuilder()
182+
.name(NAME)
183+
.groupId("org.example")
184+
.artifactId("sample-plugin")
185+
.version("1.0.0")
186+
.build());
187+
assertThat(xml).contains("<name>" + NAME + "</name>");
188+
assertThat(xml).contains("<name>" + NAME + "</name>");
189+
assertThat(xml).contains("<groupId>org.example</groupId>");
190+
assertThat(xml).contains("<artifactId>sample-plugin</artifactId>");
191+
assertThat(xml).contains("<version>1.0.0</version>");
192+
}
193193

194194
@Test
195195
void writeWithFailingWriterThrowsXmlWriterException() {
@@ -215,8 +215,9 @@ public void close() {}
215215
.build()))
216216
.actual();
217217
assertThat(exception.getMessage()).contains(unableToWritePlugin);
218-
assertThat(exception.getCause()).isInstanceOf(IOException.class);
219-
assertThat(exception.getCause().getMessage()).isEqualTo(ioEx);
218+
assertThat(exception.getCause()).isInstanceOf(XmlWriterException.class);
219+
assertThat(exception.getCause().getCause().getMessage()).isEqualTo(ioEx);
220+
assertThat(exception.getCause().getMessage()).isEqualTo(unableToWritePlugin);
220221
}
221222

222223
@Test
@@ -229,7 +230,7 @@ void writeWithNoTargetThrowsIllegalArgumentException() {
229230
.build())
230231
.build()))
231232
.getMessage())
232-
.isEqualTo("writer, output stream, or path must be non null");
233+
.isEqualTo("writer, outputStream or path must be non null");
233234
}
234235

235236
@Test

0 commit comments

Comments
 (0)