From 655feed39e14ff87a2a51a455e1b631748bed3a1 Mon Sep 17 00:00:00 2001 From: dabou Date: Thu, 13 Jul 2017 18:37:29 +0200 Subject: [PATCH 1/3] Allow to substitute key/value using a system property, otherwise use the default value --- .../springboot/SpringBootGenerator.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java b/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java index 3c479234a6..dbee940f44 100644 --- a/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java +++ b/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java @@ -22,12 +22,7 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.UUID; +import java.util.*; import java.util.zip.CRC32; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -45,6 +40,8 @@ import com.google.common.base.Strings; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.text.StrMatcher; +import org.apache.commons.lang3.text.StrSubstitutor; import org.apache.maven.model.Plugin; import org.apache.maven.model.PluginExecution; import org.apache.maven.plugin.MojoExecutionException; @@ -61,6 +58,9 @@ public class SpringBootGenerator extends JavaExecGenerator { private static final String SPRING_BOOT_MAVEN_PLUGIN_GA = "org.springframework.boot:spring-boot-maven-plugin"; private static final String DEFAULT_SERVER_PORT = "8080"; + private static final StrMatcher DEFAULT_PREFIX = StrMatcher.stringMatcher("${"); + private static final StrMatcher DEFAULT_SUFFIX = StrMatcher.stringMatcher("}"); + private static final StrMatcher DEFAULT_VALUE_DELIMITER = StrMatcher.stringMatcher(":"); public enum Config implements Configs.Key { color {{ d = "false"; }}; @@ -124,12 +124,36 @@ protected List extractPorts() { List answer = new ArrayList<>(); Properties properties = SpringBootUtil.getSpringBootApplicationProperties(this.getProject()); String port = properties.getProperty(SpringBootProperties.SERVER_PORT, DEFAULT_SERVER_PORT); - addPortIfValid(answer, getConfig(JavaExecGenerator.Config.webPort, port)); + addPortIfValid(answer, getConfig(JavaExecGenerator.Config.webPort, replaceKeyWithSystemPropertyValue(port))); addPortIfValid(answer, getConfig(JavaExecGenerator.Config.jolokiaPort)); addPortIfValid(answer, getConfig(JavaExecGenerator.Config.prometheusPort)); return answer; } + /** + * Look within all the System Properties if there is a property which corresponds to the key passed + * If a match exists, then replace the value otherwise use the default value defined within ${key:default-value} + * Example + * my.server.port=7777 is a system property that we will search in order to replace its value within the key passed to the method. + * If the key is "server.port: ${my.server.port:6666}", then "my.server.port" value will be substituted, otherwise the default value is used + * "6666" + * + * @param key The string containing the key to search + * @return String The modified key + */ + protected static String replaceKeyWithSystemPropertyValue(String key) { + HashMap values = new HashMap(); + Properties systemProperties = System.getProperties(); + for(Map.Entry x : systemProperties.entrySet()) { + values.put(x.getKey(),x.getValue()); + } + StrSubstitutor sub = new StrSubstitutor(values); + sub.setValueDelimiterMatcher(DEFAULT_VALUE_DELIMITER); + sub.setVariablePrefixMatcher(DEFAULT_PREFIX); + sub.setVariableSuffixMatcher(DEFAULT_SUFFIX); + return sub.replace(key); + } + // ============================================================================= private void ensureSpringDevToolSecretToken() throws MojoExecutionException { From 6f68cc171955807326ec1ba26589e64e292bff27 Mon Sep 17 00:00:00 2001 From: Charles Moulliard Date: Mon, 2 Oct 2017 08:35:15 +0200 Subject: [PATCH 2/3] To be reviewed --- .../maven/generator/springboot/SpringBootGenerator.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java b/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java index dbee940f44..ca6daa28fc 100644 --- a/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java +++ b/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java @@ -124,7 +124,7 @@ protected List extractPorts() { List answer = new ArrayList<>(); Properties properties = SpringBootUtil.getSpringBootApplicationProperties(this.getProject()); String port = properties.getProperty(SpringBootProperties.SERVER_PORT, DEFAULT_SERVER_PORT); - addPortIfValid(answer, getConfig(JavaExecGenerator.Config.webPort, replaceKeyWithSystemPropertyValue(port))); + addPortIfValid(answer, getConfig(JavaExecGenerator.Config.webPort, replaceKeyWithSystemPropertyValue(port,this.getProject().getProperties()))); addPortIfValid(answer, getConfig(JavaExecGenerator.Config.jolokiaPort)); addPortIfValid(answer, getConfig(JavaExecGenerator.Config.prometheusPort)); return answer; @@ -141,12 +141,16 @@ protected List extractPorts() { * @param key The string containing the key to search * @return String The modified key */ - protected static String replaceKeyWithSystemPropertyValue(String key) { + protected static String replaceKeyWithSystemPropertyValue(String key, Properties mavenProperties) { HashMap values = new HashMap(); Properties systemProperties = System.getProperties(); for(Map.Entry x : systemProperties.entrySet()) { values.put(x.getKey(),x.getValue()); } + + for(Map.Entry x : mavenProperties.entrySet()) { + values.put(x.getKey(),x.getValue()); + } StrSubstitutor sub = new StrSubstitutor(values); sub.setValueDelimiterMatcher(DEFAULT_VALUE_DELIMITER); sub.setVariablePrefixMatcher(DEFAULT_PREFIX); From 72c6ce21e639bf633ac56bae19955eae949c0a68 Mon Sep 17 00:00:00 2001 From: Charles Moulliard Date: Mon, 2 Oct 2017 08:36:06 +0200 Subject: [PATCH 3/3] To be reviewed --- .../SpringBootGeneratorServerPortTest.java | 112 ++++++++++++++++++ .../src/test/resources/application.yml | 1 + 2 files changed, 113 insertions(+) create mode 100644 generator/spring-boot/src/test/java/io/fabric8/maven/generator/springboot/SpringBootGeneratorServerPortTest.java create mode 100644 generator/spring-boot/src/test/resources/application.yml diff --git a/generator/spring-boot/src/test/java/io/fabric8/maven/generator/springboot/SpringBootGeneratorServerPortTest.java b/generator/spring-boot/src/test/java/io/fabric8/maven/generator/springboot/SpringBootGeneratorServerPortTest.java new file mode 100644 index 0000000000..726565c3c4 --- /dev/null +++ b/generator/spring-boot/src/test/java/io/fabric8/maven/generator/springboot/SpringBootGeneratorServerPortTest.java @@ -0,0 +1,112 @@ +/* + * Copyright 2017 Red Hat, Inc. + * + * Red Hat licenses this file to you under the Apache License, version + * 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package io.fabric8.maven.generator.springboot; + +import io.fabric8.maven.core.util.SpringBootProperties; +import io.fabric8.maven.generator.api.GeneratorContext; +import mockit.Deencapsulation; +import mockit.Expectations; +import mockit.Mocked; +import mockit.integration.junit4.JMockit; +import org.apache.commons.io.FileUtils; +import org.apache.maven.model.Build; +import org.apache.maven.model.Model; +import org.apache.maven.project.MavenProject; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.File; +import java.net.URL; +import java.nio.file.Files; +import java.util.List; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +/** + * @author Charles Moulliard + * @since 19/)&/2017 + */ +@RunWith(JMockit.class) +public class SpringBootGeneratorServerPortTest { + + @Mocked + private GeneratorContext context; + + @Mocked + private MavenProject project; + + @Mocked + private Build build; + + @Mocked + private Model model; + + @Test + @Ignore + public void checkPortValue() throws Exception { + SpringBootGenerator generator = new SpringBootGenerator(createGeneratorWithYamlfileContext()); + + Properties props = new Properties(); + props.setProperty("server.port","9090"); + assertEquals("9090",props.get(SpringBootProperties.SERVER_PORT)); + + List ports = generator.extractPorts(); + assertEquals(9090,ports.get(0)); + } + + @Test + public void checkPortFromMavenPropertyValue() throws Exception { + SpringBootGenerator generator = new SpringBootGenerator(createGeneratorWithMavenPropertiesContext()); + List ports = generator.extractPorts(); + assertEquals(9090,ports.get(0)); + } + + private GeneratorContext createGeneratorWithYamlfileContext() throws Exception { + new Expectations() {{ + context.getProject(); result = project; + project.getBuild(); result = build; + String tempDir = Files.createTempDirectory("springboot-test-project").toFile().getAbsolutePath(); + build.getDirectory(); result = tempDir; + build.getOutputDirectory(); result = tempDir; + URL url = SpringBootGeneratorServerPortTest.class.getResource("/application.yml"); + FileUtils.copyFileToDirectory(new File(url.toURI()),new File(tempDir)); + }}; + return context; + } + + private GeneratorContext createGeneratorWithMavenPropertiesContext() throws Exception { + new Expectations() {{ + context.getProject(); result = project; + project.getBuild(); result = build; + String tempDir = Files.createTempDirectory("springboot-test-project").toFile().getAbsolutePath(); + build.getDirectory(); result = tempDir; + build.getOutputDirectory(); result = tempDir; + + // Pass the property to the maven model + // Method of the parent ModelBase is well called and invoked + Deencapsulation.invoke(model,"addProperty","server.port","7777"); + Deencapsulation.setField(model,"name", "Spring-Boot Demo"); + Deencapsulation.setField(project,"model", model); + Deencapsulation.setField(context,"project",project); + // NPE is raised here + model.getProperties().get("server.port"); result = "7777"; + }}; + return context; + } +} diff --git a/generator/spring-boot/src/test/resources/application.yml b/generator/spring-boot/src/test/resources/application.yml new file mode 100644 index 0000000000..97f31c21da --- /dev/null +++ b/generator/spring-boot/src/test/resources/application.yml @@ -0,0 +1 @@ +server.port: 9090 \ No newline at end of file