diff --git a/buildSrc/src/main/java/org/springframework/boot/build/mavenplugin/PluginXmlParser.java b/buildSrc/src/main/java/org/springframework/boot/build/mavenplugin/PluginXmlParser.java index 688d77b89d2..fddd516374e 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/mavenplugin/PluginXmlParser.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/mavenplugin/PluginXmlParser.java @@ -36,6 +36,7 @@ import org.w3c.dom.NodeList; * A parser for a Maven plugin's {@code plugin.xml} file. * * @author Andy Wilkinson + * @author Mike Smithson */ class PluginXmlParser { @@ -98,21 +99,20 @@ class PluginXmlParser { private Parameter parseParameter(Node parameterNode, Map defaultValues, Map userProperties) throws XPathExpressionException { - Parameter parameter = new Parameter(textAt("name", parameterNode), textAt("type", parameterNode), + return new Parameter(textAt("name", parameterNode), textAt("type", parameterNode), booleanAt("required", parameterNode), booleanAt("editable", parameterNode), format(textAt("description", parameterNode)), defaultValues.get(textAt("name", parameterNode)), userProperties.get(textAt("name", parameterNode)), textAt("since", parameterNode)); - return parameter; } private boolean booleanAt(String path, Node node) throws XPathExpressionException { - return Boolean.valueOf(textAt(path, node)); + return Boolean.parseBoolean(textAt(path, node)); } private String format(String input) { return input.replace("", "`").replace("", "`").replace("<", "<").replace(">", ">") - .replace("
", " ").replace("\n", " ").replace(""", "\"").replaceAll("\\{@code (.*?)\\}", "`$1`") - .replaceAll("\\{@link (.*?)\\}", "`$1`").replaceAll("\\{@literal (.*?)\\}", "`$1`") + .replace("
", " ").replace("\n", " ").replace(""", "\"").replaceAll("\\{@code (.*?)}", "`$1`") + .replaceAll("\\{@link (.*?)}", "`$1`").replaceAll("\\{@literal (.*?)}", "`$1`") .replaceAll("(.*?)", "\\$1[\\$2]"); } diff --git a/buildSrc/src/test/java/org/springframework/boot/build/mavenplugin/PluginXmlParserTests.java b/buildSrc/src/test/java/org/springframework/boot/build/mavenplugin/PluginXmlParserTests.java index 0cbdd408ce2..d3c130dfc52 100644 --- a/buildSrc/src/test/java/org/springframework/boot/build/mavenplugin/PluginXmlParserTests.java +++ b/buildSrc/src/test/java/org/springframework/boot/build/mavenplugin/PluginXmlParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,24 +17,42 @@ package org.springframework.boot.build.mavenplugin; import java.io.File; +import java.io.FileNotFoundException; +import java.util.Arrays; +import java.util.stream.Collectors; import org.junit.jupiter.api.Test; import org.springframework.boot.build.mavenplugin.PluginXmlParser.Plugin; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + /** * Tests for {@link PluginXmlParser}. * * @author Andy Wilkinson + * @author Mike Smithson */ public class PluginXmlParserTests { private final PluginXmlParser parser = new PluginXmlParser(); @Test - void dunno() { + void parseExistingDescriptorReturnPluginDescriptor() { Plugin plugin = this.parser.parse(new File("src/test/resources/plugin.xml")); - System.out.println(plugin); + assertThat(plugin.getGroupId()).isEqualTo("org.springframework.boot"); + assertThat(plugin.getArtifactId()).isEqualTo("spring-boot-maven-plugin"); + assertThat(plugin.getVersion()).isEqualTo("2.2.0.GRADLE-SNAPSHOT"); + assertThat(plugin.getGoalPrefix()).isEqualTo("spring-boot"); + assertThat(plugin.getMojos().stream().map(PluginXmlParser.Mojo::getGoal).collect(Collectors.toList())) + .isEqualTo(Arrays.asList("build-info", "help", "repackage", "run", "start", "stop")); + } + + @Test + void parseNonExistingFileThrowException() { + assertThatThrownBy(() -> this.parser.parse(new File("src/test/resources/nonexistent.xml"))) + .isInstanceOf(RuntimeException.class).hasCauseInstanceOf(FileNotFoundException.class); } }