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 org.hamcrest.MatcherAssert;
33 import org.hamcrest.Matchers;
34 import org.junit.jupiter.api.Test;
35
36 /**
37 * Test case for {@link PreFormatter}.
38 * @since 0.1
39 */
40 final class PreFormatterTest {
41
42 /**
43 * PreFormatter can format simple texts.
44 */
45 @Test
46 void decoratesArguments() {
47 final PreFormatter pre = new PreFormatter(
48 "%[com.jcabi.log.DecorMocker]-5.2f and %1$+.6f",
49 1.0d
50 );
51 MatcherAssert.assertThat(
52 pre.getFormat(),
53 Matchers.equalTo("%-5.2f and %1$+.6f")
54 );
55 MatcherAssert.assertThat(
56 pre.getArguments()[0],
57 Matchers.instanceOf(DecorMocker.class)
58 );
59 }
60
61 /**
62 * PreFormatter can handle missed decors.
63 */
64 @Test
65 void formatsEvenWithMissedDecors() {
66 final PreFormatter pre =
67 new PreFormatter("ouch: %[missed]s", "test");
68 MatcherAssert.assertThat(
69 pre.getFormat(),
70 Matchers.equalTo("ouch: %s")
71 );
72 MatcherAssert.assertThat(
73 pre.getArguments()[0],
74 Matchers.instanceOf(String.class)
75 );
76 }
77
78 /**
79 * PreFormatter can handle directly provided decors.
80 */
81 @Test
82 void formatsWithDirectlyProvidedDecors() {
83 final DecorMocker decor = new DecorMocker("a");
84 final PreFormatter pre = new PreFormatter("test: %s", decor);
85 MatcherAssert.assertThat(
86 pre.getArguments()[0],
87 Matchers.equalTo(decor)
88 );
89 }
90
91 /**
92 * PreFormatter can handle new line specifier.
93 */
94 @Test
95 void handleNewLineSpecifier() {
96 final String fmt = "%s%n%s";
97 final Object[] args = {"new", "line"};
98 final PreFormatter pre = new PreFormatter(fmt, args);
99 MatcherAssert.assertThat(
100 pre.getFormat(),
101 Matchers.is(fmt)
102 );
103 MatcherAssert.assertThat(
104 pre.getArguments(),
105 Matchers.is(args)
106 );
107 }
108
109 /**
110 * PreFormatter can handle percent specifier.
111 */
112 @Test
113 void handlePercentSpecifier() {
114 final String fmt = "%s%%";
115 final Object[] args = {"percent: "};
116 final PreFormatter pre = new PreFormatter(fmt, args);
117 MatcherAssert.assertThat(
118 pre.getFormat(),
119 Matchers.is(fmt)
120 );
121 MatcherAssert.assertThat(
122 pre.getArguments(),
123 Matchers.is(args)
124 );
125 }
126
127 }