diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadata.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadata.java index 8d44ffe180..ac4ff2f058 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadata.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadata.java @@ -19,6 +19,8 @@ package org.springframework.boot.configurationprocessor.metadata; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Configuration meta-data. @@ -30,6 +32,8 @@ import java.util.List; */ public class ConfigurationMetadata { + private static final Pattern CAMEL_CASE_PATTERN = Pattern.compile("([^A-Z-])([A-Z])"); + private final List items; public ConfigurationMetadata() { @@ -73,15 +77,22 @@ public class ConfigurationMetadata { } static String toDashedCase(String name) { - StringBuilder dashed = new StringBuilder(); - for (int i = 0; i < name.length(); i++) { - char c = name.charAt(i); - if (Character.isUpperCase(c) && dashed.length() > 0) { - dashed.append("-"); + Matcher matcher = CAMEL_CASE_PATTERN.matcher(name); + StringBuffer result = new StringBuffer(); + while (matcher.find()) { + String first = matcher.group(1); + String second = matcher.group(2); + String target; + if (first.equals("_")) { // not a word for the binder + target = first + second; + } else { + target = first + "-" + second; } - dashed.append(Character.toLowerCase(c)); + matcher.appendReplacement(result,target); } - return dashed.toString(); + matcher.appendTail(result); + String value = result.toString(); + return value.toLowerCase(); } } diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadataTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadataTests.java new file mode 100644 index 0000000000..b99828dfd1 --- /dev/null +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadataTests.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012-2014 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. + * 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 org.springframework.boot.configurationprocessor.metadata; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Tests for {@link ConfigurationMetadata}. + * + * @author Stephane Nicoll + */ +public class ConfigurationMetadataTests { + + @Test + public void toDashedCaseCamelCase() { + assertThat(ConfigurationMetadata.toDashedCase("simpleCamelCase"), is("simple-camel-case")); + } + + @Test + public void toDashedCaseWordsUnderScore() { + assertThat(ConfigurationMetadata.toDashedCase("Word_With_underscore"), is("word_with_underscore")); + } + + @Test + public void toDashedCaseWordsSeveralUnderScores() { + assertThat(ConfigurationMetadata.toDashedCase("Word___With__underscore"), is("word___with__underscore")); + } + + @Test + public void toDashedCaseLowerCaseUnderscore() { + assertThat(ConfigurationMetadata.toDashedCase("lower_underscore"), is("lower_underscore")); + } + + @Test + public void toDashedCaseUpperUnderscore() { + assertThat(ConfigurationMetadata.toDashedCase("UPPER_UNDERSCORE"), is("upper_underscore")); + } + + @Test + public void toDashedCaseMultipleUnderscores() { + assertThat(ConfigurationMetadata.toDashedCase("super___crazy"), is("super___crazy")); + } + + @Test + public void toDashedCaseUppercase() { + assertThat(ConfigurationMetadata.toDashedCase("UPPERCASE"), is("uppercase")); + } + + @Test + public void toDashedCaseLowercase() { + assertThat(ConfigurationMetadata.toDashedCase("lowercase"), is("lowercase")); + } + +}