/**
   * Test method for {@link ResourcePortletRequest#ResourcePortletRequest(ApplicationContext,
   * PortletContext, ResourceRequest, ResourceResponse)}.
   *
   * @throws NoSuchFieldException If something goes wrong.
   * @throws SecurityException If something goes wrong.
   * @throws IllegalAccessException If something goes wrong.
   * @throws IllegalArgumentException If something goes wrong.
   */
  @Test
  public void testResourcePortletRequest() throws NoSuchFieldException, IllegalAccessException {
    ApplicationContext applicationContext = createMock(ApplicationContext.class);
    PortletContext portletContext = createMock(PortletContext.class);
    ResourceRequest request = createMock(ResourceRequest.class);
    ResourceResponse response = createMock(ResourceResponse.class);

    replay(applicationContext, portletContext, request, response);
    ResourcePortletRequest req =
        new ResourcePortletRequest(applicationContext, portletContext, request, response);
    Class<? extends ResourcePortletRequest> clazz = req.getClass();
    Field field = clazz.getSuperclass().getDeclaredField("requestDelegate");
    assertTrue(field.get(req) instanceof PortletRequestDelegate);
    field = clazz.getSuperclass().getDeclaredField("responseDelegate");
    assertTrue(field.get(req) instanceof MimeResponseDelegate);
    verify(applicationContext, portletContext, request, response);
  }
  @Test
  public void testGenerate() throws Exception {

    // Make the relevant XML adapter class bits.
    final byte[] newClass =
        this.generator.generate(
            "com.edugility.jaxb.PersonToPersonImplementationAdapter",
            Person.class,
            PersonImplementation.class);
    assertNotNull(newClass);
    assertTrue(newClass.length > 0);

    // Turn those class bits into a real Java class.
    final Class<?> adapterClass =
        new ClassDefiner()
            .define("com.edugility.jaxb.PersonToPersonImplementationAdapter", newClass);
    assertNotNull(adapterClass);
    assertEquals("com.edugility.jaxb.PersonToPersonImplementationAdapter", adapterClass.getName());
    assertTrue(UniversalXmlAdapter.class.isAssignableFrom(adapterClass));

    // Make sure that new class "is a" UniversalXmlAdapter.
    assertSame(UniversalXmlAdapter.class, adapterClass.getSuperclass());

    // Make sure all the generics work right in the new class.
    // UniversalXmlAdapter is an abstract class, and so when we
    // subclass it we can get some of the parameterized type
    // information at runtime.  Test it to make sure it's the right
    // stuff.
    final Type genericSuperclass = adapterClass.getGenericSuperclass();
    assertNotNull(genericSuperclass);
    assertTrue(genericSuperclass instanceof ParameterizedType);
    final ParameterizedType adapterSuperType = (ParameterizedType) genericSuperclass;
    assertSame(UniversalXmlAdapter.class, adapterSuperType.getRawType());
    final Type[] actualTypeArguments = adapterSuperType.getActualTypeArguments();
    assertNotNull(actualTypeArguments);
    assertEquals(2, actualTypeArguments.length);
    assertSame(Person.class, actualTypeArguments[0]);
    assertSame(PersonImplementation.class, actualTypeArguments[1]);

    final UniversalXmlAdapter adapter = (UniversalXmlAdapter) adapterClass.newInstance();
    assertNotNull(adapter);
  }
  /*
   * A helper for sanity checking de-/serialization.
   * We test two objects to be structurally equal by means of reflection.
   */
  public static boolean structurallyEqual(Object o1, Object o2) {
    try {
      if (o1.getClass().getName().equals(o2.getClass().getName())) {

        for (Class<?> obj = o1.getClass(); !obj.equals(Object.class); obj = obj.getSuperclass()) {
          Field[] f1 = obj.getDeclaredFields();
          Field[] f2 = obj.getDeclaredFields();

          for (int i = 0; i < f1.length; i++) {
            f1[i].setAccessible(true);
            f2[i].setAccessible(true);
            // check if fields are primitive types and compare
            if ((f1[i].getType().isPrimitive() && f2[i].getType().isPrimitive())) {
              if (!(f1[i].getName().equals(f2[i].getName()))) return false;
              else {
                if (!(f1[i].get(o1).equals(f2[i].get(o2)))) {
                  return false;
                }
              }
              // otherwise, they must be objects
            } else {
              // to be equal, both can not be null
              if (f1[i].get(o1) != null && f2[i].get(o2) != null) {
                // check, if they are of the same class
                if (f1[i].get(o1).getClass().getName().equals(f2[i].get(o2).getClass().getName())) {
                  // check if the class is Double,
                  // Integer or String
                  if (check(f1[i].get(o1).getClass().getName())) {
                    // compare values
                    if (!(f1[i].get(o1).equals(f2[i].get(o2)))) {
                      return false;
                    }
                  } else {
                    // special case, if the object is an
                    // linked list
                    if (f1[i].get(o1).getClass().getName().equals("java.util.LinkedList")) {
                      if (!(handleLinkedList(f1[i], o1, f2[i], o2))) {
                        return false;
                      }
                    } else {
                      // otherwise, compare the objects
                      structurallyEqual(f1[i].get(o1), f2[i].get(o2));
                    }
                  }
                } else {
                  return false;
                }
              } else {
                // if one of them is null, the objects can not
                // be equal
                if (f1[i].get(o1) == null && f2[i].get(o2) != null) {
                  return false;
                }
                if (f1[i].get(o1) != null && f2[i].get(o2) == null) {
                  return false;
                }
              }
            }
          }
        }
      } else {
        return false;
      }
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    return true;
  }