Skip to content
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

Removed useless hardcoded Strings in EnvUtils. #4007

Merged
merged 3 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,40 @@

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([email protected])
*/
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":
DiegoKrupitza marked this conversation as resolved.
Show resolved Hide resolved
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")) {
cleanedEnvName = Env.PRO.name().toUpperCase();
DiegoKrupitza marked this conversation as resolved.
Show resolved Hide resolved
}

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);
}

}