1515 */
1616package com .vaadin .experimental ;
1717
18+ import javax .annotation .concurrent .NotThreadSafe ;
1819import java .io .File ;
1920import java .io .IOException ;
2021import java .nio .charset .StandardCharsets ;
2122import 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-
3124import org .apache .commons .io .FileUtils ;
3225import org .junit .Assert ;
3326import org .junit .Before ;
3629import org .junit .rules .TemporaryFolder ;
3730import 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
4043public 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\n com.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