EndpointsToolAction extends Action { @VisibleForTesting static List<String> wrap(String source, int maxWidth, int indent) { Iterable<String> words = Splitter.on(" ").split(source); ImmutableList.Builder<String> wrappedLines = ImmutableList.builder(); StringBuffer line = new StringBuffer(Strings.repeat(" ", indent)); int lineLength = indent; for (String word : words) { if ((lineLength + word.length()) >= maxWidth) { wrappedLines.add(line.toString()); line = new StringBuffer(Strings.repeat(" ", indent)); lineLength = indent; } if (lineLength > indent && lineLength < maxWidth) { line.append(" "); lineLength++; } line.append(word); lineLength += (word.length()); } wrappedLines.add(line.toString()); return wrappedLines.build(); } EndpointsToolAction(String name); abstract String getUsageString(); abstract boolean execute(); @Override void apply(); boolean isHelpDisplayNeeded(); void setHelpDisplayNeeded(boolean helpDisplayNeeded); String getExampleString(); void setExampleString(String exampleString); static final String OPTION_CLASS_PATH_SHORT; static final String OPTION_CLASS_PATH_LONG; static final String OPTION_OUTPUT_DIR_SHORT; static final String OPTION_OUTPUT_DIR_LONG; static final String OPTION_WAR_SHORT; static final String OPTION_WAR_LONG; static final String OPTION_LANGUAGE_SHORT; static final String OPTION_LANGUAGE_LONG; static final String OPTION_BUILD_SYSTEM_SHORT; static final String OPTION_BUILD_SYSTEM_LONG; static final String OPTION_FORMAT_SHORT; static final String OPTION_FORMAT_LONG; static final String OPTION_DEBUG; static final String OPTION_HOSTNAME_SHORT; static final String OPTION_HOSTNAME_LONG; static final String OPTION_BASE_PATH_SHORT; static final String OPTION_BASE_PATH_LONG; }
@Test public void testWrap() { String source = "This is a sample option description to test static method wrap in " + "EndpointsToolAction.java"; List<String> lines = EndpointsToolAction.wrap(source, 20, 2); assertEquals(5, lines.size()); assertEquals(" This is a sample", lines.get(0)); assertEquals(" option description", lines.get(1)); assertEquals(" to test static", lines.get(2)); assertEquals(" method wrap in", lines.get(3)); assertEquals(" EndpointsToolAction.java", lines.get(4)); }