@Test
  public void testArgumentEscaping() throws Exception {
    final String ALERT_DEFINITION_NAME = "mock_alert_with_quotes";
    final String ALERT_DEFINITION_LABEL = "Mock alert with Quotes";
    final String ALERT_LABEL = "Alert Label";
    final String ALERT_SERVICE_NAME = "FOO_SERVICE";
    final String ALERT_TEXT = "Did you know, \"Quotes are hard!!!\"";
    final String ALERT_TEXT_ESCAPED = "Did you know, \\\"Quotes are hard\\!\\!\\!\\\"";
    final String ALERT_HOST = "mock_host";
    final long ALERT_TIMESTAMP = 1111111l;

    File validScript = tempFolder.newFile("valid_script.sh");
    validScript.setExecutable(true);
    String scriptAbsolutePath = validScript.getAbsolutePath();

    DispatchCallback callback = EasyMock.createNiceMock(DispatchCallback.class);
    AlertNotification notification = new AlertNotification();
    notification.Callback = callback;
    notification.CallbackIds = Collections.singletonList(UUID.randomUUID().toString());

    AlertDefinitionEntity definition = new AlertDefinitionEntity();
    definition.setDefinitionName(ALERT_DEFINITION_NAME);
    definition.setLabel(ALERT_DEFINITION_LABEL);

    AlertHistoryEntity history = new AlertHistoryEntity();
    history.setAlertDefinition(definition);
    history.setAlertLabel(ALERT_LABEL);
    history.setAlertText(ALERT_TEXT);
    history.setAlertState(AlertState.OK);
    history.setServiceName(ALERT_SERVICE_NAME);
    history.setHostName(ALERT_HOST);
    history.setAlertTimestamp(ALERT_TIMESTAMP);


    AlertInfo alertInfo = new AlertInfo(history);
    notification.setAlertInfo(alertInfo);

    AlertScriptDispatcher dispatcher = new AlertScriptDispatcher();
    m_injector.injectMembers(dispatcher);

    ProcessBuilder processBuilder = dispatcher.getProcessBuilder(scriptAbsolutePath, notification);
    List<String> commands = processBuilder.command();
    Assert.assertEquals(3, commands.size());
    Assert.assertEquals("sh", commands.get(0));
    Assert.assertEquals("-c", commands.get(1));

    StringBuilder buffer = new StringBuilder();
    buffer.append(scriptAbsolutePath).append(" ");
    buffer.append(ALERT_DEFINITION_NAME).append(" ");
    buffer.append("\"").append(ALERT_DEFINITION_LABEL).append("\"").append(" ");
    buffer.append(ALERT_SERVICE_NAME).append(" ");
    buffer.append(AlertState.OK).append(" ");
    buffer.append("\"").append(ALERT_TEXT_ESCAPED).append("\"").append(" ");
    buffer.append(ALERT_TIMESTAMP).append(" ");
    buffer.append(ALERT_HOST);

    Assert.assertEquals(buffer.toString(), commands.get(2));

    //if hostname is null
    history.setHostName(null);
    alertInfo = new AlertInfo(history);
    notification.setAlertInfo(alertInfo);

    processBuilder = dispatcher.getProcessBuilder(scriptAbsolutePath, notification);
    commands = processBuilder.command();
    buffer = new StringBuilder();
    buffer.append(scriptAbsolutePath).append(" ");
    buffer.append(ALERT_DEFINITION_NAME).append(" ");
    buffer.append("\"").append(ALERT_DEFINITION_LABEL).append("\"").append(" ");
    buffer.append(ALERT_SERVICE_NAME).append(" ");
    buffer.append(AlertState.OK).append(" ");
    buffer.append("\"").append(ALERT_TEXT_ESCAPED).append("\"").append(" ");
    buffer.append(ALERT_TIMESTAMP).append(" ");
    buffer.append("");
    Assert.assertEquals(buffer.toString(), commands.get(2));

  }
Tests that arguments given to the {@link ProcessBuilder} are properly
escaped.

@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcherTest.java