Skip to content

Commit 3d0d18b

Browse files
author
Vincent Potucek
committed
Give SPOT to DefaultPluginXmlFactory cure feature envy leveraging OOP
1 parent 3e9c164 commit 3d0d18b

File tree

5 files changed

+68
-62
lines changed

5 files changed

+68
-62
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/services/xml/XmlReaderRequest.java

+6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ interface Transformer {
7878
String transform(String source, String fieldName);
7979
}
8080

81+
default void assertReadable() {
82+
if (getInputStream() == null && getReader() == null && getPath() == null && getURL() == null) {
83+
throw new IllegalArgumentException("writer, outputStream or path must be non null");
84+
}
85+
}
86+
8187
@Nonnull
8288
static XmlReaderRequestBuilder builder() {
8389
return new XmlReaderRequestBuilder();

api/maven-api-core/src/main/java/org/apache/maven/api/services/xml/XmlWriterRequest.java

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ static <T> XmlWriterRequestBuilder<T> builder() {
5555
return new XmlWriterRequestBuilder<>();
5656
}
5757

58+
default void assertWritable() {
59+
if (getWriter() == null && getOutputStream() == null && getPath() == null) {
60+
throw new IllegalArgumentException("writer, outputStream or path must be non null");
61+
}
62+
}
63+
5864
class XmlWriterRequestBuilder<T> {
5965
Path path;
6066
OutputStream outputStream;

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
120120
OutputStream outputStream = request.getOutputStream();
121121
Writer writer = request.getWriter();
122122
Function<Object, String> inputLocationFormatter = request.getInputLocationFormatter();
123-
if (writer == null && outputStream == null && path == null) {
124-
throw new IllegalArgumentException("writer, outputStream or path must be non null");
125-
}
123+
request.assertWritable();
126124
try {
127125
MavenStaxWriter w = new MavenStaxWriter();
128126
if (inputLocationFormatter != null) {

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

+39-44
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
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;
21+
import javax.xml.stream.XMLStreamException;
22+
23+
import java.io.IOException;
2624
import java.nio.file.Files;
27-
import java.nio.file.Path;
2825

2926
import org.apache.maven.api.annotations.Nonnull;
3027
import org.apache.maven.api.di.Named;
@@ -38,65 +35,63 @@
3835
import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader;
3936
import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxWriter;
4037

41-
import static org.apache.maven.impl.ImplUtils.nonNull;
4238
import static org.apache.maven.impl.StaxLocation.getLocation;
4339
import static org.apache.maven.impl.StaxLocation.getMessage;
4440

4541
@Named
4642
@Singleton
4743
public class DefaultPluginXmlFactory implements PluginXmlFactory {
44+
4845
@Override
4946
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-
}
47+
return read(request, setAddDefaultEntities(request, new PluginDescriptorStaxReader()));
48+
}
49+
50+
@Override
51+
public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException {
52+
write(request, new PluginDescriptorStaxWriter());
53+
}
54+
55+
private static PluginDescriptor read(XmlReaderRequest request, PluginDescriptorStaxReader reader) {
56+
request.assertReadable();
5857
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());
58+
if (request.getInputStream() != null) {
59+
return reader.read(request.getInputStream(), request.isStrict());
60+
} else if (request.getReader() != null) {
61+
return reader.read(request.getReader(), request.isStrict());
62+
} else if (request.getPath() != null) {
63+
try (var is = Files.newInputStream(request.getPath())) {
64+
return reader.read(is, request.isStrict());
6865
}
6966
} else {
70-
try (InputStream is = url.openStream()) {
71-
return xml.read(is, request.isStrict());
67+
try (var is = request.getURL().openStream()) {
68+
return reader.read(is, request.isStrict());
7269
}
7370
}
74-
} catch (Exception e) {
71+
} catch (IOException | XMLStreamException e) {
7572
throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e);
7673
}
7774
}
7875

79-
@Override
80-
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-
}
76+
private static PluginDescriptorStaxReader setAddDefaultEntities(
77+
XmlReaderRequest request, PluginDescriptorStaxReader reader) {
78+
reader.setAddDefaultEntities(request.isAddDefaultEntities());
79+
return reader;
80+
}
81+
82+
private static void write(XmlWriterRequest<PluginDescriptor> request, PluginDescriptorStaxWriter writer) {
83+
request.assertWritable();
8984
try {
90-
if (writer != null) {
91-
new PluginDescriptorStaxWriter().write(writer, content);
92-
} else if (outputStream != null) {
93-
new PluginDescriptorStaxWriter().write(outputStream, content);
85+
if (request.getWriter() != null) {
86+
writer.write(request.getWriter(), request.getContent());
87+
} else if (request.getOutputStream() != null) {
88+
writer.write(request.getOutputStream(), request.getContent());
9489
} else {
95-
try (OutputStream os = Files.newOutputStream(path)) {
96-
new PluginDescriptorStaxWriter().write(os, content);
90+
try (var os = Files.newOutputStream(request.getPath())) {
91+
writer.write(os, request.getContent());
9792
}
9893
}
99-
} catch (Exception e) {
94+
} catch (XmlWriterException | XMLStreamException | IOException e) {
10095
throw new XmlWriterException("Unable to write plugin: " + getMessage(e), getLocation(e), e);
10196
}
10297
}

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

+16-15
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import static org.junit.jupiter.api.Assertions.assertNotNull;
4747
import static org.junit.jupiter.api.Assertions.assertThrows;
4848
import static org.mockito.Mockito.mock;
49+
import static org.mockito.Mockito.spy;
4950
import static org.mockito.Mockito.when;
5051

5152
class DefaultPluginXmlFactoryReadWriteTest {
@@ -82,10 +83,10 @@ void readFromInputStreamParsesPluginDescriptorCorrectly() {
8283
@Test
8384
void parsePlugin() {
8485
assertThat(defaultPluginXmlFactory
85-
.read(XmlReaderRequest.builder()
86-
.reader(new StringReader(SAMPLE_PLUGIN_XML))
87-
.build())
88-
.getName())
86+
.read(XmlReaderRequest.builder()
87+
.reader(new StringReader(SAMPLE_PLUGIN_XML))
88+
.build())
89+
.getName())
8990
.isEqualTo(NAME);
9091
}
9192

@@ -94,8 +95,8 @@ void readFromPathParsesPluginDescriptorCorrectly() throws Exception {
9495
Path xmlFile = tempDir.resolve("plugin.xml");
9596
Files.write(xmlFile, SAMPLE_PLUGIN_XML.getBytes());
9697
assertThat(defaultPluginXmlFactory
97-
.read(XmlReaderRequest.builder().path(xmlFile).build())
98-
.getName())
98+
.read(XmlReaderRequest.builder().path(xmlFile).build())
99+
.getName())
99100
.isEqualTo(NAME);
100101
}
101102

@@ -222,13 +223,13 @@ public void close() {}
222223
@Test
223224
void writeWithNoTargetThrowsIllegalArgumentException() {
224225
assertThat(assertThrows(
225-
IllegalArgumentException.class,
226-
() -> defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder()
227-
.content(PluginDescriptor.newBuilder()
228-
.name("No Output Plugin")
229-
.build())
230-
.build()))
231-
.getMessage())
226+
IllegalArgumentException.class,
227+
() -> defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder()
228+
.content(PluginDescriptor.newBuilder()
229+
.name("No Output Plugin")
230+
.build())
231+
.build()))
232+
.getMessage())
232233
.isEqualTo("writer, outputStream or path must be non null");
233234
}
234235

@@ -268,7 +269,7 @@ void readFromUrlParsesPluginDescriptorCorrectly() throws Exception {
268269
void testReadWithPath() throws Exception {
269270
Path tempPath = Files.createTempFile("plugin", ".xml");
270271
Files.writeString(tempPath, "<plugin/>");
271-
XmlReaderRequest request = mock(XmlReaderRequest.class);
272+
XmlReaderRequest request = spy(XmlReaderRequest.class);
272273
when(request.getPath()).thenReturn(tempPath);
273274
when(request.getInputStream()).thenReturn(null);
274275
when(request.getReader()).thenReturn(null);
@@ -283,7 +284,7 @@ void testReadWithPath() throws Exception {
283284
void testReadWithPathUrlDefault() throws Exception {
284285
Path tempPath = Files.createTempFile("plugin", ".xml");
285286
Files.writeString(tempPath, "<plugin/>");
286-
XmlReaderRequest request = mock(XmlReaderRequest.class);
287+
XmlReaderRequest request = spy(XmlReaderRequest.class);
287288
when(request.getPath()).thenReturn(null);
288289
when(request.getInputStream()).thenReturn(null);
289290
when(request.getReader()).thenReturn(null);

0 commit comments

Comments
 (0)