1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.log;
31
32 import java.nio.charset.Charset;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.Formattable;
36 import java.util.Formatter;
37 import java.util.Locale;
38 import org.apache.commons.lang3.StringUtils;
39 import org.hamcrest.MatcherAssert;
40 import org.hamcrest.Matchers;
41 import org.junit.jupiter.api.Assumptions;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.params.ParameterizedTest;
44 import org.junit.jupiter.params.provider.MethodSource;
45
46
47
48
49
50
51
52 final class TextDecorTest {
53
54 @ParameterizedTest
55 @MethodSource("params")
56 void testPrintsRight(final String obj, final String text,
57 final int flags, final int width, final int precision) {
58 Assumptions.assumeTrue("UTF-8".equals(Charset.defaultCharset().name()));
59 Locale.setDefault(Locale.US);
60 MatcherAssert.assertThat(
61 new Printed(new TextDecor(obj), flags, width, precision),
62 Matchers.hasToString(text)
63 );
64 }
65
66 @ParameterizedTest
67 @MethodSource("params")
68 void testLogsRight(final String obj, final String text,
69 final int flags, final int width, final int precision) {
70 Assumptions.assumeTrue("UTF-8".equals(Charset.defaultCharset().name()));
71 Locale.setDefault(Locale.US);
72 MatcherAssert.assertThat(
73 new Logged(new TextDecor(obj), flags, width, precision),
74 Matchers.hasToString(text)
75 );
76 }
77
78
79
80
81 @Test
82 void compressesLongText() {
83 final int len = 1000;
84 final String text = StringUtils.repeat('x', len);
85 final Formattable fmt = new TextDecor(text);
86 final StringBuilder output = new StringBuilder(100);
87 fmt.formatTo(new Formatter(output), 0, 0, 0);
88 MatcherAssert.assertThat(
89 output.length(),
90 Matchers.describedAs(
91 output.toString(),
92 Matchers.equalTo(TextDecor.MAX)
93 )
94 );
95 }
96
97
98
99
100
101 @SuppressWarnings(
102 {
103 "PMD.AvoidDuplicateLiterals",
104 "PMD.UnusedPrivateMethod"
105 }
106 )
107 private static Collection<Object[]> params() {
108 return Arrays.asList(
109 new Object[][] {
110
111 {"simple text", "simple text", 0, 0, 0},
112 {null, "NULL", 0, 0, 0},
113
114 {"\u0433!", "\u0433!", 0, 0, 0},
115 }
116 );
117 }
118 }