예제 #1
0
  @SuppressWarnings({"unchecked", "unchecked"})
  public void test() throws Exception {
    // request
    setRequestContent("json-1.txt");
    this.request.addHeader("content-type", "application/json");

    // interceptor
    JSONInterceptor interceptor = new JSONInterceptor();
    TestAction action = new TestAction();

    this.invocation.setAction(action);
    this.invocation.getStack().push(action);

    interceptor.intercept(this.invocation);

    // serialize and compare
    List list = action.getList();

    assertNotNull(list);
    assertEquals(list.size(), 10);

    list = action.getCollection();
    assertNotNull(list);
    assertEquals(list.size(), 3);
    assertEquals(list.get(0), "b");
    assertEquals(list.get(1), 1L);
    list = (List) list.get(2);
    assertNotNull(list);
    assertEquals(list.size(), 2);
    assertEquals(list.get(0), 10L);
    assertEquals(list.get(1), 12L);

    list = action.getCollection2();
    assertNotNull(list);
    assertEquals(list.size(), 1);

    // inside a map any primitive is either: String, Long, Boolean or Double
    Map bean = (Map) list.get(0);

    assertNotNull(bean);
    assertTrue((Boolean) bean.get("booleanField"));
    assertEquals(bean.get("charField"), "s");
    assertEquals(bean.get("doubleField"), 10.1);
    assertEquals(bean.get("floatField"), 1.5);
    assertEquals(bean.get("intField"), 10L);
    assertEquals(bean.get("longField"), 100L);
    assertEquals(bean.get("stringField"), "str");

    bean = (Map) bean.get("objectField");
    assertNotNull(bean);
    assertFalse((Boolean) bean.get("booleanField"));
    assertEquals(bean.get("charField"), "\u0000");
    assertEquals(bean.get("doubleField"), 2.2);
    assertEquals(bean.get("floatField"), 1.1);
    assertEquals(bean.get("intField"), 0L);
    assertEquals(bean.get("longField"), 0L);
    assertEquals(bean.get("stringField"), "  ");

    assertEquals(action.getFoo(), "foo");

    Map map = action.getMap();

    assertNotNull(map);
    assertEquals(map.size(), 2);
    assertEquals(map.get("a"), 1L);
    list = (List) map.get("c");
    assertNotNull(list);
    assertEquals(list.size(), 2);
    assertEquals(list.get(0), 1.0);
    assertEquals(list.get(1), 2.0);

    assertEquals(action.getResult(), null);

    Bean bean2 = action.getBean();

    assertNotNull(bean2);
    assertTrue(bean2.isBooleanField());
    assertEquals(bean2.getStringField(), "test");
    assertEquals(bean2.getIntField(), 10);
    assertEquals(bean2.getCharField(), 's');
    assertEquals(bean2.getDoubleField(), 10.1);
    assertEquals(bean2.getByteField(), 3);

    String[] strArray = action.getArray();

    assertNotNull(strArray);
    assertEquals(strArray.length, 2);
    assertEquals(strArray[0], "str0");
    assertEquals(strArray[1], "str1");

    int[] intArray = action.getIntArray();

    assertNotNull(intArray);
    assertEquals(intArray.length, 2);
    assertEquals(intArray[0], 1);
    assertEquals(intArray[1], 2);

    Bean[] beanArray = action.getBeanArray();

    assertNotNull(beanArray);
    assertNotNull(beanArray[0]);
    assertEquals(beanArray[0].getStringField(), "bean1");
    assertNotNull(beanArray[1]);
    assertEquals(beanArray[1].getStringField(), "bean2");

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(action.getDate());

    assertEquals(calendar.get(Calendar.YEAR), 1999);
    assertEquals(calendar.get(Calendar.MONTH), Calendar.DECEMBER);
    assertEquals(calendar.get(Calendar.DAY_OF_MONTH), 31);
    assertEquals(calendar.get(Calendar.HOUR), 11);
    assertEquals(calendar.get(Calendar.MINUTE), 59);
    assertEquals(calendar.get(Calendar.SECOND), 59);

    calendar.setTime(action.getDate2());
    assertEquals(calendar.get(Calendar.YEAR), 1999);
    assertEquals(calendar.get(Calendar.MONTH), Calendar.DECEMBER);
    assertEquals(calendar.get(Calendar.DAY_OF_MONTH), 31);

    // test desrialize=false
    assertNull(action.getFoo2());
  }