public void testStreamArray() throws IOException, JSONException {
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    ProtocolMessageWriter writer = new JsonMessageWriter(printWriter);
    Display display = new Display();
    Shell shell = new Shell(display);
    writer.appendPayload(
        WidgetUtil.getId(shell),
        IProtocolConstants.PAYLOAD_CONSTRUCT,
        "key",
        new Integer[] {new Integer(1), new Integer(2)});
    writer.appendPayload(
        WidgetUtil.getId(shell), IProtocolConstants.PAYLOAD_CONSTRUCT, "key2", new Boolean(true));

    writer.finish();
    String actual = stringWriter.getBuffer().toString();
    JSONObject message = new JSONObject(actual);
    JSONArray widgetArray = message.getJSONArray(IProtocolConstants.MESSAGE_WIDGETS);
    JSONObject widgetObject = widgetArray.getJSONObject(0);
    JSONObject payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
    JSONArray array = payload.getJSONArray("key");
    assertEquals(1, array.getInt(0));
    assertEquals(2, array.getInt(1));
    assertTrue(payload.getBoolean("key2"));
  }
  public void testStreamWithBlock() throws IOException, JSONException {
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    ProtocolMessageWriter writer = new JsonMessageWriter(printWriter);
    Display display = new Display();
    Shell shell = new Shell(display);
    Button button = new Button(shell, SWT.PUSH);
    writer.appendPayload(
        WidgetUtil.getId(shell), IProtocolConstants.PAYLOAD_CONSTRUCT, "key", "value");
    writer.addConstructPayload(
        WidgetUtil.getId(button),
        WidgetUtil.getId(button.getParent()),
        button.getClass().getName(),
        new String[] {"PUSH"},
        null);
    writer.appendPayload(
        WidgetUtil.getId(button), IProtocolConstants.PAYLOAD_SYNCHRONIZE, "key", "value");
    writer.appendPayload(
        WidgetUtil.getId(button), IProtocolConstants.PAYLOAD_SYNCHRONIZE, "key2", "value");

    writer.finish();
    String actual = stringWriter.getBuffer().toString();
    JSONObject message = new JSONObject(actual);
    JSONArray widgetArray = message.getJSONArray(IProtocolConstants.MESSAGE_WIDGETS);
    JSONObject widgetObject = widgetArray.getJSONObject(1);
    JSONObject payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
    String parentId = payload.getString(IProtocolConstants.KEY_PARENT_ID);
    assertEquals(WidgetUtil.getId(shell), parentId);
    widgetObject = widgetArray.getJSONObject(2);
    payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
    assertEquals("value", payload.getString("key"));
    assertEquals("value", payload.getString("key2"));
  }