1 /*
2 * Copyright (c) 2012-2024, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
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 * Test case for {@link TextDecor}.
48 *
49 * @since 0.1
50 * @checkstyle ParameterNumberCheck (500 lines)
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 * Test for a long text.
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 * Params for this parametrized test.
99 * @return Array of arrays of params for ctor
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 // @checkstyle MultipleStringLiterals (1 line)
111 {"simple text", "simple text", 0, 0, 0},
112 {null, "NULL", 0, 0, 0},
113 // @checkstyle MultipleStringLiteralsCheck (1 line)
114 {"\u0433!", "\u0433!", 0, 0, 0},
115 }
116 );
117 }
118 }