MsFlex extends AbstractCssProperty<MsFlex> implements StateChangeInformer<CssProperty> { @Override public MsFlex setCssValue(final String cssValue) { final String trimmedCssValue; if (cssValue == null) { throw new NullValueException( "null is an invalid value. The value format should be as for example \"0 1 auto\" or initial/inherit."); } else if ((trimmedCssValue = TagStringUtil .toLowerCase(StringUtil.strip(cssValue))).isEmpty()) { throw new NullValueException(cssValue + " is an invalid value. The value format should be as for example \"0 1 auto\" or initial/inherit."); } final String[] cssValueParts = StringUtil.splitBySpace(trimmedCssValue); if (cssValueParts.length > 1) { if (trimmedCssValue.contains(FlexBasis.INITIAL)) { throw new InvalidValueException( "The string 'initial' makes the given cssValue invalid, please remove 'initial' from it and try."); } if (trimmedCssValue.contains(FlexBasis.INHERIT)) { throw new InvalidValueException( "The string 'inherit' makes the given cssValue invalid, please remove 'inherit' from it and try."); } } else { if (PREDEFINED_CONSTANTS.contains(trimmedCssValue)) { if (flexGrow != null) { flexGrow.setAlreadyInUse(false); flexGrow = null; } if (flexShrink != null) { flexShrink.setAlreadyInUse(false); flexShrink = null; } if (flexBasis != null) { flexBasis.setAlreadyInUse(false); flexBasis = null; } this.cssValue = trimmedCssValue; if (getStateChangeInformer() != null) { getStateChangeInformer().stateChanged(this); } return this; } } FlexGrow flexGrow = null; FlexShrink flexShrink = null; FlexBasis flexBasis = null; for (final String eachPart : cssValueParts) { if (flexGrow == null && FlexGrow.isValid(eachPart)) { if (this.flexGrow == null) { flexGrow = new FlexGrow(eachPart); flexGrow.setStateChangeInformer(this); flexGrow.setAlreadyInUse(true); } else { this.flexGrow.setCssValue(eachPart); flexGrow = this.flexGrow; } } else if (flexShrink == null && FlexShrink.isValid(eachPart)) { if (this.flexGrow == null) { flexShrink = new FlexShrink(eachPart); flexShrink.setStateChangeInformer(this); flexShrink.setAlreadyInUse(true); } else { this.flexShrink.setCssValue(eachPart); flexShrink = this.flexShrink; } } else if (flexBasis == null && FlexBasis.isValid(eachPart)) { if (this.flexBasis == null) { flexBasis = new FlexBasis(eachPart); flexBasis.setStateChangeInformer(this); flexBasis.setAlreadyInUse(true); } else { this.flexBasis.setCssValue(eachPart); flexBasis = this.flexBasis; } } } final StringBuilder cssValueBuilder = new StringBuilder(); boolean invalid = true; if (flexGrow == null) { if (this.flexGrow == null) { flexGrow = new FlexGrow(1); flexGrow.setStateChangeInformer(this); flexGrow.setAlreadyInUse(true); } else { this.flexGrow.setValue(0); flexGrow = this.flexGrow; } } else { invalid = false; } cssValueBuilder.append(flexGrow.getCssValue()).append(' '); if (flexShrink == null) { if (this.flexShrink == null) { flexShrink = new FlexShrink(1); flexShrink.setStateChangeInformer(this); flexShrink.setAlreadyInUse(true); } else { this.flexShrink.setValue(1); flexShrink = this.flexShrink; } } else { invalid = false; } cssValueBuilder.append(flexShrink.getCssValue()).append(' '); if (flexBasis == null) { if (cssValueParts.length == 3) { throw new InvalidValueException( "the given cssValue contains invalid flex-basis value."); } if (this.flexBasis == null) { flexBasis = new FlexBasis(0); flexBasis.setStateChangeInformer(this); flexBasis.setAlreadyInUse(true); } else { this.flexBasis.setAsAuto(); flexBasis = this.flexBasis; } } else { invalid = false; } cssValueBuilder.append(flexBasis.getCssValue()); if (invalid) { throw new InvalidValueException(cssValue + " is an invalid value. The value format should be as for example \"0 1 auto\" or initial/inherit."); } this.cssValue = cssValueBuilder.toString(); flexGrow.setStateChangeInformer(this); flexGrow.setAlreadyInUse(true); flexShrink.setStateChangeInformer(this); flexShrink.setAlreadyInUse(true); flexBasis.setStateChangeInformer(this); flexBasis.setAlreadyInUse(true); this.flexGrow = flexGrow; this.flexShrink = flexShrink; this.flexBasis = flexBasis; if (getStateChangeInformer() != null) { getStateChangeInformer().stateChanged(this); } return this; } MsFlex();  MsFlex(final String cssValue);  MsFlex(final MsFlex msFlex); @Override String getCssName(); @Override String getCssValue(); @Override String toString(); @Override MsFlex setCssValue(final String cssValue); static boolean isValid(final String cssValue); void setAsInitial(); void setAsInherit(); void setAsAuto(); FlexBasis getFlexBasis(); FlexShrink getFlexShrink(); FlexGrow getFlexGrow(); MsFlex setFlexGrow(final FlexGrow flexGrow); MsFlex setFlexShrink(final FlexShrink flexShrink); MsFlex setFlexBasis(final FlexBasis flexBasis); @Override void stateChanged(final CssProperty stateChangedObject); static final String INITIAL; static final String INHERIT; static final String AUTO; }
@Test(expected = InvalidValueException.class) public void testSetCssValueStringInvalid() { final MsFlex flex = new MsFlex(); try { flex.setCssValue("2 5 25"); } catch (InvalidValueException e) { try { flex.setCssValue("fddf"); } catch (InvalidValueException e1) { throw e1; } } }