Skip to content

Commit

Permalink
Removed useless hardcoded Strings in EnvUtils. (#4007)
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoKrupitza authored Sep 29, 2021
1 parent e26294f commit 6e2e4d8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,46 @@

import com.ctrip.framework.apollo.core.utils.StringUtils;

/**
* A utility class for the {@link Env} enum.
* <p>
* 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

}

0 comments on commit 6e2e4d8

Please sign in to comment.