21
21
22
22
import org .junit .jupiter .api .Test ;
23
23
24
+ import java .util .stream .Stream ;
25
+
24
26
import static org .junit .jupiter .api .Assertions .assertEquals ;
27
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
25
28
import static org .junit .jupiter .api .Assertions .assertTrue ;
26
- import static org .junit .jupiter .api .Assertions .fail ;
27
29
28
30
public class ProxyTest {
29
- public static interface MyConfig {
31
+ public interface MyConfig {
30
32
@ DefaultValue ("0" )
31
33
int getInteger ();
32
34
@@ -43,15 +45,7 @@ public interface MySubConfig {
43
45
@ DefaultValue ("0" )
44
46
int getInteger ();
45
47
}
46
-
47
- @ Configuration (prefix ="foo" )
48
- public interface MyConfigWithPrefix {
49
- @ DefaultValue ("0" )
50
- int getInteger ();
51
-
52
- String getString ();
53
- }
54
-
48
+
55
49
@ Test
56
50
public void testConfigWithNoPrefix () throws ConfigException {
57
51
Injector injector = Guice .createInjector (
@@ -67,6 +61,7 @@ protected void configureArchaius() {
67
61
68
62
@ Provides
69
63
@ Singleton
64
+ @ SuppressWarnings ("unused" )
70
65
public MyConfig getMyConfig (ConfigProxyFactory factory ) {
71
66
return factory .newProxy (MyConfig .class );
72
67
}
@@ -98,6 +93,7 @@ protected void configureArchaius() {
98
93
99
94
@ Provides
100
95
@ Singleton
96
+ @ SuppressWarnings ("unused" )
101
97
public MyConfig getMyConfig (ConfigProxyFactory factory ) {
102
98
return factory .newProxy (MyConfig .class , "prefix" );
103
99
}
@@ -121,6 +117,7 @@ public void confirmConfigurationSourceWorksWithProxy() {
121
117
new ArchaiusModule () {
122
118
@ Provides
123
119
@ Singleton
120
+ @ SuppressWarnings ("unused" )
124
121
public ModuleTestConfig getMyConfig (ConfigProxyFactory factory ) {
125
122
return factory .newProxy (ModuleTestConfig .class , "moduleTest" );
126
123
}
@@ -135,34 +132,31 @@ public ModuleTestConfig getMyConfig(ConfigProxyFactory factory) {
135
132
136
133
public interface DefaultMethodWithAnnotation {
137
134
@ DefaultValue ("fromAnnotation" )
135
+ @ SuppressWarnings ("unused" )
138
136
default String getValue () {
139
137
return "fromDefault" ;
140
138
}
141
139
}
142
140
143
141
@ Test
144
142
public void annotationAndDefaultImplementationNotAllowed () throws ConfigException {
145
- try {
146
- Injector injector = Guice .createInjector (
143
+ Injector injector = Guice .createInjector (
147
144
new ArchaiusModule () {
148
- @ Override
149
- protected void configureArchaius () {
150
- }
151
-
152
145
@ Provides
153
146
@ Singleton
147
+ @ SuppressWarnings ("unused" )
154
148
public DefaultMethodWithAnnotation getMyConfig (ConfigProxyFactory factory ) {
155
149
return factory .newProxy (DefaultMethodWithAnnotation .class );
156
150
}
157
151
});
158
-
159
- injector . getInstance ( DefaultMethodWithAnnotation .class );
160
- fail ( "Exepcted ProvisionException" );
161
- } catch ( ProvisionException e ) {
162
- e . printStackTrace ();
163
- assertEquals ( IllegalArgumentException . class , e . getCause (). getCause (). getClass ());
164
- } catch ( Exception e ) {
165
- fail ( "Expected ProvisionException" );
166
- }
152
+
153
+ ProvisionException pe = assertThrows ( ProvisionException .class ,
154
+ () -> injector . getInstance ( DefaultMethodWithAnnotation . class ) );
155
+
156
+ Stream . iterate (( Throwable ) pe , t -> t != null ? t . getCause () : null )
157
+ . limit ( 10 ) // avoid infinite loop
158
+ . filter ( t -> t instanceof IllegalArgumentException )
159
+ . findFirst ()
160
+ . orElseThrow (() -> new AssertionError ( "Expected an IllegalArgumentException in the cause chain" , pe ));
167
161
}
168
162
}
0 commit comments