Skip to content

Commit

Permalink
fix(tests): For Java 21
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed Sep 13, 2024
1 parent 264a149 commit f7af2c8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
*/
package com.github.olivergondza.dumpling.cli;

import static com.github.olivergondza.dumpling.DumplingMatchers.frameOf;
import static com.github.olivergondza.dumpling.model.ProcessThread.nameIs;
import static org.hamcrest.Matchers.containsInRelativeOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -75,23 +77,12 @@ private void assertThreadState(ProcessRuntime<?, ?, ?> runtime) {

assertThat(actual.getName(), equalTo("remotely-observed-thread"));
assertThat(actual.getStatus(), equalTo(ThreadStatus.IN_OBJECT_WAIT));
// TODO other attributes

// Test class and method name only as MXBean way offer filename too while thread dump way does not
final StackTraceElement innerFrame = trace.getElement(0);
assertThat(innerFrame.getClassName(), equalTo("java.lang.Object"));
assertThat(innerFrame.getMethodName(), equalTo("wait"));

// Do not assert line number as it changes between JDK versions
final StackTraceElement waitElement = trace.getElement(1);
assertThat(waitElement.getClassName(), equalTo("java.lang.Object"));
assertThat(waitElement.getMethodName(), equalTo("wait"));
assertThat(waitElement.getFileName(), equalTo("Object.java"));

final StackTraceElement testFrame = trace.getElement(2);
assertThat(testFrame.getClassName(), equalTo("com.github.olivergondza.dumpling.TestThread$1"));
assertThat(testFrame.getMethodName(), equalTo("run"));
assertThat(testFrame.getFileName(), equalTo("TestThread.java"));

// Cannot do an exact match as different JDK versions have a slightly different #wait() call hierarchy
assertThat(trace.getElements(), containsInRelativeOrder(
frameOf("java.lang.Object", "wait", "Object.java"),
frameOf("com.github.olivergondza.dumpling.TestThread$1", "run", "TestThread.java")
));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.github.olivergondza.dumpling.Util.pause;
import static com.github.olivergondza.dumpling.Util.processTerminatedPrematurely;
import static com.github.olivergondza.dumpling.model.ProcessThread.nameIs;
import static org.hamcrest.Matchers.containsInRelativeOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.nullValue;
Expand All @@ -40,6 +41,7 @@

import javax.annotation.Nonnull;

import com.github.olivergondza.dumpling.DumplingMatchers;
import com.github.olivergondza.dumpling.model.ModelObject;
import org.hamcrest.Matchers;
import org.junit.Rule;
Expand All @@ -49,7 +51,6 @@
import org.junit.runners.model.Statement;

import com.github.olivergondza.dumpling.Util;
import com.github.olivergondza.dumpling.model.StackTrace;
import com.github.olivergondza.dumpling.model.ThreadLock;
import com.github.olivergondza.dumpling.model.ThreadStatus;
import com.github.olivergondza.dumpling.model.dump.ThreadDumpRuntime;
Expand Down Expand Up @@ -103,8 +104,11 @@ public void sleeping() {
assertThat(main.getAcquiredLocks(), Matchers.<ThreadLock>empty());
assertThat(main.getWaitingToLock(), nullValue());
assertThat(
main.getStackTrace().getElement(0),
equalTo(StackTrace.nativeElement("java.lang.Thread", "sleep"))
main.getStackTrace().toString(),
main.getStackTrace().getElements(),
containsInRelativeOrder(DumplingMatchers.frameOf(
"java.lang.Thread", "sleep", null
))
);
}

Expand Down Expand Up @@ -152,8 +156,11 @@ public void reacquireMonitorAfterWait() {
assertThat(only(main.getAcquiredLocks()), equalTo(reacquiring.getWaitingToLock()));
assertThat(reacquiring.getAcquiredLocks().size(), equalTo(2));
assertThat(
reacquiring.getStackTrace().getElement(0),
equalTo(StackTrace.nativeElement("java.lang.Object", "wait"))
reacquiring.getStackTrace().toString(),
reacquiring.getStackTrace().getElements(),
containsInRelativeOrder(DumplingMatchers.frameOf(
"java.lang.Object", "wait", "Object.java"
))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Thread.sleep(100);

synchronized (a) {
a.notify();

// Block forever
Thread.sleep(60000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.github.olivergondza.dumpling.factory;

import static com.github.olivergondza.dumpling.DumplingMatchers.*;
import static com.github.olivergondza.dumpling.TestThread.JMX_HOST;
import static com.github.olivergondza.dumpling.TestThread.JMX_PASSWD;
import static com.github.olivergondza.dumpling.TestThread.JMX_USER;
Expand All @@ -33,8 +34,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.fail;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import com.github.olivergondza.dumpling.DumplingMatchers;
import org.junit.Rule;
import org.junit.Test;

Expand All @@ -46,8 +46,6 @@
import com.github.olivergondza.dumpling.model.ThreadStatus;
import com.github.olivergondza.dumpling.model.jmx.JmxRuntime;

import java.util.Objects;

public class JmxRuntimeFactoryTest {

@Rule public DisposeRule disposer = new DisposeRule();
Expand Down Expand Up @@ -112,25 +110,6 @@ private void assertThreadState(ProcessRuntime<?, ?, ?> runtime) {
));
}

private static TypeSafeMatcher<StackTraceElement> frameOf(String cls, String method, String source) {
return new TypeSafeMatcher<StackTraceElement>() {
@Override
protected boolean matchesSafely(StackTraceElement item) {
if (!Objects.equals(cls, item.getClassName())) return false;
if (!Objects.equals(method, item.getMethodName())) return false;
if (source != null && !Objects.equals(source, item.getFileName())) return false;

return true;
}

@Override
public void describeTo(Description description) {
StackTraceElement expected = new StackTraceElement(cls, method, source, -1);
description.appendText("Stack frame of ").appendValue(expected);
}
};
}

private TestThread.JMXProcess runRemoteSut() throws Exception {
return runRemoteSut(false);
}
Expand Down
5 changes: 5 additions & 0 deletions test-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>3.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.olivergondza.dumpling;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

import java.util.Objects;

public class DumplingMatchers {

public static TypeSafeMatcher<StackTraceElement> frameOf(String cls, String method, String source) {
return new TypeSafeMatcher<StackTraceElement>() {
@Override
protected boolean matchesSafely(StackTraceElement item) {
if (!Objects.equals(cls, item.getClassName())) return false;
if (!Objects.equals(method, item.getMethodName())) return false;
if (source != null && !Objects.equals(source, item.getFileName())) return false;

return true;
}

@Override
public void describeTo(Description description) {
StackTraceElement expected = new StackTraceElement(cls, method, source, -1);
description.appendText("Stack frame of ").appendValue(expected);
}
};
}
}

0 comments on commit f7af2c8

Please sign in to comment.