|
| 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.Test; |
| 40 | +import org.junit.jupiter.api.io.TempDir; |
| 41 | + |
| 42 | +import static org.assertj.core.api.Assertions.assertThat; |
| 43 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 45 | +import static org.mockito.Mockito.mock; |
| 46 | + |
| 47 | +class DefaultPluginXmlFactoryReadWriteTest { |
| 48 | + |
| 49 | + private static final String SAMPLE_PLUGIN_XML = |
| 50 | + """ |
| 51 | + <?xml version="1.0" encoding="UTF-8"?> |
| 52 | + <plugin> |
| 53 | + <name>Sample Plugin</name> |
| 54 | + <groupId>org.example</groupId> |
| 55 | + <artifactId>sample-plugin</artifactId> |
| 56 | + <version>1.0.0</version> |
| 57 | + </plugin> |
| 58 | + """; |
| 59 | + |
| 60 | + private final DefaultPluginXmlFactory defaultPluginXmlFactory = new DefaultPluginXmlFactory(); |
| 61 | + |
| 62 | + @TempDir |
| 63 | + Path tempDir; |
| 64 | + |
| 65 | + @Test |
| 66 | + void readFromInputStreamParsesPluginDescriptorCorrectly() { |
| 67 | + PluginDescriptor descriptor = defaultPluginXmlFactory.read(XmlReaderRequest.builder() |
| 68 | + .inputStream(new ByteArrayInputStream(SAMPLE_PLUGIN_XML.getBytes())) |
| 69 | + .build()); |
| 70 | + assertThat(descriptor.getName()).isEqualTo("Sample Plugin"); |
| 71 | + assertThat(descriptor.getGroupId()).isEqualTo("org.example"); |
| 72 | + assertThat(descriptor.getArtifactId()).isEqualTo("sample-plugin"); |
| 73 | + assertThat(descriptor.getVersion()).isEqualTo("1.0.0"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void parsePlugin() { |
| 78 | + assertThat(defaultPluginXmlFactory |
| 79 | + .read(XmlReaderRequest.builder() |
| 80 | + .reader(new StringReader(SAMPLE_PLUGIN_XML)) |
| 81 | + .build()) |
| 82 | + .getName()) |
| 83 | + .isEqualTo("Sample Plugin"); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void readFromPathParsesPluginDescriptorCorrectly() throws Exception { |
| 88 | + Path xmlFile = tempDir.resolve("plugin.xml"); |
| 89 | + Files.write(xmlFile, SAMPLE_PLUGIN_XML.getBytes()); |
| 90 | + assertThat(defaultPluginXmlFactory |
| 91 | + .read(XmlReaderRequest.builder().path(xmlFile).build()) |
| 92 | + .getName()) |
| 93 | + .isEqualTo("Sample Plugin"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void readWithNoInputThrowsIllegalArgumentException() { |
| 98 | + assertThatExceptionOfType(IllegalArgumentException.class) |
| 99 | + .isThrownBy(() -> |
| 100 | + defaultPluginXmlFactory.read(XmlReaderRequest.builder().build())); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void writeToWriterGeneratesValidXml() { |
| 105 | + StringWriter writer = new StringWriter(); |
| 106 | + defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder() |
| 107 | + .writer(writer) |
| 108 | + .content(PluginDescriptor.newBuilder() |
| 109 | + .name("Sample Plugin") |
| 110 | + .groupId("org.example") |
| 111 | + .artifactId("sample-plugin") |
| 112 | + .version("1.0.0") |
| 113 | + .build()) |
| 114 | + .build()); |
| 115 | + String output = writer.toString(); |
| 116 | + assertThat(output).contains("<name>Sample Plugin</name>"); |
| 117 | + assertThat(output).contains("<groupId>org.example</groupId>"); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void writeToOutputStreamGeneratesValidXml() { |
| 122 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 123 | + defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder() |
| 124 | + .outputStream(outputStream) |
| 125 | + .content(PluginDescriptor.newBuilder().name("Sample Plugin").build()) |
| 126 | + .build()); |
| 127 | + assertThat(outputStream.toString()).contains("<name>Sample Plugin</name>"); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void writeToPathGeneratesValidXmlFile() throws Exception { |
| 132 | + Path xmlFile = tempDir.resolve("output-plugin.xml"); |
| 133 | + defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder() |
| 134 | + .path(xmlFile) |
| 135 | + .content(PluginDescriptor.newBuilder().name("Sample Plugin").build()) |
| 136 | + .build()); |
| 137 | + assertThat(Files.readString(xmlFile)).contains("<name>Sample Plugin</name>"); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void fromXmlStringParsesValidXml() { |
| 142 | + PluginDescriptor descriptor = defaultPluginXmlFactory.fromXmlString(SAMPLE_PLUGIN_XML); |
| 143 | + assertThat(descriptor.getName()).isEqualTo("Sample Plugin"); |
| 144 | + assertThat(descriptor.getGroupId()).isEqualTo("org.example"); |
| 145 | + assertThat(descriptor.getArtifactId()).isEqualTo("sample-plugin"); |
| 146 | + assertThat(descriptor.getVersion()).isEqualTo("1.0.0"); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void toXmlStringGeneratesValidXml() { |
| 151 | + String xml = defaultPluginXmlFactory.toXmlString(PluginDescriptor.newBuilder() |
| 152 | + .name("Sample Plugin") |
| 153 | + .groupId("org.example") |
| 154 | + .artifactId("sample-plugin") |
| 155 | + .version("1.0.0") |
| 156 | + .build()); |
| 157 | + assertThat(xml).contains("<name>Sample Plugin</name>"); |
| 158 | + assertThat(xml).contains("<groupId>org.example</groupId>"); |
| 159 | + assertThat(xml).contains("<artifactId>sample-plugin</artifactId>"); |
| 160 | + assertThat(xml).contains("<version>1.0.0</version>"); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void staticFromXmlParsesValidXml() { |
| 165 | + PluginDescriptor descriptor = DefaultPluginXmlFactory.fromXml(SAMPLE_PLUGIN_XML); |
| 166 | + assertThat(descriptor.getName()).isEqualTo("Sample Plugin"); |
| 167 | + assertThat(descriptor.getGroupId()).isEqualTo("org.example"); |
| 168 | + assertThat(descriptor.getArtifactId()).isEqualTo("sample-plugin"); |
| 169 | + assertThat(descriptor.getVersion()).isEqualTo("1.0.0"); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + void staticToXmlGeneratesValidXml() { |
| 174 | + String xml = DefaultPluginXmlFactory.toXml(PluginDescriptor.newBuilder() |
| 175 | + .name("Sample Plugin") |
| 176 | + .groupId("org.example") |
| 177 | + .artifactId("sample-plugin") |
| 178 | + .version("1.0.0") |
| 179 | + .build()); |
| 180 | + assertThat(xml).contains("<name>Sample Plugin</name>"); |
| 181 | + assertThat(xml).contains("<name>Sample Plugin</name>"); |
| 182 | + assertThat(xml).contains("<groupId>org.example</groupId>"); |
| 183 | + assertThat(xml).contains("<artifactId>sample-plugin</artifactId>"); |
| 184 | + assertThat(xml).contains("<version>1.0.0</version>"); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + void writeWithFailingWriterThrowsXmlWriterException() { |
| 189 | + XmlWriterException exception = assertThatExceptionOfType(XmlWriterException.class) |
| 190 | + .isThrownBy(() -> defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder() |
| 191 | + .writer(new Writer() { |
| 192 | + @Override |
| 193 | + public void write(char[] cbuf, int off, int len) { |
| 194 | + throw new RuntimeException("Simulated failure"); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public void flush() {} |
| 199 | + |
| 200 | + @Override |
| 201 | + public void close() {} |
| 202 | + }) |
| 203 | + .content(PluginDescriptor.newBuilder() |
| 204 | + .name("Failing Plugin") |
| 205 | + .build()) |
| 206 | + .build())) |
| 207 | + .actual(); |
| 208 | + assertThat(exception.getMessage()).contains("Unable to write plugin"); |
| 209 | + assertThat(exception.getCause()).isInstanceOf(RuntimeException.class); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + void writeWithNoTargetThrowsIllegalArgumentException() { |
| 214 | + assertThat(assertThrows( |
| 215 | + IllegalArgumentException.class, |
| 216 | + () -> defaultPluginXmlFactory.write(XmlWriterRequest.<PluginDescriptor>builder() |
| 217 | + .content(PluginDescriptor.newBuilder() |
| 218 | + .name("No Output Plugin") |
| 219 | + .build()) |
| 220 | + .build())) |
| 221 | + .getMessage()) |
| 222 | + .isEqualTo("writer, outputStream or path must be non null"); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + void readMalformedXmlThrowsXmlReaderException() { |
| 227 | + XmlReaderException exception = assertThatExceptionOfType(XmlReaderException.class) |
| 228 | + .isThrownBy(() -> defaultPluginXmlFactory.read(XmlReaderRequest.builder() |
| 229 | + .inputStream(new ByteArrayInputStream("<plugin><name>Broken Plugin".getBytes())) |
| 230 | + .build())) |
| 231 | + .actual(); |
| 232 | + assertThat(exception.getMessage()).contains("Unable to read plugin"); |
| 233 | + assertThat(exception.getCause()).isInstanceOf(WstxEOFException.class); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + void locateExistingPomWithFilePathShouldReturnSameFileIfRegularFile() throws IOException { |
| 238 | + Path pomFile = Files.createTempFile(tempDir, "pom", ".xml"); |
| 239 | + DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()); |
| 240 | + assertThat(processor.locateExistingPom(pomFile)).isEqualTo(pomFile); |
| 241 | + } |
| 242 | + |
| 243 | + @Test |
| 244 | + void readFromUrlParsesPluginDescriptorCorrectly() throws Exception { |
| 245 | + Path xmlFile = tempDir.resolve("plugin.xml"); |
| 246 | + Files.write(xmlFile, SAMPLE_PLUGIN_XML.getBytes()); |
| 247 | + PluginDescriptor descriptor = defaultPluginXmlFactory.read(XmlReaderRequest.builder() |
| 248 | + .inputStream(xmlFile.toUri().toURL().openStream()) |
| 249 | + .build()); |
| 250 | + assertThat(descriptor.getName()).isEqualTo("Sample Plugin"); |
| 251 | + assertThat(descriptor.getGroupId()).isEqualTo("org.example"); |
| 252 | + assertThat(descriptor.getArtifactId()).isEqualTo("sample-plugin"); |
| 253 | + assertThat(descriptor.getVersion()).isEqualTo("1.0.0"); |
| 254 | + } |
| 255 | +} |
0 commit comments