/**
 * Copyright 2018 Alexey Ragozin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gridkit.jvmtool;

import java.lang.management.ManagementFactory;

import org.junit.Assert;
import org.junit.Test;

public class JStackDumpParserTest {

    private static String PID;
    static {
        PID = ManagementFactory.getRuntimeMXBean().getName();
        PID = PID.substring(0, PID.indexOf('@'));
    }

    @Test
    public void ssa_jstack_dump_histo() {
        exec("ssa", "--histo", "-f", "src/test/resources/jstack.txt", "-X");
    }

    private void exec(String... cmd) {
        SJK sjk = new SJK();
        sjk.suppressSystemExit();
        StringBuilder sb = new StringBuilder();
        sb.append("SJK");
        for(String c: cmd) {
            sb.append(' ').append(escape(c));
        }
        System.out.println(sb);
        Assert.assertTrue(sjk.start(cmd));
    }

    @SuppressWarnings("unused")
    private void fail(String... cmd) {
        SJK sjk = new SJK();
        sjk.suppressSystemExit();
        StringBuilder sb = new StringBuilder();
        sb.append("SJK");
        for(String c: cmd) {
            sb.append(' ').append(escape(c));
        }
        System.out.println(sb);
        Assert.assertFalse(sjk.start(cmd));
    }

    private Object escape(String c) {
        if (c.split("\\s").length > 1) {
            return '\"' + c + '\"';
        }
        else {
            return c;
        }
    }
}
