diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/EnvUtils.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/EnvUtils.java index 19df5f39941..2a13125d7f9 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/EnvUtils.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/EnvUtils.java @@ -18,31 +18,46 @@ import com.ctrip.framework.apollo.core.utils.StringUtils; +/** + * A utility class for the {@link Env} enum. + *
+ * The class provides simple functionalities that extend the capabilities of {@link Env} + * + * @author Diego Krupitza(info@diegokrupitza.com) + */ public final class EnvUtils { - + + /** + * Transforms a given String to its matching {@link Env} + * + * @param envName the String to convert + * @return the matching {@link Env} for the given String + */ public static Env transformEnv(String envName) { if (StringUtils.isBlank(envName)) { return Env.UNKNOWN; } - switch (envName.trim().toUpperCase()) { - case "LPT": - return Env.LPT; - case "FAT": - case "FWS": - return Env.FAT; - case "UAT": - return Env.UAT; - case "PRO": - case "PROD": //just in case - return Env.PRO; - case "DEV": - return Env.DEV; - case "LOCAL": - return Env.LOCAL; - case "TOOLS": - return Env.TOOLS; - default: - return Env.UNKNOWN; + + String cleanedEnvName = envName.trim().toUpperCase(); + + // fix up in case there is a typo + // like prod/pro + if (cleanedEnvName.equals("PROD")) { + return Env.PRO; + } + + if (cleanedEnvName.equals("FWS")) { + // special case that FAT & FWS + // should return the same + return Env.FAT; + } + + try { + return Env.valueOf(cleanedEnvName); + } catch (IllegalArgumentException e) { + // the name could not be found + // or there is a typo we dont handle + return Env.UNKNOWN; } } } diff --git a/apollo-core/src/test/java/com/ctrip/framework/apollo/core/enums/EnvUtilsTest.java b/apollo-core/src/test/java/com/ctrip/framework/apollo/core/enums/EnvUtilsTest.java index a8360a2722e..58bee56e993 100644 --- a/apollo-core/src/test/java/com/ctrip/framework/apollo/core/enums/EnvUtilsTest.java +++ b/apollo-core/src/test/java/com/ctrip/framework/apollo/core/enums/EnvUtilsTest.java @@ -16,7 +16,7 @@ */ package com.ctrip.framework.apollo.core.enums; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -41,4 +41,26 @@ public void testFromString() throws Exception { public void testFromInvalidString() throws Exception { Env.fromString("someInvalidEnv"); } + + @Test + public void fixTypoInProductionTest() { + Env prod = Env.fromString("PROD"); + assertEquals(prod, Env.PRO); + } + + @Test(expected = IllegalArgumentException.class) + public void fromBlankStringTest() { + Env.fromString(""); + } + + @Test(expected = IllegalArgumentException.class) + public void fromSpacesStringTest() { + Env.fromString(" "); + } + + @Test(expected = IllegalArgumentException.class) + public void fromNullStringTest() { + Env.fromString(null); + } + }