예제 #1
0
  @SuppressWarnings("unchecked")
  public <ArrayTemplate extends AbstractArrayTemplate<E>, E> void testArrayBadInput(
      Class<ArrayTemplate> templateClass,
      ArrayDataSchema schema,
      List<E> good,
      List<Object> badInput,
      List<Object> badOutput)
      throws InstantiationException, IllegalAccessException {
    Exception exc = null;
    ArrayTemplate arrayTemplateBad = templateClass.newInstance();
    DataList badDataList = new DataList();
    ArrayTemplate badWrappedArrayTemplate =
        DataTemplateUtil.wrap(badDataList, schema, templateClass);

    List<E> badIn = (List<E>) badInput;

    // add(E element)
    for (E o : badIn) {
      try {
        exc = null;
        arrayTemplateBad.add(o);
      } catch (Exception e) {
        exc = e;
      }
      assertTrue(exc != null);
      assertTrue(o == null || exc instanceof ClassCastException);
      assertTrue(o != null || exc instanceof NullPointerException);
    }

    // add(int index, E element)
    for (Object o : badIn) {
      try {
        exc = null;
        arrayTemplateBad.add(0, (E) o);
      } catch (Exception e) {
        exc = e;
      }
      assertTrue(exc != null);
      assertTrue(o == null || exc instanceof ClassCastException);
      assertTrue(o != null || exc instanceof NullPointerException);
    }

    // addAll(Collection<E> c)
    try {
      exc = null;
      arrayTemplateBad.addAll(badIn);
    } catch (Exception e) {
      exc = e;
    }
    assertTrue(exc != null);
    assertTrue(exc instanceof ClassCastException);

    // set(int index, E element)
    arrayTemplateBad.addAll(good);
    assertTrue(arrayTemplateBad.size() > 1);
    for (Object o : badIn) {
      try {
        exc = null;
        arrayTemplateBad.set(0, (E) o);
      } catch (Exception e) {
        exc = e;
      }
      assertTrue(exc != null);
      assertTrue(o == null || exc instanceof ClassCastException);
      assertTrue(o != null || exc instanceof NullPointerException);
    }

    // listIterator add
    for (Object o : badIn) {
      try {
        exc = null;
        ListIterator<E> it = arrayTemplateBad.listIterator(0);
        it.add((E) o);
      } catch (Exception e) {
        exc = e;
      }
      assertTrue(exc != null);
      assertTrue(o == null || exc instanceof ClassCastException);
      assertTrue(o != null || exc instanceof NullPointerException);
    }

    // listIterator set
    for (Object o : badIn) {
      try {
        exc = null;
        ListIterator<E> it = arrayTemplateBad.listIterator(0);
        it.next();
        it.set((E) o);
      } catch (Exception e) {
        exc = e;
      }
      assertTrue(exc != null);
      assertTrue(o == null || exc instanceof ClassCastException);
      assertTrue(o != null || exc instanceof NullPointerException);
    }

    badDataList.clear();
    badDataList.addAll(badOutput);
    badWrappedArrayTemplate = DataTemplateUtil.wrap(badDataList, schema, templateClass);

    // Get returns bad
    for (int i = 0; i < badWrappedArrayTemplate.size(); ++i) {
      try {
        exc = null;
        badWrappedArrayTemplate.get(i);
      } catch (Exception e) {
        exc = e;
      }
      assertTrue(exc != null);
      assertTrue(exc instanceof TemplateOutputCastException);
    }

    // Set returns bad
    badDataList.clear();
    badDataList.addAll(badOutput);
    assertEquals(badWrappedArrayTemplate.size(), badOutput.size());
    for (int i = 0; i < badWrappedArrayTemplate.size(); ++i) {
      try {
        exc = null;
        badWrappedArrayTemplate.set(i, good.get(0));
      } catch (Exception e) {
        exc = e;
      }
      assertTrue(exc != null);
      assertTrue(exc instanceof TemplateOutputCastException);
    }

    // Remove returns bad
    badDataList.clear();
    badDataList.addAll(badOutput);
    assertEquals(badWrappedArrayTemplate.size(), badOutput.size());
    for (int i = 0; i < badWrappedArrayTemplate.size(); ++i) {
      try {
        exc = null;
        badWrappedArrayTemplate.remove(0);
      } catch (Exception e) {
        exc = e;
      }
      assertTrue(exc != null);
      assertTrue(exc instanceof TemplateOutputCastException);
    }

    // Iterator returns bad
    for (Object o : badOutput) {
      badDataList.clear();
      badDataList.add(o);
      try {
        exc = null;
        badWrappedArrayTemplate.iterator().next();
      } catch (Exception e) {
        exc = e;
      }
      assertTrue(exc != null);
      assertTrue(exc instanceof TemplateOutputCastException);
    }

    // ListIterator returns bad
    for (Object o : badOutput) {
      badDataList.clear();
      badDataList.add(o);
      try {
        exc = null;
        badWrappedArrayTemplate.listIterator().next();
      } catch (Exception e) {
        exc = e;
      }
      assertTrue(exc != null);
      assertTrue(exc instanceof TemplateOutputCastException);
    }
    for (Object o : badOutput) {
      badDataList.clear();
      badDataList.add(o);
      try {
        exc = null;
        badWrappedArrayTemplate.listIterator(badWrappedArrayTemplate.size()).previous();
      } catch (Exception e) {
        exc = e;
      }
      assertTrue(exc != null);
      assertTrue(exc instanceof TemplateOutputCastException);
    }
  }