Skip to content

Commit

Permalink
closes #236
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Apr 29, 2013
1 parent b694885 commit 998877b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/main/java/com/jcabi/log/TextDecor.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class TextDecor implements Formattable {
/**
* Maximum length to show.
*/
private static final int MAX = 100;
public static final int MAX = 100;

/**
* The object.
Expand Down Expand Up @@ -94,12 +94,17 @@ private static String pretty(final String text) {
if (text.length() < TextDecor.MAX) {
result = text;
} else {
result = String.format(
"%s...%s (%d chars)",
text.substring(0, (TextDecor.MAX / 2) - 2),
text.substring(text.length() - (TextDecor.MAX / 2) + 1),
text.length()
final int skip = text.length() - TextDecor.MAX;
final StringBuilder output = new StringBuilder()
.append(text.substring(0, (text.length() - skip) / 2))
// @checkstyle MultipleStringLiterals (1 line)
.append("..")
.append(skip)
.append("..");
output.append(
text.substring(text.length() - TextDecor.MAX + output.length())
);
result = output.toString();
}
return StringEscapeUtils.escapeJava(result);
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/jcabi/log/TextDecorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Formattable;
import java.util.Formatter;
import org.apache.commons.lang3.StringUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
Expand Down Expand Up @@ -75,6 +80,25 @@ public static Collection<Object[]> params() {
);
}

/**
* Test for a long text.
*/
@Test
public void compressesLongText() {
final int len = 1000;
final String text = StringUtils.repeat('x', len);
final Formattable fmt = new TextDecor(text);
final StringBuilder output = new StringBuilder();
fmt.formatTo(new Formatter(output), 0, 0, 0);
MatcherAssert.assertThat(
output.toString().length(),
Matchers.describedAs(
output.toString(),
Matchers.equalTo(TextDecor.MAX)
)
);
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 998877b

Please sign in to comment.