コード例 #1
0
ファイル: JsonUtilTest.java プロジェクト: yangwm/JavaLearn
  /**
   * test fill bean
   *
   * @throws JSONException
   */
  @Test
  public void testFillBean() throws JSONException {
    String jsonStr = BaseTest.getJsonStr();
    JSONObject jsonObject = JsonUtil.toJSONObject(jsonStr);
    Assert.assertEquals(55555555555555L, jsonObject.get("message_id"));
    Assert.assertEquals("some msg", jsonObject.get("message_subject_"));
    Assert.assertEquals((byte) 8, jsonObject.get("message_subject_length"));
    // Assert.assertEquals(new JSONObject(new Content(1, "哈哈噢噢,who am i?")),
    // jsonObject.get("messageContent"));
    Assert.assertEquals(true, jsonObject.get("success"));
    Assert.assertEquals((Byte) (byte) 10, jsonObject.get("message_size"));
    Assert.assertEquals((Byte) (byte) 20, jsonObject.get("message_size2"));

    jsonObject = JsonUtil.toJSONObject(jsonObject.toString());
    MessageInfo msg = new MessageInfo();
    JsonUtil.fillBeanFromJSONObject(msg, jsonObject, true);
    Assert.assertEquals(55555555555555L, msg.getMessageId());
    Assert.assertEquals("some msg", msg.getMessageSubject());
    Assert.assertEquals((short) 8, msg.getMessageSubjectLength());
    Assert.assertEquals(new Content(1, "哈哈噢噢,who am i?"), msg.getMessageContent());
    Assert.assertEquals(true, msg.getSuccess());
    Assert.assertEquals((Long) 10L, msg.getMessageSize());
    Assert.assertEquals((Short) (short) 20, msg.getMessageSize2());
    Assert.assertEquals(2, msg.getRecipientList().size());
    Assert.assertEquals(BaseTest.getRecipient(), msg.getRecipientList().get(0));
    Assert.assertEquals(BaseTest.getRecipient2(), msg.getRecipientList().get(1));
  }
コード例 #2
0
ファイル: JsonUtilTest.java プロジェクト: yangwm/JavaLearn
  /**
   * test to json
   *
   * @throws JSONException
   */
  @Test
  public void testToJson() throws JSONException {
    MessageInfo msg = BaseTest.getMessageInfo();
    JSONObject jsonObject = JsonUtil.toJSONObjectFromBean(msg);
    Assert.assertEquals(55555555555555L, jsonObject.get("messageId"));
    Assert.assertEquals("some msg", jsonObject.get("messageSubject"));
    Assert.assertEquals((short) 8, jsonObject.get("messageSubjectLength"));
    // Assert.assertEquals(new JSONObject(new Content(1, "哈哈噢噢,who am i?")),
    // jsonObject.get("messageContent"));
    Assert.assertEquals(true, jsonObject.get("success"));
    Assert.assertEquals((Long) 10L, jsonObject.get("messageSize"));
    Assert.assertEquals((Short) (short) 20, jsonObject.get("messageSize2"));

    jsonObject = JsonUtil.toJSONObject(jsonObject.toString());
    Assert.assertEquals(55555555555555L, jsonObject.get("messageId"));
    Assert.assertEquals("some msg", jsonObject.get("messageSubject"));
    Assert.assertEquals((byte) 8, jsonObject.get("messageSubjectLength"));
    // Assert.assertEquals(new JSONObject(new Content(1, "哈哈噢噢,who am i?")),
    // jsonObject.get("messageContent"));
    Assert.assertEquals(true, jsonObject.get("success"));
    Assert.assertEquals((Byte) (byte) 10, jsonObject.get("messageSize"));
    Assert.assertEquals((Byte) (byte) 20, jsonObject.get("messageSize2"));
  }