Skip to content

Commit

Permalink
Implemented the suggestions made by @Anilople
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoKrupitza committed Oct 7, 2021
1 parent 6af241e commit 1781809
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class provides functionalities to manage and hold all environments of the portal.
* By default all the Env from {@link com.ctrip.framework.apollo.core.enums.Env} are included.
* This class provides functionalities to manage and hold all environments of the portal. By default
* all the Env from {@link com.ctrip.framework.apollo.core.enums.Env} are included.
*
* @author wxq
* @author Diego Krupitza([email protected])
Expand Down Expand Up @@ -75,41 +74,35 @@ private Env(String name) {
* @return
*/
private static String getWellFormName(String envName) {
if (Strings.isBlank(envName)) {
if (StringUtils.isBlank(envName)) {
return "";
}
return envName.trim().toUpperCase();
}

/**
* logic same as
* logic same as {@link com.ctrip.framework.apollo.core.enums.EnvUtils#transformEnv}
*
* @param envName the name we want to transform
* @return the env object matching the <code>envName</code>
* @see com.ctrip.framework.apollo.core.enums.EnvUtils transformEnv
*/
public static Env transformEnv(String envName) {
if (StringUtils.isBlank(envName)) {

// cannot be found or blank name
return Env.UNKNOWN;
}

final String envWellFormName = getWellFormName(envName);
// special case for production in case of typo
if (envName.equalsIgnoreCase("PROD")) {
if ("PROD".equalsIgnoreCase(envWellFormName)) {
return Env.PRO;
}

// special case that FAT & FWS should map to FAT
if (envName.equalsIgnoreCase("FWS")) {
if ("FWS".equalsIgnoreCase(envWellFormName)) {
return Env.FAT;
}

if (!Env.exists(envName)) {
return Env.UNKNOWN;
if (Env.exists(envWellFormName)) {
return Env.valueOf(envWellFormName);
}

return Env.valueOf(envName);
// cannot be found or blank name
return Env.UNKNOWN;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,152 +16,184 @@
*/
package com.ctrip.framework.apollo.portal.environment;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Test;

public class EnvTest {

@Test
public void exist() {
assertFalse(Env.exists("xxxyyy234"));
assertTrue(Env.exists("local"));
assertTrue(Env.exists("dev"));
}

@Test
public void addEnv() {
String name = "someEEEE";
assertFalse(Env.exists(name));
Env.addEnvironment(name);
assertTrue(Env.exists(name));
}

@Test(expected = IllegalArgumentException.class)
public void valueOf() {
String name = "notexist";
assertFalse(Env.exists(name));
assertEquals(Env.valueOf(name), Env.UNKNOWN);
assertEquals(Env.valueOf("dev"), Env.DEV);
assertEquals(Env.valueOf("UAT"), Env.UAT);
}

@Test
public void testEquals() {
assertEquals(Env.DEV, Env.valueOf("dEv"));
String name = "someEEEE";
Env.addEnvironment(name);
assertNotEquals(Env.valueOf(name), Env.DEV);
}

@Test(expected = RuntimeException.class)
public void testEqualsWithRuntimeException()
throws NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException {
// get private constructor
Constructor<Env> envConstructor = Env.class.getDeclaredConstructor(String.class);
// make private constructor accessible
envConstructor.setAccessible(true);
// make a fake Env
Env fakeDevEnv = envConstructor.newInstance(Env.DEV.toString());
// compare, then a RuntimeException will invoke
fakeDevEnv.equals(Env.DEV);
}

@Test
public void testEqualWithoutException() {
assertEquals(Env.DEV, Env.DEV);
assertEquals(Env.DEV, Env.valueOf("dEV"));
assertNotEquals(Env.PRO, Env.DEV);
assertNotEquals(Env.DEV, Env.valueOf("uaT"));
}

@Test
public void testToString() {
assertEquals("DEV", Env.DEV.toString());
}

@Test
public void name() {
assertEquals("DEV", Env.DEV.name());
}

@Test
public void getName() {
String name = "getName";
Env.addEnvironment(name);
assertEquals(name.trim().toUpperCase(), Env.valueOf(name).toString());
}

@Test
public void transformEnvBlankTest() {
assertEquals(Env.UNKNOWN,Env.transformEnv(""));
assertEquals(Env.UNKNOWN,Env.transformEnv(null));
assertEquals(Env.UNKNOWN,Env.transformEnv(" "));
}

@Test
public void transformEnvSpecialCaseTest() {
// Prod/Pro
assertEquals(Env.PRO,Env.transformEnv("prod"));
assertEquals(Env.PRO,Env.transformEnv("PROD"));

//FAT/FWS
assertEquals(Env.FAT,Env.transformEnv("FWS"));
assertEquals(Env.FAT,Env.transformEnv("fws"));
}

@Test
public void transformEnvNotExistTest() {
assertEquals(Env.UNKNOWN,Env.transformEnv("notexisting"));
assertEquals(Env.LOCAL,Env.transformEnv("LOCAL"));
}

@Test
public void transformEnvValidTest() {
assertEquals(Env.UNKNOWN,Env.transformEnv("UNKNOWN"));
assertEquals(Env.LOCAL,Env.transformEnv("LOCAL"));
assertEquals(Env.FAT,Env.transformEnv("FAT"));
assertEquals(Env.FAT,Env.transformEnv("FWS"));
assertEquals(Env.PRO,Env.transformEnv("PRO"));
assertEquals(Env.PRO,Env.transformEnv("PROD"));
assertEquals(Env.DEV,Env.transformEnv("DEV"));
assertEquals(Env.LPT,Env.transformEnv("LPT"));
assertEquals(Env.TOOLS,Env.transformEnv("TOOLS"));
assertEquals(Env.UAT,Env.transformEnv("UAT"));

String testEnvName = "testEnv";

Env.addEnvironment(testEnvName);
Env expected = Env.valueOf(testEnvName);

assertEquals(expected,Env.transformEnv(testEnvName));
}

@Test(expected = RuntimeException.class)
public void addEnvironmentBlankStringTest() {
Env.addEnvironment("");
}

@Test(expected = RuntimeException.class)
public void addEnvironmentNullStringTest() {
Env.addEnvironment(null);
}

@Test(expected = RuntimeException.class)
public void addEnvironmentSpacesStringTest() {
Env.addEnvironment(" ");
}

@Test
public void existsForBlankNameTest() {
assertFalse(Env.exists(""));
assertFalse(Env.exists(" "));
assertFalse(Env.exists(null));
}
@Test
public void exist() {
assertFalse(Env.exists("xxxyyy234"));
assertTrue(Env.exists("local"));
assertTrue(Env.exists("dev"));
}

@Test
public void addEnv() {
String name = "someEEEE";
assertFalse(Env.exists(name));
Env.addEnvironment(name);
assertTrue(Env.exists(name));
}

@Test(expected = IllegalArgumentException.class)
public void valueOf() {
String name = "notexist";
assertFalse(Env.exists(name));
assertEquals(Env.valueOf(name), Env.UNKNOWN);
assertEquals(Env.valueOf("dev"), Env.DEV);
assertEquals(Env.valueOf("UAT"), Env.UAT);
}

@Test
public void testEquals() {
assertEquals(Env.DEV, Env.valueOf("dEv"));
String name = "someEEEE";
Env.addEnvironment(name);
assertNotEquals(Env.valueOf(name), Env.DEV);
}

@Test(expected = RuntimeException.class)
public void testEqualsWithRuntimeException()
throws NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException {
// get private constructor
Constructor<Env> envConstructor = Env.class.getDeclaredConstructor(String.class);
// make private constructor accessible
envConstructor.setAccessible(true);
// make a fake Env
Env fakeDevEnv = envConstructor.newInstance(Env.DEV.toString());
// compare, then a RuntimeException will invoke
fakeDevEnv.equals(Env.DEV);
}

@Test
public void testEqualWithoutException() {
assertEquals(Env.DEV, Env.DEV);
assertEquals(Env.DEV, Env.valueOf("dEV"));
assertNotEquals(Env.PRO, Env.DEV);
assertNotEquals(Env.DEV, Env.valueOf("uaT"));
}

@Test
public void testToString() {
assertEquals("DEV", Env.DEV.toString());
}

@Test
public void name() {
assertEquals("DEV", Env.DEV.name());
}

@Test
public void getName() {
String name = "getName";
Env.addEnvironment(name);
assertEquals(name.trim().toUpperCase(), Env.valueOf(name).toString());
}

@Test
public void testTransformEnvBlank() {
assertEquals(Env.UNKNOWN, Env.transformEnv(""));
assertEquals(Env.UNKNOWN, Env.transformEnv(null));
assertEquals(Env.UNKNOWN, Env.transformEnv(" "));
}

@Test
public void testTransformEnvSpecialCase() {
// Prod/Pro
assertEquals(Env.PRO, Env.transformEnv("prod"));
assertEquals(Env.PRO, Env.transformEnv("PROD"));

//FAT/FWS
assertEquals(Env.FAT, Env.transformEnv("FWS"));
assertEquals(Env.FAT, Env.transformEnv("fws"));
}

@Test
public void testTransformEnvNotExist() {
assertEquals(Env.UNKNOWN, Env.transformEnv("notexisting"));
assertEquals(Env.LOCAL, Env.transformEnv("LOCAL"));
}

@Test
public void testTransformEnvValid() {
assertEquals(Env.UNKNOWN, Env.transformEnv("UNKNOWN"));
assertEquals(Env.LOCAL, Env.transformEnv("LOCAL"));
assertEquals(Env.FAT, Env.transformEnv("FAT"));
assertEquals(Env.FAT, Env.transformEnv("FWS"));
assertEquals(Env.PRO, Env.transformEnv("PRO"));
assertEquals(Env.PRO, Env.transformEnv("PROD"));
assertEquals(Env.DEV, Env.transformEnv("DEV"));
assertEquals(Env.LPT, Env.transformEnv("LPT"));
assertEquals(Env.TOOLS, Env.transformEnv("TOOLS"));
assertEquals(Env.UAT, Env.transformEnv("UAT"));

String testEnvName = "testEnv";

Env.addEnvironment(testEnvName);
Env expected = Env.valueOf(testEnvName);

assertEquals(expected, Env.transformEnv(testEnvName));
}

@Test
public void testTransformEnvWithTrailingAndLeadingBlankValid() {
ArrayList<String> specialChars = new ArrayList<>();
specialChars.add(" ");
specialChars.add("\t");
specialChars.add(" \t");
specialChars.add(" \t ");
specialChars.add("\t ");

for (String specialChar : specialChars) {
assertEquals(Env.UNKNOWN, Env.transformEnv(specialChar + "UNKNOWN"));
assertEquals(Env.LOCAL, Env.transformEnv(specialChar + "LOCAL" + specialChar));
assertEquals(Env.FAT, Env.transformEnv(specialChar + "FAT" + specialChar));
assertEquals(Env.FAT, Env.transformEnv(specialChar + "FWS" + specialChar));
assertEquals(Env.PRO, Env.transformEnv(specialChar + "PRO" + specialChar));
assertEquals(Env.PRO, Env.transformEnv(specialChar + "PROD" + specialChar));
assertEquals(Env.DEV, Env.transformEnv(specialChar + "DEV" + specialChar));
assertEquals(Env.LPT, Env.transformEnv(specialChar + "LPT" + specialChar));
assertEquals(Env.TOOLS, Env.transformEnv(specialChar + "TOOLS" + specialChar));
assertEquals(Env.UAT, Env.transformEnv(specialChar + "UAT" + specialChar));

String testEnvName = "testEnv";
Env.addEnvironment(testEnvName);
Env expected = Env.valueOf(testEnvName);
assertEquals(expected, Env.transformEnv(specialChar + testEnvName + specialChar));

}
}

@Test(expected = RuntimeException.class)
public void testAddEnvironmentBlankString() {
Env.addEnvironment("");
}

@Test(expected = RuntimeException.class)
public void testAddEnvironmentNullString() {
Env.addEnvironment(null);
}

@Test(expected = RuntimeException.class)
public void testAddEnvironmentSpacesString() {
Env.addEnvironment(" ");
}

@Test
public void testExistsForBlankName() {
assertFalse(Env.exists(""));
assertFalse(Env.exists(" "));
assertFalse(Env.exists(null));
}
}

0 comments on commit 1781809

Please sign in to comment.