Format: Remove extra whitespace around parameters.
Longer lines.
This commit is contained in:
parent
2b13f5b9fd
commit
4b9c1a03cf
|
@ -50,7 +50,7 @@ import org.junit.jupiter.api.Test;
|
||||||
* Unit tests {@link org.apache.commons.lang3.ClassUtils}.
|
* Unit tests {@link org.apache.commons.lang3.ClassUtils}.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("boxing") // JUnit4 does not support primitive equality testing apart from long
|
@SuppressWarnings("boxing") // JUnit4 does not support primitive equality testing apart from long
|
||||||
public class ClassUtilsTest {
|
public class ClassUtilsTest {
|
||||||
|
|
||||||
private static class CX implements IB, IA, IE {
|
private static class CX implements IB, IA, IE {
|
||||||
// empty
|
// empty
|
||||||
|
@ -91,22 +91,21 @@ public class ClassUtilsTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertGetClassReturnsClass( final Class<?> c ) throws Exception {
|
private void assertGetClassReturnsClass(final Class<?> c) throws Exception {
|
||||||
assertEquals( c, ClassUtils.getClass( c.getName() ) );
|
assertEquals(c, ClassUtils.getClass(c.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertGetClassThrowsClassNotFound( final String className ) {
|
private void assertGetClassThrowsClassNotFound(final String className) {
|
||||||
assertGetClassThrowsException( className, ClassNotFoundException.class );
|
assertGetClassThrowsException(className, ClassNotFoundException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertGetClassThrowsException(final String className, final Class<? extends Exception> exceptionType) {
|
private void assertGetClassThrowsException(final String className, final Class<? extends Exception> exceptionType) {
|
||||||
assertThrows(exceptionType,
|
assertThrows(exceptionType, () -> ClassUtils.getClass(className),
|
||||||
() -> ClassUtils.getClass(className),
|
"ClassUtils.getClass() should fail with an exception of type " + exceptionType.getName() + " when given class name \"" + className + "\".");
|
||||||
"ClassUtils.getClass() should fail with an exception of type " + exceptionType.getName() + " when given class name \"" + className + "\"." );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertGetClassThrowsNullPointerException( final String className ) {
|
private void assertGetClassThrowsNullPointerException(final String className) {
|
||||||
assertGetClassThrowsException( className, NullPointerException.class );
|
assertGetClassThrowsException(className, NullPointerException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -127,9 +126,7 @@ public class ClassUtilsTest {
|
||||||
@SuppressWarnings("unchecked") // test what happens when non-generic code adds wrong type of element
|
@SuppressWarnings("unchecked") // test what happens when non-generic code adds wrong type of element
|
||||||
final List<Object> olist = (List<Object>) (List<?>) list;
|
final List<Object> olist = (List<Object>) (List<?>) list;
|
||||||
olist.add(new Object());
|
olist.add(new Object());
|
||||||
assertThrows(ClassCastException.class,
|
assertThrows(ClassCastException.class, () -> ClassUtils.convertClassesToClassNames(list), "Should not have been able to convert list");
|
||||||
() -> ClassUtils.convertClassesToClassNames(list),
|
|
||||||
"Should not have been able to convert list");
|
|
||||||
assertNull(ClassUtils.convertClassesToClassNames(null));
|
assertNull(ClassUtils.convertClassesToClassNames(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,9 +148,7 @@ public class ClassUtilsTest {
|
||||||
@SuppressWarnings("unchecked") // test what happens when non-generic code adds wrong type of element
|
@SuppressWarnings("unchecked") // test what happens when non-generic code adds wrong type of element
|
||||||
final List<Object> olist = (List<Object>) (List<?>) list;
|
final List<Object> olist = (List<Object>) (List<?>) list;
|
||||||
olist.add(new Object());
|
olist.add(new Object());
|
||||||
assertThrows(ClassCastException.class,
|
assertThrows(ClassCastException.class, () -> ClassUtils.convertClassNamesToClasses(list), "Should not have been able to convert list");
|
||||||
() -> ClassUtils.convertClassNamesToClasses(list),
|
|
||||||
"Should not have been able to convert list");
|
|
||||||
assertNull(ClassUtils.convertClassNamesToClasses(null));
|
assertNull(ClassUtils.convertClassNamesToClasses(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,8 +183,7 @@ public class ClassUtilsTest {
|
||||||
assertEquals("j.l.String", ClassUtils.getAbbreviatedName("java.lang.String", 1));
|
assertEquals("j.l.String", ClassUtils.getAbbreviatedName("java.lang.String", 1));
|
||||||
assertEquals("o.a.c.l.ClassUtils", ClassUtils.getAbbreviatedName("org.apache.commons.lang3.ClassUtils", 18));
|
assertEquals("o.a.c.l.ClassUtils", ClassUtils.getAbbreviatedName("org.apache.commons.lang3.ClassUtils", 18));
|
||||||
assertEquals("org.apache.commons.lang3.ClassUtils",
|
assertEquals("org.apache.commons.lang3.ClassUtils",
|
||||||
ClassUtils.getAbbreviatedName("org.apache.commons.lang3.ClassUtils",
|
ClassUtils.getAbbreviatedName("org.apache.commons.lang3.ClassUtils", "org.apache.commons.lang3.ClassUtils".length()));
|
||||||
"org.apache.commons.lang3.ClassUtils".length()));
|
|
||||||
assertEquals("o.a.c.l.ClassUtils", ClassUtils.getAbbreviatedName("o.a.c.l.ClassUtils", 18));
|
assertEquals("o.a.c.l.ClassUtils", ClassUtils.getAbbreviatedName("o.a.c.l.ClassUtils", 18));
|
||||||
assertEquals("o..c.l.ClassUtils", ClassUtils.getAbbreviatedName("o..c.l.ClassUtils", 18));
|
assertEquals("o..c.l.ClassUtils", ClassUtils.getAbbreviatedName("o..c.l.ClassUtils", 18));
|
||||||
assertEquals(".", ClassUtils.getAbbreviatedName(".", 18));
|
assertEquals(".", ClassUtils.getAbbreviatedName(".", 18));
|
||||||
|
@ -205,15 +199,15 @@ public class ClassUtilsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test that in case the required length is larger than the name and thus there is no need for any shortening
|
* Test that in case the required length is larger than the name and thus there is no need for any shortening then the
|
||||||
* then the returned string object is the same as the one passed as argument. Note, however, that this is
|
* returned string object is the same as the one passed as argument. Note, however, that this is tested as an internal
|
||||||
* tested as an internal implementation detail, but it is not a guaranteed feature of the implementation.
|
* implementation detail, but it is not a guaranteed feature of the implementation.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("When the length hint is longer than the actual length then the same String object is returned")
|
@DisplayName("When the length hint is longer than the actual length then the same String object is returned")
|
||||||
public void test_getAbbreviatedName_TooLongHint(){
|
public void test_getAbbreviatedName_TooLongHint() {
|
||||||
final String className = "java.lang.String";
|
final String className = "java.lang.String";
|
||||||
Assertions.assertSame(className, ClassUtils.getAbbreviatedName(className, className.length()+1));
|
Assertions.assertSame(className, ClassUtils.getAbbreviatedName(className, className.length() + 1));
|
||||||
Assertions.assertSame(className, ClassUtils.getAbbreviatedName(className, className.length()));
|
Assertions.assertSame(className, ClassUtils.getAbbreviatedName(className, className.length()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -435,16 +429,11 @@ public class ClassUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test_getPackageCanonicalName_String() {
|
public void test_getPackageCanonicalName_String() {
|
||||||
assertEquals("org.apache.commons.lang3",
|
assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtils"));
|
||||||
ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtils"));
|
assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("[Lorg.apache.commons.lang3.ClassUtils;"));
|
||||||
assertEquals("org.apache.commons.lang3",
|
assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("[[Lorg.apache.commons.lang3.ClassUtils;"));
|
||||||
ClassUtils.getPackageCanonicalName("[Lorg.apache.commons.lang3.ClassUtils;"));
|
assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtils[]"));
|
||||||
assertEquals("org.apache.commons.lang3",
|
assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtils[][]"));
|
||||||
ClassUtils.getPackageCanonicalName("[[Lorg.apache.commons.lang3.ClassUtils;"));
|
|
||||||
assertEquals("org.apache.commons.lang3",
|
|
||||||
ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtils[]"));
|
|
||||||
assertEquals("org.apache.commons.lang3",
|
|
||||||
ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtils[][]"));
|
|
||||||
assertEquals("", ClassUtils.getPackageCanonicalName("[I"));
|
assertEquals("", ClassUtils.getPackageCanonicalName("[I"));
|
||||||
assertEquals("", ClassUtils.getPackageCanonicalName("[[I"));
|
assertEquals("", ClassUtils.getPackageCanonicalName("[[I"));
|
||||||
assertEquals("", ClassUtils.getPackageCanonicalName("int[]"));
|
assertEquals("", ClassUtils.getPackageCanonicalName("int[]"));
|
||||||
|
@ -570,15 +559,18 @@ public class ClassUtilsTest {
|
||||||
assertEquals("int[]", int[].class.getCanonicalName());
|
assertEquals("int[]", int[].class.getCanonicalName());
|
||||||
assertEquals("[I", int[].class.getName());
|
assertEquals("[I", int[].class.getName());
|
||||||
|
|
||||||
// Inner types... the problem is that these are not canonical names, classes with this name do not even have canonical name
|
// Inner types... the problem is that these are not canonical names, classes with this name do not even have canonical
|
||||||
|
// name
|
||||||
// WARNING: this is fragile, implementation may change, naming is not guaranteed
|
// WARNING: this is fragile, implementation may change, naming is not guaranteed
|
||||||
assertEquals("ClassUtilsTest.6", ClassUtils.getShortCanonicalName("org.apache.commons.lang3.ClassUtilsTest$6"));
|
assertEquals("ClassUtilsTest.6", ClassUtils.getShortCanonicalName("org.apache.commons.lang3.ClassUtilsTest$6"));
|
||||||
// WARNING: this is fragile, implementation may change, naming is not guaranteed
|
// WARNING: this is fragile, implementation may change, naming is not guaranteed
|
||||||
assertEquals("ClassUtilsTest.5Named", ClassUtils.getShortCanonicalName("org.apache.commons.lang3.ClassUtilsTest$5Named"));
|
assertEquals("ClassUtilsTest.5Named", ClassUtils.getShortCanonicalName("org.apache.commons.lang3.ClassUtilsTest$5Named"));
|
||||||
assertEquals("ClassUtilsTest.Inner", ClassUtils.getShortCanonicalName("org.apache.commons.lang3.ClassUtilsTest$Inner"));
|
assertEquals("ClassUtilsTest.Inner", ClassUtils.getShortCanonicalName("org.apache.commons.lang3.ClassUtilsTest$Inner"));
|
||||||
// demonstrating what a canonical name is... it is a bigger issue to clean this up
|
// demonstrating what a canonical name is... it is a bigger issue to clean this up
|
||||||
assertEquals("org.apache.commons.lang3.ClassUtilsTest$10", new org.apache.commons.lang3.ClassUtilsTest(){}.getClass().getName());
|
assertEquals("org.apache.commons.lang3.ClassUtilsTest$10", new org.apache.commons.lang3.ClassUtilsTest() {
|
||||||
assertNull(new org.apache.commons.lang3.ClassUtilsTest(){}.getClass().getCanonicalName());
|
}.getClass().getName());
|
||||||
|
assertNull(new org.apache.commons.lang3.ClassUtilsTest() {
|
||||||
|
}.getClass().getCanonicalName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -620,7 +612,7 @@ public class ClassUtilsTest {
|
||||||
class Named {
|
class Named {
|
||||||
// empty
|
// empty
|
||||||
}
|
}
|
||||||
// WARNING: this is fragile, implementation may change, naming is not guaranteed
|
// WARNING: this is fragile, implementation may change, naming is not guaranteed
|
||||||
assertEquals("ClassUtilsTest.12", ClassUtils.getShortClassName(new Object() {
|
assertEquals("ClassUtilsTest.12", ClassUtils.getShortClassName(new Object() {
|
||||||
// empty
|
// empty
|
||||||
}.getClass()));
|
}.getClass()));
|
||||||
|
@ -640,7 +632,7 @@ public class ClassUtilsTest {
|
||||||
class Named {
|
class Named {
|
||||||
// empty
|
// empty
|
||||||
}
|
}
|
||||||
// WARNING: this is fragile, implementation may change, naming is not guaranteed
|
// WARNING: this is fragile, implementation may change, naming is not guaranteed
|
||||||
assertEquals("ClassUtilsTest.13", ClassUtils.getShortClassName(new Object() {
|
assertEquals("ClassUtilsTest.13", ClassUtils.getShortClassName(new Object() {
|
||||||
// empty
|
// empty
|
||||||
}, "<null>"));
|
}, "<null>"));
|
||||||
|
@ -776,8 +768,8 @@ public class ClassUtilsTest {
|
||||||
final Class<?>[] array1 = new Class[] {Object.class};
|
final Class<?>[] array1 = new Class[] {Object.class};
|
||||||
final Class<?>[] array1s = new Class[] {String.class};
|
final Class<?>[] array1s = new Class[] {String.class};
|
||||||
final Class<?>[] array0 = new Class[] {};
|
final Class<?>[] array0 = new Class[] {};
|
||||||
final Class<?>[] arrayPrimitives = { Integer.TYPE, Boolean.TYPE };
|
final Class<?>[] arrayPrimitives = {Integer.TYPE, Boolean.TYPE};
|
||||||
final Class<?>[] arrayWrappers = { Integer.class, Boolean.class };
|
final Class<?>[] arrayWrappers = {Integer.class, Boolean.class};
|
||||||
|
|
||||||
assertFalse(ClassUtils.isAssignable(array1, array2));
|
assertFalse(ClassUtils.isAssignable(array1, array2));
|
||||||
assertFalse(ClassUtils.isAssignable(null, array2));
|
assertFalse(ClassUtils.isAssignable(null, array2));
|
||||||
|
@ -804,8 +796,8 @@ public class ClassUtilsTest {
|
||||||
final Class<?>[] array1 = new Class[] {Object.class};
|
final Class<?>[] array1 = new Class[] {Object.class};
|
||||||
final Class<?>[] array1s = new Class[] {String.class};
|
final Class<?>[] array1s = new Class[] {String.class};
|
||||||
final Class<?>[] array0 = new Class[] {};
|
final Class<?>[] array0 = new Class[] {};
|
||||||
final Class<?>[] arrayPrimitives = { Integer.TYPE, Boolean.TYPE };
|
final Class<?>[] arrayPrimitives = {Integer.TYPE, Boolean.TYPE};
|
||||||
final Class<?>[] arrayWrappers = { Integer.class, Boolean.class };
|
final Class<?>[] arrayWrappers = {Integer.class, Boolean.class};
|
||||||
|
|
||||||
assertFalse(ClassUtils.isAssignable(array1, array2, true));
|
assertFalse(ClassUtils.isAssignable(array1, array2, true));
|
||||||
assertFalse(ClassUtils.isAssignable(null, array2, true));
|
assertFalse(ClassUtils.isAssignable(null, array2, true));
|
||||||
|
@ -832,8 +824,8 @@ public class ClassUtilsTest {
|
||||||
final Class<?>[] array1 = new Class[] {Object.class};
|
final Class<?>[] array1 = new Class[] {Object.class};
|
||||||
final Class<?>[] array1s = new Class[] {String.class};
|
final Class<?>[] array1s = new Class[] {String.class};
|
||||||
final Class<?>[] array0 = new Class[] {};
|
final Class<?>[] array0 = new Class[] {};
|
||||||
final Class<?>[] arrayPrimitives = { Integer.TYPE, Boolean.TYPE };
|
final Class<?>[] arrayPrimitives = {Integer.TYPE, Boolean.TYPE};
|
||||||
final Class<?>[] arrayWrappers = { Integer.class, Boolean.class };
|
final Class<?>[] arrayWrappers = {Integer.class, Boolean.class};
|
||||||
|
|
||||||
assertFalse(ClassUtils.isAssignable(array1, array2, false));
|
assertFalse(ClassUtils.isAssignable(array1, array2, false));
|
||||||
assertFalse(ClassUtils.isAssignable(null, array2, false));
|
assertFalse(ClassUtils.isAssignable(null, array2, false));
|
||||||
|
@ -1169,89 +1161,89 @@ public class ClassUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetClassByNormalNameArrays() throws ClassNotFoundException {
|
public void testGetClassByNormalNameArrays() throws ClassNotFoundException {
|
||||||
assertEquals( int[].class, ClassUtils.getClass( "int[]" ) );
|
assertEquals(int[].class, ClassUtils.getClass("int[]"));
|
||||||
assertEquals( long[].class, ClassUtils.getClass( "long[]" ) );
|
assertEquals(long[].class, ClassUtils.getClass("long[]"));
|
||||||
assertEquals( short[].class, ClassUtils.getClass( "short[]" ) );
|
assertEquals(short[].class, ClassUtils.getClass("short[]"));
|
||||||
assertEquals( byte[].class, ClassUtils.getClass( "byte[]" ) );
|
assertEquals(byte[].class, ClassUtils.getClass("byte[]"));
|
||||||
assertEquals( char[].class, ClassUtils.getClass( "char[]" ) );
|
assertEquals(char[].class, ClassUtils.getClass("char[]"));
|
||||||
assertEquals( float[].class, ClassUtils.getClass( "float[]" ) );
|
assertEquals(float[].class, ClassUtils.getClass("float[]"));
|
||||||
assertEquals( double[].class, ClassUtils.getClass( "double[]" ) );
|
assertEquals(double[].class, ClassUtils.getClass("double[]"));
|
||||||
assertEquals( boolean[].class, ClassUtils.getClass( "boolean[]" ) );
|
assertEquals(boolean[].class, ClassUtils.getClass("boolean[]"));
|
||||||
assertEquals( String[].class, ClassUtils.getClass( "java.lang.String[]" ) );
|
assertEquals(String[].class, ClassUtils.getClass("java.lang.String[]"));
|
||||||
assertEquals( java.util.Map.Entry[].class, ClassUtils.getClass( "java.util.Map.Entry[]" ) );
|
assertEquals(java.util.Map.Entry[].class, ClassUtils.getClass("java.util.Map.Entry[]"));
|
||||||
assertEquals( java.util.Map.Entry[].class, ClassUtils.getClass( "java.util.Map$Entry[]" ) );
|
assertEquals(java.util.Map.Entry[].class, ClassUtils.getClass("java.util.Map$Entry[]"));
|
||||||
assertEquals( java.util.Map.Entry[].class, ClassUtils.getClass( "[Ljava.util.Map.Entry;" ) );
|
assertEquals(java.util.Map.Entry[].class, ClassUtils.getClass("[Ljava.util.Map.Entry;"));
|
||||||
assertEquals( java.util.Map.Entry[].class, ClassUtils.getClass( "[Ljava.util.Map$Entry;" ) );
|
assertEquals(java.util.Map.Entry[].class, ClassUtils.getClass("[Ljava.util.Map$Entry;"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetClassByNormalNameArrays2D() throws ClassNotFoundException {
|
public void testGetClassByNormalNameArrays2D() throws ClassNotFoundException {
|
||||||
assertEquals( int[][].class, ClassUtils.getClass( "int[][]" ) );
|
assertEquals(int[][].class, ClassUtils.getClass("int[][]"));
|
||||||
assertEquals( long[][].class, ClassUtils.getClass( "long[][]" ) );
|
assertEquals(long[][].class, ClassUtils.getClass("long[][]"));
|
||||||
assertEquals( short[][].class, ClassUtils.getClass( "short[][]" ) );
|
assertEquals(short[][].class, ClassUtils.getClass("short[][]"));
|
||||||
assertEquals( byte[][].class, ClassUtils.getClass( "byte[][]" ) );
|
assertEquals(byte[][].class, ClassUtils.getClass("byte[][]"));
|
||||||
assertEquals( char[][].class, ClassUtils.getClass( "char[][]" ) );
|
assertEquals(char[][].class, ClassUtils.getClass("char[][]"));
|
||||||
assertEquals( float[][].class, ClassUtils.getClass( "float[][]" ) );
|
assertEquals(float[][].class, ClassUtils.getClass("float[][]"));
|
||||||
assertEquals( double[][].class, ClassUtils.getClass( "double[][]" ) );
|
assertEquals(double[][].class, ClassUtils.getClass("double[][]"));
|
||||||
assertEquals( boolean[][].class, ClassUtils.getClass( "boolean[][]" ) );
|
assertEquals(boolean[][].class, ClassUtils.getClass("boolean[][]"));
|
||||||
assertEquals( String[][].class, ClassUtils.getClass( "java.lang.String[][]" ) );
|
assertEquals(String[][].class, ClassUtils.getClass("java.lang.String[][]"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetClassClassNotFound() throws Exception {
|
public void testGetClassClassNotFound() throws Exception {
|
||||||
assertGetClassThrowsClassNotFound( "bool" );
|
assertGetClassThrowsClassNotFound("bool");
|
||||||
assertGetClassThrowsClassNotFound( "bool[]" );
|
assertGetClassThrowsClassNotFound("bool[]");
|
||||||
assertGetClassThrowsClassNotFound( "integer[]" );
|
assertGetClassThrowsClassNotFound("integer[]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetClassInvalidArguments() throws Exception {
|
public void testGetClassInvalidArguments() throws Exception {
|
||||||
assertGetClassThrowsNullPointerException( null );
|
assertGetClassThrowsNullPointerException(null);
|
||||||
assertGetClassThrowsClassNotFound( "[][][]" );
|
assertGetClassThrowsClassNotFound("[][][]");
|
||||||
assertGetClassThrowsClassNotFound( "[[]" );
|
assertGetClassThrowsClassNotFound("[[]");
|
||||||
assertGetClassThrowsClassNotFound( "[" );
|
assertGetClassThrowsClassNotFound("[");
|
||||||
assertGetClassThrowsClassNotFound( "java.lang.String][" );
|
assertGetClassThrowsClassNotFound("java.lang.String][");
|
||||||
assertGetClassThrowsClassNotFound( ".hello.world" );
|
assertGetClassThrowsClassNotFound(".hello.world");
|
||||||
assertGetClassThrowsClassNotFound( "hello..world" );
|
assertGetClassThrowsClassNotFound("hello..world");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetClassRawPrimitives() throws ClassNotFoundException {
|
public void testGetClassRawPrimitives() throws ClassNotFoundException {
|
||||||
assertEquals( int.class, ClassUtils.getClass( "int" ) );
|
assertEquals(int.class, ClassUtils.getClass("int"));
|
||||||
assertEquals( long.class, ClassUtils.getClass( "long" ) );
|
assertEquals(long.class, ClassUtils.getClass("long"));
|
||||||
assertEquals( short.class, ClassUtils.getClass( "short" ) );
|
assertEquals(short.class, ClassUtils.getClass("short"));
|
||||||
assertEquals( byte.class, ClassUtils.getClass( "byte" ) );
|
assertEquals(byte.class, ClassUtils.getClass("byte"));
|
||||||
assertEquals( char.class, ClassUtils.getClass( "char" ) );
|
assertEquals(char.class, ClassUtils.getClass("char"));
|
||||||
assertEquals( float.class, ClassUtils.getClass( "float" ) );
|
assertEquals(float.class, ClassUtils.getClass("float"));
|
||||||
assertEquals( double.class, ClassUtils.getClass( "double" ) );
|
assertEquals(double.class, ClassUtils.getClass("double"));
|
||||||
assertEquals( boolean.class, ClassUtils.getClass( "boolean" ) );
|
assertEquals(boolean.class, ClassUtils.getClass("boolean"));
|
||||||
assertEquals( void.class, ClassUtils.getClass( "void" ) );
|
assertEquals(void.class, ClassUtils.getClass("void"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetClassWithArrayClasses() throws Exception {
|
public void testGetClassWithArrayClasses() throws Exception {
|
||||||
assertGetClassReturnsClass( String[].class );
|
assertGetClassReturnsClass(String[].class);
|
||||||
assertGetClassReturnsClass( int[].class );
|
assertGetClassReturnsClass(int[].class);
|
||||||
assertGetClassReturnsClass( long[].class );
|
assertGetClassReturnsClass(long[].class);
|
||||||
assertGetClassReturnsClass( short[].class );
|
assertGetClassReturnsClass(short[].class);
|
||||||
assertGetClassReturnsClass( byte[].class );
|
assertGetClassReturnsClass(byte[].class);
|
||||||
assertGetClassReturnsClass( char[].class );
|
assertGetClassReturnsClass(char[].class);
|
||||||
assertGetClassReturnsClass( float[].class );
|
assertGetClassReturnsClass(float[].class);
|
||||||
assertGetClassReturnsClass( double[].class );
|
assertGetClassReturnsClass(double[].class);
|
||||||
assertGetClassReturnsClass( boolean[].class );
|
assertGetClassReturnsClass(boolean[].class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetClassWithArrayClasses2D() throws Exception {
|
public void testGetClassWithArrayClasses2D() throws Exception {
|
||||||
assertGetClassReturnsClass( String[][].class );
|
assertGetClassReturnsClass(String[][].class);
|
||||||
assertGetClassReturnsClass( int[][].class );
|
assertGetClassReturnsClass(int[][].class);
|
||||||
assertGetClassReturnsClass( long[][].class );
|
assertGetClassReturnsClass(long[][].class);
|
||||||
assertGetClassReturnsClass( short[][].class );
|
assertGetClassReturnsClass(short[][].class);
|
||||||
assertGetClassReturnsClass( byte[][].class );
|
assertGetClassReturnsClass(byte[][].class);
|
||||||
assertGetClassReturnsClass( char[][].class );
|
assertGetClassReturnsClass(char[][].class);
|
||||||
assertGetClassReturnsClass( float[][].class );
|
assertGetClassReturnsClass(float[][].class);
|
||||||
assertGetClassReturnsClass( double[][].class );
|
assertGetClassReturnsClass(double[][].class);
|
||||||
assertGetClassReturnsClass( boolean[][].class );
|
assertGetClassReturnsClass(boolean[][].class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1267,10 +1259,10 @@ public class ClassUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetInnerClass() throws ClassNotFoundException {
|
public void testGetInnerClass() throws ClassNotFoundException {
|
||||||
assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang3.ClassUtilsTest.Inner.DeeplyNested" ) );
|
assertEquals(Inner.DeeplyNested.class, ClassUtils.getClass("org.apache.commons.lang3.ClassUtilsTest.Inner.DeeplyNested"));
|
||||||
assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang3.ClassUtilsTest.Inner$DeeplyNested" ) );
|
assertEquals(Inner.DeeplyNested.class, ClassUtils.getClass("org.apache.commons.lang3.ClassUtilsTest.Inner$DeeplyNested"));
|
||||||
assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang3.ClassUtilsTest$Inner$DeeplyNested" ) );
|
assertEquals(Inner.DeeplyNested.class, ClassUtils.getClass("org.apache.commons.lang3.ClassUtilsTest$Inner$DeeplyNested"));
|
||||||
assertEquals( Inner.DeeplyNested.class, ClassUtils.getClass( "org.apache.commons.lang3.ClassUtilsTest$Inner.DeeplyNested" ) );
|
assertEquals(Inner.DeeplyNested.class, ClassUtils.getClass("org.apache.commons.lang3.ClassUtilsTest$Inner.DeeplyNested"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1297,8 +1289,7 @@ public class ClassUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHierarchyIncludingInterfaces() {
|
public void testHierarchyIncludingInterfaces() {
|
||||||
final Iterator<Class<?>> iter =
|
final Iterator<Class<?>> iter = ClassUtils.hierarchy(StringParameterizedChild.class, Interfaces.INCLUDE).iterator();
|
||||||
ClassUtils.hierarchy(StringParameterizedChild.class, Interfaces.INCLUDE).iterator();
|
|
||||||
assertEquals(StringParameterizedChild.class, iter.next());
|
assertEquals(StringParameterizedChild.class, iter.next());
|
||||||
assertEquals(GenericParent.class, iter.next());
|
assertEquals(GenericParent.class, iter.next());
|
||||||
assertEquals(GenericConsumer.class, iter.next());
|
assertEquals(GenericConsumer.class, iter.next());
|
||||||
|
@ -1376,20 +1367,16 @@ public class ClassUtilsTest {
|
||||||
// Other possible casts for null
|
// Other possible casts for null
|
||||||
assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.primitivesToWrappers(), "empty -> empty");
|
assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.primitivesToWrappers(), "empty -> empty");
|
||||||
final Class<?>[] castNull = ClassUtils.primitivesToWrappers((Class<?>) null); // == new Class<?>[]{null}
|
final Class<?>[] castNull = ClassUtils.primitivesToWrappers((Class<?>) null); // == new Class<?>[]{null}
|
||||||
assertArrayEquals(new Class<?>[]{null}, castNull, "(Class<?>) null -> [null]");
|
assertArrayEquals(new Class<?>[] {null}, castNull, "(Class<?>) null -> [null]");
|
||||||
// test empty array is returned unchanged
|
// test empty array is returned unchanged
|
||||||
assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.primitivesToWrappers(ArrayUtils.EMPTY_CLASS_ARRAY),
|
assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.primitivesToWrappers(ArrayUtils.EMPTY_CLASS_ARRAY), "empty -> empty");
|
||||||
"empty -> empty");
|
|
||||||
|
|
||||||
// test an array of various classes
|
// test an array of various classes
|
||||||
final Class<?>[] primitives = new Class[] {
|
final Class<?>[] primitives = new Class[] {Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Double.TYPE, Float.TYPE,
|
||||||
Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE,
|
String.class, ClassUtils.class};
|
||||||
Integer.TYPE, Long.TYPE, Double.TYPE, Float.TYPE,
|
final Class<?>[] wrappers = ClassUtils.primitivesToWrappers(primitives);
|
||||||
String.class, ClassUtils.class
|
|
||||||
};
|
|
||||||
final Class<?>[] wrappers= ClassUtils.primitivesToWrappers(primitives);
|
|
||||||
|
|
||||||
for (int i=0; i < primitives.length; i++) {
|
for (int i = 0; i < primitives.length; i++) {
|
||||||
// test each returned wrapper
|
// test each returned wrapper
|
||||||
final Class<?> primitive = primitives[i];
|
final Class<?> primitive = primitives[i];
|
||||||
final Class<?> expectedWrapper = ClassUtils.primitiveToWrapper(primitive);
|
final Class<?> expectedWrapper = ClassUtils.primitiveToWrapper(primitive);
|
||||||
|
@ -1398,9 +1385,7 @@ public class ClassUtilsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
// test an array of no primitive classes
|
// test an array of no primitive classes
|
||||||
final Class<?>[] noPrimitives = new Class[] {
|
final Class<?>[] noPrimitives = new Class[] {String.class, ClassUtils.class, Void.TYPE};
|
||||||
String.class, ClassUtils.class, Void.TYPE
|
|
||||||
};
|
|
||||||
// This used to return the exact same array, but no longer does.
|
// This used to return the exact same array, but no longer does.
|
||||||
assertNotSame(noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives), "unmodified");
|
assertNotSame(noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives), "unmodified");
|
||||||
}
|
}
|
||||||
|
@ -1420,8 +1405,7 @@ public class ClassUtilsTest {
|
||||||
|
|
||||||
// test a few other classes
|
// test a few other classes
|
||||||
assertEquals(String.class, ClassUtils.primitiveToWrapper(String.class), "String.class -> String.class");
|
assertEquals(String.class, ClassUtils.primitiveToWrapper(String.class), "String.class -> String.class");
|
||||||
assertEquals(ClassUtils.class, ClassUtils.primitiveToWrapper(ClassUtils.class),
|
assertEquals(ClassUtils.class, ClassUtils.primitiveToWrapper(ClassUtils.class), "ClassUtils.class -> ClassUtils.class");
|
||||||
"ClassUtils.class -> ClassUtils.class");
|
|
||||||
assertEquals(Void.TYPE, ClassUtils.primitiveToWrapper(Void.TYPE), "Void.TYPE -> Void.TYPE");
|
assertEquals(Void.TYPE, ClassUtils.primitiveToWrapper(Void.TYPE), "Void.TYPE -> Void.TYPE");
|
||||||
|
|
||||||
// test null
|
// test null
|
||||||
|
@ -1446,31 +1430,28 @@ public class ClassUtilsTest {
|
||||||
// Additional varargs tests
|
// Additional varargs tests
|
||||||
assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.toClass(), "empty -> empty");
|
assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.toClass(), "empty -> empty");
|
||||||
final Class<?>[] castNull = ClassUtils.toClass((Object) null); // == new Object[]{null}
|
final Class<?>[] castNull = ClassUtils.toClass((Object) null); // == new Object[]{null}
|
||||||
assertArrayEquals(new Object[]{null}, castNull, "(Object) null -> [null]");
|
assertArrayEquals(new Object[] {null}, castNull, "(Object) null -> [null]");
|
||||||
|
|
||||||
assertSame(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.toClass(ArrayUtils.EMPTY_OBJECT_ARRAY));
|
assertSame(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.toClass(ArrayUtils.EMPTY_OBJECT_ARRAY));
|
||||||
|
|
||||||
assertArrayEquals(new Class[]{String.class, Integer.class, Double.class}, ClassUtils.toClass("Test", Integer.valueOf(1), Double.valueOf(99d)));
|
assertArrayEquals(new Class[] {String.class, Integer.class, Double.class}, ClassUtils.toClass("Test", Integer.valueOf(1), Double.valueOf(99d)));
|
||||||
|
|
||||||
assertArrayEquals(new Class[]{String.class, null, Double.class}, ClassUtils.toClass("Test", null, Double.valueOf(99d)));
|
assertArrayEquals(new Class[] {String.class, null, Double.class}, ClassUtils.toClass("Test", null, Double.valueOf(99d)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithInterleavingWhitespace() throws ClassNotFoundException {
|
public void testWithInterleavingWhitespace() throws ClassNotFoundException {
|
||||||
assertEquals( int[].class, ClassUtils.getClass( " int [ ] " ) );
|
assertEquals(int[].class, ClassUtils.getClass(" int [ ] "));
|
||||||
assertEquals( long[].class, ClassUtils.getClass( "\rlong\t[\n]\r" ) );
|
assertEquals(long[].class, ClassUtils.getClass("\rlong\t[\n]\r"));
|
||||||
assertEquals( short[].class, ClassUtils.getClass( "\tshort \t\t[]" ) );
|
assertEquals(short[].class, ClassUtils.getClass("\tshort \t\t[]"));
|
||||||
assertEquals( byte[].class, ClassUtils.getClass( "byte[\t\t\n\r] " ) );
|
assertEquals(byte[].class, ClassUtils.getClass("byte[\t\t\n\r] "));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrappersToPrimitives() {
|
public void testWrappersToPrimitives() {
|
||||||
// an array with classes to test
|
// an array with classes to test
|
||||||
final Class<?>[] classes = {
|
final Class<?>[] classes = {Boolean.class, Byte.class, Character.class, Short.class, Integer.class, Long.class, Float.class, Double.class, String.class,
|
||||||
Boolean.class, Byte.class, Character.class, Short.class,
|
ClassUtils.class, null};
|
||||||
Integer.class, Long.class, Float.class, Double.class,
|
|
||||||
String.class, ClassUtils.class, null
|
|
||||||
};
|
|
||||||
|
|
||||||
final Class<?>[] primitives = ClassUtils.wrappersToPrimitives(classes);
|
final Class<?>[] primitives = ClassUtils.wrappersToPrimitives(classes);
|
||||||
// now test the result
|
// now test the result
|
||||||
|
@ -1494,16 +1475,13 @@ public class ClassUtilsTest {
|
||||||
// Other possible casts for null
|
// Other possible casts for null
|
||||||
assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.wrappersToPrimitives(), "empty -> empty");
|
assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.wrappersToPrimitives(), "empty -> empty");
|
||||||
final Class<?>[] castNull = ClassUtils.wrappersToPrimitives((Class<?>) null); // == new Class<?>[]{null}
|
final Class<?>[] castNull = ClassUtils.wrappersToPrimitives((Class<?>) null); // == new Class<?>[]{null}
|
||||||
assertArrayEquals(new Class<?>[]{null}, castNull, "(Class<?>) null -> [null]");
|
assertArrayEquals(new Class<?>[] {null}, castNull, "(Class<?>) null -> [null]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrapperToPrimitive() {
|
public void testWrapperToPrimitive() {
|
||||||
// an array with classes to convert
|
// an array with classes to convert
|
||||||
final Class<?>[] primitives = {
|
final Class<?>[] primitives = {Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE};
|
||||||
Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE,
|
|
||||||
Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE
|
|
||||||
};
|
|
||||||
for (final Class<?> primitive : primitives) {
|
for (final Class<?> primitive : primitives) {
|
||||||
final Class<?> wrapperCls = ClassUtils.primitiveToWrapper(primitive);
|
final Class<?> wrapperCls = ClassUtils.primitiveToWrapper(primitive);
|
||||||
assertFalse(wrapperCls.isPrimitive(), "Still primitive");
|
assertFalse(wrapperCls.isPrimitive(), "Still primitive");
|
||||||
|
|
Loading…
Reference in New Issue