/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2013 Felix Schulze
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package de.felixschulze.teamcity;

/**
 * Helper for TeamCity Service Messages
 *
 * @author <a href="mail@felixschulze.de">Felix Schulze</a>
 */
public class TeamCityStatusMessageHelper {

    public static String escapeString(String string) {
        String tmp = string.replace("|", "||");
        tmp = tmp.replace("'", "|'");
        tmp = tmp.replace("\"", "|'");
        tmp = tmp.replaceAll("\\n", "|n");
        tmp = tmp.replaceAll("\\r", "|r");
        tmp = tmp.replace("[", "|[");
        tmp = tmp.replace("]", "|]");
        return tmp;
    }

    public static String buildNumberString(String version) {
        return "##teamcity[buildNumber '" + TeamCityStatusMessageHelper.escapeString(version) + "']";
    }

    public static String buildStatusString(TeamCityStatusType type, String text) {
        return "##teamcity[buildStatus status='" + type.toString() + "' text='" + TeamCityStatusMessageHelper.escapeString(text) + "']";
    }

    /**
     * @deprecated  As of release 1.0, replaced by {@link #buildStatusString(TeamCityStatusType type, String text)}
     */
    @Deprecated public static String buildStatusFailureString(TeamCityStatusType type, String text) {
        return TeamCityStatusMessageHelper.buildStatusString(type, text);
    }

    public static String buildMessageString(TeamCityStatusType type, String text, String errorDetails) {
        if (type == TeamCityStatusType.ERROR) {
            return "##teamcity[message text='" + TeamCityStatusMessageHelper.escapeString(text) + "' errorDetails='" + TeamCityStatusMessageHelper.escapeString(errorDetails) + "' status='" + type.toString() + "']";
        }
        return "##teamcity[message status='" + type.toString() + "' text='" + TeamCityStatusMessageHelper.escapeString(text) + "']";
    }

    public static String buildProgressString(TeamCityProgressType type, String text) {
        if (type == TeamCityProgressType.START) {
            return "##teamcity[progressStart '" + TeamCityStatusMessageHelper.escapeString(text) + "']";
        } else if (type == TeamCityProgressType.FINISH) {
            return "##teamcity[progressFinish '" + TeamCityStatusMessageHelper.escapeString(text) + "']";
        } else if (type == TeamCityProgressType.MESSAGE) {
            return "##teamcity[progressMessage '" + TeamCityStatusMessageHelper.escapeString(text) + "']";
        }
        return "";
    }

    public static String importDataString(TeamCityImportDataType type, String path) {
        return "##teamcity[importData type='" + type.toString() + "' path='" + path + "']";
    }

}

@@ -3720,10 +3720,1304 @@
 %0A    %7D%0A%0A
+    public static String testSuiteStarted(String name) %7B%0A        return %22##teamcity%5BtestSuiteStarted name='%22 + TeamCityStatusMessageHelper.escapeString(name) + %22'%5D%22;%0A    %7D%0A%0A    public static String testSuiteFinished(String name) %7B%0A        return %22##teamcity%5BtestSuiteFinished name='%22 + TeamCityStatusMessageHelper.escapeString(name) + %22'%5D%22;%0A    %7D%0A%0A    public static String testStarted(String name) %7B%0A        return %22##teamcity%5BtestStarted name='%22 + TeamCityStatusMessageHelper.escapeString(name) + %22'%5D%22;%0A    %7D%0A%0A    public static String testFailed(String name, String message, String details) %7B%0A        return %22##teamcity%5BtestFailed name='%22 + TeamCityStatusMessageHelper.escapeString(name) + %22' message='%22 + TeamCityStatusMessageHelper.escapeString(message) + %22' details='%22 + TeamCityStatusMessageHelper.escapeString(details) + %22'%5D%22;%0A    %7D%0A%0A    public static String testIgnored(String name, String message) %7B%0A        return %22##teamcity%5BtestIgnored name='%22 + TeamCityStatusMessageHelper.escapeString(name) + %22' message='%22 + TeamCityStatusMessageHelper.escapeString(message) + %22'%5D%22;%0A    %7D%0A%0A    public static String testFinished(String name, long duration) %7B%0A        return %22##teamcity%5BtestFinished name='%22 + TeamCityStatusMessageHelper.escapeString(name) + %22' duration='%22 + duration + %22'%5D%22;%0A    %7D%0A
 %7D%0A

Add test methods