ComponentNameExtractor { public String componentFrom(final Class<?> aClass) { if (aClass.isAnnotationPresent(ServiceComponent.class)) { return aClass.getAnnotation(ServiceComponent.class).value(); } else if (aClass.isAnnotationPresent(Adapter.class)) { return aClass.getAnnotation(Adapter.class).value(); } else if (aClass.isAnnotationPresent(FrameworkComponent.class)) { return aClass.getAnnotation(FrameworkComponent.class).value(); } else if (aClass.isAnnotationPresent(CustomServiceComponent.class)) { return aClass.getAnnotation(CustomServiceComponent.class).value(); } else if (aClass.isAnnotationPresent(CustomAdapter.class)) { return aClass.getAnnotation(CustomAdapter.class).value(); } else if (aClass.isAnnotationPresent(DirectAdapter.class)) { return aClass.getAnnotation(DirectAdapter.class).value(); } else { throw new IllegalStateException(format("No annotation found to define component for class %s", aClass)); } }  String componentFrom(final Class<?> aClass); String componentFrom(final InjectionPoint injectionPoint); boolean hasComponentAnnotation(final InjectionPoint injectionPoint);  }
@Test public void shouldReturnFieldLevelComponent() throws NoSuchFieldException { assertThat(componentNameExtractor.componentFrom(injectionPointWith( ServiceComponentFieldLevelAnnotation.class.getDeclaredField(FIELD_NAME))), equalTo("COMMAND_CONTROLLER")); }
@Test public void shouldReturnClassLevelComponent() throws NoSuchFieldException { assertThat(componentNameExtractor.componentFrom(injectionPointWith( ServiceComponentClassLevelAnnotation.class.getDeclaredField(FIELD_NAME))), equalTo("COMMAND_HANDLER")); }
@Test public void shouldReturnClassLevelAdaptorComponent() throws NoSuchFieldException { assertThat(componentNameExtractor.componentFrom(injectionPointWith( AdapterAnnotation.class.getDeclaredField(FIELD_NAME))), equalTo("EVENT_LISTENER")); }
@Test public void shouldReturnClassLevelCustomAdaptorComponent() throws NoSuchFieldException { assertThat(componentNameExtractor.componentFrom(injectionPointWith( CustomAdapterAnnotation.class.getDeclaredField(FIELD_NAME))), equalTo("CUSTOM_ADAPTER")); }
@Test public void shouldReturnClassLevelDirectAdaptorComponent() throws NoSuchFieldException { assertThat(componentNameExtractor.componentFrom(injectionPointWith( DirectAdapterAnnotation.class.getDeclaredField(FIELD_NAME))), equalTo("QUERY_API")); }
@Test public void shouldReturnClassLevelComponentForMethodInjectionPoint() throws NoSuchFieldException { assertThat(componentNameExtractor.componentFrom(injectionPointWith( ServiceComponentClassLevelAnnotationMethod.class.getDeclaredMethods()[0])), equalTo("QUERY_API")); }
@Test public void shouldReturnClassLevelFrameworkComponent() throws NoSuchFieldException { assertThat(componentNameExtractor.componentFrom(injectionPointWith( FrameworkComponentClassLevelAnnotation.class.getDeclaredField(FIELD_NAME))), equalTo("CUSTOM_NAME_ABC")); }
@Test public void shouldReturnFieldLevelFrameworkComponent() throws NoSuchFieldException { assertThat(componentNameExtractor.componentFrom(injectionPointWith( FrameworkComponentFieldLevelAnnotation.class.getDeclaredField(FIELD_NAME))), equalTo("CUSTOM_NAME_BCD")); }
@Test public void shouldReturnClassLevelCustomServiceComponent() throws NoSuchFieldException { assertThat(componentNameExtractor.componentFrom(injectionPointWith( CustomServiceClassLevelAnnotation.class.getDeclaredMethods()[0])), equalTo("CUSTOM_SERVICE_NAME")); }
@Test public void shouldReturnFieldLevelCustomServiceComponent() throws NoSuchFieldException { assertThat(componentNameExtractor.componentFrom(injectionPointWith( CustomServiceFieldLevelAnnotation.class.getDeclaredField(FIELD_NAME))), equalTo("CUSTOM_SERVICE_NAME")); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionOnMissingComponentAnnotation() throws NoSuchFieldException { componentNameExtractor.componentFrom(injectionPointWith(NoAnnotation.class.getDeclaredField(FIELD_NAME))); }