#354 Added Configuration Based Example of Feature Toggle

This commit is contained in:
Joseph McCarthy 2016-01-26 21:08:28 +00:00
parent d627b7af6b
commit ba1d3a0fbf
4 changed files with 61 additions and 2 deletions

View File

@ -0,0 +1,27 @@
package com.iluwatar.featuretoggle.pattern.propertiesversion;
import com.iluwatar.featuretoggle.pattern.Service;
import com.iluwatar.featuretoggle.user.User;
import java.util.Properties;
public class PropertiesFeatureToggleVersion implements Service {
private Properties properties;
public PropertiesFeatureToggleVersion(final Properties properties) {
this.properties = properties;
}
@Override
public String getWelcomeMessage(final User user) {
final boolean enhancedWelcome = (boolean) properties.get("enhancedWelcome");
if (enhancedWelcome) {
return "Welcome " + user + ". You're using the enhanced welcome message.";
}
return "Welcome to the application.";
}
}

View File

@ -9,7 +9,7 @@ public class TieredFeatureToggleVersion implements Service {
@Override
public String getWelcomeMessage(User user) {
if (UserGroup.isPaid(user)) {
return "You're amazing " + user.getName() + ". Thanks for paying for this awesome software.";
return "You're amazing " + user + ". Thanks for paying for this awesome software.";
}
return "I suppose you can use this software.";

View File

@ -8,7 +8,8 @@ public class User {
this.name = name;
}
public String getName() {
@Override
public String toString() {
return name;
}
}

View File

@ -0,0 +1,31 @@
package com.iluwatar.featuretoggle.pattern.propertiesversion;
import com.iluwatar.featuretoggle.pattern.Service;
import com.iluwatar.featuretoggle.user.User;
import org.junit.Test;
import java.util.Properties;
import static org.junit.Assert.assertEquals;
public class PropertiesFeatureToggleVersionTest {
@Test
public void testFeatureTurnedOn() throws Exception {
final Properties properties = new Properties();
properties.put("enhancedWelcome",true);
Service service = new PropertiesFeatureToggleVersion(properties);
final String welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code"));
assertEquals("Welcome Jamie No Code. You're using the enhanced welcome message.",welcomeMessage);
}
@Test
public void testFeatureTurnedOff() throws Exception {
final Properties properties = new Properties();
properties.put("enhancedWelcome",false);
Service service = new PropertiesFeatureToggleVersion(properties);
final String welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code"));
assertEquals("Welcome to the application.",welcomeMessage);
}
}