Search in sources :

Example 6 with PropertyString

use of org.ff4j.property.PropertyString in project ff4j by ff4j.

the class PropertyStoreJCacheTest method updateKOPropertyNotFound.

/**
 * TDD.
 */
@Test(expected = PropertyNotFoundException.class)
public void updateKOPropertyNotFound() {
    // When
    PropertyString ps = new PropertyString("does-not-exist");
    testedStore.updateProperty(ps);
    // Expected error
    Assert.fail();
}
Also used : PropertyString(org.ff4j.property.PropertyString) Test(org.junit.Test)

Example 7 with PropertyString

use of org.ff4j.property.PropertyString in project ff4j by ff4j.

the class XmlParser method parsePropertiesTag.

/**
 * Parse Properties.
 *
 * @param properties tag
 * @param uid
 *      current featureid
 * @return
 *      properties map
 */
private Map<String, Property<?>> parsePropertiesTag(Element propertiesTag) {
    Map<String, Property<?>> properties = new HashMap<String, Property<?>>();
    // <properties>
    NodeList lisOfProperties = propertiesTag.getElementsByTagName(PROPERTY_TAG);
    for (int k = 0; k < lisOfProperties.getLength(); k++) {
        // <property name='' value='' (type='') >
        Element propertyTag = (Element) lisOfProperties.item(k);
        NamedNodeMap attMap = propertyTag.getAttributes();
        if (attMap.getNamedItem(PROPERTY_PARAMNAME) == null) {
            throw new IllegalArgumentException("Invalid XML Syntax, 'name' is a required attribute of 'property' TAG");
        }
        if (attMap.getNamedItem(PROPERTY_PARAMVALUE) == null) {
            throw new IllegalArgumentException("Invalid XML Syntax, 'value' is a required attribute of 'property' TAG");
        }
        String name = attMap.getNamedItem(PROPERTY_PARAMNAME).getNodeValue();
        String value = attMap.getNamedItem(PROPERTY_PARAMVALUE).getNodeValue();
        Property<?> ap = new PropertyString(name, value);
        // If specific type defined ?
        if (null != attMap.getNamedItem(PROPERTY_PARAMTYPE)) {
            String optionalType = attMap.getNamedItem(PROPERTY_PARAMTYPE).getNodeValue();
            // Substitution if relevant (e.g. 'int' -> 'org.ff4j.property.PropertyInt')
            optionalType = MappingUtil.mapPropertyType(optionalType);
            try {
                // Constructor (String, String) is mandatory in Property interface
                Constructor<?> constr = Class.forName(optionalType).getConstructor(String.class, String.class);
                ap = (Property<?>) constr.newInstance(name, value);
            } catch (Exception e) {
                throw new IllegalArgumentException("Cannot instantiate '" + optionalType + "' check default constructor", e);
            }
        }
        if (null != attMap.getNamedItem(PROPERTY_PARAMDESCRIPTION)) {
            ap.setDescription(attMap.getNamedItem(PROPERTY_PARAMDESCRIPTION).getNodeValue());
        }
        // Is there any fixed Value ?
        NodeList listOfFixedValue = propertyTag.getElementsByTagName(PROPERTY_PARAMFIXED_VALUES);
        if (listOfFixedValue.getLength() != 0) {
            Element fixedValueTag = (Element) listOfFixedValue.item(0);
            NodeList listOfValues = fixedValueTag.getElementsByTagName(PROPERTY_PARAMVALUE);
            for (int l = 0; l < listOfValues.getLength(); l++) {
                Element valueTag = (Element) listOfValues.item(l);
                ap.add2FixedValueFromString(valueTag.getTextContent());
            }
        }
        // Check fixed value
        if (ap.getFixedValues() != null && !ap.getFixedValues().contains(ap.getValue())) {
            throw new IllegalArgumentException("Cannot create property <" + ap.getName() + "> invalid value <" + ap.getValue() + "> expected one of " + ap.getFixedValues());
        }
        properties.put(name, ap);
    }
    return properties;
}
Also used : PropertyString(org.ff4j.property.PropertyString) NamedNodeMap(org.w3c.dom.NamedNodeMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) PropertyString(org.ff4j.property.PropertyString) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Property(org.ff4j.property.Property)

Example 8 with PropertyString

use of org.ff4j.property.PropertyString in project ff4j by ff4j.

the class FF4jTest method testImportProperties.

@Test
public void testImportProperties() {
    FF4j ff4j = new FF4j();
    List<Property<?>> listOfProperties = new ArrayList<Property<?>>();
    listOfProperties.add(new PropertyString("p1", "v1"));
    ff4j.importProperties(listOfProperties);
    Assert.assertTrue(ff4j.getPropertiesStore().existProperty("p1"));
    // no Error
    ff4j.importProperties(null);
}
Also used : PropertyString(org.ff4j.property.PropertyString) FF4j(org.ff4j.FF4j) ArrayList(java.util.ArrayList) Property(org.ff4j.property.Property) Test(org.junit.Test)

Example 9 with PropertyString

use of org.ff4j.property.PropertyString in project ff4j by ff4j.

the class FeatureTest method testCopyConstructorFeature.

@Test
public void testCopyConstructorFeature() {
    Feature f = new Feature("abc", true, "samething", "groupA", Util.set("a", "b"));
    f.getPermissions().add("USER");
    f.setFlippingStrategy(new PonderationStrategy(0.5d));
    f.getCustomProperties().put("p1", new PropertyString("p1", "v1"));
    f.getCustomProperties().put("p2", new PropertyString("p1", "v1", Util.set("v1", "v2")));
    Feature f2 = new Feature(f);
    Assert.assertEquals(f2.getUid(), f.getUid());
    Assert.assertEquals(f2.getPermissions(), f.getPermissions());
    new Feature("f3", true, "samething", "groupA", Util.set("a", "b"), null);
    new Feature(new Feature("f4", true));
}
Also used : PonderationStrategy(org.ff4j.strategy.PonderationStrategy) PropertyString(org.ff4j.property.PropertyString) Feature(org.ff4j.core.Feature) Test(org.junit.Test)

Example 10 with PropertyString

use of org.ff4j.property.PropertyString in project ff4j by ff4j.

the class FeatureTest method testProperty.

@Test
public void testProperty() {
    Feature f = new Feature("f1");
    f.toggle();
    f.toggle();
    f.getCustomProperties().put("p1", new PropertyString("p1", "v1"));
    f.getProperty("p1");
}
Also used : PropertyString(org.ff4j.property.PropertyString) Feature(org.ff4j.core.Feature) Test(org.junit.Test)

Aggregations

PropertyString (org.ff4j.property.PropertyString)54 Test (org.junit.Test)52 Feature (org.ff4j.core.Feature)18 InMemoryCacheManager (org.ff4j.cache.InMemoryCacheManager)11 Property (org.ff4j.property.Property)6 HashSet (java.util.HashSet)5 Set (java.util.Set)4 InMemoryPropertyStore (org.ff4j.property.store.InMemoryPropertyStore)4 HashMap (java.util.HashMap)3 FF4j (org.ff4j.FF4j)3 PonderationStrategy (org.ff4j.strategy.PonderationStrategy)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 DataSource (javax.sql.DataSource)2 FF4jCacheProxy (org.ff4j.cache.FF4jCacheProxy)2 PropertyJsonBean (org.ff4j.property.util.PropertyJsonBean)2 InMemoryFeatureStore (org.ff4j.store.InMemoryFeatureStore)2 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1