EnumBasedFeatureProvider implements FeatureProvider { @Override public FeatureMetaData getMetaData(Feature feature) { if (metaDataCache == null) { throw new IllegalStateException("There are no features added in this provider instance."); } return metaDataCache.get(feature.name()); } EnumBasedFeatureProvider();  EnumBasedFeatureProvider(Class<? extends Feature>... featureEnums); EnumBasedFeatureProvider addFeatureEnum(Class<? extends Feature> featureEnum); @Override Set<Feature> getFeatures(); @Override FeatureMetaData getMetaData(Feature feature);  }
@Test public void shouldReturnMetaDataWithCorrectLabel() { FeatureProvider provider = new EnumBasedFeatureProvider(ValidFeatureEnum.class); FeatureMetaData metaData = provider.getMetaData(ValidFeatureEnum.FEATURE1); assertEquals("First feature", metaData.getLabel()); }
@Test public void shouldReturnMetaDataWhenRequestedWithOtherFeatureImplementation() { FeatureProvider provider = new EnumBasedFeatureProvider(ValidFeatureEnum.class); FeatureMetaData metaData = provider.getMetaData(new OtherFeatureImpl(ValidFeatureEnum.FEATURE1.name())); assertEquals("First feature", metaData.getLabel()); }
@Test public void shouldReturnOwnerNameIfAnnotationPresent() { FeatureProvider provider = new EnumBasedFeatureProvider(ValidFeatureEnum.class); FeatureMetaData metaData = provider.getMetaData(ValidFeatureEnum.WITH_OWNER); assertTrue(metaData.getAttributes().containsValue("Christian")); }
@Test public void shouldReturnNullForOwnerNameByDefault() { FeatureProvider provider = new EnumBasedFeatureProvider(ValidFeatureEnum.class); FeatureMetaData metaData = provider.getMetaData(ValidFeatureEnum.FEATURE1); assertFalse(metaData.getAttributes().containsValue("Christian")); }
@Test public void shouldReturnInfoLinkIfAnnotationPresent() { FeatureProvider provider = new EnumBasedFeatureProvider(ValidFeatureEnum.class); FeatureMetaData metaData = provider.getMetaData(ValidFeatureEnum.WITH_LINK); assertTrue(metaData.getAttributes().containsValue("https: }
@Test public void shouldReturnNullForInfoLinkByDefault() { FeatureProvider provider = new EnumBasedFeatureProvider(ValidFeatureEnum.class); FeatureMetaData metaData = provider.getMetaData(ValidFeatureEnum.FEATURE1); assertFalse(metaData.getAttributes().containsValue("https: }
@Test public void shouldBuildMetadataForMultipleEnumsViaConstructor() { FeatureProvider provider = new EnumBasedFeatureProvider(ValidFeatureEnum.class, OtherFeatureEnum.class); assertEquals("First feature", provider.getMetaData(ValidFeatureEnum.FEATURE1).getLabel()); assertEquals("Additional Feature", provider.getMetaData(OtherFeatureEnum.ADDITIONAL_FEATURE).getLabel()); }
@Test public void shouldNotAllowTheDefaultFeatureStateToBeChangedByExternalClasses() { FeatureProvider provider = new EnumBasedFeatureProvider(ValidFeatureEnum.class, OtherFeatureEnum.class); FeatureMetaData metaData = provider.getMetaData(ValidFeatureEnum.FEATURE1); assertFalse(metaData.getDefaultFeatureState().isEnabled()); metaData.getDefaultFeatureState().setEnabled(true); assertFalse(provider.getMetaData(ValidFeatureEnum.FEATURE1).getDefaultFeatureState().isEnabled()); }
@Test public void providerShouldThrowIllegalStateExceptionIfGetMetaDataIsCalledonEmptyProviderInstance() { FeatureProvider provider = new EnumBasedFeatureProvider(); assertThrows(IllegalStateException.class, () -> { provider.getMetaData(ValidFeatureEnum.FEATURE1).getDefaultFeatureState().isEnabled(); }); }