|
| 1 | +package io.jooby.di; |
| 2 | + |
| 3 | +import com.google.inject.Binder; |
| 4 | +import com.google.inject.Key; |
| 5 | +import com.google.inject.TypeLiteral; |
| 6 | +import com.google.inject.binder.AnnotatedBindingBuilder; |
| 7 | +import com.google.inject.binder.AnnotatedConstantBindingBuilder; |
| 8 | +import com.google.inject.binder.ConstantBindingBuilder; |
| 9 | +import com.google.inject.binder.LinkedBindingBuilder; |
| 10 | +import com.google.inject.name.Names; |
| 11 | +import com.google.inject.util.Types; |
| 12 | +import com.typesafe.config.Config; |
| 13 | +import com.typesafe.config.ConfigFactory; |
| 14 | +import io.jooby.Environment; |
| 15 | +import io.jooby.Jooby; |
| 16 | +import io.jooby.ServiceRegistry; |
| 17 | +import org.junit.jupiter.api.DisplayName; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.verify; |
| 27 | +import static org.mockito.Mockito.when; |
| 28 | + |
| 29 | +public class JoobyModuleTest { |
| 30 | + @Test |
| 31 | + @DisplayName("Guice should not break on empty list property. Empty list fallback to List<String>") |
| 32 | + public void issue1337() { |
| 33 | + List<String> emptyList = Collections.emptyList(); |
| 34 | + Map<String, Object> map = new HashMap<>(); |
| 35 | + map.put("some", emptyList); |
| 36 | + |
| 37 | + Config config = ConfigFactory.parseMap(map); |
| 38 | + |
| 39 | + Environment environment = mock(Environment.class); |
| 40 | + when(environment.getConfig()).thenReturn(config); |
| 41 | + |
| 42 | + ServiceRegistry registry = mock(ServiceRegistry.class); |
| 43 | + when(registry.entrySet()).thenReturn(Collections.emptySet()); |
| 44 | + |
| 45 | + Jooby app = mock(Jooby.class); |
| 46 | + when(app.getEnvironment()).thenReturn(environment); |
| 47 | + when(app.getServices()).thenReturn(registry); |
| 48 | + |
| 49 | + Binder binder = mock(Binder.class); |
| 50 | + |
| 51 | + AnnotatedBindingBuilder<Environment> envbinding = mock(AnnotatedBindingBuilder.class); |
| 52 | + when(binder.bind(Environment.class)).thenReturn(envbinding); |
| 53 | + |
| 54 | + AnnotatedBindingBuilder<Config> confbinding = mock(AnnotatedBindingBuilder.class); |
| 55 | + when(binder.bind(Config.class)).thenReturn(confbinding); |
| 56 | + |
| 57 | + LinkedBindingBuilder emptyListBinding = mock(LinkedBindingBuilder.class); |
| 58 | + emptyListBinding.toInstance(emptyList); |
| 59 | + when(binder.bind(Key.get(Types.listOf(String.class), Names.named("some")))).thenReturn(emptyListBinding); |
| 60 | + |
| 61 | + ConstantBindingBuilder constantBindingBuilder = mock(ConstantBindingBuilder.class); |
| 62 | + constantBindingBuilder.to(""); |
| 63 | + AnnotatedConstantBindingBuilder constantBinding = mock(AnnotatedConstantBindingBuilder.class); |
| 64 | + when(constantBinding.annotatedWith(Names.named("some"))).thenReturn(constantBindingBuilder); |
| 65 | + when(binder.bindConstant()).thenReturn(constantBinding); |
| 66 | + |
| 67 | + JoobyModule module = new JoobyModule(app); |
| 68 | + |
| 69 | + module.configure(binder); |
| 70 | + |
| 71 | + verify(envbinding).toInstance(environment); |
| 72 | + verify(confbinding).toInstance(config); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments