Rework tests with JMockit. Remove EnvProvider adaptor

pull/1923/head
Andrii Abramov 6 years ago
parent c08d66de71
commit c5a0a1985b

@ -62,16 +62,6 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
*/
public class ConfigSupport {
private final EnvProvider env;
public ConfigSupport() {
this(new SystemEnvProvider());
}
public ConfigSupport(EnvProvider env) {
this.env = env;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "class")
@JsonFilter("classFilter")
public static class ClassMixIn {
@ -150,7 +140,7 @@ public class ConfigSupport {
Matcher m = pattern.matcher(content);
while (m.find()) {
String[] parts = m.group(1).split(":-");
String v = env.get(parts[0]);
String v = System.getenv(parts[0]);
if (v != null) {
content = content.replace(m.group(), v);
} else if (parts.length == 2) {

@ -1,20 +0,0 @@
package org.redisson.config;
/**
* Adaptor for key value storage, a.k.a. Environment.
*/
@FunctionalInterface
interface EnvProvider {
/**
* Gets the value of the specified environment variable. An
* environment variable is a system-dependent external named
* value.
*
* @param name the name of the environment variable
* @return the string value of the variable, or <code>null</code>
* if the variable is not defined in the system environment
*/
String get(String name);
}

@ -1,16 +0,0 @@
package org.redisson.config;
/**
* Default implementation for environment values adaptor.
*/
public class SystemEnvProvider implements EnvProvider {
/**
* @see System#getenv()
*/
@Override
public String get(String name) {
return System.getenv(name);
}
}

@ -1,12 +1,12 @@
package org.redisson.config;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import mockit.Mock;
import mockit.MockUp;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
@ -14,46 +14,54 @@ public class ConfigSupportTest {
@Test
public void testParsingLiteral() throws IOException {
SingleServerConfig config = mkConfig("127.0.0.1", new HashMap<String, String>() {{
put("REDIS_URI", "1.1.1.1");
}});
mockHostEnv("1.1.1.1");
SingleServerConfig config = mkConfig("127.0.0.1");
assertEquals(URI.create("redis://127.0.0.1"), config.getAddress());
}
@Test
public void testParsingEnv() throws IOException {
SingleServerConfig config = mkConfig("${REDIS_URI}", new HashMap<String, String>() {{
put("REDIS_URI", "1.1.1.1");
}});
mockHostEnv("1.1.1.1");
SingleServerConfig config = mkConfig("${REDIS_URI}");
assertEquals(URI.create("redis://1.1.1.1"), config.getAddress());
}
@Test(expected = InvalidFormatException.class)
public void testParsingEnv_envMissing() throws IOException {
mkConfig("${REDIS_URI}", new HashMap<>());
mockHostEnv(null);
mkConfig("${REDIS_URI}");
}
@Test
public void testParsingDefault_envPresent() throws IOException {
SingleServerConfig config = mkConfig("${REDIS_URI:-10.0.0.1}", new HashMap<String, String>() {{
put("REDIS_URI", "11.0.0.1");
}});
mockHostEnv("11.0.0.1");
SingleServerConfig config = mkConfig("${REDIS_URI:-10.0.0.1}");
assertEquals(URI.create("redis://11.0.0.1"), config.getAddress());
}
@Test
public void testParsingDefault_envMissing() throws IOException {
SingleServerConfig config = mkConfig("${REDIS_URI:-10.0.0.1}", new HashMap<>());
mockHostEnv(null);
SingleServerConfig config = mkConfig("${REDIS_URI:-10.0.0.1}");
assertEquals(URI.create("redis://10.0.0.1"), config.getAddress());
}
private SingleServerConfig mkConfig(String authorityValue, Map<String, String> env) throws IOException {
private SingleServerConfig mkConfig(String authorityValue) throws IOException {
String config = "singleServerConfig:\n address: redis://" + authorityValue;
return new ConfigSupport(env::get).fromYAML(config, Config.class).getSingleServerConfig();
return new ConfigSupport().fromYAML(config, Config.class).getSingleServerConfig();
}
private void mockHostEnv(String value) {
new MockUp<System>() {
@Mock
String getenv(String name) {
return value;
}
};
}
}

Loading…
Cancel
Save