-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Chore: Future-proofed ConfigFileFormat
#4030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nobodyiam
merged 4 commits into
apolloconfig:master
from
DiegoKrupitza:chore/configFileFormat-futureproofing
Oct 13, 2021
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
49f663e
Future-proofed `ConfigFileFormat`
DiegoKrupitza ef83d0d
Fixing the breaking change of making Properties compatible with themself
DiegoKrupitza 1e087cc
Added more javadoc
DiegoKrupitza 213ca56
Update the javadoc for `isPropertiesCompatible`
DiegoKrupitza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,44 +17,91 @@ | |
| package com.ctrip.framework.apollo.core.enums; | ||
|
|
||
| import com.ctrip.framework.apollo.core.utils.StringUtils; | ||
| import java.util.stream.Stream; | ||
|
|
||
| /** | ||
| * This enum represents all the possible Configuration file formats apollo currently supports. | ||
| * <p> | ||
| * Currently the following formats are supported: | ||
| * <ul> | ||
| * <li>{@link ConfigFileFormat#Properties}</li> | ||
| * <li>{@link ConfigFileFormat#XML}</li> | ||
| * <li>{@link ConfigFileFormat#JSON}</li> | ||
| * <li>{@link ConfigFileFormat#YML}</li> | ||
| * <li>{@link ConfigFileFormat#YAML}</li> | ||
| * <li>{@link ConfigFileFormat#TXT}</li> | ||
| * </ul> | ||
| * | ||
| * @author Jason Song([email protected]) | ||
| * @author Diego Krupitza([email protected]) | ||
| */ | ||
| public enum ConfigFileFormat { | ||
| Properties("properties"), XML("xml"), JSON("json"), YML("yml"), YAML("yaml"), TXT("txt"); | ||
|
|
||
| private String value; | ||
| private final String value; | ||
|
|
||
| ConfigFileFormat(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public String getValue() { | ||
| return value; | ||
| /** | ||
| * Cleans a given configFilename so it does not contain leading or trailing spaces and is always | ||
| * lowercase. | ||
| * <p> | ||
| * For example: | ||
| * | ||
| * <table border="1" cellspacing="1"> | ||
| * <tr> | ||
| * <th>Before</th> | ||
| * <th>After</th> | ||
| * </tr> | ||
| * <tr> | ||
| * <td>"Properties "</td> | ||
| * <td>"properties"</td> | ||
| * </tr> | ||
| * <tr> | ||
| * <td>" "</td> | ||
| * <td>""</td> | ||
| * </tr> | ||
| * </table> | ||
| * | ||
| * @param configFileName the name we want to clean | ||
| * @return the cleansed configFileName | ||
| */ | ||
| private static String getWellFormedName(String configFileName) { | ||
| if (StringUtils.isBlank(configFileName)) { | ||
| return ""; | ||
| } | ||
| return configFileName.trim().toLowerCase(); | ||
| } | ||
|
|
||
| /** | ||
| * Transforms a given string to its matching {@link ConfigFileFormat}. | ||
| * | ||
| * @param value the string that matches | ||
| * @return the matching {@link ConfigFileFormat} | ||
| * @throws IllegalArgumentException in case the <code>value</code> is empty or there is no | ||
| * matching {@link ConfigFileFormat} | ||
| */ | ||
| public static ConfigFileFormat fromString(String value) { | ||
| if (StringUtils.isEmpty(value)) { | ||
| throw new IllegalArgumentException("value can not be empty"); | ||
| } | ||
| switch (value.toLowerCase()) { | ||
| case "properties": | ||
| return Properties; | ||
| case "xml": | ||
| return XML; | ||
| case "json": | ||
| return JSON; | ||
| case "yml": | ||
| return YML; | ||
| case "yaml": | ||
| return YAML; | ||
| case "txt": | ||
| return TXT; | ||
| } | ||
| throw new IllegalArgumentException(value + " can not map enum"); | ||
|
|
||
| final String cleansedName = getWellFormedName(value); | ||
|
|
||
| return Stream.of(ConfigFileFormat.values()) | ||
| .filter(item -> cleansedName.equalsIgnoreCase(item.getValue())) | ||
| .findFirst() | ||
| .orElseThrow(() -> new IllegalArgumentException(value + " can not map enum")); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a given string is a valid {@link ConfigFileFormat}. | ||
| * | ||
| * @param value the string to check on | ||
| * @return is it a valid format | ||
| */ | ||
| public static boolean isValidFormat(String value) { | ||
| try { | ||
| fromString(value); | ||
|
|
@@ -64,7 +111,21 @@ public static boolean isValidFormat(String value) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether a given {@link ConfigFileFormat} is compatible with {@link | ||
| * ConfigFileFormat#Properties} | ||
| * | ||
| * @param format the format to check its compatibility | ||
| * @return is it compatible with {@link ConfigFileFormat#Properties} | ||
| */ | ||
| public static boolean isPropertiesCompatible(ConfigFileFormat format) { | ||
| return format == YAML || format == YML; | ||
| } | ||
|
|
||
| /** | ||
| * @return The string representation of the given {@link ConfigFileFormat} | ||
| */ | ||
| public String getValue() { | ||
| return value; | ||
| } | ||
| } | ||
131 changes: 131 additions & 0 deletions
131
apollo-core/src/test/java/com/ctrip/framework/apollo/core/enums/ConfigFileFormatTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * Copyright 2021 Apollo 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 com.ctrip.framework.apollo.core.enums; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
|
||
| import java.util.ArrayList; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.ExpectedException; | ||
|
|
||
| /** | ||
| * Tests the {@link ConfigFileFormat} enum. | ||
| * | ||
| * @author Diego Krupitza([email protected]) | ||
| */ | ||
| public class ConfigFileFormatTest { | ||
|
|
||
| @Rule | ||
| public ExpectedException expectedEx = ExpectedException.none(); | ||
|
|
||
| @Test | ||
| public void testFromStringEqualsOriginal() { | ||
| assertEquals(ConfigFileFormat.Properties, | ||
| ConfigFileFormat.fromString(ConfigFileFormat.Properties.getValue())); | ||
| assertEquals(ConfigFileFormat.XML, | ||
| ConfigFileFormat.fromString(ConfigFileFormat.XML.getValue())); | ||
| assertEquals(ConfigFileFormat.JSON, | ||
| ConfigFileFormat.fromString(ConfigFileFormat.JSON.getValue())); | ||
| assertEquals(ConfigFileFormat.YML, | ||
| ConfigFileFormat.fromString(ConfigFileFormat.YML.getValue())); | ||
| assertEquals(ConfigFileFormat.YAML, | ||
| ConfigFileFormat.fromString(ConfigFileFormat.YAML.getValue())); | ||
| assertEquals(ConfigFileFormat.TXT, | ||
| ConfigFileFormat.fromString(ConfigFileFormat.TXT.getValue())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNonExistingValueFromString() { | ||
| expectedEx.expect(IllegalArgumentException.class); | ||
| expectedEx.expectMessage("thisShouldNotExistPropertiesXML can not map enum"); | ||
|
|
||
| ConfigFileFormat.fromString("thisShouldNotExistPropertiesXML"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyValueFromString() { | ||
| expectedEx.expect(IllegalArgumentException.class); | ||
| expectedEx.expectMessage("value can not be empty"); | ||
|
|
||
| ConfigFileFormat.fromString(""); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSpacedValueFromString() { | ||
| expectedEx.expect(IllegalArgumentException.class); | ||
| expectedEx.expectMessage(" can not map enum"); | ||
|
|
||
| ConfigFileFormat.fromString(" "); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSpecialCharsValueFromString() { | ||
| ArrayList<String> specialChars = new ArrayList<>(); | ||
| specialChars.add(" "); | ||
| specialChars.add("\t"); | ||
| specialChars.add(" \t"); | ||
| specialChars.add(" \t "); | ||
| specialChars.add("\t "); | ||
|
|
||
| specialChars.forEach(item -> { | ||
| assertEquals(ConfigFileFormat.Properties, | ||
| ConfigFileFormat.fromString(item + ConfigFileFormat.Properties.getValue() + item)); | ||
| assertEquals(ConfigFileFormat.XML, | ||
| ConfigFileFormat.fromString(item + ConfigFileFormat.XML.getValue() + item)); | ||
| assertEquals(ConfigFileFormat.JSON, | ||
| ConfigFileFormat.fromString(item + ConfigFileFormat.JSON.getValue() + item)); | ||
| assertEquals(ConfigFileFormat.YML, | ||
| ConfigFileFormat.fromString(item + ConfigFileFormat.YML.getValue() + item)); | ||
| assertEquals(ConfigFileFormat.YAML, | ||
| ConfigFileFormat.fromString(item + ConfigFileFormat.YAML.getValue() + item)); | ||
| assertEquals(ConfigFileFormat.TXT, | ||
| ConfigFileFormat.fromString(item + ConfigFileFormat.TXT.getValue() + item)); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIsValidFormatForOriginalContent() { | ||
| assertTrue(ConfigFileFormat.isValidFormat(ConfigFileFormat.Properties.getValue())); | ||
| assertTrue(ConfigFileFormat.isValidFormat(ConfigFileFormat.XML.getValue())); | ||
| assertTrue(ConfigFileFormat.isValidFormat(ConfigFileFormat.JSON.getValue())); | ||
| assertTrue(ConfigFileFormat.isValidFormat(ConfigFileFormat.YML.getValue())); | ||
| assertTrue(ConfigFileFormat.isValidFormat(ConfigFileFormat.YAML.getValue())); | ||
| assertTrue(ConfigFileFormat.isValidFormat(ConfigFileFormat.TXT.getValue())); | ||
|
|
||
| assertTrue( | ||
| ConfigFileFormat.isValidFormat(ConfigFileFormat.Properties.getValue().toUpperCase())); | ||
| assertTrue(ConfigFileFormat.isValidFormat(ConfigFileFormat.XML.getValue().toUpperCase())); | ||
| assertTrue(ConfigFileFormat.isValidFormat(ConfigFileFormat.JSON.getValue().toUpperCase())); | ||
| assertTrue(ConfigFileFormat.isValidFormat(ConfigFileFormat.YML.getValue().toUpperCase())); | ||
| assertTrue(ConfigFileFormat.isValidFormat(ConfigFileFormat.YAML.getValue().toUpperCase())); | ||
| assertTrue(ConfigFileFormat.isValidFormat(ConfigFileFormat.TXT.getValue().toUpperCase())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIsValidFormatForInvalid() { | ||
| assertFalse(ConfigFileFormat.isValidFormat("thisshouldnotexist")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIfPropertiesCompatible() { | ||
| assertTrue(ConfigFileFormat.isPropertiesCompatible(ConfigFileFormat.YAML)); | ||
| assertTrue(ConfigFileFormat.isPropertiesCompatible(ConfigFileFormat.YML)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.