diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigFileFormat.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigFileFormat.java index d1159e22275..5892c613e27 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigFileFormat.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigFileFormat.java @@ -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. + *
+ * Currently the following formats are supported: + *
+ * For example: + * + *
Before | + *After | + *
---|---|
"Properties " | + *"properties" | + *
" " | + *"" | + *
value
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,24 @@ public static boolean isValidFormat(String value) {
}
}
+ /**
+ * Checks whether a given {@link ConfigFileFormat} is compatible with {@link
+ * ConfigFileFormat#Properties}
+ *
+ * Note: if you call this method with {@link ConfigFileFormat#Properties} it will return
+ * false
.
+ *
+ * @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;
+ }
}
diff --git a/apollo-core/src/test/java/com/ctrip/framework/apollo/core/enums/ConfigFileFormatTest.java b/apollo-core/src/test/java/com/ctrip/framework/apollo/core/enums/ConfigFileFormatTest.java
new file mode 100644
index 00000000000..d406c3ba280
--- /dev/null
+++ b/apollo-core/src/test/java/com/ctrip/framework/apollo/core/enums/ConfigFileFormatTest.java
@@ -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(info@diegokrupitza.com)
+ */
+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