All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sealights.agents.plugin.SurefireManager Maven / Gradle / Ivy

There is a newer version: 4.0.1125
Show newest version
package io.sealights.agents.plugin;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import io.sealights.onpremise.agents.java.agent.infra.constants.SLProperties;
import io.sealights.onpremise.agents.java.agent.infra.utils.StringUtils;

/**
 * Created by shahar on 4/27/2016.
 */
public class SurefireManager {

    public static final String SL_TEST_LISTENER = "sl-test-listener";
    public static final String JAVA_TEST_LISTENER = "java-test-listener";
    public static final String DEV_SL_TEST_LISTENER = "java-agent-bootstrapper-1.0.0-SNAPSHOT";
    
    public String preventArgLineDuplication(String oldArgLine) {
        List args = toArgsArray(oldArgLine);
        return deleteSeaLightsLeftovers(args);
    }

    private String deleteSeaLightsLeftovers(List args) {
        for (Iterator iterator = args.iterator(); iterator.hasNext();) {
            String argument = iterator.next();
            if (isSeaLightsProperty(argument) || isSeaLightsTestListener(argument)) {
                iterator.remove();
            }
        }
        String ret = StringUtils.join(args, ' ');
        return ret;
    }

    static int IGNORE_WHITE_SPACES = 0;
    static int IN_QUOTES = 1;
    static int AFTER_QUOTES = 2;
    static int IN_SEQUENCE = 3;
    static int ESCAPED = 4;

    private static List toArgsArray(String argsAsString) {
        List args = new ArrayList<>();
        int state = 0;
        StringBuilder currentArg = new StringBuilder();

        for (char c : argsAsString.toCharArray()) {
            if (state == IGNORE_WHITE_SPACES) {
                if (!Character.isWhitespace(c)) {
                    currentArg.append(c);
                    if (c == '"') {
                        state = IN_QUOTES;
                    } else {
                        state = IN_SEQUENCE;
                    }
                }
            }

            else if (state == IN_QUOTES) {
                currentArg.append(c);
                if (c == '"') {
                    state = AFTER_QUOTES;
//                }else if (c == '\\'){
//                    state = ESCAPED;
                }
            }

//            else if (state == ESCAPED){
//                if (c == '"' || c == '\\') {
//                    currentArg.setCharAt(currentArg.length() - 1, c);
//                }else {
//                    currentArg.append(c);
//                }
//                state = IN_QUOTES;
//            }

            else if (state == AFTER_QUOTES) {
                if (!Character.isWhitespace(c)) {
                    currentArg.append(c);
                    if (c == '"') {
                        state = IN_QUOTES;
                    } else {
                        state = IN_SEQUENCE;
                    }
                } else {
                    args.add(currentArg.toString());
                    currentArg.setLength(0);
                    state = IGNORE_WHITE_SPACES;
                }
            }

            else if (state == IN_SEQUENCE){
                if (!Character.isWhitespace(c)) {
                    currentArg.append(c);
                    if (c == '"') {
                        state = IN_QUOTES;
                    }
                } else {
                    args.add(currentArg.toString());
                    currentArg.setLength(0);
                    state = IGNORE_WHITE_SPACES;
                }
            }
        }
        args.add(currentArg.toString());
        return args;
    }

    private boolean isSeaLightsProperty(String argument){
        return argument.startsWith("-D" + SLProperties.PREFIX);
    }

    private boolean isSeaLightsTestListener(String argument){
        return (argument.startsWith("-javaagent:") && ( isValidTestListenerName(argument)));
    }
    
    private static boolean isValidTestListenerName(String testListenerName) {
        return testListenerName != null && (
                testListenerName.contains(SL_TEST_LISTENER) ||
                        testListenerName.contains(JAVA_TEST_LISTENER) ||
                        testListenerName.contains(DEV_SL_TEST_LISTENER)
        );
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy