ConvertUtil { @SuppressWarnings("unchecked") public static <E> List<E> toList(Object obj, Class<E> elementClass) throws IllegalArgumentException { if (elementClass == null) { throw new IllegalArgumentException("Argument 'elementClass' (" + Class.class.getName() + ") is null"); } Object[] array = toArray(obj); List<E> result = new ArrayList<E>(); for (Object element : array) { if (element != null && !elementClass.isAssignableFrom(element.getClass())) { String message = "Unable to cast '" + element.getClass().getName() + "' to '" + elementClass.getName() + "'"; throw new IllegalArgumentException( message, new ClassCastException(message)); } result.add((E) element); } return result; }  static Object[] toArray(Object obj); @SuppressWarnings("unchecked") static List<E> toList(Object obj, Class<E> elementClass); static T convert(Object obj, Class<T> clazz); static T convertIfNotNull(Object obj, Class<T> clazz); @SuppressWarnings("unchecked") static T convert(
            Object obj, Class<T> clazz, boolean allowsNull); static Object convertPrimitiveArrayToList(Object value); @SuppressWarnings("unchecked") static List<Map<String, Object>> toListOfMap(Object obj); static final String CLASS_FIELDNAME; }
@Test public void testToList01() throws Exception { try { ConvertUtil.toList(null, null); fail("IllegalArgumentExceptionがスローされませんでした。"); } catch (IllegalArgumentException e) { assertEquals(IllegalArgumentException.class.getName(), e.getClass() .getName()); assertEquals("Argument 'elementClass' (" + Class.class.getName() + ") is null", e.getMessage()); } }
@Test public void testToList02() throws Exception { List<Object> result = ConvertUtil.toList(null, Object.class); assertEquals(0, result.size()); }
@Test public void testToList03() throws Exception { List<Object> result = ConvertUtil.toList("", Object.class); assertEquals(1, result.size()); assertEquals("", result.get(0)); }
@Test public void testToList04() throws Exception { List<Object> result = ConvertUtil.toList(" ", Object.class); assertEquals(1, result.size()); assertEquals(" ", result.get(0)); }
@Test public void testToList05() throws Exception { List<Object> result = ConvertUtil.toList("list", Object.class); assertEquals(1, result.size()); assertEquals("list", result.get(0)); }
@Test public void testToList06() throws Exception { try { ConvertUtil.toList("list", Thread.class); fail("IllegalArgumentExceptionがスローされませんでした。"); } catch (IllegalArgumentException e) { assertEquals(IllegalArgumentException.class.getName(), e.getClass() .getName()); assertEquals("Unable to cast '" + "list".getClass().getName() + "' to '" + Thread.class.getName() + "'", e.getMessage()); assertEquals(ClassCastException.class.getName(), e.getCause() .getClass().getName()); assertEquals("Unable to cast '" + "list".getClass().getName() + "' to '" + Thread.class.getName() + "'", e.getCause() .getMessage()); } }
@Test public void testToList07() throws Exception { Object[] obj = new Object[0]; List<String> result = ConvertUtil.toList(obj, String.class); assertEquals(0, result.size()); }
@Test public void testToList08() throws Exception { Object[] obj = { "foo" }; List<String> result = ConvertUtil.toList(obj, String.class); assertEquals(1, result.size()); assertEquals("foo", result.get(0)); }
@Test public void testToList09() throws Exception { Object[] obj = new Object[3]; obj[0] = "foo"; obj[1] = "bar"; obj[2] = "baz"; List<String> result = ConvertUtil.toList(obj, String.class); assertEquals(3, result.size()); assertEquals("foo", result.get(0)); assertEquals("bar", result.get(1)); assertEquals("baz", result.get(2)); }
@Test public void testToList10() throws Exception { Object[] obj = new Object[3]; obj[0] = "foo"; obj[1] = new Thread(); obj[2] = "baz"; try { ConvertUtil.toList(obj, String.class); fail("IllegalArgumentExceptionがスローされませんでした。"); } catch (IllegalArgumentException e) { assertEquals(IllegalArgumentException.class.getName(), e.getClass() .getName()); assertEquals("Unable to cast '" + Thread.class.getName() + "' to '" + String.class.getName() + "'", e.getMessage()); assertEquals(ClassCastException.class.getName(), e.getCause() .getClass().getName()); assertEquals("Unable to cast '" + Thread.class.getName() + "' to '" + String.class.getName() + "'", e.getCause().getMessage()); } }
@Test public void testToList11() throws Exception { Collection<?> obj = new Vector<Object>(); List<String> result = ConvertUtil.toList(obj, String.class); assertEquals(0, result.size()); }
@Test public void testToList12() throws Exception { Collection<String> obj = new Vector<String>(); obj.add("foo"); List<String> result = ConvertUtil.toList(obj, String.class); assertEquals(1, result.size()); assertEquals("foo", result.get(0)); }
@Test public void testToList13() throws Exception { Collection<String> obj = new Vector<String>(); obj.add("foo"); obj.add("bar"); obj.add("baz"); List<String> result = ConvertUtil.toList(obj, String.class); assertEquals(3, result.size()); assertEquals("foo", result.get(0)); assertEquals("bar", result.get(1)); assertEquals("baz", result.get(2)); }
@Test public void testToList14() throws Exception { Object[] obj = new Object[3]; obj[0] = "foo"; obj[1] = new Thread(); obj[2] = "baz"; try { ConvertUtil.toList(obj, String.class); fail("IllegalArgumentExceptionがスローされませんでした。"); } catch (IllegalArgumentException e) { assertEquals(IllegalArgumentException.class.getName(), e.getClass() .getName()); assertEquals("Unable to cast '" + Thread.class.getName() + "' to '" + String.class.getName() + "'", e.getMessage()); assertEquals(ClassCastException.class.getName(), e.getCause() .getClass().getName()); assertEquals("Unable to cast '" + Thread.class.getName() + "' to '" + String.class.getName() + "'", e.getCause().getMessage()); } }