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

Upgrade to junit 5 #706

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -1,17 +1,18 @@
package com.netflix.archaius.bridge;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.netflix.archaius.guice.ArchaiusModule;
import com.netflix.config.ConfigurationManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.PrintWriter;
import java.io.StringWriter;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AbstractConfigurationBridgeFailureTest {
public static class TestModule extends AbstractModule {
@Override
Expand All @@ -29,26 +30,23 @@ protected void configure() {
}
}

@Before
public void before() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
@BeforeEach
public void before() throws SecurityException {
StaticAbstractConfiguration.reset();
StaticDeploymentContext.reset();
}

@Test
public void testStaticInModule() {
try {
Guice.createInjector(
new TestModule(),
new BadModule());
Assert.fail();
} catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String stack = sw.toString();
Exception exception = assertThrows(Exception.class, () ->
Guice.createInjector(
new TestModule(),
new BadModule()));
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
String stack = sw.toString();

Assert.assertTrue(stack.contains("com.netflix.archaius.bridge.AbstractConfigurationBridgeFailureTest$BadModule"));
Assert.assertTrue(stack.contains("**** Remove static reference"));
}
assertTrue(stack.contains("com.netflix.archaius.bridge.AbstractConfigurationBridgeFailureTest$BadModule"));
assertTrue(stack.contains("**** Remove static reference"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.MapConfiguration;
import org.apache.commons.configuration.event.ConfigurationListener;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import com.google.inject.AbstractModule;
Expand All @@ -28,6 +25,12 @@
import java.io.IOException;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AbstractConfigurationBridgeTest extends BaseBridgeTest {
public static class TestModule extends AbstractModule {
private Properties properties;
Expand All @@ -47,17 +50,13 @@ protected void configure() {
bind(SomeClient.class).asEagerSingleton();
}
}

@Rule
public TestName testName = new TestName();

private static SettableConfig settable;
private static Injector injector;
private static AbstractConfiguration commonsConfig;
private static Config config;

@BeforeClass
public static void before() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
@BeforeAll
public static void before() {
final Properties props = new Properties();
props.setProperty("app.override.foo", "bar");
props.setProperty(ContextKey.environment.getKey(), "test");
Expand All @@ -73,16 +72,16 @@ protected void configure() {

config = injector.getInstance(Config.class);
settable = injector.getInstance(Key.get(SettableConfig.class, RuntimeLayer.class));
Assert.assertTrue(ConfigurationManager.isConfigurationInstalled());
assertTrue(ConfigurationManager.isConfigurationInstalled());
commonsConfig = ConfigurationManager.getConfigInstance();
Assert.assertEquals(StaticAbstractConfiguration.class, commonsConfig.getClass());
assertEquals(StaticAbstractConfiguration.class, commonsConfig.getClass());
}

@Test
public void testBasicWiring() {
SomeClient client = injector.getInstance(SomeClient.class);
Assert.assertNotNull(ConfigurationManager.getConfigInstance());
Assert.assertEquals("bar", client.fooValue);
assertNotNull(ConfigurationManager.getConfigInstance());
assertEquals("bar", client.fooValue);
}

@ConfigurationSource(value={"AbstractConfigurationBridgeTest_libA"})
Expand All @@ -94,52 +93,52 @@ static class LibB {
}

@Test
public void lastLoadedLibraryWins() throws IOException {
public void lastLoadedLibraryWins() {
Config config = injector.getInstance(Config.class);

injector.getInstance(LibA.class);

Assert.assertTrue(config.getBoolean("libA.loaded", false));
Assert.assertEquals("libA", config.getString("lib.override", null));
assertTrue(config.getBoolean("libA.loaded", false));
assertEquals("libA", config.getString("lib.override", null));

injector.getInstance(LibB.class);

Assert.assertTrue(config.getBoolean("libB.loaded", false));
Assert.assertEquals("libA", config.getString("lib.override", null));
assertTrue(config.getBoolean("libB.loaded", false));
assertEquals("libA", config.getString("lib.override", null));
}

@Test
public void basicBridgeTest() throws IOException {
DeploymentContext context1 = ConfigurationManager.getDeploymentContext();
Assert.assertNotNull(context1);
Assert.assertEquals("test", context1.getDeploymentEnvironment());
assertNotNull(context1);
assertEquals("test", context1.getDeploymentEnvironment());

AbstractConfiguration config1 = ConfigurationManager.getConfigInstance();
DeploymentContext contextDi = injector.getInstance(DeploymentContext.class);
Assert.assertNotSame(contextDi, context1);
assertNotSame(contextDi, context1);
ConfigurationManager.loadCascadedPropertiesFromResources("AbstractConfigurationBridgeTest_libA");
Assert.assertTrue(config1.getBoolean("libA.loaded", false));
Assert.assertEquals("libA", config1.getString("lib.override", null));
assertTrue(config1.getBoolean("libA.loaded", false));
assertEquals("libA", config1.getString("lib.override", null));

Config config2 = injector.getInstance(Config.class);
SettableConfig settable = injector.getInstance(Key.get(SettableConfig.class, RuntimeLayer.class));
settable.setProperty("@environment", "foo");

DeploymentContext context2 = ConfigurationManager.getDeploymentContext();

Assert.assertEquals("foo", ConfigurationManager.getDeploymentContext().getDeploymentEnvironment());
Assert.assertEquals("foo", context2.getDeploymentEnvironment());
Assert.assertNotSame(contextDi, context1);
Assert.assertEquals("foo", context1.getDeploymentEnvironment());
assertEquals("foo", ConfigurationManager.getDeploymentContext().getDeploymentEnvironment());
assertEquals("foo", context2.getDeploymentEnvironment());
assertNotSame(contextDi, context1);
assertEquals("foo", context1.getDeploymentEnvironment());

Assert.assertTrue(config2.getBoolean("libA.loaded", false));
Assert.assertEquals("libA", config2.getString("lib.override", null));
assertTrue(config2.getBoolean("libA.loaded", false));
assertEquals("libA", config2.getString("lib.override", null));

ConfigurationManager.loadCascadedPropertiesFromResources("AbstractConfigurationBridgeTest_libB");
Assert.assertTrue(config1.getBoolean("libB.loaded", false));
Assert.assertEquals("libA", config1.getString("lib.override", null));
Assert.assertTrue(config2.getBoolean("libB.loaded", false));
Assert.assertEquals("libA", config2.getString("lib.override", null));
assertTrue(config1.getBoolean("libB.loaded", false));
assertEquals("libA", config1.getString("lib.override", null));
assertTrue(config2.getBoolean("libB.loaded", false));
assertEquals("libA", config2.getString("lib.override", null));

}

Expand All @@ -155,16 +154,16 @@ public void testBridgePropertiesFromLegacyToNew() throws IOException {
Config config2 = injector.getInstance(Config.class);

ConfigurationManager.loadCascadedPropertiesFromResources("AbstractConfigurationBridgeTest_libA");
Assert.assertTrue(config1.getBoolean("libA.loaded", false));
Assert.assertEquals("libA", config1.getString("lib.override", null));
Assert.assertTrue(config2.getBoolean("libA.loaded", false));
Assert.assertEquals("libA", config2.getString("lib.override", null));
assertTrue(config1.getBoolean("libA.loaded", false));
assertEquals("libA", config1.getString("lib.override", null));
assertTrue(config2.getBoolean("libA.loaded", false));
assertEquals("libA", config2.getString("lib.override", null));

ConfigurationManager.loadCascadedPropertiesFromResources("AbstractConfigurationBridgeTest_libB");
Assert.assertTrue(config1.getBoolean("libB.loaded", false));
Assert.assertEquals("libA", config1.getString("lib.override", null));
Assert.assertTrue(config2.getBoolean("libB.loaded", false));
Assert.assertEquals("libA", config2.getString("lib.override", null));
assertTrue(config1.getBoolean("libB.loaded", false));
assertEquals("libA", config1.getString("lib.override", null));
assertTrue(config2.getBoolean("libB.loaded", false));
assertEquals("libA", config2.getString("lib.override", null));

}

Expand All @@ -179,12 +178,12 @@ public void confirmLegacyOverrideOrder() throws IOException {
AbstractConfiguration config = ConfigurationManager.getConfigInstance();

ConfigurationManager.loadCascadedPropertiesFromResources("AbstractConfigurationBridgeTest_libA");
Assert.assertTrue(config.getBoolean("libA.loaded", false));
Assert.assertEquals("libA", config.getString("lib.override", null));
assertTrue(config.getBoolean("libA.loaded", false));
assertEquals("libA", config.getString("lib.override", null));

ConfigurationManager.loadCascadedPropertiesFromResources("AbstractConfigurationBridgeTest_libB");
Assert.assertTrue(config.getBoolean("libB.loaded", false));
Assert.assertEquals("libA", config.getString("lib.override", null));
assertTrue(config.getBoolean("libB.loaded", false));
assertEquals("libA", config.getString("lib.override", null));

ConfigurationManager.loadCascadedPropertiesFromResources("AbstractConfigurationBridgeTest_libB");
}
Expand All @@ -200,9 +199,9 @@ public void confirmLegacyOverrideOrder() throws IOException {
public void confirmLegacyOverrideOrderResources() throws IOException, ConfigurationException {
super.confirmLegacyOverrideOrderResources();

Assert.assertEquals("libA", config.getString("lib.legacy.override"));
Assert.assertTrue(config.getBoolean("libA.legacy.loaded"));
Assert.assertTrue(config.getBoolean("libB.legacy.loaded"));
assertEquals("libA", config.getString("lib.legacy.override"));
assertTrue(config.getBoolean("libA.legacy.loaded"));
assertTrue(config.getBoolean("libB.legacy.loaded"));
}

/**
Expand All @@ -220,16 +219,16 @@ public void confirmLegacyOverrideOrderAddConfig() throws IOException {
p1.setProperty("libA.loaded", "true");
aggregatedConfig.addConfiguration(new MapConfiguration(p1));

Assert.assertTrue(aggregatedConfig.getBoolean("libA.loaded", false));
Assert.assertEquals("libA", aggregatedConfig.getString("lib.override", null));
assertTrue(aggregatedConfig.getBoolean("libA.loaded", false));
assertEquals("libA", aggregatedConfig.getString("lib.override", null));

Properties p2 = new Properties();
p2.setProperty("lib.override", "libB");
p2.setProperty("libB.loaded", "true");
aggregatedConfig.addConfiguration(new MapConfiguration(p2));

Assert.assertTrue(aggregatedConfig.getBoolean("libB.loaded", false));
Assert.assertEquals("libA", aggregatedConfig.getString("lib.override", null));
assertTrue(aggregatedConfig.getBoolean("libB.loaded", false));
assertEquals("libA", aggregatedConfig.getString("lib.override", null));
}

@Test
Expand All @@ -250,13 +249,13 @@ public void verifyValueInterpollation() {
AbstractConfiguration config = ConfigurationManager.getConfigInstance();
config.setProperty("foo", "${ABC:true}");
boolean value = config.getBoolean("foo");
Assert.assertTrue(value);
assertTrue(value);
}

@Test
public void verifyMissingProperty() {
AbstractConfiguration config = ConfigurationManager.getConfigInstance();
Boolean value = config.getBoolean("foo", null);
Assert.assertNull(value);
assertNull(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import com.netflix.archaius.api.Config;
import com.netflix.config.AggregatedConfiguration;
import com.netflix.config.ConfigurationManager;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import javax.inject.Inject;
import javax.inject.Singleton;

@Ignore
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Disabled
public class BaseBridgeTest {
@Singleton
public static class SomeClient {
Expand All @@ -34,16 +36,14 @@ public void confirmLegacyOverrideOrderResources() throws IOException, Configurat
ConfigurationManager.loadPropertiesFromConfiguration(
new PropertiesConfiguration("AbstractConfigurationBridgeTest_libA_legacy.properties"));

Assert.assertTrue(config.getBoolean("libA.legacy.loaded", false));
Assert.assertEquals("libA", config.getString("lib.legacy.override", null));
Assert.assertTrue(config.getBoolean("libA.legacy.loaded"));
assertTrue(config.getBoolean("libA.legacy.loaded", false));
assertEquals("libA", config.getString("lib.legacy.override", null));
assertTrue(config.getBoolean("libA.legacy.loaded"));

ConfigurationManager.loadPropertiesFromConfiguration(
new PropertiesConfiguration("AbstractConfigurationBridgeTest_libB_legacy.properties"));
Assert.assertTrue(config.getBoolean("libB.legacy.loaded", false));
Assert.assertEquals("libA", config.getString("lib.legacy.override", null));
Assert.assertTrue(config.getBoolean("libB.legacy.loaded"));

assertTrue(config.getBoolean("libB.legacy.loaded", false));
assertEquals("libA", config.getString("lib.legacy.override", null));
assertTrue(config.getBoolean("libB.legacy.loaded"));
}

}
Loading
Loading