View Javadoc
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.aspects.aj;
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 Mnemos}.
38   *
39   * @since 0.0.0
40   */
41  final class MnemosTest {
42  
43      /**
44       * The representation of a empty array.
45       */
46      private static final transient String EMPTY_ARRAY = "[]";
47  
48      @Test
49      void buildsTextFromObject() {
50          final Object[][] pairs = {
51              new Object[] {1, "1"},
52              new Object[] {1.43f, "1.43"},
53              new Object[] {"\u20ac-plain", "'\u20ac-plain'"},
54              new Object[] {"test ", "'test '"},
55              new Object[] {null, "NULL"},
56              new Object[] {new String[0], MnemosTest.EMPTY_ARRAY},
57              new Object[] {new String[] {"abc", "x"}, "['abc', 'x']"},
58              new Object[] {new Object[] {null, 5}, "[NULL, 5]"},
59          };
60          this.validateText(pairs);
61      }
62  
63      @Test
64      void handlesToxicObjectsGracefully() {
65          MatcherAssert.assertThat(
66              Mnemos.toText(
67                  new Object() {
68                      @Override
69                      public String toString() {
70                          throw new IllegalArgumentException("boom");
71                      }
72                  }, true, false
73              ),
74              Matchers.equalTo(
75                  // @checkstyle LineLength (1 line)
76                  "[com.jcabi.aspects.aj.MnemosTest$1 thrown java.lang.IllegalArgumentException(boom)]"
77              )
78          );
79      }
80  
81      @Test
82      void buildsTextFromIntArray() {
83          final Object[][] pairs = {
84              new Object[] {new int[0], MnemosTest.EMPTY_ARRAY},
85              new Object[] {new int[] {1}, "[1]"},
86              new Object[] {new int[] {1, 2, 3}, "[1, 2, 3]"},
87          };
88          this.validateText(pairs);
89      }
90  
91      @Test
92      void buildsTextFromLongArray() {
93          final Object[][] pairs = {
94              new Object[] {new long[0], MnemosTest.EMPTY_ARRAY},
95              new Object[] {new long[] {2L}, "[2]"},
96              new Object[] {new long[] {2L, 3L, 4L}, "[2, 3, 4]"},
97          };
98          this.validateText(pairs);
99      }
100 
101     @Test
102     void buildsTextFromFloatArray() {
103         final Object[][] pairs = {
104             new Object[] {new float[0], MnemosTest.EMPTY_ARRAY},
105             new Object[] {new float[] {1.01f}, "[1.01]"},
106             new Object[] {
107                 new float[] {1.01f, 2.02f, 3.03f},
108                 "[1.01, 2.02, 3.03]",
109             },
110         };
111         this.validateText(pairs);
112     }
113 
114     @Test
115     void buildsTextFromDoubleArray() {
116         final Object[][] pairs = {
117             new Object[] {new double[0], MnemosTest.EMPTY_ARRAY},
118             new Object[] {new double[] {2.01}, "[2.01]"},
119             new Object[] {
120                 new double[] {2.01, 2.02, 2.03},
121                 "[2.01, 2.02, 2.03]",
122             },
123         };
124         this.validateText(pairs);
125     }
126 
127     @Test
128     void buildsTextFromCharArray() {
129         final Object[][] pairs = {
130             new Object[] {new char[0], MnemosTest.EMPTY_ARRAY},
131             new Object[] {new char[] {'a'}, "[a]"},
132             new Object[] {new char[] {'a', 'b', 'c'}, "[a, b, c]"},
133         };
134         this.validateText(pairs);
135     }
136 
137     @Test
138     void buildsTextFromBooleanArray() {
139         final Object[][] pairs = {
140             new Object[] {new boolean[0], MnemosTest.EMPTY_ARRAY},
141             new Object[] {new boolean[] {true}, "[true]"},
142             new Object[] {
143                 new boolean[] {true, false, false},
144                 "[true, false, false]",
145             },
146         };
147         this.validateText(pairs);
148     }
149 
150     /**
151      * Method that validates the text built from an object.
152      * @param pairs The object pairs to validate.
153      */
154     private void validateText(final Object[]... pairs) {
155         for (final Object[] pair : pairs) {
156             MatcherAssert.assertThat(
157                 Mnemos.toText(pair[0], false, false),
158                 Matchers.equalTo(pair[1].toString())
159             );
160         }
161     }
162 }