/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.flink.runtime.rest.messages;

import org.junit.Test;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.Arrays;
import java.util.Collection;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/** Test for (un)marshalling of the {@link ThreadDumpInfo}. */
public class ThreadDumpInfoTest extends RestResponseMarshallingTestBase<ThreadDumpInfo> {

    @Override
    protected Class<ThreadDumpInfo> getTestResponseClass() {
        return ThreadDumpInfo.class;
    }

    @Override
    protected ThreadDumpInfo getTestResponseInstance() throws Exception {
        final Collection<ThreadDumpInfo.ThreadInfo> threadInfos =
                Arrays.asList(
                        ThreadDumpInfo.ThreadInfo.create("foobar", "barfoo"),
                        ThreadDumpInfo.ThreadInfo.create("bar", "foo"));

        return ThreadDumpInfo.create(threadInfos);
    }

    @Override
    protected void assertOriginalEqualsToUnmarshalled(
            ThreadDumpInfo expected, ThreadDumpInfo actual) {
        assertThat(
                actual.getThreadInfos(), containsInAnyOrder(expected.getThreadInfos().toArray()));
    }

    @Test
    public void testComparedWithDefaultJDKImplemetation() {
        ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
        ThreadInfo threadInfo =
                threadMxBean.getThreadInfo(Thread.currentThread().getId(), Integer.MAX_VALUE);

        String stringifyThreadInfo = ThreadDumpInfo.stringifyThreadInfo(threadInfo, 8);
        assertEquals(threadInfo.toString(), stringifyThreadInfo);
    }

    @Test
    public void testStacktraceDepthLimitation() {
        ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
        ThreadInfo threadInfo =
                threadMxBean.getThreadInfo(Thread.currentThread().getId(), Integer.MAX_VALUE);

        int expectedStacktraceDepth = threadInfo.getStackTrace().length;

        String stringifiedInfo = ThreadDumpInfo.stringifyThreadInfo(threadInfo, Integer.MAX_VALUE);
        assertEquals(expectedStacktraceDepth, getOutputDepth(stringifiedInfo));

        String stringifiedInfoExceedMaxDepth =
                ThreadDumpInfo.stringifyThreadInfo(threadInfo, expectedStacktraceDepth - 1);
        assertEquals(expectedStacktraceDepth - 1, getOutputDepth(stringifiedInfoExceedMaxDepth));
        assertTrue(stringifiedInfoExceedMaxDepth.contains("\t..."));
    }

    private long getOutputDepth(String stringifiedInfo) {
        return Arrays.stream(stringifiedInfo.split("\n")).filter(x -> x.contains("\tat ")).count();
    }
}

@@ -1112,16 +1112,66 @@
 yOrder;%0A
+import static org.junit.Assert.assertArrayEquals;%0A
 import s
@@ -2502,151 +2502,697 @@
     
-String stringifyThreadInfo = ThreadDumpInfo.stringifyThreadInfo(threadInfo, 8);%0A        assertEquals(threadInfo.toString(), stringifyThreadInfo
+// JDK11 has increased the output info of threadInfo.daemon and threadInfo.priority compared%0A        // to JDK8, hence only compare the output of stacktrace content for compatibility.%0A        String%5B%5D threadInfoLines = threadInfo.toString().split(%22%5Cn%22);%0A        String%5B%5D expected = Arrays.copyOfRange(threadInfoLines, 1, threadInfoLines.length);%0A%0A        String stringifyThreadInfo = ThreadDumpInfo.stringifyThreadInfo(threadInfo, 8);%0A        String%5B%5D stringifyThreadInfoLines = stringifyThreadInfo.split(%22%5Cn%22);%0A        String%5B%5D stringified =%0A                Arrays.copyOfRange(stringifyThreadInfoLines, 1, stringifyThreadInfoLines.length);%0A%0A        assertArrayEquals(expected, stringified
 );%0A 

Fix the compatibility issue of jdk8 and 11 in ThreadDumpInfoTest.testComparedWithDefaultJDKImplemetation