@Test public void testCommandSwitchesFormat() { SolarisEntry.Builder bldr = new SolarisEntry.Builder("foo"); Map<NativeAttribute, String> switches = new HashMap<NativeAttribute, String>(); for (NativeAttribute attr : NativeAttribute.values()) { Object attrValue = VALUE_MARKER + attr.getName(); if (attr.equals(NativeAttribute.PWSTAT)) { attrValue = true; } bldr.addAttr(attr, attrValue); switches.put(attr, COMMAND_LINE_SWITCH_MARKER + attr.getName()); } SolarisEntry entry = bldr.build(); String generatedCommandLineSwitches = CommandSwitches.formatCommandSwitches(entry, getConnection(), switches); for (NativeAttribute attr : NativeAttribute.values()) { String regexp = null; String attrName = attr.getName(); switch (attr) { case LOCK: case PWSTAT: regexp = COMMAND_LINE_SWITCH_MARKER + attrName + "([\\s][^\"]|[\\s]*$|[\\s]+[" + COMMAND_LINE_SWITCH_MARKER + "])"; break; default: regexp = COMMAND_LINE_SWITCH_MARKER + attrName + "[\\s]+\"" + VALUE_MARKER + attrName + "\""; break; } String msg = String.format( "Invalid command line formatter: output: <%s> doesn't match regexp: <%s>", generatedCommandLineSwitches, regexp); Pattern p = Pattern.compile(regexp); Matcher m = p.matcher(generatedCommandLineSwitches); assertTrue(m.find(), msg); } }
public static String formatCommandSwitches(SolarisEntry entry, SolarisConnection conn, Map<NativeAttribute, String> switches) { StringBuilder buffer = new StringBuilder(); for (Attribute attr : entry.getAttributeSet()) { NativeAttribute nAttrName = NativeAttribute.forAttributeName(attr.getName()); List<Object> values = attr.getValue(); if (nAttrName.isSingleValue()) { if (values == null) { throw new ConnectorException(String.format( "Attribute '%s' has a null value, expecting singleValue", attr .getName())); } Object value = AttributeUtil.getSingleValue(attr); if (value == null || StringUtil.isBlank(value.toString()) || values.isEmpty()) { if (nAttrName == NativeAttribute.USER_INACTIVE) { values = CollectionUtil.newList((Object) "-1"); } else if (PASS_NULL_PARAMS.contains(nAttrName)) { values = CollectionUtil.newList((Object) ""); } } } else { if (values == null) { values = CollectionUtil.newList(); } } String cmdSwitchForAttr = switches.get(nAttrName); if (cmdSwitchForAttr == null) { continue; } switch (nAttrName) { case LOCK: buffer.append(cmdSwitchForAttr).append(" "); break; case PWSTAT: boolean isPasswordForceChange = (Boolean) values.get(0); if (isPasswordForceChange) { buffer.append(cmdSwitchForAttr).append(" "); } else { throw new ConnectorException( "Solaris allows to set 'force_change' attribute only to 'true' value. Anything else is invalid and will be ignored."); } break; case USER_EXPIRE: buffer.append(cmdSwitchForAttr).append(" "); buffer.append("\""); if (values == null || values.isEmpty() || values.get(0) == null) { } else { Long value = AttrUtil.toLong(values.get(0)); if (value != null) { buffer.append(conn.getModeDriver().formatDate(value)); } } buffer.append("\" "); break; default: buffer.append(cmdSwitchForAttr).append(" "); buffer.append("\""); boolean first = true; for (Object itValue : values) { if (!first) { buffer.append(","); } buffer.append(itValue.toString()); first = false; } buffer.append("\" "); break; } } return buffer.toString().trim(); }
CommandSwitches { public static String formatCommandSwitches(SolarisEntry entry, SolarisConnection conn, Map<NativeAttribute, String> switches) { StringBuilder buffer = new StringBuilder(); for (Attribute attr : entry.getAttributeSet()) { NativeAttribute nAttrName = NativeAttribute.forAttributeName(attr.getName()); List<Object> values = attr.getValue(); if (nAttrName.isSingleValue()) { if (values == null) { throw new ConnectorException(String.format( "Attribute '%s' has a null value, expecting singleValue", attr .getName())); } Object value = AttributeUtil.getSingleValue(attr); if (value == null || StringUtil.isBlank(value.toString()) || values.isEmpty()) { if (nAttrName == NativeAttribute.USER_INACTIVE) { values = CollectionUtil.newList((Object) "-1"); } else if (PASS_NULL_PARAMS.contains(nAttrName)) { values = CollectionUtil.newList((Object) ""); } } } else { if (values == null) { values = CollectionUtil.newList(); } } String cmdSwitchForAttr = switches.get(nAttrName); if (cmdSwitchForAttr == null) { continue; } switch (nAttrName) { case LOCK: buffer.append(cmdSwitchForAttr).append(" "); break; case PWSTAT: boolean isPasswordForceChange = (Boolean) values.get(0); if (isPasswordForceChange) { buffer.append(cmdSwitchForAttr).append(" "); } else { throw new ConnectorException( "Solaris allows to set 'force_change' attribute only to 'true' value. Anything else is invalid and will be ignored."); } break; case USER_EXPIRE: buffer.append(cmdSwitchForAttr).append(" "); buffer.append("\""); if (values == null || values.isEmpty() || values.get(0) == null) { } else { Long value = AttrUtil.toLong(values.get(0)); if (value != null) { buffer.append(conn.getModeDriver().formatDate(value)); } } buffer.append("\" "); break; default: buffer.append(cmdSwitchForAttr).append(" "); buffer.append("\""); boolean first = true; for (Object itValue : values) { if (!first) { buffer.append(","); } buffer.append(itValue.toString()); first = false; } buffer.append("\" "); break; } } return buffer.toString().trim(); } }
CommandSwitches { public static String formatCommandSwitches(SolarisEntry entry, SolarisConnection conn, Map<NativeAttribute, String> switches) { StringBuilder buffer = new StringBuilder(); for (Attribute attr : entry.getAttributeSet()) { NativeAttribute nAttrName = NativeAttribute.forAttributeName(attr.getName()); List<Object> values = attr.getValue(); if (nAttrName.isSingleValue()) { if (values == null) { throw new ConnectorException(String.format( "Attribute '%s' has a null value, expecting singleValue", attr .getName())); } Object value = AttributeUtil.getSingleValue(attr); if (value == null || StringUtil.isBlank(value.toString()) || values.isEmpty()) { if (nAttrName == NativeAttribute.USER_INACTIVE) { values = CollectionUtil.newList((Object) "-1"); } else if (PASS_NULL_PARAMS.contains(nAttrName)) { values = CollectionUtil.newList((Object) ""); } } } else { if (values == null) { values = CollectionUtil.newList(); } } String cmdSwitchForAttr = switches.get(nAttrName); if (cmdSwitchForAttr == null) { continue; } switch (nAttrName) { case LOCK: buffer.append(cmdSwitchForAttr).append(" "); break; case PWSTAT: boolean isPasswordForceChange = (Boolean) values.get(0); if (isPasswordForceChange) { buffer.append(cmdSwitchForAttr).append(" "); } else { throw new ConnectorException( "Solaris allows to set 'force_change' attribute only to 'true' value. Anything else is invalid and will be ignored."); } break; case USER_EXPIRE: buffer.append(cmdSwitchForAttr).append(" "); buffer.append("\""); if (values == null || values.isEmpty() || values.get(0) == null) { } else { Long value = AttrUtil.toLong(values.get(0)); if (value != null) { buffer.append(conn.getModeDriver().formatDate(value)); } } buffer.append("\" "); break; default: buffer.append(cmdSwitchForAttr).append(" "); buffer.append("\""); boolean first = true; for (Object itValue : values) { if (!first) { buffer.append(","); } buffer.append(itValue.toString()); first = false; } buffer.append("\" "); break; } } return buffer.toString().trim(); }  }
CommandSwitches { public static String formatCommandSwitches(SolarisEntry entry, SolarisConnection conn, Map<NativeAttribute, String> switches) { StringBuilder buffer = new StringBuilder(); for (Attribute attr : entry.getAttributeSet()) { NativeAttribute nAttrName = NativeAttribute.forAttributeName(attr.getName()); List<Object> values = attr.getValue(); if (nAttrName.isSingleValue()) { if (values == null) { throw new ConnectorException(String.format( "Attribute '%s' has a null value, expecting singleValue", attr .getName())); } Object value = AttributeUtil.getSingleValue(attr); if (value == null || StringUtil.isBlank(value.toString()) || values.isEmpty()) { if (nAttrName == NativeAttribute.USER_INACTIVE) { values = CollectionUtil.newList((Object) "-1"); } else if (PASS_NULL_PARAMS.contains(nAttrName)) { values = CollectionUtil.newList((Object) ""); } } } else { if (values == null) { values = CollectionUtil.newList(); } } String cmdSwitchForAttr = switches.get(nAttrName); if (cmdSwitchForAttr == null) { continue; } switch (nAttrName) { case LOCK: buffer.append(cmdSwitchForAttr).append(" "); break; case PWSTAT: boolean isPasswordForceChange = (Boolean) values.get(0); if (isPasswordForceChange) { buffer.append(cmdSwitchForAttr).append(" "); } else { throw new ConnectorException( "Solaris allows to set 'force_change' attribute only to 'true' value. Anything else is invalid and will be ignored."); } break; case USER_EXPIRE: buffer.append(cmdSwitchForAttr).append(" "); buffer.append("\""); if (values == null || values.isEmpty() || values.get(0) == null) { } else { Long value = AttrUtil.toLong(values.get(0)); if (value != null) { buffer.append(conn.getModeDriver().formatDate(value)); } } buffer.append("\" "); break; default: buffer.append(cmdSwitchForAttr).append(" "); buffer.append("\""); boolean first = true; for (Object itValue : values) { if (!first) { buffer.append(","); } buffer.append(itValue.toString()); first = false; } buffer.append("\" "); break; } } return buffer.toString().trim(); }  static String formatCommandSwitches(SolarisEntry entry, SolarisConnection conn,
            Map<NativeAttribute, String> switches); }
