Skip to content

Commit

Permalink
Fix ProxyTest
Browse files Browse the repository at this point in the history
  • Loading branch information
rgallardo-netflix committed Sep 25, 2024
1 parent 664d444 commit 5e9da20
Showing 1 changed file with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

import org.junit.jupiter.api.Test;

import java.util.stream.Stream;

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

public class ProxyTest {
public static interface MyConfig {
public interface MyConfig {
@DefaultValue("0")
int getInteger();

Expand All @@ -43,15 +45,7 @@ public interface MySubConfig {
@DefaultValue("0")
int getInteger();
}

@Configuration(prefix="foo")
public interface MyConfigWithPrefix {
@DefaultValue("0")
int getInteger();

String getString();
}


@Test
public void testConfigWithNoPrefix() throws ConfigException {
Injector injector = Guice.createInjector(
Expand All @@ -67,6 +61,7 @@ protected void configureArchaius() {

@Provides
@Singleton
@SuppressWarnings("unused")
public MyConfig getMyConfig(ConfigProxyFactory factory) {
return factory.newProxy(MyConfig.class);
}
Expand Down Expand Up @@ -98,6 +93,7 @@ protected void configureArchaius() {

@Provides
@Singleton
@SuppressWarnings("unused")
public MyConfig getMyConfig(ConfigProxyFactory factory) {
return factory.newProxy(MyConfig.class, "prefix");
}
Expand All @@ -121,6 +117,7 @@ public void confirmConfigurationSourceWorksWithProxy() {
new ArchaiusModule() {
@Provides
@Singleton
@SuppressWarnings("unused")
public ModuleTestConfig getMyConfig(ConfigProxyFactory factory) {
return factory.newProxy(ModuleTestConfig.class, "moduleTest");
}
Expand All @@ -135,34 +132,31 @@ public ModuleTestConfig getMyConfig(ConfigProxyFactory factory) {

public interface DefaultMethodWithAnnotation {
@DefaultValue("fromAnnotation")
@SuppressWarnings("unused")
default String getValue() {
return "fromDefault";
}
}

@Test
public void annotationAndDefaultImplementationNotAllowed() throws ConfigException {
try {
Injector injector = Guice.createInjector(
Injector injector = Guice.createInjector(
new ArchaiusModule() {
@Override
protected void configureArchaius() {
}

@Provides
@Singleton
@SuppressWarnings("unused")
public DefaultMethodWithAnnotation getMyConfig(ConfigProxyFactory factory) {
return factory.newProxy(DefaultMethodWithAnnotation.class);
}
});
injector.getInstance(DefaultMethodWithAnnotation.class);
fail("Exepcted ProvisionException");
} catch (ProvisionException e) {
e.printStackTrace();
assertEquals(IllegalArgumentException.class, e.getCause().getCause().getClass());
} catch (Exception e) {
fail("Expected ProvisionException");
}

ProvisionException pe = assertThrows(ProvisionException.class,
() -> injector.getInstance(DefaultMethodWithAnnotation.class));

Stream.iterate((Throwable) pe, t -> t != null ? t.getCause() : null)
.limit(10) // avoid infinite loop
.filter(t -> t instanceof IllegalArgumentException)
.findFirst()
.orElseThrow(() -> new AssertionError("Expected an IllegalArgumentException in the cause chain", pe));
}
}

0 comments on commit 5e9da20

Please sign in to comment.