Skip to content

Commit f7e0024

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

File tree

4 files changed

+437
-86
lines changed

4 files changed

+437
-86
lines changed

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

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
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-
2921
import org.apache.maven.api.annotations.Nonnull;
3022
import org.apache.maven.api.di.Named;
3123
import org.apache.maven.api.di.Singleton;
@@ -35,93 +27,18 @@
3527
import org.apache.maven.api.services.xml.XmlReaderRequest;
3628
import org.apache.maven.api.services.xml.XmlWriterException;
3729
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;
4430

4531
@Named
4632
@Singleton
4733
public class DefaultPluginXmlFactory implements PluginXmlFactory {
34+
4835
@Override
4936
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();
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-
}
37+
return new ReadRequest(request).read();
7738
}
7839

7940
@Override
8041
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();
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(outputStream, 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);
42+
new WriteRequest(request).write();
12643
}
12744
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 javax.xml.stream.XMLStreamException;
22+
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.io.Reader;
26+
import java.net.URL;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
29+
30+
import org.apache.maven.api.plugin.descriptor.PluginDescriptor;
31+
import org.apache.maven.api.services.xml.XmlReaderException;
32+
import org.apache.maven.api.services.xml.XmlReaderRequest;
33+
import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader;
34+
35+
import static org.apache.maven.impl.StaxLocation.getLocation;
36+
import static org.apache.maven.impl.StaxLocation.getMessage;
37+
38+
record ReadRequest(
39+
Path path, URL url, Reader reader, InputStream inputStream, boolean addDefaultEntities, boolean strict) {
40+
41+
ReadRequest(XmlReaderRequest request) {
42+
this(
43+
request.getPath(),
44+
request.getURL(),
45+
request.getReader(),
46+
request.getInputStream(),
47+
request.isAddDefaultEntities(),
48+
request.isStrict());
49+
50+
if (inputStream == null && reader == null && path == null && url == null) {
51+
throw new IllegalArgumentException("At least one of path, url, reader or inputStream must be non-null");
52+
}
53+
}
54+
55+
PluginDescriptor read() throws XmlReaderException {
56+
try {
57+
PluginDescriptorStaxReader xml = new PluginDescriptorStaxReader();
58+
xml.setAddDefaultEntities(addDefaultEntities);
59+
60+
if (inputStream != null) {
61+
return xml.read(inputStream, strict);
62+
} else if (reader != null) {
63+
return xml.read(reader, strict);
64+
} else if (path != null) {
65+
try (InputStream is = Files.newInputStream(path)) {
66+
return xml.read(is, strict);
67+
}
68+
}
69+
try (InputStream is = url.openStream()) {
70+
return xml.read(is, strict);
71+
}
72+
} catch (IOException | XMLStreamException e) {
73+
throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e);
74+
}
75+
}
76+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 javax.xml.stream.XMLStreamException;
22+
23+
import java.io.IOException;
24+
import java.io.OutputStream;
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.XmlWriterException;
31+
import org.apache.maven.api.services.xml.XmlWriterRequest;
32+
import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxWriter;
33+
34+
import static org.apache.maven.impl.StaxLocation.getLocation;
35+
import static org.apache.maven.impl.StaxLocation.getMessage;
36+
37+
record WriteRequest(Path path, OutputStream outputStream, Writer writer, PluginDescriptor content) {
38+
39+
WriteRequest(XmlWriterRequest<PluginDescriptor> request) {
40+
this(request.getPath(), request.getOutputStream(), request.getWriter(), request.getContent());
41+
42+
if (writer == null && outputStream == null && path == null) {
43+
throw new IllegalArgumentException("writer, outputStream or path must be non null");
44+
}
45+
}
46+
47+
void write() throws XmlWriterException {
48+
try {
49+
PluginDescriptorStaxWriter writer = new PluginDescriptorStaxWriter();
50+
if (this.writer != null) {
51+
writer.write(this.writer, content);
52+
} else if (outputStream != null) {
53+
writer.write(outputStream, content);
54+
} else {
55+
try (OutputStream os = Files.newOutputStream(path)) {
56+
writer.write(os, content);
57+
}
58+
}
59+
} catch (IOException | XMLStreamException e) {
60+
throw new XmlWriterException("Unable to write plugin: " + getMessage(e), getLocation(e), e);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)