Skip to content

Commit

Permalink
fix: add warnings for unsupported feature flags in properties file (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
schastlivcev committed Mar 26, 2024
1 parent ca02373 commit f24304b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class FeatureFlags implements Serializable {

private ApplicationConfiguration configuration;

private boolean isPropertiesFileChecked = false;

/**
* Generate FeatureFlags with given lookup data.
*
Expand Down Expand Up @@ -208,6 +210,11 @@ void loadProperties(InputStream propertiesStream) {

if (propertiesStream != null) {
props.load(propertiesStream);
// Check once if there are unsupported feature flags in the file
if (!isPropertiesFileChecked) {
checkForUnsupportedFeatureFlags(props);
isPropertiesFileChecked = true;
}
}
for (Feature f : features) {
// Allow users to override a feature flag with a system property
Expand Down Expand Up @@ -346,4 +353,13 @@ public String getEnableHelperMessage(Feature feature) {
private Logger getLogger() {
return LoggerFactory.getLogger(FeatureFlags.class);
}

private void checkForUnsupportedFeatureFlags(Properties props) {
for (Object property : props.keySet()) {
if (features.stream()
.noneMatch(feature -> getPropertyName(feature.getId()).equals(property))) {
getLogger().warn("Unsupported feature flag is present: {}", property);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.flow.di.Lookup;
Expand Down Expand Up @@ -405,6 +407,43 @@ public void get_concurrentAccess_vaadinContextLock_noDeadlock()
latch.await(1, TimeUnit.SECONDS));
}

@Test
public void propertiesFileCheckedForUnsupportedFeatureFlags() throws IOException {
Logger mockedLogger = Mockito.mock(Logger.class);

try (MockedStatic<LoggerFactory> context = Mockito.mockStatic(LoggerFactory.class)) {
context.when(() -> LoggerFactory.getLogger(FeatureFlags.class))
.thenReturn(mockedLogger);

createFeatureFlagsFile(
"com.vaadin.experimental.unsupportedFeature=true\ncom.vaadin.experimental.exampleFeatureFlag=true\n");
featureFlags.loadProperties();

Mockito.verify(mockedLogger, Mockito.never())
.warn("Unsupported feature flag is present: {}", "com.vaadin.experimental.exampleFeatureFlag");
Mockito.verify(mockedLogger, Mockito.times(1))
.warn("Unsupported feature flag is present: {}", "com.vaadin.experimental.unsupportedFeature");
}
}

@Test
public void propertiesFileCheckForUnsupportedFeatureFlagsRanOnlyOnce() throws IOException {
Logger mockedLogger = Mockito.mock(Logger.class);

try (MockedStatic<LoggerFactory> context = Mockito.mockStatic(LoggerFactory.class)) {
context.when(() -> LoggerFactory.getLogger(FeatureFlags.class))
.thenReturn(mockedLogger);

createFeatureFlagsFile(
"com.vaadin.experimental.unsupportedFeature=true\n");
featureFlags.loadProperties();
featureFlags.loadProperties();

Mockito.verify(mockedLogger, Mockito.times(1))
.warn("Unsupported feature flag is present: {}", "com.vaadin.experimental.unsupportedFeature");
}
}

private boolean hasUsageStatsEntry(String name) {
return UsageStatistics.getEntries()
.filter(entry -> entry.getName().equals(name)).findFirst()
Expand Down

0 comments on commit f24304b

Please sign in to comment.