View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3.builder;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotEquals;
22  
23  import java.util.Objects;
24  import java.util.function.Supplier;
25  
26  import org.apache.commons.lang3.AbstractLangTest;
27  
28  /**
29   * Helps test this package.
30   */
31  class AbstractBuilderTest extends AbstractLangTest {
32  
33      /**
34       * Helps test reflection classes.
35       *
36       * @param string An input string.
37       * @return The given string or "<null>".
38       */
39      protected static String accessibleString(final String string) {
40          return isForceAccessible() ? string : "<null>";
41      }
42  
43      protected static void assertEqualsIfAccessible(final Object expected, final Object actual) {
44          assertEquals(isForceAccessible(), Objects.equals(expected, actual));
45      }
46  
47      protected static void assertFalseIfAccessible(final boolean test) {
48          assertNotEquals(isForceAccessible(), test);
49      }
50  
51      protected static void assertNotEqualsIfAccessible(final Object expected, final Object actual) {
52          assertNotEquals(isForceAccessible(), Objects.equals(expected, actual));
53      }
54  
55      protected static void assertTrueIfAccessible(final boolean test) {
56          assertEquals(isForceAccessible(), test);
57      }
58  
59      protected static void assertTrueIfAccessible(final boolean test, final Supplier<String> messageSupplier) {
60          assertEquals(isForceAccessible(), test, messageSupplier);
61      }
62  
63      /**
64       * Tests whether reflection should force accessibility, using {@link AbstractReflection#getForceAccessible()}.
65       *
66       * @return {@link AbstractReflection#getForceAccessible()}.
67       */
68      protected static boolean isForceAccessible() {
69          return AbstractReflection.getForceAccessible();
70      }
71  }