Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gzsombor committed Jan 1, 2025
1 parent 1306600 commit dcd993d
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*******************************************************************************
* Copyright (c) 2025 Zsombor Gegesy.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Zsombor Gegesy - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.debug.tests.ui;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import org.eclipse.jdt.debug.core.IJavaStackFrame;
import org.eclipse.jdt.internal.debug.core.model.GroupedStackFrame;
import org.junit.Before;
import org.junit.Test;

public class GroupedStackFrameTest {

private GroupedStackFrame groupedStackFrame;

private IJavaStackFrame mockFrame1;
private IJavaStackFrame mockFrame2;

@Before
public void setUp() throws Exception {
groupedStackFrame = new GroupedStackFrame(null);
mockFrame1 = JavaStackFrameMock.createFrame(JavaReferenceTypeMock.createReference("java.util.ArrayList"), false);
mockFrame2 = JavaStackFrameMock.createFrame(JavaReferenceTypeMock.createReference("java.util.LinkedList"), false);
}

@Test
public void testAddFrame() {
groupedStackFrame.add(mockFrame1);
assertEquals(1, groupedStackFrame.getFrameCount());

groupedStackFrame.add(mockFrame2);
assertEquals(2, groupedStackFrame.getFrameCount());
}

@Test
public void testGetFrameCount() {
assertEquals(0, groupedStackFrame.getFrameCount());

groupedStackFrame.add(mockFrame1);
assertEquals(1, groupedStackFrame.getFrameCount());

groupedStackFrame.add(mockFrame2);
assertEquals(2, groupedStackFrame.getFrameCount());
}

@Test
public void testGetFramesAsArray() {
groupedStackFrame.add(mockFrame1);
groupedStackFrame.add(mockFrame2);

Object[] frames = groupedStackFrame.getFramesAsArray(0, 2);
assertNotNull(frames);
assertEquals(2, frames.length);
assertSame(mockFrame1, frames[0]);
assertSame(mockFrame2, frames[1]);

frames = groupedStackFrame.getFramesAsArray(1, 1);
assertNotNull(frames);
assertEquals(1, frames.length);
assertSame(mockFrame2, frames[0]);

frames = groupedStackFrame.getFramesAsArray(2, 1);
assertNull(frames);
}

@Test
public void testGetTopMostFrame() {
assertNull(groupedStackFrame.getTopMostFrame());

groupedStackFrame.add(mockFrame1);
assertSame(mockFrame1, groupedStackFrame.getTopMostFrame());

groupedStackFrame.add(mockFrame2);
assertSame(mockFrame1, groupedStackFrame.getTopMostFrame());
}

@Test
public void testGetAdapter() {
var adapterType = String.class;

groupedStackFrame.add(mockFrame1);
Object adapter = groupedStackFrame.getAdapter(adapterType);
assertEquals("getAdapter called with class java.lang.String", adapter);

groupedStackFrame = new GroupedStackFrame(null);
adapter = groupedStackFrame.getAdapter(adapterType);
assertNull(adapter);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*******************************************************************************
* Copyright (c) 2025 Zsombor Gegesy.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Zsombor Gegesy - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.debug.tests.ui;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.eclipse.jdt.debug.core.IJavaReferenceType;

/**
* Class to mock {@link IJavaReferenceType}.
*/
class JavaReferenceTypeMock implements InvocationHandler {

final String name;

JavaReferenceTypeMock(String name) {
this.name = name;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("getName".equals(method.getName())) {
return name;
}
return null;
}

/**
* Create a new mocked {@link IJavaReferenceType}.
*
* @param name
* @return
*/
public static IJavaReferenceType createReference(String name) {
return (IJavaReferenceType) Proxy.newProxyInstance(JavaReferenceTypeMock.class.getClassLoader(), new Class[] {
IJavaReferenceType.class }, new JavaReferenceTypeMock(name));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* Copyright (c) 2025 Zsombor Gegesy.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Zsombor Gegesy - initial API and implementation
*******************************************************************************/

package org.eclipse.jdt.debug.tests.ui;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.eclipse.jdt.debug.core.IJavaReferenceType;
import org.eclipse.jdt.debug.core.IJavaStackFrame;

class JavaStackFrameMock implements InvocationHandler {

final IJavaReferenceType referenceType;
final boolean synthetic;

JavaStackFrameMock(IJavaReferenceType referenceType, boolean synthetic) {
this.referenceType = referenceType;
this.synthetic = synthetic;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
var methodName = method.getName();
if ("getReferenceType".equals(methodName)) {
return referenceType;
}
if ("isSynthetic".equals(methodName)) {
return synthetic;
}
if ("getLaunch".equals(methodName)) {
return LaunchMock.createLaunch();
}
if ("getAdapter".equals(methodName)) {
return "getAdapter called with " + args[0];
}
return null;
}

/**
* Create a mocked {@link IJavaStackFrame}
*
* @param refType
* the type in which this stack frame's method is declared
* @param syntetic
* if the frame is synthetic or not.
* @return a mocked stack frame.
*/
public static IJavaStackFrame createFrame(IJavaReferenceType refType, boolean syntetic) {
return (IJavaStackFrame) Proxy.newProxyInstance(JavaStackFrameMock.class.getClassLoader(), new Class[] {
IJavaStackFrame.class }, new JavaStackFrameMock(refType, syntetic));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2025 Zsombor Gegesy.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Zsombor Gegesy - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.debug.tests.ui;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.eclipse.debug.core.ILaunch;

/**
* Class to mock {@link ILaunch}.
*/
class LaunchMock implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return null;
}

public static ILaunch createLaunch() {
return (ILaunch) Proxy.newProxyInstance(LaunchMock.class.getClassLoader(), new Class[] { ILaunch.class }, new LaunchMock());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
*******************************************************************************/
package org.eclipse.jdt.debug.tests.ui;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.jdt.debug.core.IJavaReferenceType;
import org.eclipse.jdt.debug.core.IJavaStackFrame;
import org.eclipse.jdt.debug.tests.AbstractDebugTest;
Expand All @@ -36,57 +31,6 @@ public StackFramePresentationProviderTest(String name) {
private StackFramePresentationProvider provider;
private IPreferenceStore preferenceStore;

private static class LaunchMock implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return null;
}
}
private static class JavaStackFrameMock implements InvocationHandler {

final IJavaReferenceType referenceType;
final boolean synthetic;

public JavaStackFrameMock(IJavaReferenceType referenceType, boolean synthetic) {
this.referenceType = referenceType;
this.synthetic = synthetic;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
var methodName = method.getName();
if ("getReferenceType".equals(methodName)) {
return referenceType;
}
if ("isSynthetic".equals(methodName)) {
return synthetic;
}
if ("getLaunch".equals(methodName)) {
return Proxy.newProxyInstance(StackFramePresentationProviderTest.class.getClassLoader(), new Class[] {
ILaunch.class }, new LaunchMock());

}
return null;
}
}

private static class JavaReferenceTypeMock implements InvocationHandler {

final String name;

public JavaReferenceTypeMock(String name) {
this.name = name;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("getName".equals(method.getName())) {
return name;
}
return null;
}
}

@Override
protected void setUp() throws Exception {
super.setUp();
Expand All @@ -105,22 +49,12 @@ protected void tearDown() throws Exception {
provider.close();
}

private IJavaReferenceType createReference(String name) {
return (IJavaReferenceType) Proxy.newProxyInstance(StackFramePresentationProviderTest.class.getClassLoader(), new Class[] {
IJavaReferenceType.class }, new JavaReferenceTypeMock(name));
}

private IJavaStackFrame createFrame(IJavaReferenceType refType, boolean syntetic) {
return (IJavaStackFrame) Proxy.newProxyInstance(StackFramePresentationProviderTest.class.getClassLoader(), new Class[] {
IJavaStackFrame.class }, new JavaStackFrameMock(refType, syntetic));
}

private IJavaStackFrame.Category categorize(String refTypeName, boolean syntetic) throws DebugException {
return categorize(createReference(refTypeName), syntetic);
return categorize(JavaReferenceTypeMock.createReference(refTypeName), syntetic);
}

private IJavaStackFrame.Category categorize(IJavaReferenceType refType, boolean syntetic) throws DebugException {
return provider.categorize(createFrame(refType, syntetic));
return provider.categorize(JavaStackFrameMock.createFrame(refType, syntetic));
}

public void testFiltering() throws DebugException {
Expand All @@ -130,8 +64,8 @@ public void testFiltering() throws DebugException {
}

public void testUpdateWorks() throws DebugException {
var something = createReference("org.eclipse.Something");
var other = createReference("org.eclipse.Other");
var something = JavaReferenceTypeMock.createReference("org.eclipse.Something");
var other = JavaReferenceTypeMock.createReference("org.eclipse.Other");
assertEquals(IJavaStackFrame.Category.UNKNOWN, categorize(something, false));
assertEquals(IJavaStackFrame.Category.UNKNOWN, categorize(other, false));
preferenceStore.setValue(IJDIPreferencesConstants.PREF_ACTIVE_CUSTOM_FRAME_FILTER_LIST, "org.eclipse.Something");
Expand Down

0 comments on commit dcd993d

Please sign in to comment.