Skip to content

Commit 0002980

Browse files
authored
fix: make system property feature flag value always override stored one (#14681)
Enable or disable a feature flag by providing a system property is not working when feature flags file is loaded by a ResourceProvider. This change makes system properties always take precedence over stored values Fixes #14679
1 parent 742ce67 commit 0002980

2 files changed

Lines changed: 120 additions & 32 deletions

File tree

‎flow-server/src/main/java/com/vaadin/experimental/FeatureFlags.java‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ public void loadProperties() {
200200
if (featureFlagFile == null || !featureFlagFile.exists()) {
201201
// Disable all features if no file exists
202202
for (Feature f : features) {
203-
f.setEnabled(false);
203+
f.setEnabled(
204+
Boolean.getBoolean(SYSTEM_PROPERTY_PREFIX + f.getId()));
204205
}
205206
} else {
206207
try (FileInputStream propertiesStream = new FileInputStream(
@@ -213,15 +214,6 @@ public void loadProperties() {
213214
"Failed to read properties file from filesystem", e);
214215
}
215216
}
216-
217-
// Allow users to override a feature flag with a system property
218-
for (Feature f : features) {
219-
var prop = System.getProperty(SYSTEM_PROPERTY_PREFIX + f.getId());
220-
221-
if (prop != null) {
222-
f.setEnabled(Boolean.parseBoolean(prop));
223-
}
224-
}
225217
}
226218

227219
void loadProperties(InputStream propertiesStream) {
@@ -232,9 +224,14 @@ void loadProperties(InputStream propertiesStream) {
232224
props.load(propertiesStream);
233225
}
234226
for (Feature f : features) {
235-
f.setEnabled(Boolean.valueOf(
236-
props.getProperty(getPropertyName(f.getId()))));
227+
// Allow users to override a feature flag with a system property
228+
var propertyValue = System.getProperty(
229+
SYSTEM_PROPERTY_PREFIX + f.getId(),
230+
props.getProperty(getPropertyName(f.getId())));
231+
232+
f.setEnabled(Boolean.parseBoolean(propertyValue));
237233
}
234+
238235
} catch (IOException e) {
239236
getLogger().error("Unable to read feature flags", e);
240237
}

‎flow-server/src/test/java/com/vaadin/experimental/FeatureFlagsTest.java‎

Lines changed: 111 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,12 @@
1515
*/
1616
package com.vaadin.experimental;
1717

18+
import javax.annotation.concurrent.NotThreadSafe;
1819
import java.io.File;
1920
import java.io.IOException;
2021
import java.nio.charset.StandardCharsets;
2122
import java.nio.file.Files;
2223

23-
import javax.annotation.concurrent.NotThreadSafe;
24-
25-
import com.vaadin.flow.internal.UsageStatistics;
26-
import com.vaadin.flow.server.MockVaadinContext;
27-
import com.vaadin.flow.server.VaadinContext;
28-
import com.vaadin.flow.server.VaadinService;
29-
import com.vaadin.flow.server.startup.ApplicationConfiguration;
30-
3124
import org.apache.commons.io.FileUtils;
3225
import org.junit.Assert;
3326
import org.junit.Before;
@@ -36,6 +29,16 @@
3629
import org.junit.rules.TemporaryFolder;
3730
import org.mockito.Mockito;
3831

32+
import com.vaadin.flow.di.Lookup;
33+
import com.vaadin.flow.di.ResourceProvider;
34+
import com.vaadin.flow.internal.UsageStatistics;
35+
import com.vaadin.flow.server.MockVaadinContext;
36+
import com.vaadin.flow.server.VaadinContext;
37+
import com.vaadin.flow.server.VaadinService;
38+
import com.vaadin.flow.server.startup.ApplicationConfiguration;
39+
40+
import static com.vaadin.experimental.FeatureFlags.PROPERTIES_FILENAME;
41+
3942
@NotThreadSafe
4043
public class FeatureFlagsTest {
4144

@@ -46,6 +49,7 @@ public class FeatureFlagsTest {
4649
private FeatureFlags featureFlags;
4750
private File propertiesDir;
4851
private ApplicationConfiguration configuration;
52+
private File featureFlagsFile;
4953

5054
@Before
5155
public void before() throws IOException {
@@ -61,6 +65,9 @@ public void before() throws IOException {
6165
featureFlags.setPropertiesLocation(propertiesDir);
6266

6367
mockResourcesLocation();
68+
69+
featureFlagsFile = new File(propertiesDir, PROPERTIES_FILENAME);
70+
Files.deleteIfExists(featureFlagsFile.toPath());
6471
}
6572

6673
@Test
@@ -122,20 +129,15 @@ public void enableDisableFeature() throws IOException {
122129
featureFlags.isEnabled(FeatureFlags.EXAMPLE));
123130
Assert.assertEquals(
124131
"# Example feature. Will be removed once the first real feature flag is added\ncom.vaadin.experimental.exampleFeatureFlag=true\n",
125-
FileUtils.readFileToString(
126-
new File(propertiesDir,
127-
FeatureFlags.PROPERTIES_FILENAME),
132+
FileUtils.readFileToString(featureFlagsFile,
128133
StandardCharsets.UTF_8));
129134

130135
featureFlags.setEnabled(FeatureFlags.EXAMPLE.getId(), false);
131136
Assert.assertFalse("Feature should have been disabled",
132137
featureFlags.isEnabled(FeatureFlags.EXAMPLE));
133138
Assert.assertEquals(
134139
"Feature flags file should be empty when no features are enabled",
135-
"",
136-
FileUtils.readFileToString(
137-
new File(propertiesDir,
138-
FeatureFlags.PROPERTIES_FILENAME),
140+
"", FileUtils.readFileToString(featureFlagsFile,
139141
StandardCharsets.UTF_8));
140142
}
141143

@@ -202,10 +204,15 @@ public void featureFlagShouldBeOverridableWithSystemProperty()
202204

203205
try {
204206
System.setProperty(propertyName, "true");
205-
createFeatureFlagsFile(String
206-
.format("com.vaadin.experimental.%s=false\n", feature));
207+
String fileContents = String
208+
.format("com.vaadin.experimental.%s=false\n", feature);
209+
createFeatureFlagsFile(fileContents);
207210
featureFlags.loadProperties();
208211
Assert.assertTrue(featureFlags.isEnabled(FeatureFlags.EXAMPLE));
212+
Assert.assertEquals(
213+
"Feature flags file should not be overwritten by system property value",
214+
fileContents, FileUtils.readFileToString(featureFlagsFile,
215+
StandardCharsets.UTF_8));
209216
} finally {
210217
if (previousValue == null) {
211218
System.clearProperty(propertyName);
@@ -215,6 +222,92 @@ public void featureFlagShouldBeOverridableWithSystemProperty()
215222
}
216223
}
217224

225+
@Test
226+
public void featureFlagLoadedByResourceProviderShouldBeOverridableWithSystemProperty()
227+
throws IOException {
228+
var feature = "exampleFeatureFlag";
229+
var propertyName = FeatureFlags.SYSTEM_PROPERTY_PREFIX + feature;
230+
var previousValue = System.getProperty(propertyName);
231+
232+
File flagsFile = new File(propertiesDir,
233+
"another-" + PROPERTIES_FILENAME);
234+
235+
ResourceProvider resourceProvider = Mockito
236+
.mock(ResourceProvider.class);
237+
Mockito.when(
238+
resourceProvider.getApplicationResource(PROPERTIES_FILENAME))
239+
.thenReturn(flagsFile.toURI().toURL());
240+
Lookup lookup = context.getAttribute(Lookup.class);
241+
Mockito.when(lookup.lookup(ResourceProvider.class))
242+
.thenReturn(resourceProvider);
243+
244+
try {
245+
String fileContents = String
246+
.format("com.vaadin.experimental.%s=false\n", feature);
247+
FileUtils.write(flagsFile, fileContents, StandardCharsets.UTF_8);
248+
249+
System.setProperty(propertyName, "true");
250+
featureFlags.loadProperties();
251+
Assert.assertTrue(featureFlags.isEnabled(FeatureFlags.EXAMPLE));
252+
Assert.assertFalse(
253+
"Setting feature flag by system properties should not create feature flag file",
254+
featureFlagsFile.exists());
255+
} finally {
256+
if (previousValue == null) {
257+
System.clearProperty(propertyName);
258+
} else {
259+
System.setProperty(propertyName, previousValue);
260+
}
261+
}
262+
}
263+
264+
@Test
265+
public void noFeatureFlagFile_systemPropertyProvided_featureEnabled()
266+
throws IOException {
267+
var feature = "exampleFeatureFlag";
268+
var propertyName = FeatureFlags.SYSTEM_PROPERTY_PREFIX + feature;
269+
var previousValue = System.getProperty(propertyName);
270+
271+
try {
272+
System.setProperty(propertyName, "true");
273+
featureFlags.loadProperties();
274+
Assert.assertTrue(
275+
"Feature set with system property should be enabled",
276+
featureFlags.isEnabled(FeatureFlags.EXAMPLE));
277+
Assert.assertFalse(
278+
"Setting feature flag by system properties should not create feature flag file",
279+
featureFlagsFile.exists());
280+
} finally {
281+
if (previousValue == null) {
282+
System.clearProperty(propertyName);
283+
} else {
284+
System.setProperty(propertyName, previousValue);
285+
}
286+
}
287+
}
288+
289+
@Test
290+
public void noFeatureFlagFile_noSystemPropertyProvided_allFeatureDisabled()
291+
throws IOException {
292+
var feature = "exampleFeatureFlag";
293+
var propertyName = FeatureFlags.SYSTEM_PROPERTY_PREFIX + feature;
294+
var previousValue = System.getProperty(propertyName);
295+
296+
try {
297+
if (previousValue != null) {
298+
System.clearProperty(propertyName);
299+
}
300+
featureFlags.loadProperties();
301+
Assert.assertFalse(
302+
"Feature not set with system property should be disabled by default",
303+
featureFlags.isEnabled(FeatureFlags.EXAMPLE));
304+
} finally {
305+
if (previousValue != null) {
306+
System.setProperty(propertyName, previousValue);
307+
}
308+
}
309+
}
310+
218311
private boolean hasUsageStatsEntry(String name) {
219312
return UsageStatistics.getEntries()
220313
.filter(entry -> entry.getName().equals(name)).findFirst()
@@ -238,8 +331,6 @@ private void mockResourcesLocation() {
238331
}
239332

240333
private void createFeatureFlagsFile(String data) throws IOException {
241-
FileUtils.write(
242-
new File(propertiesDir, FeatureFlags.PROPERTIES_FILENAME), data,
243-
StandardCharsets.UTF_8);
334+
FileUtils.write(featureFlagsFile, data, StandardCharsets.UTF_8);
244335
}
245336
}

0 commit comments

Comments
 (0)