BindInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException { if (isObjectMethod(method)) { return method.invoke(this, args); } final Type returnType = method.getGenericReturnType(); return simpleConfigurationProvider.getProperty(prefix + (prefix.isEmpty() ? "" : ".") + method.getName(), () -> returnType); } BindInvocationHandler(ConfigurationProvider configurationProvider, String prefix); @Override Object invoke(Object proxy, Method method, Object[] args);  }
@Test void usesProvidedPrefix() throws Exception { BindInvocationHandler handler = new BindInvocationHandler(configurationProvider, "abc"); handler.invoke(this, this.getClass().getMethod("stringMethod"), new Object[]{}); verify(configurationProvider, times(1)).getProperty(eq("abc.stringMethod"), any(GenericTypeInterface.class)); }
@Test void usesDefaultNamespaceWhenNoPrefix() throws Exception { BindInvocationHandler handler = new BindInvocationHandler(configurationProvider, ""); handler.invoke(this, this.getClass().getMethod("stringMethod"), new Object[]{}); verify(configurationProvider, times(1)).getProperty(eq("stringMethod"), any(GenericTypeInterface.class)); }
@Test void queriesForProvidedType() throws Exception { BindInvocationHandler handler = new BindInvocationHandler(configurationProvider, ""); handler.invoke(this, this.getClass().getMethod("mapMethod"), new Object[]{}); verify(configurationProvider, times(1)).getProperty(eq("mapMethod"), captor.capture()); assertThat(captor.getValue().getType().toString()).isEqualTo("java.util.Map<java.util.List<java.lang.Integer>, java.lang.Boolean>"); }
@Test void propagatesNoSuchElementException() throws Exception { when(configurationProvider.getProperty(anyString(), any(GenericTypeInterface.class))).thenThrow(new NoSuchElementException()); BindInvocationHandler handler = new BindInvocationHandler(configurationProvider, ""); assertThatThrownBy(() -> handler.invoke(this, this.getClass().getMethod("stringMethod"), new Object[]{})).isExactlyInstanceOf(NoSuchElementException.class); }
@Test void propagatesIllegalArgumentException() throws Exception { when(configurationProvider.getProperty(anyString(), any(GenericTypeInterface.class))).thenThrow(new IllegalArgumentException()); BindInvocationHandler handler = new BindInvocationHandler(configurationProvider, ""); assertThatThrownBy(() -> handler.invoke(this, this.getClass().getMethod("stringMethod"), new Object[]{})).isExactlyInstanceOf(IllegalArgumentException.class); }
@Test void propagatesIllegalStateException() throws Exception { when(configurationProvider.getProperty(anyString(), any(GenericTypeInterface.class))).thenThrow(new IllegalStateException()); BindInvocationHandler handler = new BindInvocationHandler(configurationProvider, ""); assertThatThrownBy(() -> handler.invoke(this, this.getClass().getMethod("stringMethod"), new Object[]{})).isExactlyInstanceOf(IllegalStateException.class); }
@Test void passesCallToNonObjectLevelMethodWithCollidingName() throws Exception { when(configurationProvider.getProperty(eq("equals"), any(GenericTypeInterface.class))).thenReturn(true); BindInvocationHandler handler = new BindInvocationHandler(configurationProvider, ""); assertThat((boolean) handler.invoke(this, this.getClass().getMethod("equals", String.class), new Object[]{})).isTrue(); }
@Test void passesCallToNonObjectLevelMethodWithCollidingNameAndDifferentNumberOfParams() throws Exception { when(configurationProvider.getProperty(eq("equals"), any(GenericTypeInterface.class))).thenReturn(true); BindInvocationHandler handler = new BindInvocationHandler(configurationProvider, ""); assertThat((boolean) handler.invoke(this, this.getClass().getMethod("equals", String.class, String.class), new Object[]{})).isTrue(); }
@Test void invokesObjectLevelMethod() throws Exception { BindInvocationHandler handler = new BindInvocationHandler(configurationProvider, ""); int hashCode = (int) handler.invoke(this, this.getClass().getMethod("hashCode"), new Object[]{}); assertThat(hashCode).isEqualTo(handler.hashCode()); }