CommandSwitches { public static String formatCommandSwitches(SolarisEntry entry, SolarisConnection conn, Map<NativeAttribute, String> switches) { StringBuilder buffer = new StringBuilder(); for (Attribute attr : entry.getAttributeSet()) { NativeAttribute nAttrName = NativeAttribute.forAttributeName(attr.getName()); List<Object> values = attr.getValue(); if (nAttrName.isSingleValue()) { if (values == null) { throw new ConnectorException(String.format( "Attribute '%s' has a null value, expecting singleValue", attr .getName())); } Object value = AttributeUtil.getSingleValue(attr); if (value == null || StringUtil.isBlank(value.toString()) || values.isEmpty()) { if (nAttrName == NativeAttribute.USER_INACTIVE) { values = CollectionUtil.newList((Object) "-1"); } else if (PASS_NULL_PARAMS.contains(nAttrName)) { values = CollectionUtil.newList((Object) ""); } } } else { if (values == null) { values = CollectionUtil.newList(); } } String cmdSwitchForAttr = switches.get(nAttrName); if (cmdSwitchForAttr == null) { continue; } switch (nAttrName) { case LOCK: buffer.append(cmdSwitchForAttr).append(" "); break; case PWSTAT: boolean isPasswordForceChange = (Boolean) values.get(0); if (isPasswordForceChange) { buffer.append(cmdSwitchForAttr).append(" "); } else { throw new ConnectorException( "Solaris allows to set 'force_change' attribute only to 'true' value. Anything else is invalid and will be ignored."); } break; case USER_EXPIRE: buffer.append(cmdSwitchForAttr).append(" "); buffer.append("\""); if (values == null || values.isEmpty() || values.get(0) == null) { } else { Long value = AttrUtil.toLong(values.get(0)); if (value != null) { buffer.append(conn.getModeDriver().formatDate(value)); } } buffer.append("\" "); break; default: buffer.append(cmdSwitchForAttr).append(" "); buffer.append("\""); boolean first = true; for (Object itValue : values) { if (!first) { buffer.append(","); } buffer.append(itValue.toString()); first = false; } buffer.append("\" "); break; } } return buffer.toString().trim(); }  static String formatCommandSwitches(SolarisEntry entry, SolarisConnection conn,
            Map<NativeAttribute, String> switches);  }