Skip to content

Commit 4a49f82

Browse files
committed
iluwatar#354 add general boolean method to services for feature status. Change user.toString
1 parent ba1d3a0 commit 4a49f82

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

feature-toggle/src/main/java/com/iluwatar/featuretoggle/pattern/Service.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ public interface Service {
77

88
String getWelcomeMessage(User user);
99

10+
boolean isEnhanced();
11+
1012
}

feature-toggle/src/main/java/com/iluwatar/featuretoggle/pattern/propertiesversion/PropertiesFeatureToggleVersion.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,36 @@
77

88
public class PropertiesFeatureToggleVersion implements Service {
99

10-
private Properties properties;
10+
private boolean isEnhanced;
1111

12+
/**
13+
*
14+
* @param properties {@link Properties} used to configure the service and toggle features.
15+
*/
1216
public PropertiesFeatureToggleVersion(final Properties properties) {
13-
this.properties = properties;
17+
if (properties == null) {
18+
throw new IllegalArgumentException("No Properties Provided.");
19+
} else {
20+
try {
21+
isEnhanced = (boolean) properties.get("enhancedWelcome");
22+
} catch (Exception e) {
23+
throw new IllegalArgumentException("Invalid Enhancement Settings Provided.");
24+
}
25+
}
1426
}
1527

1628
@Override
1729
public String getWelcomeMessage(final User user) {
1830

19-
final boolean enhancedWelcome = (boolean) properties.get("enhancedWelcome");
20-
21-
if (enhancedWelcome) {
31+
if (isEnhanced()) {
2232
return "Welcome " + user + ". You're using the enhanced welcome message.";
2333
}
2434

2535
return "Welcome to the application.";
2636
}
37+
38+
@Override
39+
public boolean isEnhanced() {
40+
return isEnhanced;
41+
}
2742
}

feature-toggle/src/main/java/com/iluwatar/featuretoggle/pattern/tieredversion/TieredFeatureToggleVersion.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ public String getWelcomeMessage(User user) {
1515
return "I suppose you can use this software.";
1616
}
1717

18+
@Override
19+
public boolean isEnhanced() {
20+
return true;
21+
}
22+
1823
}

feature-toggle/src/test/java/com/iluwatar/featuretoggle/pattern/propertiesversion/PropertiesFeatureToggleVersionTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.Properties;
99

1010
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertFalse;
12+
import static org.junit.Assert.assertTrue;
1113

1214
public class PropertiesFeatureToggleVersionTest {
1315

@@ -16,6 +18,7 @@ public void testFeatureTurnedOn() throws Exception {
1618
final Properties properties = new Properties();
1719
properties.put("enhancedWelcome",true);
1820
Service service = new PropertiesFeatureToggleVersion(properties);
21+
assertTrue(service.isEnhanced());
1922
final String welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code"));
2023
assertEquals("Welcome Jamie No Code. You're using the enhanced welcome message.",welcomeMessage);
2124
}
@@ -25,6 +28,7 @@ public void testFeatureTurnedOff() throws Exception {
2528
final Properties properties = new Properties();
2629
properties.put("enhancedWelcome",false);
2730
Service service = new PropertiesFeatureToggleVersion(properties);
31+
assertFalse(service.isEnhanced());
2832
final String welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code"));
2933
assertEquals("Welcome to the application.",welcomeMessage);
3034
}

feature-toggle/src/test/java/com/iluwatar/featuretoggle/pattern/tieredversion/TieredFeatureToggleVersionTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.Test;
88

99
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertTrue;
1011

1112
public class TieredFeatureToggleVersionTest {
1213

@@ -33,4 +34,9 @@ public void testGetWelcomeMessageForFreeUser() throws Exception {
3334
final String expected = "I suppose you can use this software.";
3435
assertEquals(expected, welcomeMessage);
3536
}
37+
38+
@Test
39+
public void testIsEnhancedAlwaysTrueAsTiered() throws Exception {
40+
assertTrue(service.isEnhanced());
41+
}
3642
}

0 commit comments

Comments
 (0)