Add WhitespaceAround and WhitespaceAroundCheck
This commit is contained in:
parent
3d5c928656
commit
8e03321d14
|
@ -406,14 +406,10 @@ public class StringUtils {
|
||||||
if (isAnyEmpty(str, middle) || length >= str.length() || length < middle.length() + 2) {
|
if (isAnyEmpty(str, middle) || length >= str.length() || length < middle.length() + 2) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
final int targetSting = length - middle.length();
|
final int targetSting = length - middle.length();
|
||||||
final int startOffset = targetSting / 2 + targetSting % 2;
|
final int startOffset = targetSting / 2 + targetSting % 2;
|
||||||
final int endOffset = str.length() - targetSting / 2;
|
final int endOffset = str.length() - targetSting / 2;
|
||||||
|
return str.substring(0, startOffset) + middle + str.substring(endOffset);
|
||||||
return str.substring(0, startOffset) +
|
|
||||||
middle +
|
|
||||||
str.substring(endOffset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -51,7 +51,7 @@ import org.apache.commons.lang3.ClassUtils;
|
||||||
* <code>Person@7f54[ <br>
|
* <code>Person@7f54[ <br>
|
||||||
* name=Stephen, <br>
|
* name=Stephen, <br>
|
||||||
* age=29, <br>
|
* age=29, <br>
|
||||||
* smoker=false, <br>
|
* smokealse, <br>
|
||||||
* job=Job@43cd2[ <br>
|
* job=Job@43cd2[ <br>
|
||||||
* title=Manager <br>
|
* title=Manager <br>
|
||||||
* ] <br>
|
* ] <br>
|
||||||
|
|
|
@ -240,5 +240,7 @@ public class ComparableUtils {
|
||||||
return ObjectUtils.compare(comparable1, comparable2, true) < 0 ? comparable1 : comparable2;
|
return ObjectUtils.compare(comparable1, comparable2, true) < 0 ? comparable1 : comparable2;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ComparableUtils() {}
|
private ComparableUtils() {
|
||||||
|
// empty
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -770,8 +770,7 @@ public final class Fraction extends Number implements Comparable<Fraction> {
|
||||||
// make sure we don't overflow unless the result *must* overflow.
|
// make sure we don't overflow unless the result *must* overflow.
|
||||||
final int d1 = greatestCommonDivisor(numerator, fraction.denominator);
|
final int d1 = greatestCommonDivisor(numerator, fraction.denominator);
|
||||||
final int d2 = greatestCommonDivisor(fraction.numerator, denominator);
|
final int d2 = greatestCommonDivisor(fraction.numerator, denominator);
|
||||||
return getReducedFraction(mulAndCheck(numerator / d1, fraction.numerator / d2),
|
return getReducedFraction(mulAndCheck(numerator / d1, fraction.numerator / d2), mulPosAndCheck(denominator / d2, fraction.denominator / d1));
|
||||||
mulPosAndCheck(denominator / d2, fraction.denominator / d1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -257,8 +257,7 @@ public class ConstructorUtils {
|
||||||
parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
|
parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
|
||||||
final Constructor<T> ctor = getAccessibleConstructor(cls, parameterTypes);
|
final Constructor<T> ctor = getAccessibleConstructor(cls, parameterTypes);
|
||||||
if (ctor == null) {
|
if (ctor == null) {
|
||||||
throw new NoSuchMethodException(
|
throw new NoSuchMethodException("No such accessible constructor on object: " + cls.getName());
|
||||||
"No such accessible constructor on object: "+ cls.getName());
|
|
||||||
}
|
}
|
||||||
return ctor.newInstance(args);
|
return ctor.newInstance(args);
|
||||||
}
|
}
|
||||||
|
|
|
@ -307,7 +307,6 @@ public class Streams {
|
||||||
* Performs a mutable reduction operation on the elements of this FailableStream. A mutable reduction is one in which
|
* Performs a mutable reduction operation on the elements of this FailableStream. A mutable reduction is one in which
|
||||||
* the reduced value is a mutable result container, such as an {@link ArrayList}, and elements are incorporated by
|
* the reduced value is a mutable result container, such as an {@link ArrayList}, and elements are incorporated by
|
||||||
* updating the state of the result rather than by replacing the result. This produces a result equivalent to:
|
* updating the state of the result rather than by replacing the result. This produces a result equivalent to:
|
||||||
*
|
|
||||||
* <pre>
|
* <pre>
|
||||||
* {@code
|
* {@code
|
||||||
* R result = supplier.get();
|
* R result = supplier.get();
|
||||||
|
@ -316,29 +315,25 @@ public class Streams {
|
||||||
* return result;
|
* return result;
|
||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
|
||||||
* <p>
|
* <p>
|
||||||
* Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations can be parallelized without requiring
|
* Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations can be parallelized without requiring
|
||||||
* additional synchronization.
|
* additional synchronization.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
|
||||||
* <p>
|
* <p>
|
||||||
* This is a terminal operation.
|
* This is a terminal operation.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
* <p>
|
||||||
* Note There are many existing classes in the JDK whose signatures are well-suited for use with method references as
|
* Note There are many existing classes in the JDK whose signatures are well-suited for use with method references as
|
||||||
* arguments to {@code collect()}. For example, the following will accumulate strings into an {@link ArrayList}:
|
* arguments to {@code collect()}. For example, the following will accumulate strings into an {@link ArrayList}:
|
||||||
*
|
* </p>
|
||||||
* <pre>
|
* <pre>
|
||||||
* {@code
|
* {@code
|
||||||
* List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
|
* List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
|
||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
|
||||||
* <p>
|
* <p>
|
||||||
* The following will take a stream of strings and concatenates them into a single string:
|
* The following will take a stream of strings and concatenates them into a single string:
|
||||||
* </p>
|
* </p>
|
||||||
*
|
|
||||||
* <pre>
|
* <pre>
|
||||||
* {@code
|
* {@code
|
||||||
* String concat = stringStream.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString();
|
* String concat = stringStream.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString();
|
||||||
|
@ -362,7 +357,6 @@ public class Streams {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a FailableStream consisting of the elements of this stream that match the given FailablePredicate.
|
* Returns a FailableStream consisting of the elements of this stream that match the given FailablePredicate.
|
||||||
*
|
|
||||||
* <p>
|
* <p>
|
||||||
* This is an intermediate operation.
|
* This is an intermediate operation.
|
||||||
* </p>
|
* </p>
|
||||||
|
@ -379,11 +373,9 @@ public class Streams {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs an action for each element of this stream.
|
* Performs an action for each element of this stream.
|
||||||
*
|
|
||||||
* <p>
|
* <p>
|
||||||
* This is a terminal operation.
|
* This is a terminal operation.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
|
||||||
* <p>
|
* <p>
|
||||||
* The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does
|
* The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does
|
||||||
* <em>not</em> guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of
|
* <em>not</em> guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of
|
||||||
|
|
|
@ -15,11 +15,9 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
See the License for the specific language governing permissions and
|
See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<!DOCTYPE module PUBLIC
|
<!DOCTYPE module PUBLIC
|
||||||
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
|
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
|
||||||
"https://checkstyle.org/dtds/configuration_1_3.dtd">
|
"https://checkstyle.org/dtds/configuration_1_3.dtd">
|
||||||
|
|
||||||
<!-- Apache Commons Lang customization of default Checkstyle behavior -->
|
<!-- Apache Commons Lang customization of default Checkstyle behavior -->
|
||||||
<module name="Checker">
|
<module name="Checker">
|
||||||
<property name="localeLanguage" value="en"/>
|
<property name="localeLanguage" value="en"/>
|
||||||
|
@ -53,6 +51,8 @@ limitations under the License.
|
||||||
<module name="RightCurly"/>
|
<module name="RightCurly"/>
|
||||||
<module name="GenericWhitespace"/>
|
<module name="GenericWhitespace"/>
|
||||||
<module name="WhitespaceAfter"/>
|
<module name="WhitespaceAfter"/>
|
||||||
|
<module name="WhitespaceAround"/>
|
||||||
|
<module name="WhitespaceAroundCheck"/>
|
||||||
<module name="NoWhitespaceBefore"/>
|
<module name="NoWhitespaceBefore"/>
|
||||||
<module name="ImportOrder">
|
<module name="ImportOrder">
|
||||||
<property name="option" value="top"/>
|
<property name="option" value="top"/>
|
||||||
|
|
|
@ -443,7 +443,8 @@ public class ArrayUtilsAddTest extends AbstractLangTest {
|
||||||
assertArrayEquals(new byte[] { (byte) 0, (byte) 1 }, ArrayUtils.addAll(new byte[] { (byte) 0, (byte) 1 }, null));
|
assertArrayEquals(new byte[] { (byte) 0, (byte) 1 }, ArrayUtils.addAll(new byte[] { (byte) 0, (byte) 1 }, null));
|
||||||
|
|
||||||
// short
|
// short
|
||||||
assertArrayEquals(new short[]{(short) 10, (short) 20, (short) 30, (short) 40}, ArrayUtils.addAll(new short[]{(short) 10, (short) 20}, (short) 30, (short) 40));
|
assertArrayEquals(new short[] { (short) 10, (short) 20, (short) 30, (short) 40 },
|
||||||
|
ArrayUtils.addAll(new short[] { (short) 10, (short) 20 }, (short) 30, (short) 40));
|
||||||
|
|
||||||
assertArrayEquals(new short[] { (short) 30, (short) 40 }, ArrayUtils.addAll(null, new short[] { (short) 30, (short) 40 }));
|
assertArrayEquals(new short[] { (short) 30, (short) 40 }, ArrayUtils.addAll(null, new short[] { (short) 30, (short) 40 }));
|
||||||
|
|
||||||
|
@ -522,8 +523,7 @@ public class ArrayUtilsAddTest extends AbstractLangTest {
|
||||||
// boolean tests
|
// boolean tests
|
||||||
boolean[] booleanArray = ArrayUtils.add(null, 0, true);
|
boolean[] booleanArray = ArrayUtils.add(null, 0, true);
|
||||||
assertArrayEquals(new boolean[] { true }, booleanArray);
|
assertArrayEquals(new boolean[] { true }, booleanArray);
|
||||||
IndexOutOfBoundsException e =
|
IndexOutOfBoundsException e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(null, -1, true));
|
||||||
assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( null, -1, true));
|
|
||||||
assertEquals("Index: -1, Length: 0", e.getMessage());
|
assertEquals("Index: -1, Length: 0", e.getMessage());
|
||||||
booleanArray = ArrayUtils.add(new boolean[] { true }, 0, false);
|
booleanArray = ArrayUtils.add(new boolean[] { true }, 0, false);
|
||||||
assertArrayEquals(new boolean[] { false, true }, booleanArray);
|
assertArrayEquals(new boolean[] { false, true }, booleanArray);
|
||||||
|
@ -659,8 +659,7 @@ public class ArrayUtilsAddTest extends AbstractLangTest {
|
||||||
assertEquals(2, n.length);
|
assertEquals(2, n.length);
|
||||||
assertEquals(Number.class, n.getClass().getComponentType());
|
assertEquals(Number.class, n.getClass().getComponentType());
|
||||||
// Invalid - can't store Long in Integer array
|
// Invalid - can't store Long in Integer array
|
||||||
assertThrows(IllegalArgumentException.class,
|
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.addAll(new Integer[] { Integer.valueOf(1) }, new Long[] { Long.valueOf(2) }));
|
||||||
() -> ArrayUtils.addAll(new Integer[]{Integer.valueOf(1)}, new Long[]{Long.valueOf(2)}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -5509,29 +5509,23 @@ public class ArrayUtilsTest extends AbstractLangTest {
|
||||||
assertTrue(ArrayUtils.isEquals(leftSubarray, ArrayUtils.subarray(array, 0, 4)), "0 start, mid end");
|
assertTrue(ArrayUtils.isEquals(leftSubarray, ArrayUtils.subarray(array, 0, 4)), "0 start, mid end");
|
||||||
assertTrue(ArrayUtils.isEquals(array, ArrayUtils.subarray(array, 0, array.length)), "0 start, length end");
|
assertTrue(ArrayUtils.isEquals(array, ArrayUtils.subarray(array, 0, array.length)), "0 start, length end");
|
||||||
assertTrue(ArrayUtils.isEquals(midSubarray, ArrayUtils.subarray(array, 1, 5)), "mid start, mid end");
|
assertTrue(ArrayUtils.isEquals(midSubarray, ArrayUtils.subarray(array, 1, 5)), "mid start, mid end");
|
||||||
assertTrue(ArrayUtils.isEquals(rightSubarray, ArrayUtils.subarray(array, 2, array.length)),
|
assertTrue(ArrayUtils.isEquals(rightSubarray, ArrayUtils.subarray(array, 2, array.length)), "mid start, length end");
|
||||||
"mid start, length end");
|
|
||||||
|
|
||||||
assertNull(ArrayUtils.subarray(nullArray, 0, 3), "null input");
|
assertNull(ArrayUtils.subarray(nullArray, 0, 3), "null input");
|
||||||
assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(ArrayUtils.EMPTY_CHAR_ARRAY, 1, 2),
|
assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(ArrayUtils.EMPTY_CHAR_ARRAY, 1, 2), "empty array");
|
||||||
"empty array");
|
|
||||||
assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 4, 2), "start > end");
|
assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 4, 2), "start > end");
|
||||||
assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 3, 3), "start == end");
|
assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 3, 3), "start == end");
|
||||||
assertTrue(ArrayUtils.isEquals(leftSubarray, ArrayUtils.subarray(array, -2, 4)),
|
assertTrue(ArrayUtils.isEquals(leftSubarray, ArrayUtils.subarray(array, -2, 4)), "start undershoot, normal end");
|
||||||
"start undershoot, normal end");
|
|
||||||
assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 33, 4), "start overshoot, any end");
|
assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 33, 4), "start overshoot, any end");
|
||||||
assertTrue(ArrayUtils.isEquals(rightSubarray, ArrayUtils.subarray(array, 2, 33)),
|
assertTrue(ArrayUtils.isEquals(rightSubarray, ArrayUtils.subarray(array, 2, 33)), "normal start, end overshoot");
|
||||||
"normal start, end overshoot");
|
|
||||||
assertTrue(ArrayUtils.isEquals(array, ArrayUtils.subarray(array, -2, 12)), "start undershoot, end overshoot");
|
assertTrue(ArrayUtils.isEquals(array, ArrayUtils.subarray(array, -2, 12)), "start undershoot, end overshoot");
|
||||||
|
|
||||||
// empty-return tests
|
// empty-return tests
|
||||||
|
|
||||||
assertSame(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(ArrayUtils.EMPTY_CHAR_ARRAY, 1, 2),
|
assertSame(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(ArrayUtils.EMPTY_CHAR_ARRAY, 1, 2), "empty array, object test");
|
||||||
"empty array, object test");
|
|
||||||
assertSame(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 4, 1), "start > end, object test");
|
assertSame(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 4, 1), "start > end, object test");
|
||||||
assertSame(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 3, 3), "start == end, object test");
|
assertSame(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 3, 3), "start == end, object test");
|
||||||
assertSame(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 8733, 4),
|
assertSame(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.subarray(array, 8733, 4), "start overshoot, any end, object test");
|
||||||
"start overshoot, any end, object test");
|
|
||||||
|
|
||||||
// array type tests
|
// array type tests
|
||||||
|
|
||||||
|
@ -6154,12 +6148,9 @@ public class ArrayUtilsTest extends AbstractLangTest {
|
||||||
assertEquals("world", map.get("hello"));
|
assertEquals("world", map.get("hello"));
|
||||||
|
|
||||||
assertNull(ArrayUtils.toMap(null));
|
assertNull(ArrayUtils.toMap(null));
|
||||||
assertThrows(IllegalArgumentException.class, () ->
|
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.toMap(new String[][] { { "foo", "bar" }, { "short" } }));
|
||||||
ArrayUtils.toMap(new String[][]{{"foo", "bar"}, {"short"}}));
|
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.toMap(new Object[] { new Object[] { "foo", "bar" }, "illegal type" }));
|
||||||
assertThrows(IllegalArgumentException.class, () ->
|
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.toMap(new Object[] { new Object[] { "foo", "bar" }, null }));
|
||||||
ArrayUtils.toMap(new Object[]{new Object[]{"foo", "bar"}, "illegal type"}));
|
|
||||||
assertThrows(IllegalArgumentException.class, () ->
|
|
||||||
ArrayUtils.toMap(new Object[]{new Object[]{"foo", "bar"}, null}));
|
|
||||||
|
|
||||||
map = ArrayUtils.toMap(new Object[] { new Map.Entry<Object, Object>() {
|
map = ArrayUtils.toMap(new Object[] { new Map.Entry<Object, Object>() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -65,8 +65,9 @@ public class CharSequenceUtilsTest extends AbstractLangTest {
|
||||||
final int len;
|
final int len;
|
||||||
final boolean expected;
|
final boolean expected;
|
||||||
final Class<? extends Throwable> throwable;
|
final Class<? extends Throwable> throwable;
|
||||||
TestData(final String source, final boolean ignoreCase, final int toffset,
|
|
||||||
final String other, final int ooffset, final int len, final boolean expected) {
|
TestData(final String source, final boolean ignoreCase, final int toffset, final String other, final int ooffset, final int len,
|
||||||
|
final boolean expected) {
|
||||||
this.source = source;
|
this.source = source;
|
||||||
this.ignoreCase = ignoreCase;
|
this.ignoreCase = ignoreCase;
|
||||||
this.toffset = toffset;
|
this.toffset = toffset;
|
||||||
|
@ -76,8 +77,9 @@ public class CharSequenceUtilsTest extends AbstractLangTest {
|
||||||
this.expected = expected;
|
this.expected = expected;
|
||||||
this.throwable = null;
|
this.throwable = null;
|
||||||
}
|
}
|
||||||
TestData(final String source, final boolean ignoreCase, final int toffset,
|
|
||||||
final String other, final int ooffset, final int len, final Class<? extends Throwable> throwable) {
|
TestData(final String source, final boolean ignoreCase, final int toffset, final String other, final int ooffset, final int len,
|
||||||
|
final Class<? extends Throwable> throwable) {
|
||||||
this.source = source;
|
this.source = source;
|
||||||
this.ignoreCase = ignoreCase;
|
this.ignoreCase = ignoreCase;
|
||||||
this.toffset = toffset;
|
this.toffset = toffset;
|
||||||
|
@ -87,6 +89,7 @@ public class CharSequenceUtilsTest extends AbstractLangTest {
|
||||||
this.expected = false;
|
this.expected = false;
|
||||||
this.throwable = throwable;
|
this.throwable = throwable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
final StringBuilder sb = new StringBuilder();
|
final StringBuilder sb = new StringBuilder();
|
||||||
|
@ -142,6 +145,7 @@ public class CharSequenceUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final TestData[] TEST_DATA = {
|
private static final TestData[] TEST_DATA = {
|
||||||
|
// @formatter:off
|
||||||
// Source IgnoreCase Offset Other Offset Length Result
|
// Source IgnoreCase Offset Other Offset Length Result
|
||||||
new TestData("", true, -1, "", -1, -1, false),
|
new TestData("", true, -1, "", -1, -1, false),
|
||||||
new TestData("", true, 0, "", 0, 1, false),
|
new TestData("", true, 0, "", 0, 1, false),
|
||||||
|
@ -156,9 +160,11 @@ public class CharSequenceUtilsTest extends AbstractLangTest {
|
||||||
new TestData("Abc", false, 1, "abc", 1, 2, true),
|
new TestData("Abc", false, 1, "abc", 1, 2, true),
|
||||||
new TestData("Abcd", true, 1, "abcD", 1, 2, true),
|
new TestData("Abcd", true, 1, "abcD", 1, 2, true),
|
||||||
new TestData("Abcd", false, 1, "abcD", 1, 2, true),
|
new TestData("Abcd", false, 1, "abcD", 1, 2, true),
|
||||||
|
// @formatter:on
|
||||||
};
|
};
|
||||||
|
|
||||||
static Stream<Arguments> lastIndexWithStandardCharSequence() {
|
static Stream<Arguments> lastIndexWithStandardCharSequence() {
|
||||||
|
// @formatter:off
|
||||||
return Stream.of(
|
return Stream.of(
|
||||||
arguments("abc", "b", 2, 1),
|
arguments("abc", "b", 2, 1),
|
||||||
arguments(new StringBuilder("abc"), "b", 2, 1),
|
arguments(new StringBuilder("abc"), "b", 2, 1),
|
||||||
|
@ -168,6 +174,7 @@ public class CharSequenceUtilsTest extends AbstractLangTest {
|
||||||
arguments(new StringBuffer("abc"), new StringBuffer("b"), 2, 1),
|
arguments(new StringBuffer("abc"), new StringBuffer("b"), 2, 1),
|
||||||
arguments(new StringBuilder("abc"), new StringBuffer("b"), 2, 1)
|
arguments(new StringBuilder("abc"), new StringBuffer("b"), 2, 1)
|
||||||
);
|
);
|
||||||
|
// @formatter:on
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -182,8 +189,7 @@ public class CharSequenceUtilsTest extends AbstractLangTest {
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@MethodSource("lastIndexWithStandardCharSequence")
|
@MethodSource("lastIndexWithStandardCharSequence")
|
||||||
public void testLastIndexOfWithDifferentCharSequences(final CharSequence cs, final CharSequence search, final int start,
|
public void testLastIndexOfWithDifferentCharSequences(final CharSequence cs, final CharSequence search, final int start, final int expected) {
|
||||||
final int expected) {
|
|
||||||
assertEquals(expected, CharSequenceUtils.lastIndexOf(cs, search, start));
|
assertEquals(expected, CharSequenceUtils.lastIndexOf(cs, search, start));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,11 +246,9 @@ public class CharSequenceUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testNewLastIndexOfSingleSingle(final CharSequence a, final CharSequence b, final int start) {
|
private void testNewLastIndexOfSingleSingle(final CharSequence a, final CharSequence b, final int start) {
|
||||||
assertEquals(
|
assertEquals(a.toString().lastIndexOf(b.toString(), start),
|
||||||
a.toString().lastIndexOf(b.toString(), start),
|
|
||||||
CharSequenceUtils.lastIndexOf(new WrapperString(a.toString()), new WrapperString(b.toString()), start),
|
CharSequenceUtils.lastIndexOf(new WrapperString(a.toString()), new WrapperString(b.toString()), start),
|
||||||
"testNewLastIndexOf fails! original : " + a + " seg : " + b + " start : " + start
|
"testNewLastIndexOf fails! original : " + a + " seg : " + b + " start : " + start);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -46,6 +46,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes Chi-Square statistic given observed and expected counts
|
* Computes Chi-Square statistic given observed and expected counts
|
||||||
|
*
|
||||||
* @param observed array of observed frequency counts
|
* @param observed array of observed frequency counts
|
||||||
* @param expected array of expected frequency counts
|
* @param expected array of expected frequency counts
|
||||||
*/
|
*/
|
||||||
|
@ -59,9 +60,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for LANG-1286. Creates situation where old code would
|
* Test for LANG-1286. Creates situation where old code would overflow a char and result in a code point outside the specified range.
|
||||||
* overflow a char and result in a code point outside the specified
|
|
||||||
* range.
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testCharOverflow() {
|
public void testCharOverflow() {
|
||||||
|
@ -69,8 +68,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
final int end = Integer.MAX_VALUE;
|
final int end = Integer.MAX_VALUE;
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
final
|
final Random fixedRandom = new Random() {
|
||||||
Random fixedRandom = new Random() {
|
|
||||||
@Override
|
@Override
|
||||||
public int nextInt(final int n) {
|
public int nextInt(final int n) {
|
||||||
// Prevents selection of 'start' as the character
|
// Prevents selection of 'start' as the character
|
||||||
|
@ -104,9 +102,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, (String) null));
|
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, (String) null));
|
||||||
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, 'a', 'z', false, false));
|
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, 'a', 'z', false, false));
|
||||||
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, 'a', 'z', false, false, DUMMY));
|
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, 'a', 'z', false, false, DUMMY));
|
||||||
assertThrows(
|
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, 'a', 'z', false, false, DUMMY, new Random()));
|
||||||
IllegalArgumentException.class,
|
|
||||||
() -> RandomStringUtils.random(-1, 'a', 'z', false, false, DUMMY, new Random()));
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(8, 32, 48, false, true));
|
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(8, 32, 48, false, true));
|
||||||
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(8, 32, 65, true, false));
|
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(8, 32, 65, true, false));
|
||||||
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomAlphabetic(-1));
|
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomAlphabetic(-1));
|
||||||
|
@ -117,8 +113,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the string got by {@link RandomStringUtils#random(int)}
|
* Checks if the string got by {@link RandomStringUtils#random(int)} can be converted to UTF-8 and back without loss.
|
||||||
* can be converted to UTF-8 and back without loss.
|
|
||||||
*
|
*
|
||||||
* @see <a href="https://issues.apache.org/jira/browse/LANG-100">LANG-100</a>
|
* @see <a href="https://issues.apache.org/jira/browse/LANG-100">LANG-100</a>
|
||||||
*/
|
*/
|
||||||
|
@ -134,9 +129,8 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
for (int i = 0; i < orig.length() && i < copy.length(); i++) {
|
for (int i = 0; i < orig.length() && i < copy.length(); i++) {
|
||||||
final char o = orig.charAt(i);
|
final char o = orig.charAt(i);
|
||||||
final char c = copy.charAt(i);
|
final char c = copy.charAt(i);
|
||||||
assertEquals(o, c,
|
assertEquals(o, c, "differs at " + i + "(" + Integer.toHexString(Character.valueOf(o).hashCode()) + ","
|
||||||
"differs at " + i + "(" + Integer.toHexString(Character.valueOf(o).hashCode()) + "," +
|
+ Integer.toHexString(Character.valueOf(c).hashCode()) + ")");
|
||||||
Integer.toHexString(Character.valueOf(c).hashCode()) + ")");
|
|
||||||
}
|
}
|
||||||
// compare length also
|
// compare length also
|
||||||
assertEquals(orig.length(), copy.length());
|
assertEquals(orig.length(), copy.length());
|
||||||
|
@ -152,16 +146,14 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLANG807() {
|
public void testLANG807() {
|
||||||
final IllegalArgumentException ex =
|
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(3, 5, 5, false, false));
|
||||||
assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(3, 5, 5, false, false));
|
|
||||||
final String msg = ex.getMessage();
|
final String msg = ex.getMessage();
|
||||||
assertTrue(msg.contains("start"), "Message (" + msg + ") must contain 'start'");
|
assertTrue(msg.contains("start"), "Message (" + msg + ") must contain 'start'");
|
||||||
assertTrue(msg.contains("end"), "Message (" + msg + ") must contain 'end'");
|
assertTrue(msg.contains("end"), "Message (" + msg + ") must contain 'end'");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make sure boundary alpha characters are generated by randomAlphabetic
|
* Make sure boundary alpha characters are generated by randomAlphabetic This test will fail randomly with probability = 4 * (51/52)**1000 ~ 1.58E-8
|
||||||
* This test will fail randomly with probability = 4 * (51/52)**1000 ~ 1.58E-8
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testRandomAlphabetic() {
|
public void testRandomAlphabetic() {
|
||||||
|
@ -176,9 +168,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < testChars.length; i++) {
|
for (int i = 0; i < testChars.length; i++) {
|
||||||
assertTrue(found[i],
|
assertTrue(found[i], "alphanumeric character not generated in 1000 attempts: " + testChars[i] + " -- repeated failures indicate a problem ");
|
||||||
"alphanumeric character not generated in 1000 attempts: " + testChars[i] +
|
|
||||||
" -- repeated failures indicate a problem ");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,8 +198,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make sure boundary alphanumeric characters are generated by randomAlphaNumeric
|
* Make sure boundary alphanumeric characters are generated by randomAlphaNumeric This test will fail randomly with probability = 6 * (61/62)**1000 ~ 5.2E-7
|
||||||
* This test will fail randomly with probability = 6 * (61/62)**1000 ~ 5.2E-7
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testRandomAlphaNumeric() {
|
public void testRandomAlphaNumeric() {
|
||||||
|
@ -224,9 +213,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < testChars.length; i++) {
|
for (int i = 0; i < testChars.length; i++) {
|
||||||
assertTrue(found[i],
|
assertTrue(found[i], "alphanumeric character not generated in 1000 attempts: " + testChars[i] + " -- repeated failures indicate a problem ");
|
||||||
"alphanumeric character not generated in 1000 attempts: " +
|
|
||||||
testChars[i] + " -- repeated failures indicate a problem ");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,8 +243,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make sure 32 and 127 are generated by randomNumeric
|
* Make sure 32 and 127 are generated by randomNumeric This test will fail randomly with probability = 2*(95/96)**1000 ~ 5.7E-5
|
||||||
* This test will fail randomly with probability = 2*(95/96)**1000 ~ 5.7E-5
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testRandomAscii() {
|
public void testRandomAscii() {
|
||||||
|
@ -273,9 +259,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < testChars.length; i++) {
|
for (int i = 0; i < testChars.length; i++) {
|
||||||
assertTrue(found[i],
|
assertTrue(found[i], "ascii character not generated in 1000 attempts: " + (int) testChars[i] + " -- repeated failures indicate a problem");
|
||||||
"ascii character not generated in 1000 attempts: " + (int) testChars[i] +
|
|
||||||
" -- repeated failures indicate a problem");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -330,8 +314,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make sure '0' and '9' are generated by randomNumeric
|
* Make sure '0' and '9' are generated by randomNumeric This test will fail randomly with probability = 2 * (9/10)**1000 ~ 3.5E-46
|
||||||
* This test will fail randomly with probability = 2 * (9/10)**1000 ~ 3.5E-46
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testRandomNumeric() {
|
public void testRandomNumeric() {
|
||||||
|
@ -346,9 +329,7 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < testChars.length; i++) {
|
for (int i = 0; i < testChars.length; i++) {
|
||||||
assertTrue(found[i],
|
assertTrue(found[i], "digit not generated in 1000 attempts: " + testChars[i] + " -- repeated failures indicate a problem ");
|
||||||
"digit not generated in 1000 attempts: " + testChars[i] +
|
|
||||||
" -- repeated failures indicate a problem ");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -501,10 +482,8 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test homogeneity of random strings generated --
|
* Test homogeneity of random strings generated -- i.e., test that characters show up with expected frequencies in generated strings. Will fail randomly
|
||||||
* i.e., test that characters show up with expected frequencies
|
* about 1 in 100,000 times. Repeated failures indicate a problem.
|
||||||
* in generated strings. Will fail randomly about 1 in 100,000 times.
|
|
||||||
* Repeated failures indicate a problem.
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testRandomStringUtilsHomog() {
|
public void testRandomStringUtilsHomog() {
|
||||||
|
@ -540,4 +519,3 @@ public class RandomStringUtilsTest extends AbstractLangTest {
|
||||||
assertThat("test homogeneity -- will fail about 1 in 100,000 times", chiSquare(expected, counts), lessThan(23.025850929940457d));
|
assertThat("test homogeneity -- will fail about 1 in 100,000 times", chiSquare(expected, counts), lessThan(23.025850929940457d));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -80,9 +80,7 @@ public class StringEscapeUtilsTest extends AbstractLangTest {
|
||||||
final String actual = StringEscapeUtils.unescapeJava(original);
|
final String actual = StringEscapeUtils.unescapeJava(original);
|
||||||
|
|
||||||
assertEquals(expected, actual,
|
assertEquals(expected, actual,
|
||||||
"unescape(String) failed" +
|
"unescape(String) failed" + (message == null ? "" : ": " + message) + ": expected '" + StringEscapeUtils.escapeJava(expected) +
|
||||||
(message == null ? "" : ": " + message) +
|
|
||||||
": expected '" + StringEscapeUtils.escapeJava(expected) +
|
|
||||||
// we escape this so we can see it in the error message
|
// we escape this so we can see it in the error message
|
||||||
"' actual '" + StringEscapeUtils.escapeJava(actual) + "'");
|
"' actual '" + StringEscapeUtils.escapeJava(actual) + "'");
|
||||||
|
|
||||||
|
@ -163,8 +161,7 @@ public class StringEscapeUtilsTest extends AbstractLangTest {
|
||||||
// Some random Japanese Unicode characters
|
// Some random Japanese Unicode characters
|
||||||
final String original = "\u304B\u304C\u3068";
|
final String original = "\u304B\u304C\u3068";
|
||||||
final String escaped = StringEscapeUtils.escapeHtml4(original);
|
final String escaped = StringEscapeUtils.escapeHtml4(original);
|
||||||
assertEquals(original, escaped,
|
assertEquals(original, escaped, "Hiragana character Unicode behavior should not be being escaped by escapeHtml4");
|
||||||
"Hiragana character Unicode behavior should not be being escaped by escapeHtml4");
|
|
||||||
|
|
||||||
final String unescaped = StringEscapeUtils.unescapeHtml4(escaped);
|
final String unescaped = StringEscapeUtils.unescapeHtml4(escaped);
|
||||||
|
|
||||||
|
@ -235,12 +232,9 @@ public class StringEscapeUtilsTest extends AbstractLangTest {
|
||||||
assertEscapeJava("\\u0001", "\u0001");
|
assertEscapeJava("\\u0001", "\u0001");
|
||||||
assertEscapeJava("Should use capitalized Unicode hex", "\\uABCD", "\uabcd");
|
assertEscapeJava("Should use capitalized Unicode hex", "\\uABCD", "\uabcd");
|
||||||
|
|
||||||
assertEscapeJava("He didn't say, \\\"stop!\\\"",
|
assertEscapeJava("He didn't say, \\\"stop!\\\"", "He didn't say, \"stop!\"");
|
||||||
"He didn't say, \"stop!\"");
|
assertEscapeJava("non-breaking space", "This space is non-breaking:" + "\\u00A0", "This space is non-breaking:\u00a0");
|
||||||
assertEscapeJava("non-breaking space", "This space is non-breaking:" + "\\u00A0",
|
assertEscapeJava("\\uABCD\\u1234\\u012C", "\uABCD\u1234\u012C");
|
||||||
"This space is non-breaking:\u00a0");
|
|
||||||
assertEscapeJava("\\uABCD\\u1234\\u012C",
|
|
||||||
"\uABCD\u1234\u012C");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -254,8 +248,7 @@ public class StringEscapeUtilsTest extends AbstractLangTest {
|
||||||
final String actual = StringEscapeUtils.escapeJava(input);
|
final String actual = StringEscapeUtils.escapeJava(input);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* In 2.4 StringEscapeUtils.escapeJava(String) escapes '/' characters, which are not a valid character to escape
|
* In 2.4 StringEscapeUtils.escapeJava(String) escapes '/' characters, which are not a valid character to escape in a Java string.
|
||||||
* in a Java string.
|
|
||||||
*/
|
*/
|
||||||
assertEquals(expected, actual);
|
assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
@ -281,12 +274,9 @@ public class StringEscapeUtilsTest extends AbstractLangTest {
|
||||||
|
|
||||||
assertEquals("\u00A1", StringEscapeUtils.escapeXml("\u00A1"), "XML should not escape >0x7f values");
|
assertEquals("\u00A1", StringEscapeUtils.escapeXml("\u00A1"), "XML should not escape >0x7f values");
|
||||||
assertEquals("\u00A0", StringEscapeUtils.unescapeXml(" "), "XML should be able to unescape >0x7f values");
|
assertEquals("\u00A0", StringEscapeUtils.unescapeXml(" "), "XML should be able to unescape >0x7f values");
|
||||||
assertEquals("\u00A0", StringEscapeUtils.unescapeXml(" "),
|
assertEquals("\u00A0", StringEscapeUtils.unescapeXml(" "), "XML should be able to unescape >0x7f values with one leading 0");
|
||||||
"XML should be able to unescape >0x7f values with one leading 0");
|
assertEquals("\u00A0", StringEscapeUtils.unescapeXml(" "), "XML should be able to unescape >0x7f values with two leading 0s");
|
||||||
assertEquals("\u00A0", StringEscapeUtils.unescapeXml(" "),
|
assertEquals("\u00A0", StringEscapeUtils.unescapeXml(" "), "XML should be able to unescape >0x7f values with three leading 0s");
|
||||||
"XML should be able to unescape >0x7f values with two leading 0s");
|
|
||||||
assertEquals("\u00A0", StringEscapeUtils.unescapeXml(" "),
|
|
||||||
"XML should be able to unescape >0x7f values with three leading 0s");
|
|
||||||
|
|
||||||
assertEquals("ain't", StringEscapeUtils.unescapeXml("ain't"));
|
assertEquals("ain't", StringEscapeUtils.unescapeXml("ain't"));
|
||||||
assertEquals("ain't", StringEscapeUtils.escapeXml("ain't"));
|
assertEquals("ain't", StringEscapeUtils.escapeXml("ain't"));
|
||||||
|
@ -309,12 +299,9 @@ public class StringEscapeUtilsTest extends AbstractLangTest {
|
||||||
assertEquals("a\tb\rc\nd", StringEscapeUtils.escapeXml10("a\tb\rc\nd"), "XML 1.0 should not escape \t \n \r");
|
assertEquals("a\tb\rc\nd", StringEscapeUtils.escapeXml10("a\tb\rc\nd"), "XML 1.0 should not escape \t \n \r");
|
||||||
assertEquals("ab", StringEscapeUtils.escapeXml10("a\u0000\u0001\u0008\u000b\u000c\u000e\u001fb"),
|
assertEquals("ab", StringEscapeUtils.escapeXml10("a\u0000\u0001\u0008\u000b\u000c\u000e\u001fb"),
|
||||||
"XML 1.0 should omit most #x0-x8 | #xb | #xc | #xe-#x19");
|
"XML 1.0 should omit most #x0-x8 | #xb | #xc | #xe-#x19");
|
||||||
assertEquals("a\ud7ff \ue000b", StringEscapeUtils.escapeXml10("a\ud7ff\ud800 \udfff \ue000b"),
|
assertEquals("a\ud7ff \ue000b", StringEscapeUtils.escapeXml10("a\ud7ff\ud800 \udfff \ue000b"), "XML 1.0 should omit #xd800-#xdfff");
|
||||||
"XML 1.0 should omit #xd800-#xdfff");
|
assertEquals("a\ufffdb", StringEscapeUtils.escapeXml10("a\ufffd\ufffe\uffffb"), "XML 1.0 should omit #xfffe | #xffff");
|
||||||
assertEquals("a\ufffdb", StringEscapeUtils.escapeXml10("a\ufffd\ufffe\uffffb"),
|
assertEquals("a\u007e„\u0085†Ÿ\u00a0b", StringEscapeUtils.escapeXml10("a\u007e\u007f\u0084\u0085\u0086\u009f\u00a0b"),
|
||||||
"XML 1.0 should omit #xfffe | #xffff");
|
|
||||||
assertEquals("a\u007e„\u0085†Ÿ\u00a0b",
|
|
||||||
StringEscapeUtils.escapeXml10("a\u007e\u007f\u0084\u0085\u0086\u009f\u00a0b"),
|
|
||||||
"XML 1.0 should escape #x7f-#x84 | #x86 - #x9f, for XML 1.1 compatibility");
|
"XML 1.0 should escape #x7f-#x84 | #x86 - #x9f, for XML 1.1 compatibility");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -323,16 +310,12 @@ public class StringEscapeUtilsTest extends AbstractLangTest {
|
||||||
assertEquals("a<b>c"d'e&f", StringEscapeUtils.escapeXml11("a<b>c\"d'e&f"));
|
assertEquals("a<b>c"d'e&f", StringEscapeUtils.escapeXml11("a<b>c\"d'e&f"));
|
||||||
assertEquals("a\tb\rc\nd", StringEscapeUtils.escapeXml11("a\tb\rc\nd"), "XML 1.1 should not escape \t \n \r");
|
assertEquals("a\tb\rc\nd", StringEscapeUtils.escapeXml11("a\tb\rc\nd"), "XML 1.1 should not escape \t \n \r");
|
||||||
assertEquals("ab", StringEscapeUtils.escapeXml11("a\u0000b"), "XML 1.1 should omit #x0");
|
assertEquals("ab", StringEscapeUtils.escapeXml11("a\u0000b"), "XML 1.1 should omit #x0");
|
||||||
assertEquals("ab",
|
assertEquals("ab", StringEscapeUtils.escapeXml11("a\u0001\u0008\u000b\u000c\u000e\u001fb"),
|
||||||
StringEscapeUtils.escapeXml11("a\u0001\u0008\u000b\u000c\u000e\u001fb"),
|
|
||||||
"XML 1.1 should escape #x1-x8 | #xb | #xc | #xe-#x19");
|
"XML 1.1 should escape #x1-x8 | #xb | #xc | #xe-#x19");
|
||||||
assertEquals("a\u007e„\u0085†Ÿ\u00a0b",
|
assertEquals("a\u007e„\u0085†Ÿ\u00a0b", StringEscapeUtils.escapeXml11("a\u007e\u007f\u0084\u0085\u0086\u009f\u00a0b"),
|
||||||
StringEscapeUtils.escapeXml11("a\u007e\u007f\u0084\u0085\u0086\u009f\u00a0b"),
|
|
||||||
"XML 1.1 should escape #x7F-#x84 | #x86-#x9F");
|
"XML 1.1 should escape #x7F-#x84 | #x86-#x9F");
|
||||||
assertEquals("a\ud7ff \ue000b", StringEscapeUtils.escapeXml11("a\ud7ff\ud800 \udfff \ue000b"),
|
assertEquals("a\ud7ff \ue000b", StringEscapeUtils.escapeXml11("a\ud7ff\ud800 \udfff \ue000b"), "XML 1.1 should omit #xd800-#xdfff");
|
||||||
"XML 1.1 should omit #xd800-#xdfff");
|
assertEquals("a\ufffdb", StringEscapeUtils.escapeXml11("a\ufffd\ufffe\uffffb"), "XML 1.1 should omit #xfffe | #xffff");
|
||||||
assertEquals("a\ufffdb", StringEscapeUtils.escapeXml11("a\ufffd\ufffe\uffffb"),
|
|
||||||
"XML 1.1 should omit #xfffe | #xffff");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -340,9 +323,9 @@ public class StringEscapeUtilsTest extends AbstractLangTest {
|
||||||
// https://www.w3.org/TR/xml/#charsets says:
|
// https://www.w3.org/TR/xml/#charsets says:
|
||||||
// Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character,
|
// Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character,
|
||||||
// excluding the surrogate blocks, FFFE, and FFFF. */
|
// excluding the surrogate blocks, FFFE, and FFFF. */
|
||||||
final CharSequenceTranslator escapeXml = StringEscapeUtils.ESCAPE_XML
|
final CharSequenceTranslator escapeXml = StringEscapeUtils.ESCAPE_XML.with(NumericEntityEscaper.below(9), NumericEntityEscaper.between(0xB, 0xC),
|
||||||
.with(NumericEntityEscaper.below(9), NumericEntityEscaper.between(0xB, 0xC), NumericEntityEscaper.between(0xE, 0x19),
|
NumericEntityEscaper.between(0xE, 0x19), NumericEntityEscaper.between(0xD800, 0xDFFF), NumericEntityEscaper.between(0xFFFE, 0xFFFF),
|
||||||
NumericEntityEscaper.between(0xD800, 0xDFFF), NumericEntityEscaper.between(0xFFFE, 0xFFFF), NumericEntityEscaper.above(0x110000));
|
NumericEntityEscaper.above(0x110000));
|
||||||
|
|
||||||
assertEquals("�", escapeXml.translate("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008"));
|
assertEquals("�", escapeXml.translate("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008"));
|
||||||
assertEquals("\t", escapeXml.translate("\t")); // 0x9
|
assertEquals("\t", escapeXml.translate("\t")); // 0x9
|
||||||
|
@ -358,23 +341,19 @@ public class StringEscapeUtilsTest extends AbstractLangTest {
|
||||||
* <p>
|
* <p>
|
||||||
* From https://www.w3.org/International/questions/qa-escapes
|
* From https://www.w3.org/International/questions/qa-escapes
|
||||||
* </p>
|
* </p>
|
||||||
* <blockquote>
|
* <blockquote> Supplementary characters are those Unicode characters that have code points higher than the characters in the Basic Multilingual Plane
|
||||||
* Supplementary characters are those Unicode characters that have code points higher than the characters in
|
* (BMP). In UTF-16 a supplementary character is encoded using two 16-bit surrogate code points from the BMP. Because of this, some people think that
|
||||||
* the Basic Multilingual Plane (BMP). In UTF-16 a supplementary character is encoded using two 16-bit surrogate code points from the
|
* supplementary characters need to be represented using two escapes, but this is incorrect - you must use the single, code point value for that character.
|
||||||
* BMP. Because of this, some people think that supplementary characters need to be represented using two escapes, but this is incorrect
|
* For example, use &#x233B4; rather than &#xD84C;&#xDFB4;. </blockquote>
|
||||||
* - you must use the single, code point value for that character. For example, use &#x233B4; rather than
|
*
|
||||||
* &#xD84C;&#xDFB4;.
|
|
||||||
* </blockquote>
|
|
||||||
* @see <a href="https://www.w3.org/International/questions/qa-escapes">Using character escapes in markup and CSS</a>
|
* @see <a href="https://www.w3.org/International/questions/qa-escapes">Using character escapes in markup and CSS</a>
|
||||||
* @see <a href="https://issues.apache.org/jira/browse/LANG-728">LANG-728</a>
|
* @see <a href="https://issues.apache.org/jira/browse/LANG-728">LANG-728</a>
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testEscapeXmlSupplementaryCharacters() {
|
public void testEscapeXmlSupplementaryCharacters() {
|
||||||
final CharSequenceTranslator escapeXml =
|
final CharSequenceTranslator escapeXml = StringEscapeUtils.ESCAPE_XML.with(NumericEntityEscaper.between(0x7f, Integer.MAX_VALUE));
|
||||||
StringEscapeUtils.ESCAPE_XML.with( NumericEntityEscaper.between(0x7f, Integer.MAX_VALUE) );
|
|
||||||
|
|
||||||
assertEquals("𣎴", escapeXml.translate("\uD84C\uDFB4"),
|
assertEquals("𣎴", escapeXml.translate("\uD84C\uDFB4"), "Supplementary character must be represented using a single escape");
|
||||||
"Supplementary character must be represented using a single escape");
|
|
||||||
|
|
||||||
assertEquals("a b c 𣎴", escapeXml.translate("a b c \uD84C\uDFB4"),
|
assertEquals("a b c 𣎴", escapeXml.translate("a b c \uD84C\uDFB4"),
|
||||||
"Supplementary characters mixed with basic characters should be encoded correctly");
|
"Supplementary characters mixed with basic characters should be encoded correctly");
|
||||||
|
@ -388,8 +367,7 @@ public class StringEscapeUtilsTest extends AbstractLangTest {
|
||||||
/**
|
/**
|
||||||
* Tests https://issues.apache.org/jira/browse/LANG-708
|
* Tests https://issues.apache.org/jira/browse/LANG-708
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException if an I/O error occurs
|
||||||
* if an I/O error occurs
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testLang708() throws IOException {
|
public void testLang708() throws IOException {
|
||||||
|
@ -563,8 +541,7 @@ public class StringEscapeUtilsTest extends AbstractLangTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testUnescapeXmlSupplementaryCharacters() {
|
public void testUnescapeXmlSupplementaryCharacters() {
|
||||||
assertEquals("\uD84C\uDFB4", StringEscapeUtils.unescapeXml("𣎴"),
|
assertEquals("\uD84C\uDFB4", StringEscapeUtils.unescapeXml("𣎴"), "Supplementary character must be represented using a single escape");
|
||||||
"Supplementary character must be represented using a single escape");
|
|
||||||
|
|
||||||
assertEquals("a b c \uD84C\uDFB4", StringEscapeUtils.unescapeXml("a b c 𣎴"),
|
assertEquals("a b c \uD84C\uDFB4", StringEscapeUtils.unescapeXml("a b c 𣎴"),
|
||||||
"Supplementary characters mixed with basic characters should be decoded correctly");
|
"Supplementary characters mixed with basic characters should be decoded correctly");
|
||||||
|
|
|
@ -235,20 +235,17 @@ public class StringUtilsContainsTest extends AbstractLangTest {
|
||||||
public void testContainsIgnoreCase_LocaleIndependence() {
|
public void testContainsIgnoreCase_LocaleIndependence() {
|
||||||
final Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() };
|
final Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() };
|
||||||
|
|
||||||
final String[][] tdata = { { "i", "I" }, { "I", "i" }, { "\u03C2", "\u03C3" }, { "\u03A3", "\u03C2" },
|
final String[][] tdata = { { "i", "I" }, { "I", "i" }, { "\u03C2", "\u03C3" }, { "\u03A3", "\u03C2" }, { "\u03A3", "\u03C3" }, };
|
||||||
{ "\u03A3", "\u03C3" }, };
|
|
||||||
|
|
||||||
final String[][] fdata = { { "\u00DF", "SS" }, };
|
final String[][] fdata = { { "\u00DF", "SS" }, };
|
||||||
|
|
||||||
for (final Locale testLocale : locales) {
|
for (final Locale testLocale : locales) {
|
||||||
Locale.setDefault(testLocale);
|
Locale.setDefault(testLocale);
|
||||||
for (int j = 0; j < tdata.length; j++) {
|
for (int j = 0; j < tdata.length; j++) {
|
||||||
assertTrue(StringUtils.containsIgnoreCase(tdata[j][0], tdata[j][1]),
|
assertTrue(StringUtils.containsIgnoreCase(tdata[j][0], tdata[j][1]), Locale.getDefault() + ": " + j + " " + tdata[j][0] + " " + tdata[j][1]);
|
||||||
Locale.getDefault() + ": " + j + " " + tdata[j][0] + " " + tdata[j][1]);
|
|
||||||
}
|
}
|
||||||
for (int j = 0; j < fdata.length; j++) {
|
for (int j = 0; j < fdata.length; j++) {
|
||||||
assertFalse(StringUtils.containsIgnoreCase(fdata[j][0], fdata[j][1]),
|
assertFalse(StringUtils.containsIgnoreCase(fdata[j][0], fdata[j][1]), Locale.getDefault() + ": " + j + " " + fdata[j][0] + " " + fdata[j][1]);
|
||||||
Locale.getDefault() + ": " + j + " " + fdata[j][0] + " " + fdata[j][1]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -333,10 +333,9 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
|
|
||||||
assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(now).toString());
|
assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(now).toString());
|
||||||
|
|
||||||
assertEquals("{\"now\":\"" + now +"\"}", new ToStringBuilder(base).append("now", now)
|
assertEquals("{\"now\":\"" + now + "\"}", new ToStringBuilder(base).append("now", now).toString());
|
||||||
.toString());
|
assertEquals("{\"now\":\"" + now + "\",\"after\":\"" + afterNow + "\"}",
|
||||||
assertEquals("{\"now\":\"" + now +"\",\"after\":\"" + afterNow + "\"}", new ToStringBuilder(base).append("now", now).append("after", afterNow)
|
new ToStringBuilder(base).append("now", now).append("after", afterNow).toString());
|
||||||
.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -346,17 +345,13 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
||||||
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
||||||
|
|
||||||
assertEquals("{\"doubleArray\":[1.0,2.0,-3.0,4.0]}", toStringBuilder.append("doubleArray", array)
|
assertEquals("{\"doubleArray\":[1.0,2.0,-3.0,4.0]}", toStringBuilder.append("doubleArray", array).toString());
|
||||||
.toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -366,17 +361,13 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
||||||
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
||||||
|
|
||||||
assertEquals("{\"floatArray\":[1.0,2.0,-3.0,4.0]}", toStringBuilder.append("floatArray", array)
|
assertEquals("{\"floatArray\":[1.0,2.0,-3.0,4.0]}", toStringBuilder.append("floatArray", array).toString());
|
||||||
.toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -386,17 +377,13 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
||||||
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
||||||
|
|
||||||
assertEquals("{\"intArray\":[1,2,-3,4]}", toStringBuilder.append("intArray", array)
|
assertEquals("{\"intArray\":[1,2,-3,4]}", toStringBuilder.append("intArray", array).toString());
|
||||||
.toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -449,17 +436,13 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
||||||
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
||||||
|
|
||||||
assertEquals("{\"longArray\":[1,2,-3,4]}", toStringBuilder.append("longArray", array)
|
assertEquals("{\"longArray\":[1,2,-3,4]}", toStringBuilder.append("longArray", array).toString());
|
||||||
.toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -469,14 +452,11 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
||||||
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[][]) null).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((long[][]) null).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -501,15 +481,13 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
final InnerMapObject object = new InnerMapObject() {
|
final InnerMapObject object = new InnerMapObject() {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new ToStringBuilder(this).append("pid", this.pid)
|
return new ToStringBuilder(this).append("pid", this.pid).append("map", this.map).toString();
|
||||||
.append("map", this.map).toString();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
object.pid = "dummy-text";
|
object.pid = "dummy-text";
|
||||||
object.map = baseMap;
|
object.map = baseMap;
|
||||||
|
|
||||||
assertEquals("{\"object\":{\"pid\":\"dummy-text\",\"map\":{\"k1\":\"v1\"," +
|
assertEquals("{\"object\":{\"pid\":\"dummy-text\",\"map\":{\"k1\":\"v1\"," + "\"k2\":{\"k2.1\":\"v2.1\",\"k2.2\":\"v2.2\"}}}}",
|
||||||
"\"k2\":{\"k2.1\":\"v2.1\",\"k2.2\":\"v2.2\"}}}}",
|
|
||||||
new ToStringBuilder(base).append("object", object).toString());
|
new ToStringBuilder(base).append("object", object).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -518,9 +496,7 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
final Person p = new Person() {
|
final Person p = new Person() {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new ToStringBuilder(this).append("name", this.name)
|
return new ToStringBuilder(this).append("name", this.name).append("age", this.age).append("smoker", this.smoker).toString();
|
||||||
.append("age", this.age).append("smoker", this.smoker)
|
|
||||||
.toString();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
p.name = "Jane Doe";
|
p.name = "Jane Doe";
|
||||||
|
@ -531,11 +507,8 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
nestP.pid = "#1@Jane";
|
nestP.pid = "#1@Jane";
|
||||||
nestP.person = p;
|
nestP.person = p;
|
||||||
|
|
||||||
assertEquals(
|
assertEquals("{\"pid\":\"#1@Jane\",\"person\":{\"name\":\"Jane Doe\",\"age\":25,\"smoker\":true}}",
|
||||||
"{\"pid\":\"#1@Jane\",\"person\":{\"name\":\"Jane Doe\",\"age\":25,\"smoker\":true}}",
|
new ToStringBuilder(nestP).append("pid", nestP.pid).append("person", nestP.person).toString());
|
||||||
new ToStringBuilder(nestP).append("pid", nestP.pid)
|
|
||||||
.append("person", nestP.person)
|
|
||||||
.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -545,71 +518,39 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testObject() {
|
public void testObject() {
|
||||||
|
|
||||||
final Integer i3 = Integer.valueOf(3);
|
final Integer i3 = Integer.valueOf(3);
|
||||||
final Integer i4 = Integer.valueOf(4);
|
final Integer i4 = Integer.valueOf(4);
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) null).toString());
|
||||||
UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) null).toString());
|
|
||||||
|
|
||||||
assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(i3).toString());
|
assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(i3).toString());
|
||||||
|
|
||||||
assertEquals("{\"a\":null}",
|
assertEquals("{\"a\":null}", new ToStringBuilder(base).append("a", (Object) null).toString());
|
||||||
new ToStringBuilder(base).append("a", (Object) null).toString());
|
assertEquals("{\"a\":3}", new ToStringBuilder(base).append("a", i3).toString());
|
||||||
assertEquals("{\"a\":3}", new ToStringBuilder(base).append("a", i3)
|
assertEquals("{\"a\":3,\"b\":4}", new ToStringBuilder(base).append("a", i3).append("b", i4).toString());
|
||||||
.toString());
|
|
||||||
assertEquals("{\"a\":3,\"b\":4}",
|
|
||||||
new ToStringBuilder(base).append("a", i3).append("b", i4)
|
|
||||||
.toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", i3, false).toString());
|
||||||
UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", i3, false).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", new ArrayList<>(), false).toString());
|
||||||
UnsupportedOperationException.class,
|
|
||||||
() -> new ToStringBuilder(base).append("a", new ArrayList<>(), false).toString());
|
|
||||||
|
|
||||||
assertEquals(
|
assertEquals("{\"a\":[]}", new ToStringBuilder(base).append("a", new ArrayList<>(), true).toString());
|
||||||
"{\"a\":[]}",
|
|
||||||
new ToStringBuilder(base).append("a", new ArrayList<>(),
|
|
||||||
true).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", new HashMap<>(), false).toString());
|
||||||
UnsupportedOperationException.class,
|
|
||||||
() -> new ToStringBuilder(base).append("a", new HashMap<>(), false).toString());
|
|
||||||
|
|
||||||
assertEquals(
|
assertEquals("{\"a\":{}}", new ToStringBuilder(base).append("a", new HashMap<>(), true).toString());
|
||||||
"{\"a\":{}}",
|
|
||||||
new ToStringBuilder(base).append("a",
|
|
||||||
new HashMap<>(), true).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", (Object) new String[0], false).toString());
|
||||||
UnsupportedOperationException.class,
|
|
||||||
() -> new ToStringBuilder(base).append("a", (Object) new String[0], false).toString());
|
|
||||||
|
|
||||||
assertEquals(
|
assertEquals("{\"a\":[]}", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString());
|
||||||
"{\"a\":[]}",
|
|
||||||
new ToStringBuilder(base).append("a", (Object) new String[0],
|
|
||||||
true).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", (Object) new int[] { 1, 2, 3 }, false).toString());
|
||||||
UnsupportedOperationException.class,
|
|
||||||
() -> new ToStringBuilder(base).append("a", (Object) new int[]{1, 2, 3}, false).toString());
|
|
||||||
|
|
||||||
assertEquals(
|
assertEquals("{\"a\":[1,2,3]}", new ToStringBuilder(base).append("a", (Object) new int[] { 1, 2, 3 }, true).toString());
|
||||||
"{\"a\":[1,2,3]}",
|
|
||||||
new ToStringBuilder(base).append("a",
|
|
||||||
(Object) new int[]{1, 2, 3}, true).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class,
|
||||||
UnsupportedOperationException.class,
|
|
||||||
() -> new ToStringBuilder(base).append("a", (Object) new String[] { "v", "x", "y", "z" }, false).toString());
|
() -> new ToStringBuilder(base).append("a", (Object) new String[] { "v", "x", "y", "z" }, false).toString());
|
||||||
|
|
||||||
assertEquals(
|
assertEquals("{\"a\":[\"v\",\"x\",\"y\",\"z\"]}", new ToStringBuilder(base).append("a", (Object) new String[] { "v", "x", "y", "z" }, true).toString());
|
||||||
"{\"a\":[\"v\",\"x\",\"y\",\"z\"]}",
|
|
||||||
new ToStringBuilder(base).append("a",
|
|
||||||
(Object) new String[]{"v", "x", "y", "z"}, true)
|
|
||||||
.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -619,17 +560,13 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
|
||||||
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
|
||||||
|
|
||||||
assertEquals("{\"objectArray\":[null,5,[3,6]]}", toStringBuilder.append("objectArray", array)
|
assertEquals("{\"objectArray\":[null,5,[3,6]]}", toStringBuilder.append("objectArray", array).toString());
|
||||||
.toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object[]) null).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object[]) null).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
||||||
UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -641,8 +578,7 @@ public class JsonToStringStyleTest extends AbstractLangTest {
|
||||||
final InnerMapObject object = new InnerMapObject() {
|
final InnerMapObject object = new InnerMapObject() {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new ToStringBuilder(this).append("pid", this.pid)
|
return new ToStringBuilder(this).append("pid", this.pid).append("map", this.map).toString();
|
||||||
.append("map", this.map).toString();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
object.pid = "dummy-text";
|
object.pid = "dummy-text";
|
||||||
|
|
|
@ -41,17 +41,19 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
static final String staticString2 = "staticString2";
|
static final String staticString2 = "staticString2";
|
||||||
static final int staticInt2 = 67890;
|
static final int staticInt2 = 67890;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Points out failure to print anything from appendToString methods using MULTI_LINE_STYLE.
|
* Points out failure to print anything from appendToString methods using MULTI_LINE_STYLE. See issue LANG-372.
|
||||||
* See issue LANG-372.
|
|
||||||
*/
|
*/
|
||||||
final class MultiLineTestObject {
|
final class MultiLineTestObject {
|
||||||
Integer i = Integer.valueOf(31337);
|
Integer i = Integer.valueOf(31337);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new ToStringBuilder(this).append("testInt", i).toString();
|
return new ToStringBuilder(this).append("testInt", i).toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ObjectCycle {
|
static class ObjectCycle {
|
||||||
Object obj;
|
Object obj;
|
||||||
|
|
||||||
|
@ -68,7 +70,9 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
return ToStringBuilder.reflectionToString(this);
|
return ToStringBuilder.reflectionToString(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Inner inner = new Inner();
|
Inner inner = new Inner();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return ToStringBuilder.reflectionToString(this);
|
return ToStringBuilder.reflectionToString(this);
|
||||||
|
@ -248,12 +252,9 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
final Boolean objectToAppend2 = Boolean.TRUE;
|
final Boolean objectToAppend2 = Boolean.TRUE;
|
||||||
final Object objectToAppend3 = new Object();
|
final Object objectToAppend3 = new Object();
|
||||||
|
|
||||||
assertEquals(baseStr + "[" + toBaseString(objectToAppend1) + "]",
|
assertEquals(baseStr + "[" + toBaseString(objectToAppend1) + "]", new ToStringBuilder(base).appendAsObjectToString(objectToAppend1).toString());
|
||||||
new ToStringBuilder(base).appendAsObjectToString(objectToAppend1).toString());
|
assertEquals(baseStr + "[" + toBaseString(objectToAppend2) + "]", new ToStringBuilder(base).appendAsObjectToString(objectToAppend2).toString());
|
||||||
assertEquals(baseStr + "[" + toBaseString(objectToAppend2) + "]",
|
assertEquals(baseStr + "[" + toBaseString(objectToAppend3) + "]", new ToStringBuilder(base).appendAsObjectToString(objectToAppend3).toString());
|
||||||
new ToStringBuilder(base).appendAsObjectToString(objectToAppend2).toString());
|
|
||||||
assertEquals(baseStr + "[" + toBaseString(objectToAppend3) + "]",
|
|
||||||
new ToStringBuilder(base).appendAsObjectToString(objectToAppend3).toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -266,10 +267,8 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendBooleanArrayWithFieldName() {
|
public void testAppendBooleanArrayWithFieldName() {
|
||||||
final boolean[] array = { true, false, false };
|
final boolean[] array = { true, false, false };
|
||||||
assertEquals(baseStr + "[flags={true,false,false}]",
|
assertEquals(baseStr + "[flags={true,false,false}]", new ToStringBuilder(base).append("flags", array).toString());
|
||||||
new ToStringBuilder(base).append("flags", array).toString());
|
assertEquals(baseStr + "[flags=<null>]", new ToStringBuilder(base).append("flags", (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[flags=<null>]",
|
|
||||||
new ToStringBuilder(base).append("flags", (boolean[]) null).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[{true,false,false}]", new ToStringBuilder(base).append(null, array).toString());
|
assertEquals(baseStr + "[{true,false,false}]", new ToStringBuilder(base).append(null, array).toString());
|
||||||
}
|
}
|
||||||
|
@ -277,12 +276,9 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendBooleanArrayWithFieldNameAndFullDetail() {
|
public void testAppendBooleanArrayWithFieldNameAndFullDetail() {
|
||||||
final boolean[] array = { true, false, false };
|
final boolean[] array = { true, false, false };
|
||||||
assertEquals(baseStr + "[flags={true,false,false}]",
|
assertEquals(baseStr + "[flags={true,false,false}]", new ToStringBuilder(base).append("flags", array, true).toString());
|
||||||
new ToStringBuilder(base).append("flags", array, true).toString());
|
assertEquals(baseStr + "[length=<size=3>]", new ToStringBuilder(base).append("length", array, false).toString());
|
||||||
assertEquals(baseStr + "[length=<size=3>]",
|
assertEquals(baseStr + "[flags=<null>]", new ToStringBuilder(base).append("flags", (boolean[]) null, true).toString());
|
||||||
new ToStringBuilder(base).append("length", array, false).toString());
|
|
||||||
assertEquals(baseStr + "[flags=<null>]",
|
|
||||||
new ToStringBuilder(base).append("flags", (boolean[]) null, true).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
||||||
assertEquals(baseStr + "[<size=3>]", new ToStringBuilder(base).append(null, array, false).toString());
|
assertEquals(baseStr + "[<size=3>]", new ToStringBuilder(base).append(null, array, false).toString());
|
||||||
}
|
}
|
||||||
|
@ -293,8 +289,7 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
public void testAppendByteArrayWithFieldName() {
|
public void testAppendByteArrayWithFieldName() {
|
||||||
final byte[] array = { 1, 2, -3, 4 };
|
final byte[] array = { 1, 2, -3, 4 };
|
||||||
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString());
|
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString());
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString());
|
assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString());
|
||||||
}
|
}
|
||||||
|
@ -302,12 +297,9 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendByteArrayWithFieldNameAndFullDetail() {
|
public void testAppendByteArrayWithFieldNameAndFullDetail() {
|
||||||
final byte[] array = { 1, 2, -3, 4 };
|
final byte[] array = { 1, 2, -3, 4 };
|
||||||
assertEquals(baseStr + "[values={1,2,-3,4}]",
|
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array, true).toString());
|
||||||
new ToStringBuilder(base).append("values", array, true).toString());
|
assertEquals(baseStr + "[length=<size=4>]", new ToStringBuilder(base).append("length", array, false).toString());
|
||||||
assertEquals(baseStr + "[length=<size=4>]",
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
||||||
new ToStringBuilder(base).append("length", array, false).toString());
|
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
||||||
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
||||||
}
|
}
|
||||||
|
@ -317,8 +309,7 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
final char[] array = { 'A', '2', '_', 'D' };
|
final char[] array = { 'A', '2', '_', 'D' };
|
||||||
assertEquals(baseStr + "[chars={A,2,_,D}]", new ToStringBuilder(base).append("chars", array).toString());
|
assertEquals(baseStr + "[chars={A,2,_,D}]", new ToStringBuilder(base).append("chars", array).toString());
|
||||||
assertEquals(baseStr + "[letters={A,2,_,D}]", new ToStringBuilder(base).append("letters", array).toString());
|
assertEquals(baseStr + "[letters={A,2,_,D}]", new ToStringBuilder(base).append("letters", array).toString());
|
||||||
assertEquals(baseStr + "[flags=<null>]",
|
assertEquals(baseStr + "[flags=<null>]", new ToStringBuilder(base).append("flags", (boolean[]) null).toString());
|
||||||
new ToStringBuilder(base).append("flags", (boolean[]) null).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[{A,2,_,D}]", new ToStringBuilder(base).append(null, array).toString());
|
assertEquals(baseStr + "[{A,2,_,D}]", new ToStringBuilder(base).append(null, array).toString());
|
||||||
}
|
}
|
||||||
|
@ -327,10 +318,8 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
public void testAppendCharArrayWithFieldNameAndFullDetail() {
|
public void testAppendCharArrayWithFieldNameAndFullDetail() {
|
||||||
final char[] array = { 'A', '2', '_', 'D' };
|
final char[] array = { 'A', '2', '_', 'D' };
|
||||||
assertEquals(baseStr + "[chars={A,2,_,D}]", new ToStringBuilder(base).append("chars", array, true).toString());
|
assertEquals(baseStr + "[chars={A,2,_,D}]", new ToStringBuilder(base).append("chars", array, true).toString());
|
||||||
assertEquals(baseStr + "[letters=<size=4>]",
|
assertEquals(baseStr + "[letters=<size=4>]", new ToStringBuilder(base).append("letters", array, false).toString());
|
||||||
new ToStringBuilder(base).append("letters", array, false).toString());
|
assertEquals(baseStr + "[flags=<null>]", new ToStringBuilder(base).append("flags", (boolean[]) null, true).toString());
|
||||||
assertEquals(baseStr + "[flags=<null>]",
|
|
||||||
new ToStringBuilder(base).append("flags", (boolean[]) null, true).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
||||||
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
||||||
}
|
}
|
||||||
|
@ -338,10 +327,8 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendDoubleArrayWithFieldName() {
|
public void testAppendDoubleArrayWithFieldName() {
|
||||||
final double[] array = { 1.0, 2.9876, -3.00001, 4.3 };
|
final double[] array = { 1.0, 2.9876, -3.00001, 4.3 };
|
||||||
assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]",
|
assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append("values", array).toString());
|
||||||
new ToStringBuilder(base).append("values", array).toString());
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(null, array).toString());
|
assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(null, array).toString());
|
||||||
}
|
}
|
||||||
|
@ -349,12 +336,9 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendDoubleArrayWithFieldNameAndFullDetail() {
|
public void testAppendDoubleArrayWithFieldNameAndFullDetail() {
|
||||||
final double[] array = { 1.0, 2.9876, -3.00001, 4.3 };
|
final double[] array = { 1.0, 2.9876, -3.00001, 4.3 };
|
||||||
assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]",
|
assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append("values", array, true).toString());
|
||||||
new ToStringBuilder(base).append("values", array, true).toString());
|
assertEquals(baseStr + "[length=<size=4>]", new ToStringBuilder(base).append("length", array, false).toString());
|
||||||
assertEquals(baseStr + "[length=<size=4>]",
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
||||||
new ToStringBuilder(base).append("length", array, false).toString());
|
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
||||||
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
||||||
}
|
}
|
||||||
|
@ -362,10 +346,8 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendFloatArrayWithFieldName() {
|
public void testAppendFloatArrayWithFieldName() {
|
||||||
final float[] array = { 1.0f, 2.9876f, -3.00001f, 4.3f };
|
final float[] array = { 1.0f, 2.9876f, -3.00001f, 4.3f };
|
||||||
assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]",
|
assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append("values", array).toString());
|
||||||
new ToStringBuilder(base).append("values", array).toString());
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(null, array).toString());
|
assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(null, array).toString());
|
||||||
}
|
}
|
||||||
|
@ -373,12 +355,9 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendFloatArrayWithFieldNameAndFullDetail() {
|
public void testAppendFloatArrayWithFieldNameAndFullDetail() {
|
||||||
final float[] array = { 1.0f, 2.9876f, -3.00001f, 4.3f };
|
final float[] array = { 1.0f, 2.9876f, -3.00001f, 4.3f };
|
||||||
assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]",
|
assertEquals(baseStr + "[values={1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append("values", array, true).toString());
|
||||||
new ToStringBuilder(base).append("values", array, true).toString());
|
assertEquals(baseStr + "[length=<size=4>]", new ToStringBuilder(base).append("length", array, false).toString());
|
||||||
assertEquals(baseStr + "[length=<size=4>]",
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
||||||
new ToStringBuilder(base).append("length", array, false).toString());
|
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
||||||
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
||||||
}
|
}
|
||||||
|
@ -387,8 +366,7 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
public void testAppendIntArrayWithFieldName() {
|
public void testAppendIntArrayWithFieldName() {
|
||||||
final int[] array = { 1, 2, -3, 4 };
|
final int[] array = { 1, 2, -3, 4 };
|
||||||
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString());
|
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString());
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString());
|
assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString());
|
||||||
}
|
}
|
||||||
|
@ -396,12 +374,9 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendIntArrayWithFieldNameAndFullDetail() {
|
public void testAppendIntArrayWithFieldNameAndFullDetail() {
|
||||||
final int[] array = { 1, 2, -3, 4 };
|
final int[] array = { 1, 2, -3, 4 };
|
||||||
assertEquals(baseStr + "[values={1,2,-3,4}]",
|
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array, true).toString());
|
||||||
new ToStringBuilder(base).append("values", array, true).toString());
|
assertEquals(baseStr + "[length=<size=4>]", new ToStringBuilder(base).append("length", array, false).toString());
|
||||||
assertEquals(baseStr + "[length=<size=4>]",
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
||||||
new ToStringBuilder(base).append("length", array, false).toString());
|
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
||||||
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
||||||
}
|
}
|
||||||
|
@ -410,8 +385,7 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
public void testAppendLongArrayWithFieldName() {
|
public void testAppendLongArrayWithFieldName() {
|
||||||
final long[] array = { 1, 2, -3, 4 };
|
final long[] array = { 1, 2, -3, 4 };
|
||||||
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString());
|
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString());
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString());
|
assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString());
|
||||||
}
|
}
|
||||||
|
@ -419,12 +393,9 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendLongArrayWithFieldNameAndFullDetail() {
|
public void testAppendLongArrayWithFieldNameAndFullDetail() {
|
||||||
final long[] array = { 1, 2, -3, 4 };
|
final long[] array = { 1, 2, -3, 4 };
|
||||||
assertEquals(baseStr + "[values={1,2,-3,4}]",
|
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array, true).toString());
|
||||||
new ToStringBuilder(base).append("values", array, true).toString());
|
assertEquals(baseStr + "[length=<size=4>]", new ToStringBuilder(base).append("length", array, false).toString());
|
||||||
assertEquals(baseStr + "[length=<size=4>]",
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
||||||
new ToStringBuilder(base).append("length", array, false).toString());
|
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
||||||
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
||||||
}
|
}
|
||||||
|
@ -432,10 +403,8 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendObjectArrayWithFieldName() {
|
public void testAppendObjectArrayWithFieldName() {
|
||||||
final Object[] array = { null, base, new int[] { 3, 6 } };
|
final Object[] array = { null, base, new int[] { 3, 6 } };
|
||||||
assertEquals(baseStr + "[values={<null>,5,{3,6}}]",
|
assertEquals(baseStr + "[values={<null>,5,{3,6}}]", new ToStringBuilder(base).append("values", array).toString());
|
||||||
new ToStringBuilder(base).append("values", array).toString());
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append(null, array).toString());
|
assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append(null, array).toString());
|
||||||
}
|
}
|
||||||
|
@ -443,12 +412,9 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendObjectArrayWithFieldNameAndFullDetail() {
|
public void testAppendObjectArrayWithFieldNameAndFullDetail() {
|
||||||
final Object[] array = { null, base, new int[] { 3, 6 } };
|
final Object[] array = { null, base, new int[] { 3, 6 } };
|
||||||
assertEquals(baseStr + "[values={<null>,5,{3,6}}]",
|
assertEquals(baseStr + "[values={<null>,5,{3,6}}]", new ToStringBuilder(base).append("values", array, true).toString());
|
||||||
new ToStringBuilder(base).append("values", array, true).toString());
|
assertEquals(baseStr + "[length=<size=3>]", new ToStringBuilder(base).append("length", array, false).toString());
|
||||||
assertEquals(baseStr + "[length=<size=3>]",
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
||||||
new ToStringBuilder(base).append("length", array, false).toString());
|
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
||||||
assertEquals(baseStr + "[<size=3>]", new ToStringBuilder(base).append(null, array, false).toString());
|
assertEquals(baseStr + "[<size=3>]", new ToStringBuilder(base).append(null, array, false).toString());
|
||||||
}
|
}
|
||||||
|
@ -459,8 +425,7 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
public void testAppendShortArrayWithFieldName() {
|
public void testAppendShortArrayWithFieldName() {
|
||||||
final short[] array = { 1, 2, -3, 4 };
|
final short[] array = { 1, 2, -3, 4 };
|
||||||
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString());
|
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array).toString());
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null).toString());
|
||||||
assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString());
|
assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(null, array).toString());
|
||||||
}
|
}
|
||||||
|
@ -468,12 +433,9 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendShortArrayWithFieldNameAndFullDetail() {
|
public void testAppendShortArrayWithFieldNameAndFullDetail() {
|
||||||
final short[] array = { 1, 2, -3, 4 };
|
final short[] array = { 1, 2, -3, 4 };
|
||||||
assertEquals(baseStr + "[values={1,2,-3,4}]",
|
assertEquals(baseStr + "[values={1,2,-3,4}]", new ToStringBuilder(base).append("values", array, true).toString());
|
||||||
new ToStringBuilder(base).append("values", array, true).toString());
|
assertEquals(baseStr + "[length=<size=4>]", new ToStringBuilder(base).append("length", array, false).toString());
|
||||||
assertEquals(baseStr + "[length=<size=4>]",
|
assertEquals(baseStr + "[values=<null>]", new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
||||||
new ToStringBuilder(base).append("length", array, false).toString());
|
|
||||||
assertEquals(baseStr + "[values=<null>]",
|
|
||||||
new ToStringBuilder(base).append("values", (boolean[]) null, true).toString());
|
|
||||||
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(null, (boolean[]) null, false).toString());
|
||||||
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
assertEquals(baseStr + "[<size=4>]", new ToStringBuilder(base).append(null, array, false).toString());
|
||||||
}
|
}
|
||||||
|
@ -501,8 +463,7 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAppendToStringUsingMultiLineStyle() {
|
public void testAppendToStringUsingMultiLineStyle() {
|
||||||
final MultiLineTestObject obj = new MultiLineTestObject();
|
final MultiLineTestObject obj = new MultiLineTestObject();
|
||||||
final ToStringBuilder testBuilder = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
|
final ToStringBuilder testBuilder = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).appendToString(obj.toString());
|
||||||
.appendToString(obj.toString());
|
|
||||||
assertEquals(-1, testBuilder.toString().indexOf("testInt=31337"));
|
assertEquals(-1, testBuilder.toString().indexOf("testInt=31337"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -693,17 +654,13 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testInheritedReflectionStatics() {
|
public void testInheritedReflectionStatics() {
|
||||||
final InheritedReflectionStaticFieldsFixture instance1 = new InheritedReflectionStaticFieldsFixture();
|
final InheritedReflectionStaticFieldsFixture instance1 = new InheritedReflectionStaticFieldsFixture();
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1) + "[staticInt2=67890,staticString2=staticString2]",
|
||||||
this.toBaseString(instance1) + "[staticInt2=67890,staticString2=staticString2]",
|
|
||||||
ReflectionToStringBuilder.toString(instance1, null, false, true, InheritedReflectionStaticFieldsFixture.class));
|
ReflectionToStringBuilder.toString(instance1, null, false, true, InheritedReflectionStaticFieldsFixture.class));
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1) + "[staticInt2=67890,staticString2=staticString2,staticInt=12345,staticString=staticString]",
|
||||||
this.toBaseString(instance1) + "[staticInt2=67890,staticString2=staticString2,staticInt=12345,staticString=staticString]",
|
|
||||||
ReflectionToStringBuilder.toString(instance1, null, false, true, SimpleReflectionStaticFieldsFixture.class));
|
ReflectionToStringBuilder.toString(instance1, null, false, true, SimpleReflectionStaticFieldsFixture.class));
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1) + "[staticInt2=67890,staticString2=staticString2,staticInt=12345,staticString=staticString]",
|
||||||
this.toBaseString(instance1) + "[staticInt2=67890,staticString2=staticString2,staticInt=12345,staticString=staticString]",
|
|
||||||
this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
|
this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1) + "[staticInt2=67890,staticString2=staticString2,staticInt=12345,staticString=staticString]",
|
||||||
this.toBaseString(instance1) + "[staticInt2=67890,staticString2=staticString2,staticInt=12345,staticString=staticString]",
|
|
||||||
this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
|
this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -825,29 +782,16 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test a nasty combination of arrays and Objects pointing to each other.
|
* Test a nasty combination of arrays and Objects pointing to each other. objects[0] -> SimpleReflectionTestFixture[ o -> objects ]
|
||||||
* objects[0] -> SimpleReflectionTestFixture[ o -> objects ]
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testReflectionArrayAndObjectCycle() {
|
public void testReflectionArrayAndObjectCycle() {
|
||||||
final Object[] objects = new Object[1];
|
final Object[] objects = new Object[1];
|
||||||
final SimpleReflectionTestFixture simple = new SimpleReflectionTestFixture(objects);
|
final SimpleReflectionTestFixture simple = new SimpleReflectionTestFixture(objects);
|
||||||
objects[0] = simple;
|
objects[0] = simple;
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(objects) + "[{" + this.toBaseString(simple) + "[o=" + this.toBaseString(objects) + "]" + "}]",
|
||||||
this.toBaseString(objects)
|
|
||||||
+ "[{"
|
|
||||||
+ this.toBaseString(simple)
|
|
||||||
+ "[o="
|
|
||||||
+ this.toBaseString(objects)
|
|
||||||
+ "]"
|
|
||||||
+ "}]",
|
|
||||||
ToStringBuilder.reflectionToString(objects));
|
ToStringBuilder.reflectionToString(objects));
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(simple) + "[o={" + this.toBaseString(simple) + "}]", ToStringBuilder.reflectionToString(simple));
|
||||||
this.toBaseString(simple)
|
|
||||||
+ "[o={"
|
|
||||||
+ this.toBaseString(simple)
|
|
||||||
+ "}]",
|
|
||||||
ToStringBuilder.reflectionToString(simple));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -858,17 +802,7 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
objects[1][0] = objects;
|
objects[1][0] = objects;
|
||||||
objects[1][1] = objects;
|
objects[1][1] = objects;
|
||||||
final String basicToString = this.toBaseString(objects);
|
final String basicToString = this.toBaseString(objects);
|
||||||
assertEquals(
|
assertEquals(basicToString + "[{{" + basicToString + "," + basicToString + "},{" + basicToString + "," + basicToString + "}}]",
|
||||||
basicToString
|
|
||||||
+ "[{{"
|
|
||||||
+ basicToString
|
|
||||||
+ ","
|
|
||||||
+ basicToString
|
|
||||||
+ "},{"
|
|
||||||
+ basicToString
|
|
||||||
+ ","
|
|
||||||
+ basicToString
|
|
||||||
+ "}}]",
|
|
||||||
ToStringBuilder.reflectionToString(objects));
|
ToStringBuilder.reflectionToString(objects));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -879,9 +813,7 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
public void testReflectionArrayCycle() {
|
public void testReflectionArrayCycle() {
|
||||||
final Object[] objects = new Object[1];
|
final Object[] objects = new Object[1];
|
||||||
objects[0] = objects;
|
objects[0] = objects;
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(objects) + "[{" + this.toBaseString(objects) + "}]", ToStringBuilder.reflectionToString(objects));
|
||||||
this.toBaseString(objects) + "[{" + this.toBaseString(objects) + "}]",
|
|
||||||
ToStringBuilder.reflectionToString(objects));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -893,12 +825,8 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
final Object[] objectsLevel2 = new Object[1];
|
final Object[] objectsLevel2 = new Object[1];
|
||||||
objects[0] = objectsLevel2;
|
objects[0] = objectsLevel2;
|
||||||
objectsLevel2[0] = objects;
|
objectsLevel2[0] = objects;
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(objects) + "[{{" + this.toBaseString(objects) + "}}]", ToStringBuilder.reflectionToString(objects));
|
||||||
this.toBaseString(objects) + "[{{" + this.toBaseString(objects) + "}}]",
|
assertEquals(this.toBaseString(objectsLevel2) + "[{{" + this.toBaseString(objectsLevel2) + "}}]", ToStringBuilder.reflectionToString(objectsLevel2));
|
||||||
ToStringBuilder.reflectionToString(objects));
|
|
||||||
assertEquals(
|
|
||||||
this.toBaseString(objectsLevel2) + "[{{" + this.toBaseString(objectsLevel2) + "}}]",
|
|
||||||
ToStringBuilder.reflectionToString(objectsLevel2));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1044,7 +972,8 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
// LANG-1337 without this, the generated string can differ depending on the JVM version/vendor
|
// LANG-1337 without this, the generated string can differ depending on the JVM version/vendor
|
||||||
final List<Object> list = new ArrayList<>(ARRAYLIST_INITIAL_CAPACITY);
|
final List<Object> list = new ArrayList<>(ARRAYLIST_INITIAL_CAPACITY);
|
||||||
final String baseString = this.toBaseString(list);
|
final String baseString = this.toBaseString(list);
|
||||||
final String expectedWithTransients = baseString + "[elementData={<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>},size=0,modCount=0]";
|
final String expectedWithTransients = baseString
|
||||||
|
+ "[elementData={<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>},size=0,modCount=0]";
|
||||||
final String toStringWithTransients = ToStringBuilder.reflectionToString(list, null, true);
|
final String toStringWithTransients = ToStringBuilder.reflectionToString(list, null, true);
|
||||||
if (!expectedWithTransients.equals(toStringWithTransients)) {
|
if (!expectedWithTransients.equals(toStringWithTransients)) {
|
||||||
assertEquals(expectedWithTransients, toStringWithTransients);
|
assertEquals(expectedWithTransients, toStringWithTransients);
|
||||||
|
@ -1123,9 +1052,7 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
final ReflectionTestCycleB b = new ReflectionTestCycleB();
|
final ReflectionTestCycleB b = new ReflectionTestCycleB();
|
||||||
a.b = b;
|
a.b = b;
|
||||||
b.a = a;
|
b.a = a;
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(a) + "[b=" + this.toBaseString(b) + "[a=" + this.toBaseString(a) + "]]", a.toString());
|
||||||
this.toBaseString(a) + "[b=" + this.toBaseString(b) + "[a=" + this.toBaseString(a) + "]]",
|
|
||||||
a.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1152,28 +1079,26 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testReflectionStatics() {
|
public void testReflectionStatics() {
|
||||||
final ReflectionStaticFieldsFixture instance1 = new ReflectionStaticFieldsFixture();
|
final ReflectionStaticFieldsFixture instance1 = new ReflectionStaticFieldsFixture();
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1) + "[instanceInt=67890,instanceString=instanceString,staticInt=12345,staticString=staticString]",
|
||||||
this.toBaseString(instance1) + "[instanceInt=67890,instanceString=instanceString,staticInt=12345,staticString=staticString]",
|
|
||||||
ReflectionToStringBuilder.toString(instance1, null, false, true, ReflectionStaticFieldsFixture.class));
|
ReflectionToStringBuilder.toString(instance1, null, false, true, ReflectionStaticFieldsFixture.class));
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1)
|
||||||
this.toBaseString(instance1) + "[instanceInt=67890,instanceString=instanceString,staticInt=12345,staticString=staticString,staticTransientInt=54321,staticTransientString=staticTransientString,transientInt=98765,transientString=transientString]",
|
+ "[instanceInt=67890,instanceString=instanceString,staticInt=12345,staticString=staticString,staticTransientInt=54321,staticTransientString=staticTransientString,transientInt=98765,transientString=transientString]",
|
||||||
ReflectionToStringBuilder.toString(instance1, null, true, true, ReflectionStaticFieldsFixture.class));
|
ReflectionToStringBuilder.toString(instance1, null, true, true, ReflectionStaticFieldsFixture.class));
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1) + "[instanceInt=67890,instanceString=instanceString,staticInt=12345,staticString=staticString]",
|
||||||
this.toBaseString(instance1) + "[instanceInt=67890,instanceString=instanceString,staticInt=12345,staticString=staticString]",
|
|
||||||
this.toStringWithStatics(instance1, null, ReflectionStaticFieldsFixture.class));
|
this.toStringWithStatics(instance1, null, ReflectionStaticFieldsFixture.class));
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1) + "[instanceInt=67890,instanceString=instanceString,staticInt=12345,staticString=staticString]",
|
||||||
this.toBaseString(instance1) + "[instanceInt=67890,instanceString=instanceString,staticInt=12345,staticString=staticString]",
|
|
||||||
this.toStringWithStatics(instance1, null, ReflectionStaticFieldsFixture.class));
|
this.toStringWithStatics(instance1, null, ReflectionStaticFieldsFixture.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test a class that defines an ivar pointing to itself. This test was
|
* Test a class that defines an ivar pointing to itself. This test was created to show that handling cyclical object resulted in a missing endFieldSeparator
|
||||||
* created to show that handling cyclical object resulted in a missing endFieldSeparator call.
|
* call.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testSelfInstanceTwoVarsReflectionObjectCycle() {
|
public void testSelfInstanceTwoVarsReflectionObjectCycle() {
|
||||||
final SelfInstanceTwoVarsReflectionTestFixture test = new SelfInstanceTwoVarsReflectionTestFixture();
|
final SelfInstanceTwoVarsReflectionTestFixture test = new SelfInstanceTwoVarsReflectionTestFixture();
|
||||||
assertEquals(this.toBaseString(test) + "[otherType=" + test.getOtherType().toString() + ",typeIsSelf=" + this.toBaseString(test) + "]", test.toString());
|
assertEquals(this.toBaseString(test) + "[otherType=" + test.getOtherType().toString() + ",typeIsSelf=" + this.toBaseString(test) + "]",
|
||||||
|
test.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1230,22 +1155,19 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testSimpleReflectionStatics() {
|
public void testSimpleReflectionStatics() {
|
||||||
final SimpleReflectionStaticFieldsFixture instance1 = new SimpleReflectionStaticFieldsFixture();
|
final SimpleReflectionStaticFieldsFixture instance1 = new SimpleReflectionStaticFieldsFixture();
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1) + "[staticInt=12345,staticString=staticString]",
|
||||||
this.toBaseString(instance1) + "[staticInt=12345,staticString=staticString]",
|
|
||||||
ReflectionToStringBuilder.toString(instance1, null, false, true, SimpleReflectionStaticFieldsFixture.class));
|
ReflectionToStringBuilder.toString(instance1, null, false, true, SimpleReflectionStaticFieldsFixture.class));
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1) + "[staticInt=12345,staticString=staticString]",
|
||||||
this.toBaseString(instance1) + "[staticInt=12345,staticString=staticString]",
|
|
||||||
ReflectionToStringBuilder.toString(instance1, null, true, true, SimpleReflectionStaticFieldsFixture.class));
|
ReflectionToStringBuilder.toString(instance1, null, true, true, SimpleReflectionStaticFieldsFixture.class));
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1) + "[staticInt=12345,staticString=staticString]",
|
||||||
this.toBaseString(instance1) + "[staticInt=12345,staticString=staticString]",
|
|
||||||
this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
|
this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
|
||||||
assertEquals(
|
assertEquals(this.toBaseString(instance1) + "[staticInt=12345,staticString=staticString]",
|
||||||
this.toBaseString(instance1) + "[staticInt=12345,staticString=staticString]",
|
|
||||||
this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
|
this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the same toString() as Object.toString().
|
* Create the same toString() as Object.toString().
|
||||||
|
*
|
||||||
* @param o the object to create the string for.
|
* @param o the object to create the string for.
|
||||||
* @return a String in the Object.toString format.
|
* @return a String in the Object.toString format.
|
||||||
*/
|
*/
|
||||||
|
@ -1254,28 +1176,25 @@ public class ToStringBuilderTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>This method uses reflection to build a suitable
|
* This method uses reflection to build a suitable {@code toString} value which includes static fields.
|
||||||
* {@code toString} value which includes static fields.</p>
|
* <p>
|
||||||
*
|
* It uses {@code AccessibleObject.setAccessible} to gain access to private fields. This means that it will throw a security exception if run under a
|
||||||
* <p>It uses {@code AccessibleObject.setAccessible} to gain access to private
|
* security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.
|
||||||
* fields. This means that it will throw a security exception if run
|
* </p>
|
||||||
* under a security manager, if the permissions are not set up correctly.
|
* <p>
|
||||||
* It is also not as efficient as testing explicitly.</p>
|
* Transient fields are not output.
|
||||||
*
|
* </p>
|
||||||
* <p>Transient fields are not output.</p>
|
* <p>
|
||||||
*
|
* Superclass fields will be appended up to and including the specified superclass. A null superclass is treated as {@link Object}.
|
||||||
* <p>Superclass fields will be appended up to and including the specified superclass.
|
* </p>
|
||||||
* A null superclass is treated as {@link Object}.</p>
|
* <p>
|
||||||
*
|
* If the style is {@code null}, the default {@code ToStringStyle} is used.
|
||||||
* <p>If the style is {@code null}, the default
|
* </p>
|
||||||
* {@code ToStringStyle} is used.</p>
|
|
||||||
*
|
*
|
||||||
* @param <T> the type of the output object
|
* @param <T> the type of the output object
|
||||||
* @param object the Object to be output
|
* @param object the Object to be output
|
||||||
* @param style the style of the {@code toString} to create,
|
* @param style the style of the {@code toString} to create, may be {@code null}
|
||||||
* may be {@code null}
|
* @param reflectUpToClass the superclass to reflect up to (inclusive), may be {@code null}
|
||||||
* @param reflectUpToClass the superclass to reflect up to (inclusive),
|
|
||||||
* may be {@code null}
|
|
||||||
* @return the String result
|
* @return the String result
|
||||||
* @throws IllegalArgumentException if the Object is {@code null}
|
* @throws IllegalArgumentException if the Object is {@code null}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -148,9 +148,7 @@ public class FractionTest extends AbstractLangTest {
|
||||||
assertThrows(ArithmeticException.class, () -> overflower.add(Fraction.ONE)); // should overflow
|
assertThrows(ArithmeticException.class, () -> overflower.add(Fraction.ONE)); // should overflow
|
||||||
|
|
||||||
// denominator should not be a multiple of 2 or 3 to trigger overflow
|
// denominator should not be a multiple of 2 or 3 to trigger overflow
|
||||||
assertThrows(
|
assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Integer.MIN_VALUE, 5).add(Fraction.getFraction(-1, 5)));
|
||||||
ArithmeticException.class,
|
|
||||||
() -> Fraction.getFraction(Integer.MIN_VALUE, 5).add(Fraction.getFraction(-1, 5)));
|
|
||||||
|
|
||||||
final Fraction maxValue = Fraction.getFraction(-Integer.MAX_VALUE, 1);
|
final Fraction maxValue = Fraction.getFraction(-Integer.MAX_VALUE, 1);
|
||||||
assertThrows(ArithmeticException.class, () -> maxValue.add(maxValue));
|
assertThrows(ArithmeticException.class, () -> maxValue.add(maxValue));
|
||||||
|
@ -1033,26 +1031,17 @@ public class FractionTest extends AbstractLangTest {
|
||||||
assertEquals(1, f.getDenominator());
|
assertEquals(1, f.getDenominator());
|
||||||
|
|
||||||
// Should overflow
|
// Should overflow
|
||||||
assertThrows(
|
assertThrows(ArithmeticException.class, () -> Fraction.getFraction(1, Integer.MAX_VALUE).subtract(Fraction.getFraction(1, Integer.MAX_VALUE - 1)));
|
||||||
ArithmeticException.class,
|
|
||||||
() -> Fraction.getFraction(1, Integer.MAX_VALUE).subtract(Fraction.getFraction(1, Integer.MAX_VALUE - 1)));
|
|
||||||
|
|
||||||
// denominator should not be a multiple of 2 or 3 to trigger overflow
|
// denominator should not be a multiple of 2 or 3 to trigger overflow
|
||||||
assertThrows(
|
assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Integer.MIN_VALUE, 5).subtract(Fraction.getFraction(1, 5)));
|
||||||
ArithmeticException.class,
|
|
||||||
() -> Fraction.getFraction(Integer.MIN_VALUE, 5).subtract(Fraction.getFraction(1, 5)));
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Integer.MIN_VALUE, 1).subtract(Fraction.ONE));
|
||||||
ArithmeticException.class, () -> Fraction.getFraction(Integer.MIN_VALUE, 1).subtract(Fraction.ONE));
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Integer.MAX_VALUE, 1).subtract(Fraction.ONE.negate()));
|
||||||
ArithmeticException.class,
|
|
||||||
() -> Fraction.getFraction(Integer.MAX_VALUE, 1).subtract(Fraction.ONE.negate()));
|
|
||||||
|
|
||||||
// Should overflow
|
// Should overflow
|
||||||
assertThrows(
|
assertThrows(ArithmeticException.class, () -> Fraction.getFraction(3, 327680).subtract(Fraction.getFraction(2, 59049)));
|
||||||
ArithmeticException.class,
|
|
||||||
() -> Fraction.getFraction(3, 327680).subtract(Fraction.getFraction(2, 59049)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -38,7 +38,8 @@ import org.junit.jupiter.api.Test;
|
||||||
* Unit tests ConstructorUtils
|
* Unit tests ConstructorUtils
|
||||||
*/
|
*/
|
||||||
public class ConstructorUtilsTest extends AbstractLangTest {
|
public class ConstructorUtilsTest extends AbstractLangTest {
|
||||||
private static class BaseClass {}
|
private static class BaseClass {
|
||||||
|
}
|
||||||
|
|
||||||
static class PrivateClass {
|
static class PrivateClass {
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
|
@ -52,7 +53,8 @@ public class ConstructorUtilsTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class SubClass extends BaseClass {}
|
private static final class SubClass extends BaseClass {
|
||||||
|
}
|
||||||
|
|
||||||
public static class TestBean {
|
public static class TestBean {
|
||||||
private final String toString;
|
private final String toString;
|
||||||
|
@ -128,10 +130,8 @@ public class ConstructorUtilsTest extends AbstractLangTest {
|
||||||
classCache = new HashMap<>();
|
classCache = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void expectMatchingAccessibleConstructorParameterTypes(final Class<?> cls,
|
private void expectMatchingAccessibleConstructorParameterTypes(final Class<?> cls, final Class<?>[] requestTypes, final Class<?>[] actualTypes) {
|
||||||
final Class<?>[] requestTypes, final Class<?>[] actualTypes) {
|
final Constructor<?> c = ConstructorUtils.getMatchingAccessibleConstructor(cls, requestTypes);
|
||||||
final Constructor<?> c = ConstructorUtils.getMatchingAccessibleConstructor(cls,
|
|
||||||
requestTypes);
|
|
||||||
assertArrayEquals(actualTypes, c.getParameterTypes(), toString(c.getParameterTypes()) + " not equals " + toString(actualTypes));
|
assertArrayEquals(actualTypes, c.getParameterTypes(), toString(c.getParameterTypes()) + " not equals " + toString(actualTypes));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,134 +156,84 @@ public class ConstructorUtilsTest extends AbstractLangTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAccessibleConstructor() throws Exception {
|
public void testGetAccessibleConstructor() throws Exception {
|
||||||
assertNotNull(ConstructorUtils.getAccessibleConstructor(Object.class
|
assertNotNull(ConstructorUtils.getAccessibleConstructor(Object.class.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY)));
|
||||||
.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY)));
|
assertNull(ConstructorUtils.getAccessibleConstructor(PrivateClass.class.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY)));
|
||||||
assertNull(ConstructorUtils.getAccessibleConstructor(PrivateClass.class
|
|
||||||
.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY)));
|
|
||||||
assertNull(ConstructorUtils.getAccessibleConstructor(PrivateClass.PublicInnerClass.class));
|
assertNull(ConstructorUtils.getAccessibleConstructor(PrivateClass.PublicInnerClass.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAccessibleConstructorFromDescription() {
|
public void testGetAccessibleConstructorFromDescription() {
|
||||||
assertNotNull(ConstructorUtils.getAccessibleConstructor(Object.class,
|
assertNotNull(ConstructorUtils.getAccessibleConstructor(Object.class, ArrayUtils.EMPTY_CLASS_ARRAY));
|
||||||
ArrayUtils.EMPTY_CLASS_ARRAY));
|
assertNull(ConstructorUtils.getAccessibleConstructor(PrivateClass.class, ArrayUtils.EMPTY_CLASS_ARRAY));
|
||||||
assertNull(ConstructorUtils.getAccessibleConstructor(
|
|
||||||
PrivateClass.class, ArrayUtils.EMPTY_CLASS_ARRAY));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetMatchingAccessibleMethod() {
|
public void testGetMatchingAccessibleMethod() {
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, ArrayUtils.EMPTY_CLASS_ARRAY, ArrayUtils.EMPTY_CLASS_ARRAY);
|
||||||
ArrayUtils.EMPTY_CLASS_ARRAY, ArrayUtils.EMPTY_CLASS_ARRAY);
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, null, ArrayUtils.EMPTY_CLASS_ARRAY);
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, null,
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(String.class), singletonArray(String.class));
|
||||||
ArrayUtils.EMPTY_CLASS_ARRAY);
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Object.class), singletonArray(Object.class));
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Boolean.class), singletonArray(Object.class));
|
||||||
singletonArray(String.class), singletonArray(String.class));
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Byte.class), singletonArray(Integer.TYPE));
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Byte.TYPE), singletonArray(Integer.TYPE));
|
||||||
singletonArray(Object.class), singletonArray(Object.class));
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Short.class), singletonArray(Integer.TYPE));
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Short.TYPE), singletonArray(Integer.TYPE));
|
||||||
singletonArray(Boolean.class), singletonArray(Object.class));
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Character.class), singletonArray(Integer.TYPE));
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Character.TYPE), singletonArray(Integer.TYPE));
|
||||||
singletonArray(Byte.class), singletonArray(Integer.TYPE));
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Integer.class), singletonArray(Integer.class));
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Integer.TYPE), singletonArray(Integer.TYPE));
|
||||||
singletonArray(Byte.TYPE), singletonArray(Integer.TYPE));
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Long.class), singletonArray(Double.TYPE));
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Long.TYPE), singletonArray(Double.TYPE));
|
||||||
singletonArray(Short.class), singletonArray(Integer.TYPE));
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Float.class), singletonArray(Double.TYPE));
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Float.TYPE), singletonArray(Double.TYPE));
|
||||||
singletonArray(Short.TYPE), singletonArray(Integer.TYPE));
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Double.class), singletonArray(Double.TYPE));
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, singletonArray(Double.TYPE), singletonArray(Double.TYPE));
|
||||||
singletonArray(Character.class), singletonArray(Integer.TYPE));
|
expectMatchingAccessibleConstructorParameterTypes(TestBean.class, new Class<?>[] { SubClass.class, String[].class },
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
|
||||||
singletonArray(Character.TYPE), singletonArray(Integer.TYPE));
|
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
|
||||||
singletonArray(Integer.class), singletonArray(Integer.class));
|
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
|
||||||
singletonArray(Integer.TYPE), singletonArray(Integer.TYPE));
|
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
|
||||||
singletonArray(Long.class), singletonArray(Double.TYPE));
|
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
|
||||||
singletonArray(Long.TYPE), singletonArray(Double.TYPE));
|
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
|
||||||
singletonArray(Float.class), singletonArray(Double.TYPE));
|
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
|
||||||
singletonArray(Float.TYPE), singletonArray(Double.TYPE));
|
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
|
||||||
singletonArray(Double.class), singletonArray(Double.TYPE));
|
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
|
||||||
singletonArray(Double.TYPE), singletonArray(Double.TYPE));
|
|
||||||
expectMatchingAccessibleConstructorParameterTypes(TestBean.class,
|
|
||||||
new Class<?>[]{SubClass.class, String[].class},
|
|
||||||
new Class<?>[] { BaseClass.class, String[].class });
|
new Class<?>[] { BaseClass.class, String[].class });
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvokeConstructor() throws Exception {
|
public void testInvokeConstructor() throws Exception {
|
||||||
assertEquals("()", ConstructorUtils.invokeConstructor(TestBean.class,
|
assertEquals("()", ConstructorUtils.invokeConstructor(TestBean.class, (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY).toString());
|
||||||
(Object[]) ArrayUtils.EMPTY_CLASS_ARRAY).toString());
|
assertEquals("()", ConstructorUtils.invokeConstructor(TestBean.class, (Object[]) null).toString());
|
||||||
assertEquals("()", ConstructorUtils.invokeConstructor(TestBean.class,
|
|
||||||
(Object[]) null).toString());
|
|
||||||
assertEquals("()", ConstructorUtils.invokeConstructor(TestBean.class).toString());
|
assertEquals("()", ConstructorUtils.invokeConstructor(TestBean.class).toString());
|
||||||
assertEquals("(String)", ConstructorUtils.invokeConstructor(
|
assertEquals("(String)", ConstructorUtils.invokeConstructor(TestBean.class, "").toString());
|
||||||
TestBean.class, "").toString());
|
assertEquals("(Object)", ConstructorUtils.invokeConstructor(TestBean.class, new Object()).toString());
|
||||||
assertEquals("(Object)", ConstructorUtils.invokeConstructor(
|
assertEquals("(Object)", ConstructorUtils.invokeConstructor(TestBean.class, Boolean.TRUE).toString());
|
||||||
TestBean.class, new Object()).toString());
|
assertEquals("(Integer)", ConstructorUtils.invokeConstructor(TestBean.class, NumberUtils.INTEGER_ONE).toString());
|
||||||
assertEquals("(Object)", ConstructorUtils.invokeConstructor(
|
assertEquals("(int)", ConstructorUtils.invokeConstructor(TestBean.class, NumberUtils.BYTE_ONE).toString());
|
||||||
TestBean.class, Boolean.TRUE).toString());
|
assertEquals("(double)", ConstructorUtils.invokeConstructor(TestBean.class, NumberUtils.LONG_ONE).toString());
|
||||||
assertEquals("(Integer)", ConstructorUtils.invokeConstructor(
|
assertEquals("(double)", ConstructorUtils.invokeConstructor(TestBean.class, NumberUtils.DOUBLE_ONE).toString());
|
||||||
TestBean.class, NumberUtils.INTEGER_ONE).toString());
|
ConstructorUtils.invokeConstructor(TestBean.class, NumberUtils.INTEGER_ONE).verify("(Integer)", null);
|
||||||
assertEquals("(int)", ConstructorUtils.invokeConstructor(
|
ConstructorUtils.invokeConstructor(TestBean.class, "a", "b").verify("(String...)", new String[] { "a", "b" });
|
||||||
TestBean.class, NumberUtils.BYTE_ONE).toString());
|
ConstructorUtils.invokeConstructor(TestBean.class, NumberUtils.INTEGER_ONE, "a", "b").verify("(Integer, String...)", new String[] { "a", "b" });
|
||||||
assertEquals("(double)", ConstructorUtils.invokeConstructor(
|
ConstructorUtils.invokeConstructor(TestBean.class, new SubClass(), new String[] { "a", "b" }).verify("(BaseClass, String...)",
|
||||||
TestBean.class, NumberUtils.LONG_ONE).toString());
|
new String[] { "a", "b" });
|
||||||
assertEquals("(double)", ConstructorUtils.invokeConstructor(
|
|
||||||
TestBean.class, NumberUtils.DOUBLE_ONE).toString());
|
|
||||||
ConstructorUtils.invokeConstructor(TestBean.class, NumberUtils.INTEGER_ONE)
|
|
||||||
.verify("(Integer)", null);
|
|
||||||
ConstructorUtils.invokeConstructor(TestBean.class, "a", "b")
|
|
||||||
.verify("(String...)", new String[]{"a", "b"});
|
|
||||||
ConstructorUtils.invokeConstructor(TestBean.class, NumberUtils.INTEGER_ONE, "a", "b")
|
|
||||||
.verify("(Integer, String...)", new String[]{"a", "b"});
|
|
||||||
ConstructorUtils.invokeConstructor(TestBean.class, new SubClass(), new String[]{"a", "b"})
|
|
||||||
.verify("(BaseClass, String...)", new String[]{"a", "b"});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvokeExactConstructor() throws Exception {
|
public void testInvokeExactConstructor() throws Exception {
|
||||||
assertEquals("()", ConstructorUtils.invokeExactConstructor(
|
assertEquals("()", ConstructorUtils.invokeExactConstructor(TestBean.class, (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY).toString());
|
||||||
TestBean.class, (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY).toString());
|
assertEquals("()", ConstructorUtils.invokeExactConstructor(TestBean.class, (Object[]) null).toString());
|
||||||
assertEquals("()", ConstructorUtils.invokeExactConstructor(
|
assertEquals("(String)", ConstructorUtils.invokeExactConstructor(TestBean.class, "").toString());
|
||||||
TestBean.class, (Object[]) null).toString());
|
assertEquals("(Object)", ConstructorUtils.invokeExactConstructor(TestBean.class, new Object()).toString());
|
||||||
assertEquals("(String)", ConstructorUtils.invokeExactConstructor(
|
assertEquals("(Integer)", ConstructorUtils.invokeExactConstructor(TestBean.class, NumberUtils.INTEGER_ONE).toString());
|
||||||
TestBean.class, "").toString());
|
assertEquals("(double)",
|
||||||
assertEquals("(Object)", ConstructorUtils.invokeExactConstructor(
|
ConstructorUtils.invokeExactConstructor(TestBean.class, new Object[] { NumberUtils.DOUBLE_ONE }, new Class[] { Double.TYPE }).toString());
|
||||||
TestBean.class, new Object()).toString());
|
|
||||||
assertEquals("(Integer)", ConstructorUtils.invokeExactConstructor(
|
|
||||||
TestBean.class, NumberUtils.INTEGER_ONE).toString());
|
|
||||||
assertEquals("(double)", ConstructorUtils.invokeExactConstructor(
|
|
||||||
TestBean.class, new Object[] { NumberUtils.DOUBLE_ONE },
|
|
||||||
new Class[] { Double.TYPE }).toString());
|
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(NoSuchMethodException.class, () -> ConstructorUtils.invokeExactConstructor(TestBean.class, NumberUtils.BYTE_ONE));
|
||||||
NoSuchMethodException.class,
|
assertThrows(NoSuchMethodException.class, () -> ConstructorUtils.invokeExactConstructor(TestBean.class, NumberUtils.LONG_ONE));
|
||||||
() -> ConstructorUtils.invokeExactConstructor(TestBean.class, NumberUtils.BYTE_ONE));
|
assertThrows(NoSuchMethodException.class, () -> ConstructorUtils.invokeExactConstructor(TestBean.class, Boolean.TRUE));
|
||||||
assertThrows(
|
|
||||||
NoSuchMethodException.class,
|
|
||||||
() -> ConstructorUtils.invokeExactConstructor(TestBean.class, NumberUtils.LONG_ONE));
|
|
||||||
assertThrows(
|
|
||||||
NoSuchMethodException.class,
|
|
||||||
() -> ConstructorUtils.invokeExactConstructor(TestBean.class, Boolean.TRUE));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNullArgument() {
|
public void testNullArgument() {
|
||||||
expectMatchingAccessibleConstructorParameterTypes(MutableObject.class,
|
expectMatchingAccessibleConstructorParameterTypes(MutableObject.class, singletonArray(null), singletonArray(Object.class));
|
||||||
singletonArray(null), singletonArray(Object.class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVarArgsUnboxing() throws Exception {
|
public void testVarArgsUnboxing() throws Exception {
|
||||||
final TestBean testBean = ConstructorUtils.invokeConstructor(
|
final TestBean testBean = ConstructorUtils.invokeConstructor(TestBean.class, Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3));
|
||||||
TestBean.class, Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3));
|
|
||||||
|
|
||||||
assertArrayEquals(new String[] { "2", "3" }, testBean.varArgs);
|
assertArrayEquals(new String[] { "2", "3" }, testBean.varArgs);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,29 +30,39 @@ public class TypeLiteralTest extends AbstractLangTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBasic() {
|
public void testBasic() {
|
||||||
assertTrue(TypeUtils.equals(String.class, new TypeLiteral<String>() {}.value));
|
assertTrue(TypeUtils.equals(String.class, new TypeLiteral<String>() {
|
||||||
assertTrue(TypeUtils.equals(TypeUtils.parameterize(List.class, String.class),
|
}.value));
|
||||||
new TypeLiteral<List<String>>() {}.value));
|
assertTrue(TypeUtils.equals(TypeUtils.parameterize(List.class, String.class), new TypeLiteral<List<String>>() {
|
||||||
|
}.value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEquals() {
|
public void testEquals() {
|
||||||
assertEquals(new TypeLiteral<String>() {}, new TypeLiteral<String>() {});
|
assertEquals(new TypeLiteral<String>() {
|
||||||
assertEquals(new TypeLiteral<List<String>>() {}, new TypeLiteral<List<String>>() {});
|
}, new TypeLiteral<String>() {
|
||||||
assertNotEquals(new TypeLiteral<String>() {}, new TypeLiteral<List<String>>() {});
|
});
|
||||||
|
assertEquals(new TypeLiteral<List<String>>() {
|
||||||
|
}, new TypeLiteral<List<String>>() {
|
||||||
|
});
|
||||||
|
assertNotEquals(new TypeLiteral<String>() {
|
||||||
|
}, new TypeLiteral<List<String>>() {
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
@Test
|
@Test
|
||||||
public void testRaw() {
|
public void testRaw() {
|
||||||
assertThrows(NullPointerException.class, () -> new TypeLiteral() {});
|
assertThrows(NullPointerException.class, () -> new TypeLiteral() {
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTyped() {
|
public void testTyped() {
|
||||||
final Typed<String> stringType = new TypeLiteral<String>() {};
|
final Typed<String> stringType = new TypeLiteral<String>() {
|
||||||
|
};
|
||||||
assertTrue(TypeUtils.equals(String.class, stringType.getType()));
|
assertTrue(TypeUtils.equals(String.class, stringType.getType()));
|
||||||
final Typed<List<String>> listOfStringType = new TypeLiteral<List<String>>() {};
|
final Typed<List<String>> listOfStringType = new TypeLiteral<List<String>>() {
|
||||||
|
};
|
||||||
assertTrue(TypeUtils.equals(TypeUtils.parameterize(List.class, String.class), listOfStringType.getType()));
|
assertTrue(TypeUtils.equals(TypeUtils.parameterize(List.class, String.class), listOfStringType.getType()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,7 @@
|
||||||
package org.apache.commons.lang3.test;
|
package org.apache.commons.lang3.test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows for testing an exception that is not visible to
|
* Allows for testing an exception that is not visible to {@link org.apache.commons.lang3.exception.ExceptionUtils}
|
||||||
* {@link org.apache.commons.lang3.exception.ExceptionUtils}
|
|
||||||
*/
|
*/
|
||||||
public class NotVisibleExceptionFactory {
|
public class NotVisibleExceptionFactory {
|
||||||
|
|
||||||
|
@ -39,8 +38,8 @@ public class NotVisibleExceptionFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Exception whose getCause method returns the
|
* Create a new Exception whose getCause method returns the provided cause.
|
||||||
* provided cause.
|
*
|
||||||
* @param cause the cause of the exception
|
* @param cause the cause of the exception
|
||||||
* @return a new {@link Exception}
|
* @return a new {@link Exception}
|
||||||
*/
|
*/
|
||||||
|
@ -48,5 +47,6 @@ public class NotVisibleExceptionFactory {
|
||||||
return new NotVisibleException(cause);
|
return new NotVisibleException(cause);
|
||||||
}
|
}
|
||||||
|
|
||||||
private NotVisibleExceptionFactory() {}
|
private NotVisibleExceptionFactory() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,25 +127,19 @@ public class DateUtilsFragmentTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testHoursOfMonthWithCalendar() {
|
public void testHoursOfMonthWithCalendar() {
|
||||||
final long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.MONTH);
|
final long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.MONTH);
|
||||||
assertEquals( hours +(days - 1) * DateUtils.MILLIS_PER_DAY
|
assertEquals(hours + (days - 1) * DateUtils.MILLIS_PER_DAY / DateUtils.MILLIS_PER_HOUR, testResult);
|
||||||
/ DateUtils.MILLIS_PER_HOUR,
|
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHoursOfMonthWithDate() {
|
public void testHoursOfMonthWithDate() {
|
||||||
final long testResult = DateUtils.getFragmentInHours(aDate, Calendar.MONTH);
|
final long testResult = DateUtils.getFragmentInHours(aDate, Calendar.MONTH);
|
||||||
assertEquals(hours + (days - 1) * DateUtils.MILLIS_PER_DAY
|
assertEquals(hours + (days - 1) * DateUtils.MILLIS_PER_DAY / DateUtils.MILLIS_PER_HOUR, testResult);
|
||||||
/ DateUtils.MILLIS_PER_HOUR,
|
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHoursOfYearWithCalendar() {
|
public void testHoursOfYearWithCalendar() {
|
||||||
final long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.YEAR);
|
final long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.YEAR);
|
||||||
assertEquals( hours +(aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY
|
assertEquals(hours + (aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY / DateUtils.MILLIS_PER_HOUR, testResult);
|
||||||
/ DateUtils.MILLIS_PER_HOUR,
|
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -153,9 +147,7 @@ public class DateUtilsFragmentTest extends AbstractLangTest {
|
||||||
final long testResult = DateUtils.getFragmentInHours(aDate, Calendar.YEAR);
|
final long testResult = DateUtils.getFragmentInHours(aDate, Calendar.YEAR);
|
||||||
final Calendar cal = Calendar.getInstance();
|
final Calendar cal = Calendar.getInstance();
|
||||||
cal.setTime(aDate);
|
cal.setTime(aDate);
|
||||||
assertEquals(hours + (cal.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY
|
assertEquals(hours + (cal.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY / DateUtils.MILLIS_PER_HOUR, testResult);
|
||||||
/ DateUtils.MILLIS_PER_HOUR,
|
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calendar.SECOND as useful fragment
|
// Calendar.SECOND as useful fragment
|
||||||
|
@ -246,18 +238,16 @@ public class DateUtilsFragmentTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testMillisecondsOfMonthWithCalendar() {
|
public void testMillisecondsOfMonthWithCalendar() {
|
||||||
final long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MONTH);
|
final long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MONTH);
|
||||||
assertEquals(millis + seconds * DateUtils.MILLIS_PER_SECOND + minutes * DateUtils.MILLIS_PER_MINUTE
|
assertEquals(millis + seconds * DateUtils.MILLIS_PER_SECOND + minutes * DateUtils.MILLIS_PER_MINUTE + hours * DateUtils.MILLIS_PER_HOUR
|
||||||
+ hours * DateUtils.MILLIS_PER_HOUR + (days - 1) * DateUtils.MILLIS_PER_DAY,
|
+ (days - 1) * DateUtils.MILLIS_PER_DAY, testResult);
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calendar.MONTH as useful fragment
|
// Calendar.MONTH as useful fragment
|
||||||
@Test
|
@Test
|
||||||
public void testMillisecondsOfMonthWithDate() {
|
public void testMillisecondsOfMonthWithDate() {
|
||||||
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.MONTH);
|
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.MONTH);
|
||||||
assertEquals(millis + seconds * DateUtils.MILLIS_PER_SECOND + minutes * DateUtils.MILLIS_PER_MINUTE
|
assertEquals(millis + seconds * DateUtils.MILLIS_PER_SECOND + minutes * DateUtils.MILLIS_PER_MINUTE + hours * DateUtils.MILLIS_PER_HOUR
|
||||||
+ hours * DateUtils.MILLIS_PER_HOUR + (days - 1) * DateUtils.MILLIS_PER_DAY,
|
+ (days - 1) * DateUtils.MILLIS_PER_DAY, testResult);
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -276,9 +266,8 @@ testResult);
|
||||||
@Test
|
@Test
|
||||||
public void testMillisecondsOfYearWithCalendar() {
|
public void testMillisecondsOfYearWithCalendar() {
|
||||||
final long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.YEAR);
|
final long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.YEAR);
|
||||||
assertEquals(millis + seconds * DateUtils.MILLIS_PER_SECOND + minutes * DateUtils.MILLIS_PER_MINUTE
|
assertEquals(millis + seconds * DateUtils.MILLIS_PER_SECOND + minutes * DateUtils.MILLIS_PER_MINUTE + hours * DateUtils.MILLIS_PER_HOUR
|
||||||
+ hours * DateUtils.MILLIS_PER_HOUR + (aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY,
|
+ (aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY, testResult);
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calendar.YEAR as useful fragment
|
// Calendar.YEAR as useful fragment
|
||||||
|
@ -287,9 +276,8 @@ testResult);
|
||||||
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.YEAR);
|
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.YEAR);
|
||||||
final Calendar cal = Calendar.getInstance();
|
final Calendar cal = Calendar.getInstance();
|
||||||
cal.setTime(aDate);
|
cal.setTime(aDate);
|
||||||
assertEquals(millis + seconds * DateUtils.MILLIS_PER_SECOND + minutes * DateUtils.MILLIS_PER_MINUTE
|
assertEquals(millis + seconds * DateUtils.MILLIS_PER_SECOND + minutes * DateUtils.MILLIS_PER_MINUTE + hours * DateUtils.MILLIS_PER_HOUR
|
||||||
+ hours * DateUtils.MILLIS_PER_HOUR + (cal.get(Calendar.DAY_OF_YEAR) - 1)* DateUtils.MILLIS_PER_DAY,
|
+ (cal.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY, testResult);
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -339,25 +327,20 @@ testResult);
|
||||||
@Test
|
@Test
|
||||||
public void testMinutesOfMonthWithCalendar() {
|
public void testMinutesOfMonthWithCalendar() {
|
||||||
final long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.MONTH);
|
final long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.MONTH);
|
||||||
assertEquals( minutes +(hours * DateUtils.MILLIS_PER_HOUR + (days - 1) * DateUtils.MILLIS_PER_DAY)
|
assertEquals(minutes + (hours * DateUtils.MILLIS_PER_HOUR + (days - 1) * DateUtils.MILLIS_PER_DAY) / DateUtils.MILLIS_PER_MINUTE, testResult);
|
||||||
/ DateUtils.MILLIS_PER_MINUTE,
|
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMinutesOfMonthWithDate() {
|
public void testMinutesOfMonthWithDate() {
|
||||||
final long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.MONTH);
|
final long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.MONTH);
|
||||||
assertEquals(minutes
|
assertEquals(minutes + (hours * DateUtils.MILLIS_PER_HOUR + (days - 1) * DateUtils.MILLIS_PER_DAY) / DateUtils.MILLIS_PER_MINUTE, testResult);
|
||||||
+ (hours * DateUtils.MILLIS_PER_HOUR + (days - 1) * DateUtils.MILLIS_PER_DAY)
|
|
||||||
/ DateUtils.MILLIS_PER_MINUTE,
|
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMinutesOfYearWithCalendar() {
|
public void testMinutesOfYearWithCalendar() {
|
||||||
final long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.YEAR);
|
final long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.YEAR);
|
||||||
assertEquals( minutes +(hours * DateUtils.MILLIS_PER_HOUR + (aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY)
|
assertEquals(minutes
|
||||||
/ DateUtils.MILLIS_PER_MINUTE,
|
+ (hours * DateUtils.MILLIS_PER_HOUR + (aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY) / DateUtils.MILLIS_PER_MINUTE,
|
||||||
testResult);
|
testResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -366,9 +349,8 @@ testResult);
|
||||||
final long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.YEAR);
|
final long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.YEAR);
|
||||||
final Calendar cal = Calendar.getInstance();
|
final Calendar cal = Calendar.getInstance();
|
||||||
cal.setTime(aDate);
|
cal.setTime(aDate);
|
||||||
assertEquals(minutes
|
assertEquals(
|
||||||
+ (hours * DateUtils.MILLIS_PER_HOUR + (cal.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY)
|
minutes + (hours * DateUtils.MILLIS_PER_HOUR + (cal.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY) / DateUtils.MILLIS_PER_MINUTE,
|
||||||
/ DateUtils.MILLIS_PER_MINUTE,
|
|
||||||
testResult);
|
testResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -440,21 +422,13 @@ testResult);
|
||||||
@Test
|
@Test
|
||||||
public void testSecondsofHourWithCalendar() {
|
public void testSecondsofHourWithCalendar() {
|
||||||
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.HOUR_OF_DAY);
|
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.HOUR_OF_DAY);
|
||||||
assertEquals(
|
assertEquals(seconds + minutes * DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND, testResult);
|
||||||
seconds
|
|
||||||
+ minutes
|
|
||||||
* DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND,
|
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSecondsofHourWithDate() {
|
public void testSecondsofHourWithDate() {
|
||||||
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.HOUR_OF_DAY);
|
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.HOUR_OF_DAY);
|
||||||
assertEquals(
|
assertEquals(seconds + minutes * DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND, testResult);
|
||||||
seconds
|
|
||||||
+ minutes
|
|
||||||
* DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND,
|
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -473,34 +447,22 @@ testResult);
|
||||||
@Test
|
@Test
|
||||||
public void testSecondsOfMonthWithCalendar() {
|
public void testSecondsOfMonthWithCalendar() {
|
||||||
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MONTH);
|
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MONTH);
|
||||||
assertEquals(
|
assertEquals(seconds + (minutes * DateUtils.MILLIS_PER_MINUTE + hours * DateUtils.MILLIS_PER_HOUR + (days - 1) * DateUtils.MILLIS_PER_DAY)
|
||||||
seconds
|
/ DateUtils.MILLIS_PER_SECOND, testResult);
|
||||||
+ (minutes * DateUtils.MILLIS_PER_MINUTE
|
|
||||||
+ hours * DateUtils.MILLIS_PER_HOUR + (days - 1) * DateUtils.MILLIS_PER_DAY)
|
|
||||||
/ DateUtils.MILLIS_PER_SECOND,
|
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSecondsOfMonthWithDate() {
|
public void testSecondsOfMonthWithDate() {
|
||||||
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MONTH);
|
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MONTH);
|
||||||
assertEquals(
|
assertEquals(seconds + (minutes * DateUtils.MILLIS_PER_MINUTE + hours * DateUtils.MILLIS_PER_HOUR + (days - 1) * DateUtils.MILLIS_PER_DAY)
|
||||||
seconds
|
/ DateUtils.MILLIS_PER_SECOND, testResult);
|
||||||
+ (minutes * DateUtils.MILLIS_PER_MINUTE
|
|
||||||
+ hours * DateUtils.MILLIS_PER_HOUR + (days - 1) * DateUtils.MILLIS_PER_DAY)
|
|
||||||
/ DateUtils.MILLIS_PER_SECOND,
|
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSecondsOfYearWithCalendar() {
|
public void testSecondsOfYearWithCalendar() {
|
||||||
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.YEAR);
|
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.YEAR);
|
||||||
assertEquals(
|
assertEquals(seconds + (minutes * DateUtils.MILLIS_PER_MINUTE + hours * DateUtils.MILLIS_PER_HOUR
|
||||||
seconds
|
+ (aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY) / DateUtils.MILLIS_PER_SECOND, testResult);
|
||||||
+ (minutes * DateUtils.MILLIS_PER_MINUTE
|
|
||||||
+ hours * DateUtils.MILLIS_PER_HOUR + (aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY)
|
|
||||||
/ DateUtils.MILLIS_PER_SECOND,
|
|
||||||
testResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -508,10 +470,8 @@ testResult);
|
||||||
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.YEAR);
|
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.YEAR);
|
||||||
final Calendar cal = Calendar.getInstance();
|
final Calendar cal = Calendar.getInstance();
|
||||||
cal.setTime(aDate);
|
cal.setTime(aDate);
|
||||||
assertEquals(
|
assertEquals(seconds
|
||||||
seconds
|
+ (minutes * DateUtils.MILLIS_PER_MINUTE + hours * DateUtils.MILLIS_PER_HOUR + (cal.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY)
|
||||||
+ (minutes * DateUtils.MILLIS_PER_MINUTE
|
|
||||||
+ hours * DateUtils.MILLIS_PER_HOUR + (cal.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY)
|
|
||||||
/ DateUtils.MILLIS_PER_SECOND,
|
/ DateUtils.MILLIS_PER_SECOND,
|
||||||
testResult);
|
testResult);
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,9 +121,11 @@ public class DateUtilsRoundingTest extends AbstractLangTest {
|
||||||
final Date nextTruncateDate = DateUtils.addMilliseconds(lastTruncateDate, 1);
|
final Date nextTruncateDate = DateUtils.addMilliseconds(lastTruncateDate, 1);
|
||||||
|
|
||||||
// Date-comparison
|
// Date-comparison
|
||||||
assertEquals(truncatedDate, DateUtils.truncate(truncatedDate, calendarField), "Truncating "+ fdf.format(truncatedDate) +" as Date with CalendarField-value "+ calendarField +" must return itself");
|
assertEquals(truncatedDate, DateUtils.truncate(truncatedDate, calendarField),
|
||||||
|
"Truncating " + fdf.format(truncatedDate) + " as Date with CalendarField-value " + calendarField + " must return itself");
|
||||||
assertEquals(truncatedDate, DateUtils.truncate(lastTruncateDate, calendarField));
|
assertEquals(truncatedDate, DateUtils.truncate(lastTruncateDate, calendarField));
|
||||||
assertNotEquals(truncatedDate, DateUtils.truncate(nextTruncateDate, calendarField), fdf.format(lastTruncateDate) + " is not an extreme when truncating as Date with CalendarField-value " + calendarField);
|
assertNotEquals(truncatedDate, DateUtils.truncate(nextTruncateDate, calendarField),
|
||||||
|
fdf.format(lastTruncateDate) + " is not an extreme when truncating as Date with CalendarField-value " + calendarField);
|
||||||
|
|
||||||
// Calendar-initiations
|
// Calendar-initiations
|
||||||
final Calendar truncatedCalendar;
|
final Calendar truncatedCalendar;
|
||||||
|
@ -137,17 +139,23 @@ public class DateUtilsRoundingTest extends AbstractLangTest {
|
||||||
nextTruncateCalendar.setTime(nextTruncateDate);
|
nextTruncateCalendar.setTime(nextTruncateDate);
|
||||||
|
|
||||||
// Calendar-comparison
|
// Calendar-comparison
|
||||||
assertEquals(truncatedCalendar, DateUtils.truncate(truncatedCalendar, calendarField), "Truncating "+ fdf.format(truncatedCalendar) +" as Calendar with CalendarField-value "+ calendarField +" must return itself");
|
assertEquals(truncatedCalendar, DateUtils.truncate(truncatedCalendar, calendarField),
|
||||||
|
"Truncating " + fdf.format(truncatedCalendar) + " as Calendar with CalendarField-value " + calendarField + " must return itself");
|
||||||
assertEquals(truncatedCalendar, DateUtils.truncate(lastTruncateCalendar, calendarField));
|
assertEquals(truncatedCalendar, DateUtils.truncate(lastTruncateCalendar, calendarField));
|
||||||
assertNotEquals(truncatedCalendar, DateUtils.truncate(nextTruncateCalendar, calendarField), fdf.format(lastTruncateCalendar) + " is not an extreme when truncating as Calendar with CalendarField-value " + calendarField);
|
assertNotEquals(truncatedCalendar, DateUtils.truncate(nextTruncateCalendar, calendarField),
|
||||||
|
fdf.format(lastTruncateCalendar) + " is not an extreme when truncating as Calendar with CalendarField-value " + calendarField);
|
||||||
|
|
||||||
// Object-comparison
|
// Object-comparison
|
||||||
assertEquals(truncatedDate, DateUtils.truncate((Object) truncatedDate, calendarField), "Truncating "+ fdf.format(truncatedDate) +" as Date cast to Object with CalendarField-value "+ calendarField +" must return itself as Date");
|
assertEquals(truncatedDate, DateUtils.truncate((Object) truncatedDate, calendarField), "Truncating " + fdf.format(truncatedDate)
|
||||||
|
+ " as Date cast to Object with CalendarField-value " + calendarField + " must return itself as Date");
|
||||||
assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateDate, calendarField));
|
assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateDate, calendarField));
|
||||||
assertNotEquals(truncatedDate, DateUtils.truncate((Object) nextTruncateDate, calendarField), fdf.format(lastTruncateDate) + " is not an extreme when truncating as Date cast to Object with CalendarField-value " + calendarField);
|
assertNotEquals(truncatedDate, DateUtils.truncate((Object) nextTruncateDate, calendarField),
|
||||||
assertEquals(truncatedDate, DateUtils.truncate((Object) truncatedCalendar, calendarField), "Truncating "+ fdf.format(truncatedCalendar) +" as Calendar cast to Object with CalendarField-value "+ calendarField +" must return itself as Date");
|
fdf.format(lastTruncateDate) + " is not an extreme when truncating as Date cast to Object with CalendarField-value " + calendarField);
|
||||||
|
assertEquals(truncatedDate, DateUtils.truncate((Object) truncatedCalendar, calendarField), "Truncating " + fdf.format(truncatedCalendar)
|
||||||
|
+ " as Calendar cast to Object with CalendarField-value " + calendarField + " must return itself as Date");
|
||||||
assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateCalendar, calendarField));
|
assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateCalendar, calendarField));
|
||||||
assertNotEquals(truncatedDate, DateUtils.truncate((Object) nextTruncateCalendar, calendarField), fdf.format(lastTruncateCalendar) + " is not an extreme when truncating as Calendar cast to Object with CalendarField-value " + calendarField);
|
assertNotEquals(truncatedDate, DateUtils.truncate((Object) nextTruncateCalendar, calendarField),
|
||||||
|
fdf.format(lastTruncateCalendar) + " is not an extreme when truncating as Calendar cast to Object with CalendarField-value " + calendarField);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -160,7 +168,8 @@ public class DateUtilsRoundingTest extends AbstractLangTest {
|
||||||
* @param calendarField a Calendar.field value
|
* @param calendarField a Calendar.field value
|
||||||
*/
|
*/
|
||||||
protected void roundToJanuaryFirst(final Date minDate, final Date maxDate, final int calendarField) {
|
protected void roundToJanuaryFirst(final Date minDate, final Date maxDate, final int calendarField) {
|
||||||
assertEquals(januaryOneDate, DateUtils.round(januaryOneDate, calendarField), "Rounding "+ fdf.format(januaryOneDate) +" as Date with CalendarField-value "+ calendarField +" must return itself");
|
assertEquals(januaryOneDate, DateUtils.round(januaryOneDate, calendarField),
|
||||||
|
"Rounding " + fdf.format(januaryOneDate) + " as Date with CalendarField-value " + calendarField + " must return itself");
|
||||||
assertEquals(januaryOneDate, DateUtils.round(minDate, calendarField));
|
assertEquals(januaryOneDate, DateUtils.round(minDate, calendarField));
|
||||||
assertEquals(januaryOneDate, DateUtils.round(maxDate, calendarField));
|
assertEquals(januaryOneDate, DateUtils.round(maxDate, calendarField));
|
||||||
|
|
||||||
|
@ -168,21 +177,26 @@ public class DateUtilsRoundingTest extends AbstractLangTest {
|
||||||
minCalendar.setTime(minDate);
|
minCalendar.setTime(minDate);
|
||||||
final Calendar maxCalendar = Calendar.getInstance();
|
final Calendar maxCalendar = Calendar.getInstance();
|
||||||
maxCalendar.setTime(maxDate);
|
maxCalendar.setTime(maxDate);
|
||||||
assertEquals(januaryOneCalendar, DateUtils.round(januaryOneCalendar, calendarField), "Rounding "+ fdf.format(januaryOneCalendar) +" as Date with CalendarField-value "+ calendarField +" must return itself");
|
assertEquals(januaryOneCalendar, DateUtils.round(januaryOneCalendar, calendarField),
|
||||||
|
"Rounding " + fdf.format(januaryOneCalendar) + " as Date with CalendarField-value " + calendarField + " must return itself");
|
||||||
assertEquals(januaryOneCalendar, DateUtils.round(minCalendar, calendarField));
|
assertEquals(januaryOneCalendar, DateUtils.round(minCalendar, calendarField));
|
||||||
assertEquals(januaryOneCalendar, DateUtils.round(maxCalendar, calendarField));
|
assertEquals(januaryOneCalendar, DateUtils.round(maxCalendar, calendarField));
|
||||||
|
|
||||||
final Date toPrevRoundDate = DateUtils.addMilliseconds(minDate, -1);
|
final Date toPrevRoundDate = DateUtils.addMilliseconds(minDate, -1);
|
||||||
final Date toNextRoundDate = DateUtils.addMilliseconds(maxDate, 1);
|
final Date toNextRoundDate = DateUtils.addMilliseconds(maxDate, 1);
|
||||||
assertNotEquals(januaryOneDate, DateUtils.round(toPrevRoundDate, calendarField), fdf.format(minDate) + " is not an lower-extreme when rounding as Date with CalendarField-value " + calendarField);
|
assertNotEquals(januaryOneDate, DateUtils.round(toPrevRoundDate, calendarField),
|
||||||
assertNotEquals(januaryOneDate, DateUtils.round(toNextRoundDate, calendarField), fdf.format(maxDate) + " is not an upper-extreme when rounding as Date with CalendarField-value " + calendarField);
|
fdf.format(minDate) + " is not an lower-extreme when rounding as Date with CalendarField-value " + calendarField);
|
||||||
|
assertNotEquals(januaryOneDate, DateUtils.round(toNextRoundDate, calendarField),
|
||||||
|
fdf.format(maxDate) + " is not an upper-extreme when rounding as Date with CalendarField-value " + calendarField);
|
||||||
|
|
||||||
final Calendar toPrevRoundCalendar = Calendar.getInstance();
|
final Calendar toPrevRoundCalendar = Calendar.getInstance();
|
||||||
toPrevRoundCalendar.setTime(toPrevRoundDate);
|
toPrevRoundCalendar.setTime(toPrevRoundDate);
|
||||||
final Calendar toNextRoundCalendar = Calendar.getInstance();
|
final Calendar toNextRoundCalendar = Calendar.getInstance();
|
||||||
toNextRoundCalendar.setTime(toNextRoundDate);
|
toNextRoundCalendar.setTime(toNextRoundDate);
|
||||||
assertNotEquals(januaryOneDate, DateUtils.round(toPrevRoundDate, calendarField), fdf.format(minCalendar) + " is not an lower-extreme when rounding as Date with CalendarField-value " + calendarField);
|
assertNotEquals(januaryOneDate, DateUtils.round(toPrevRoundDate, calendarField),
|
||||||
assertNotEquals(januaryOneDate, DateUtils.round(toNextRoundDate, calendarField), fdf.format(maxCalendar) + " is not an upper-extreme when rounding as Date with CalendarField-value " + calendarField);
|
fdf.format(minCalendar) + " is not an lower-extreme when rounding as Date with CalendarField-value " + calendarField);
|
||||||
|
assertNotEquals(januaryOneDate, DateUtils.round(toNextRoundDate, calendarField),
|
||||||
|
fdf.format(maxCalendar) + " is not an upper-extreme when rounding as Date with CalendarField-value " + calendarField);
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
|
@ -559,11 +573,9 @@ public class DateUtilsRoundingTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testTruncateAmPm() throws Exception {
|
public void testTruncateAmPm() throws Exception {
|
||||||
final int calendarField = Calendar.AM_PM;
|
final int calendarField = Calendar.AM_PM;
|
||||||
|
|
||||||
// AM
|
// AM
|
||||||
Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 11:59:59.999");
|
Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 11:59:59.999");
|
||||||
baseTruncateTest(targetAmDate, lastTruncateDate, calendarField);
|
baseTruncateTest(targetAmDate, lastTruncateDate, calendarField);
|
||||||
|
|
||||||
// PM
|
// PM
|
||||||
lastTruncateDate = dateTimeParser.parse("June 1, 2008 23:59:59.999");
|
lastTruncateDate = dateTimeParser.parse("June 1, 2008 23:59:59.999");
|
||||||
baseTruncateTest(targetPmDate, lastTruncateDate, calendarField);
|
baseTruncateTest(targetPmDate, lastTruncateDate, calendarField);
|
||||||
|
|
|
@ -93,37 +93,17 @@ public class DurationFormatUtilsTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAlternatingLiteralOptionals() {
|
public void testAlternatingLiteralOptionals() {
|
||||||
final String format = "['d'dH'h'][m'm']['s's]['ms'S]";
|
final String format = "['d'dH'h'][m'm']['s's]['ms'S]";
|
||||||
|
assertEquals("d1", DurationFormatUtils.formatDuration(Duration.ofDays(1).toMillis(), format));
|
||||||
assertEquals("d1",
|
assertEquals("1h", DurationFormatUtils.formatDuration(Duration.ofHours(1).toMillis(), format));
|
||||||
DurationFormatUtils.formatDuration(Duration.ofDays(1).toMillis(), format));
|
assertEquals("1m", DurationFormatUtils.formatDuration(Duration.ofMinutes(1).toMillis(), format));
|
||||||
|
assertEquals("s1", DurationFormatUtils.formatDuration(Duration.ofSeconds(1).toMillis(), format));
|
||||||
assertEquals("1h",
|
assertEquals("ms001", DurationFormatUtils.formatDuration(Duration.ofMillis(1).toMillis(), format));
|
||||||
DurationFormatUtils.formatDuration(Duration.ofHours(1).toMillis(), format));
|
assertEquals("d1s1", DurationFormatUtils.formatDuration(Duration.ofDays(1).plusSeconds(1).toMillis(), format));
|
||||||
|
assertEquals("d11h", DurationFormatUtils.formatDuration(Duration.ofDays(1).plusHours(1).toMillis(), format));
|
||||||
assertEquals("1m",
|
assertEquals("d11h1m", DurationFormatUtils.formatDuration(Duration.ofDays(1).plusHours(1).plusMinutes(1).toMillis(), format));
|
||||||
DurationFormatUtils.formatDuration(Duration.ofMinutes(1).toMillis(), format));
|
assertEquals("d11h1ms1", DurationFormatUtils.formatDuration(Duration.ofDays(1).plusHours(1).plusMinutes(1).plusSeconds(1).toMillis(), format));
|
||||||
|
|
||||||
assertEquals("s1",
|
|
||||||
DurationFormatUtils.formatDuration(Duration.ofSeconds(1).toMillis(), format));
|
|
||||||
|
|
||||||
assertEquals("ms001",
|
|
||||||
DurationFormatUtils.formatDuration(Duration.ofMillis(1).toMillis(), format));
|
|
||||||
|
|
||||||
assertEquals("d1s1",
|
|
||||||
DurationFormatUtils.formatDuration(Duration.ofDays(1).plusSeconds(1).toMillis(), format));
|
|
||||||
|
|
||||||
assertEquals("d11h",
|
|
||||||
DurationFormatUtils.formatDuration(Duration.ofDays(1).plusHours(1).toMillis(), format));
|
|
||||||
|
|
||||||
assertEquals("d11h1m",
|
|
||||||
DurationFormatUtils.formatDuration(Duration.ofDays(1).plusHours(1).plusMinutes(1).toMillis(), format));
|
|
||||||
|
|
||||||
assertEquals("d11h1ms1",
|
|
||||||
DurationFormatUtils.formatDuration(Duration.ofDays(1).plusHours(1).plusMinutes(1).plusSeconds(1).toMillis(), format));
|
|
||||||
|
|
||||||
assertEquals("d11h1ms1ms001",
|
assertEquals("d11h1ms1ms001",
|
||||||
DurationFormatUtils.formatDuration(Duration.ofDays(1).plusHours(1).plusMinutes(1).plusSeconds(1).plusMillis(1).toMillis(), format));
|
DurationFormatUtils.formatDuration(Duration.ofDays(1).plusHours(1).plusMinutes(1).plusSeconds(1).plusMillis(1).toMillis(), format));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See https://issues.apache.org/bugzilla/show_bug.cgi?id=38401 */
|
/** See https://issues.apache.org/bugzilla/show_bug.cgi?id=38401 */
|
||||||
|
@ -657,8 +637,7 @@ public class DurationFormatUtilsTest extends AbstractLangTest {
|
||||||
createTokenWithCount(DurationFormatUtils.s, 1),
|
createTokenWithCount(DurationFormatUtils.s, 1),
|
||||||
createTokenWithCount(new StringBuilder("."), 1),
|
createTokenWithCount(new StringBuilder("."), 1),
|
||||||
createTokenWithCount(DurationFormatUtils.S, 3),
|
createTokenWithCount(DurationFormatUtils.S, 3),
|
||||||
createTokenWithCount(new StringBuilder("S"), 1)}, DurationFormatUtils
|
createTokenWithCount(new StringBuilder("S"), 1) }, DurationFormatUtils.lexx(DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN));
|
||||||
.lexx(DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN));
|
|
||||||
|
|
||||||
// test failures in equals
|
// test failures in equals
|
||||||
final DurationFormatUtils.Token token = createTokenWithCount(DurationFormatUtils.y, 4);
|
final DurationFormatUtils.Token token = createTokenWithCount(DurationFormatUtils.y, 4);
|
||||||
|
@ -671,12 +650,8 @@ public class DurationFormatUtilsTest extends AbstractLangTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLiteralPrefixOptionalToken() {
|
public void testLiteralPrefixOptionalToken() {
|
||||||
assertEquals(
|
assertEquals(DurationFormatUtils.formatDuration(10000L, "s's'"), DurationFormatUtils.formatDuration(10000L, "['['d']']['<'H'>']['{'m'}']s's'"));
|
||||||
DurationFormatUtils.formatDuration(10000L, "s's'"),
|
assertEquals(DurationFormatUtils.formatDuration(10000L, "s's'"), DurationFormatUtils.formatDuration(10000L, "['{'m'}']s's'"));
|
||||||
DurationFormatUtils.formatDuration(10000L, "['['d']']['<'H'>']['{'m'}']s's'"));
|
|
||||||
assertEquals(
|
|
||||||
DurationFormatUtils.formatDuration(10000L, "s's'"),
|
|
||||||
DurationFormatUtils.formatDuration(10000L, "['{'m'}']s's'"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Testing the under a day range in DurationFormatUtils.formatPeriod
|
// Testing the under a day range in DurationFormatUtils.formatPeriod
|
||||||
|
@ -685,11 +660,7 @@ public class DurationFormatUtilsTest extends AbstractLangTest {
|
||||||
for (int hr = 0; hr < 24; hr++) {
|
for (int hr = 0; hr < 24; hr++) {
|
||||||
for (int min = 0; min < 60; min++) {
|
for (int min = 0; min < 60; min++) {
|
||||||
for (int sec = 0; sec < 60; sec++) {
|
for (int sec = 0; sec < 60; sec++) {
|
||||||
assertEqualDuration(hr + ":" + min + ":" + sec,
|
assertEqualDuration(hr + ":" + min + ":" + sec, new int[] { 2000, 0, 1, 0, 0, 0, 0 }, new int[] { 2000, 0, 1, hr, min, sec }, "H:m:s");
|
||||||
new int[] { 2000, 0, 1, 0, 0, 0, 0 },
|
|
||||||
new int[] { 2000, 0, 1, hr, min, sec },
|
|
||||||
"H:m:s"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -697,13 +668,9 @@ public class DurationFormatUtilsTest extends AbstractLangTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultipleOptionalBlocks() {
|
public void testMultipleOptionalBlocks() {
|
||||||
|
assertEquals(DurationFormatUtils.formatDuration(Duration.ofHours(1).toMillis(), "'[['H']]'"),
|
||||||
assertEquals(
|
|
||||||
DurationFormatUtils.formatDuration(Duration.ofHours(1).toMillis(), "'[['H']]'"),
|
|
||||||
DurationFormatUtils.formatDuration(Duration.ofHours(1).toMillis(), "['{'d'}']['[['H']]']"));
|
DurationFormatUtils.formatDuration(Duration.ofHours(1).toMillis(), "['{'d'}']['[['H']]']"));
|
||||||
|
assertEquals(DurationFormatUtils.formatDuration(Duration.ofDays(1).toMillis(), "['{'d'}']"),
|
||||||
assertEquals(
|
|
||||||
DurationFormatUtils.formatDuration(Duration.ofDays(1).toMillis(), "['{'d'}']"),
|
|
||||||
DurationFormatUtils.formatDuration(Duration.ofDays(1).toMillis(), "['{'d'}']['['H']']"));
|
DurationFormatUtils.formatDuration(Duration.ofDays(1).toMillis(), "['{'d'}']['['H']']"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -277,9 +277,7 @@ public class FastDateFormatTest extends AbstractLangTest {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StringBuffer format(final Object obj,
|
public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition fieldPosition) {
|
||||||
final StringBuffer toAppendTo,
|
|
||||||
final FieldPosition fieldPosition) {
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
return inner.format(obj, toAppendTo, fieldPosition);
|
return inner.format(obj, toAppendTo, fieldPosition);
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ public class FastDateParserSDFTest extends AbstractLangTest {
|
||||||
private static final TimeZone timeZone = TimeZone.getDefault();
|
private static final TimeZone timeZone = TimeZone.getDefault();
|
||||||
|
|
||||||
public static Stream<Arguments> data() {
|
public static Stream<Arguments> data() {
|
||||||
|
// @formatter:off
|
||||||
return Stream.of(
|
return Stream.of(
|
||||||
// General Time zone tests
|
// General Time zone tests
|
||||||
Arguments.of("z yyyy", "GMT 2010", Locale.UK, true), // no offset specified, but this is allowed as a TimeZone name
|
Arguments.of("z yyyy", "GMT 2010", Locale.UK, true), // no offset specified, but this is allowed as a TimeZone name
|
||||||
|
@ -103,6 +104,7 @@ public class FastDateParserSDFTest extends AbstractLangTest {
|
||||||
Arguments.of( "hh", "48", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
|
Arguments.of( "hh", "48", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
|
||||||
Arguments.of( "kk", "48", Locale.UK, true) // Hour in day (1-24), i.e. midnight is 24, not 0
|
Arguments.of( "kk", "48", Locale.UK, true) // Hour in day (1-24), i.e. midnight is 24, not 0
|
||||||
);
|
);
|
||||||
|
// @formatter:on
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkParse(final String formattedDate, final String format, final Locale locale, final boolean valid) {
|
private void checkParse(final String formattedDate, final String format, final Locale locale, final boolean valid) {
|
||||||
|
@ -176,8 +178,7 @@ public class FastDateParserSDFTest extends AbstractLangTest {
|
||||||
assertEquals(expectedTime, actualTime, locale + " " + formattedDate + "\n");
|
assertEquals(expectedTime, actualTime, locale + " " + formattedDate + "\n");
|
||||||
} else {
|
} else {
|
||||||
assertNotEquals(-1, fdferrorIndex, "Test data error: expected FDF parse to fail, but got " + actualTime);
|
assertNotEquals(-1, fdferrorIndex, "Test data error: expected FDF parse to fail, but got " + actualTime);
|
||||||
assertTrue(sdferrorIndex - fdferrorIndex <= 4,
|
assertTrue(sdferrorIndex - fdferrorIndex <= 4, "FDF error index (" + fdferrorIndex + ") should approximate SDF index (" + sdferrorIndex + ")");
|
||||||
"FDF error index ("+ fdferrorIndex + ") should approximate SDF index (" + sdferrorIndex + ")");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,14 +42,14 @@ import org.junitpioneer.jupiter.DefaultTimeZone;
|
||||||
public class FastDatePrinterTest extends AbstractLangTest {
|
public class FastDatePrinterTest extends AbstractLangTest {
|
||||||
|
|
||||||
private enum Expected1806 {
|
private enum Expected1806 {
|
||||||
India(INDIA, "+05", "+0530", "+05:30"), Greenwich(TimeZones.GMT, "Z", "Z", "Z"), NewYork(
|
India(INDIA, "+05", "+0530", "+05:30"), Greenwich(TimeZones.GMT, "Z", "Z", "Z"), NewYork(NEW_YORK, "-05", "-0500", "-05:00");
|
||||||
NEW_YORK, "-05", "-0500", "-05:00");
|
|
||||||
|
|
||||||
final TimeZone zone;
|
final TimeZone zone;
|
||||||
|
|
||||||
final String one;
|
final String one;
|
||||||
final String two;
|
final String two;
|
||||||
final String three;
|
final String three;
|
||||||
|
|
||||||
Expected1806(final TimeZone zone, final String one, final String two, final String three) {
|
Expected1806(final TimeZone zone, final String one, final String two, final String three) {
|
||||||
this.zone = zone;
|
this.zone = zone;
|
||||||
this.one = one;
|
this.one = one;
|
||||||
|
@ -57,6 +57,7 @@ public class FastDatePrinterTest extends AbstractLangTest {
|
||||||
this.three = three;
|
this.three = three;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String YYYY_MM_DD = "yyyy/MM/dd";
|
private static final String YYYY_MM_DD = "yyyy/MM/dd";
|
||||||
private static final TimeZone NEW_YORK = TimeZone.getTimeZone("America/New_York");
|
private static final TimeZone NEW_YORK = TimeZone.getTimeZone("America/New_York");
|
||||||
private static final TimeZone INDIA = TimeZone.getTimeZone("Asia/Calcutta");
|
private static final TimeZone INDIA = TimeZone.getTimeZone("Asia/Calcutta");
|
||||||
|
@ -93,6 +94,7 @@ public class FastDatePrinterTest extends AbstractLangTest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Override this method in derived tests to change the construction of instances
|
* Override this method in derived tests to change the construction of instances
|
||||||
|
*
|
||||||
* @param format the format string to use
|
* @param format the format string to use
|
||||||
* @param timeZone the time zone to use
|
* @param timeZone the time zone to use
|
||||||
* @param locale the locale to use
|
* @param locale the locale to use
|
||||||
|
@ -117,6 +119,7 @@ public class FastDatePrinterTest extends AbstractLangTest {
|
||||||
assertEquals(trial.three, printer.format(cal));
|
assertEquals(trial.three, printer.format(cal));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test1806Argument() {
|
public void test1806Argument() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> getInstance("XXXX"));
|
assertThrows(IllegalArgumentException.class, () -> getInstance("XXXX"));
|
||||||
|
@ -203,8 +206,8 @@ public class FastDatePrinterTest extends AbstractLangTest {
|
||||||
assertEquals("-04:00", fdf.format(cal2));
|
assertEquals("-04:00", fdf.format(cal2));
|
||||||
assertEquals("-04:00", fdf.format(millis2));
|
assertEquals("-04:00", fdf.format(millis2));
|
||||||
|
|
||||||
final String pattern = "GGGG GGG GG G yyyy yyy yy y MMMM MMM MM M LLLL LLL LL L" +
|
final String pattern = "GGGG GGG GG G yyyy yyy yy y MMMM MMM MM M LLLL LLL LL L"
|
||||||
" dddd ddd dd d DDDD DDD DD D EEEE EEE EE E aaaa aaa aa a zzzz zzz zz z";
|
+ " dddd ddd dd d DDDD DDD DD D EEEE EEE EE E aaaa aaa aa a zzzz zzz zz z";
|
||||||
fdf = getInstance(pattern);
|
fdf = getInstance(pattern);
|
||||||
sdf = new SimpleDateFormat(pattern);
|
sdf = new SimpleDateFormat(pattern);
|
||||||
// SDF bug fix starting with Java 7
|
// SDF bug fix starting with Java 7
|
||||||
|
@ -279,8 +282,7 @@ public class FastDatePrinterTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* According to LANG-916 (https://issues.apache.org/jira/browse/LANG-916),
|
* According to LANG-916 (https://issues.apache.org/jira/browse/LANG-916), the format method did contain a bug: it did not use the TimeZone data.
|
||||||
* the format method did contain a bug: it did not use the TimeZone data.
|
|
||||||
*
|
*
|
||||||
* This method test that the bug is fixed.
|
* This method test that the bug is fixed.
|
||||||
*/
|
*/
|
||||||
|
@ -366,8 +368,7 @@ public class FastDatePrinterTest extends AbstractLangTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* testLowYearPadding showed that the date was buggy
|
* testLowYearPadding showed that the date was buggy This test confirms it, getting 366 back as a date
|
||||||
* This test confirms it, getting 366 back as a date
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testSimpleDate() {
|
public void testSimpleDate() {
|
||||||
|
|
Loading…
Reference in New Issue