Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-2180 - removed JMockit usage from some tests (part 2)

dragonmacherd40cc076cd32
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionDialog.java
index 89a58c18a7..d62fcb5027 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionDialog.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionDialog.java
@@ -65,7 +65,7 @@ public class DataTypeSelectionDialog extends DialogComponentProvider {
private void buildEditor() {
removeWorkPanel();
- editor = new DataTypeSelectionEditor(pluginTool, allowedTypes);
+ editor = createEditor(pluginTool, allowedTypes);
editor.setPreferredDataTypeManager(dtm);
editor.setConsumeEnterKeyPress(false); // we want to handle Enter key presses
editor.addCellEditorListener(new CellEditorListener() {
@@ -108,6 +108,11 @@ public class DataTypeSelectionDialog extends DialogComponentProvider {
rootPanel.validate();
}
+ protected DataTypeSelectionEditor createEditor(PluginTool tool,
+ AllowedDataTypes allowedDataTypes) {
+ return new DataTypeSelectionEditor(tool, allowedDataTypes);
+ }
+
protected JComponent createEditorPanel(DataTypeSelectionEditor dtEditor) {
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(editor.getEditorComponent(), BorderLayout.NORTH);
@@ -168,7 +173,7 @@ public class DataTypeSelectionDialog extends DialogComponentProvider {
/**
* If true then a Tab key press will work the same as pressing the Enter key. If false, then
- * a Tab key press will trigger navigation, as is normally done in Java.
+ * a Tab key press will trigger navigation, as is normally done in Java.
* <p>
* This method is useful for widgets that have embedded editors that launch this dialog. For
* these editors, like tables, it is nice to be able to tab through various editors. This
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionEditor.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionEditor.java
index 5264b21bb0..4b23b9172f 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionEditor.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/datatype/DataTypeSelectionEditor.java
@@ -50,8 +50,8 @@ import ghidra.util.exception.CancelledException;
* via the {@link #addDocumentListener(DocumentListener)} method. The added listener will be
* notified as the user enters text into the editor's text field. Then, to determine when there
* is as valid DataType in the field you may call {@link #validateUserSelection()}.
- *
- *
+ *
+ *
*/
public class DataTypeSelectionEditor extends AbstractCellEditor {
@@ -89,10 +89,10 @@ public class DataTypeSelectionEditor extends AbstractCellEditor {
/**
* Sets the {@link DataTypeManager} to use when the chooser is forced to parse the given
* data type text to resolve the data type. If the users chooses a type, then this value
- * is not used. Note that setting this value does not restrict the parser to just the
+ * is not used. Note that setting this value does not restrict the parser to just the
* given value, but rather the given value is the preferred manager and is thus searched
- * first.
- *
+ * first.
+ *
* @param dataTypeManager the preferred data type manager
*/
public void setPreferredDataTypeManager(DataTypeManager dataTypeManager) {
@@ -102,15 +102,20 @@ public class DataTypeSelectionEditor extends AbstractCellEditor {
/**
* Sets whether this editor should consumer Enter key presses
* @see DropDownSelectionTextField#setConsumeEnterKeyPress(boolean)
- *
+ *
* @param consume true to consume
*/
public void setConsumeEnterKeyPress(boolean consume) {
selectionField.setConsumeEnterKeyPress(consume);
}
+ protected DropDownSelectionTextField<DataType> createDropDownSelectionTextField(
+ DataTypeDropDownSelectionDataModel model) {
+ return new DropDownSelectionTextField<>(model);
+ }
+
private void init() {
- selectionField = new DropDownSelectionTextField<>(
+ selectionField = createDropDownSelectionTextField(
new DataTypeDropDownSelectionDataModel(dataTypeManagerService));
selectionField.addCellEditorListener(new CellEditorListener() {
@Override
@@ -200,9 +205,9 @@ public class DataTypeSelectionEditor extends AbstractCellEditor {
}
/**
- * Sets the initially selected node in the data type tree that the user can choose to
+ * Sets the initially selected node in the data type tree that the user can choose to
* show.
- *
+ *
* @param path The path to set
*/
public void setDefaultSelectedTreePath(TreePath path) {
@@ -222,7 +227,7 @@ public class DataTypeSelectionEditor extends AbstractCellEditor {
/**
* Sets the value to be edited on this cell editor.
- *
+ *
* @param dataType The data type which is to be edited.
*/
public void setCellEditorValue(DataType dataType) {
@@ -237,7 +242,7 @@ public class DataTypeSelectionEditor extends AbstractCellEditor {
/**
* Adds a document listener to the text field editing component of this editor so that users
- * can be notified when the text contents of the editor change. You may verify whether the
+ * can be notified when the text contents of the editor change. You may verify whether the
* text changes represent a valid DataType by calling {@link #validateUserSelection()}.
* @param listener the listener to add.
* @see #validateUserSelection()
@@ -314,7 +319,7 @@ public class DataTypeSelectionEditor extends AbstractCellEditor {
}
private boolean isValidDataType() throws InvalidDataTypeException {
- // look for the case where the user made a selection from the matching window, but
+ // look for the case where the user made a selection from the matching window, but
// then changed the text field text.
DataType selectedDataType = selectionField.getSelectedValue();
if (selectedDataType != null &&
@@ -325,7 +330,7 @@ public class DataTypeSelectionEditor extends AbstractCellEditor {
return false;
}
- // looks at the current text and the current data type and will return a non-null value if
+ // looks at the current text and the current data type and will return a non-null value if
// the current text starts with the name of the data type
private DataType getDataTypeRootForCurrentText() {
DataType dataType = selectionField.getSelectedValue();
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/codebrowser/AbstractCodeBrowserNavigationTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/codebrowser/AbstractCodeBrowserNavigationTest.java
index da6bced67b..5346a52b7c 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/codebrowser/AbstractCodeBrowserNavigationTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/codebrowser/AbstractCodeBrowserNavigationTest.java
@@ -38,7 +38,7 @@ import ghidra.test.*;
public class AbstractCodeBrowserNavigationTest extends AbstractGhidraHeadedIntegrationTest {
private TestEnv env;
- private PluginTool tool;
+ protected PluginTool tool;
private AddressFactory addrFactory;
private DockingActionIf prev;
private DockingActionIf clearHistory;
@@ -199,9 +199,8 @@ public class AbstractCodeBrowserNavigationTest extends AbstractGhidraHeadedInteg
while (i++ < 50) {
TableComponentProvider<?>[] providers = getProviders();
if (providers.length > 0) {
- GThreadedTablePanel<?> panel =
- (GThreadedTablePanel<?>) TestUtils.getInstanceField("threadedPanel",
- providers[0]);
+ GThreadedTablePanel<?> panel = (GThreadedTablePanel<?>) TestUtils
+ .getInstanceField("threadedPanel", providers[0]);
GTable table = panel.getTable();
while (panel.isBusy()) {
Thread.sleep(50);
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/codebrowser/ExternalCodeBrowserNavigationTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/codebrowser/ExternalCodeBrowserNavigationTest.java
index 09438322c2..a5f9cb83fb 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/codebrowser/ExternalCodeBrowserNavigationTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/codebrowser/ExternalCodeBrowserNavigationTest.java
@@ -28,6 +28,8 @@ import ghidra.app.cmd.function.CreateThunkFunctionCmd;
import ghidra.app.nav.Navigatable;
import ghidra.app.plugin.core.gotoquery.GoToHelper;
import ghidra.app.plugin.core.navigation.NavigationOptions;
+import ghidra.app.services.GoToService;
+import ghidra.app.util.navigation.GoToServiceImpl;
import ghidra.framework.main.DataTreeDialog;
import ghidra.framework.main.datatree.DataTree;
import ghidra.framework.main.datatree.ProjectDataTreePanel;
@@ -42,7 +44,6 @@ import ghidra.program.util.ProgramLocation;
import ghidra.util.Msg;
import ghidra.util.table.GhidraTable;
import ghidra.util.task.TaskMonitor;
-import mockit.*;
public class ExternalCodeBrowserNavigationTest extends AbstractCodeBrowserNavigationTest {
@@ -53,8 +54,21 @@ public class ExternalCodeBrowserNavigationTest extends AbstractCodeBrowserNaviga
public void setUp() throws Exception {
super.setUp();
- // this call triggers jMockit to load our spy
- new SpyGoToHelper();
+ GoToHelper spyGoToHelper = new GoToHelper(tool) {
+ @Override
+ public boolean goTo(final Navigatable navigatable, ProgramLocation loc, Program p) {
+
+ Msg.debug(this, "goTo() called with " + loc);
+
+ // Track last navigation location
+ lastNavigationLocation = loc;
+ lastNavigationProgram = p;
+ return super.goTo(navigatable, loc, p);
+ }
+ };
+
+ GoToServiceImpl goToServiceImpl = (GoToServiceImpl) tool.getService(GoToService.class);
+ setInstanceField("helper", goToServiceImpl, spyGoToHelper);
Project project = getTool().getProject();
DomainFolder rootFolder = project.getProjectData().getRootFolder();
@@ -183,8 +197,9 @@ public class ExternalCodeBrowserNavigationTest extends AbstractCodeBrowserNaviga
@Test
public void testOperandExternalProgramNavigation() throws Exception {
- getTool().getOptions("Navigation").setEnum("External Navigation",
- NavigationOptions.ExternalNavigationEnum.NavigateToExternalProgram);
+ getTool().getOptions("Navigation")
+ .setEnum("External Navigation",
+ NavigationOptions.ExternalNavigationEnum.NavigateToExternalProgram);
cb.goTo(new OperandFieldLocation(program, addr("1001020"), null, null, null, 0, 0));
assertEquals(addr("1001020"), cb.getCurrentAddress());
@@ -205,8 +220,9 @@ public class ExternalCodeBrowserNavigationTest extends AbstractCodeBrowserNaviga
@Test
public void testOperandExternalProgramNavigation_OnThunk() throws Exception {
- getTool().getOptions("Navigation").setEnum("External Navigation",
- NavigationOptions.ExternalNavigationEnum.NavigateToExternalProgram);
+ getTool().getOptions("Navigation")
+ .setEnum("External Navigation",
+ NavigationOptions.ExternalNavigationEnum.NavigateToExternalProgram);
String fromAddress = "1001030";
cb.goTo(new OperandFieldLocation(program, addr(fromAddress), null, null, null, 0, 0));
@@ -234,8 +250,9 @@ public class ExternalCodeBrowserNavigationTest extends AbstractCodeBrowserNaviga
@Test
public void testOperandExternalProgramMissingPathNavigation() throws Exception {
- getTool().getOptions("Navigation").setEnum("External Navigation",
- NavigationOptions.ExternalNavigationEnum.NavigateToExternalProgram);
+ getTool().getOptions("Navigation")
+ .setEnum("External Navigation",
+ NavigationOptions.ExternalNavigationEnum.NavigateToExternalProgram);
// clear external program path
int txId = program.startTransaction("Set Path");
@@ -294,19 +311,4 @@ public class ExternalCodeBrowserNavigationTest extends AbstractCodeBrowserNaviga
DataTree dataTree = treePanel.getDataTree();
waitForTree(dataTree);
}
-
- public class SpyGoToHelper extends MockUp<GoToHelper> {
-
- @Mock
- public boolean goTo(Invocation inv, final Navigatable navigatable, ProgramLocation loc,
- Program p) {
-
- Msg.debug(this, "goTo() called with " + loc);
-
- // Track last navigation location
- lastNavigationLocation = loc;
- lastNavigationProgram = p;
- return inv.proceed(navigatable, loc, p); // pass-thru to real
- }
- }
}
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/datatype/DataTypeSelectionDialogTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/datatype/DataTypeSelectionDialogTest.java
index 19efc9886b..2592c99220 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/datatype/DataTypeSelectionDialogTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/datatype/DataTypeSelectionDialogTest.java
@@ -39,7 +39,7 @@ import docking.DialogComponentProvider;
import docking.DockingDialog;
import docking.options.editor.ButtonPanelFactory;
import docking.widgets.DropDownSelectionTextField;
-import docking.widgets.DropDownTextField;
+import docking.widgets.DropDownTextFieldDataModel;
import docking.widgets.tree.GTree;
import docking.widgets.tree.GTreeNode;
import generic.test.AbstractGTest;
@@ -63,8 +63,6 @@ import ghidra.test.TestEnv;
import ghidra.util.Msg;
import ghidra.util.data.DataTypeParser.AllowedDataTypes;
import ghidra.util.task.TaskMonitorAdapter;
-import mockit.Mock;
-import mockit.MockUp;
public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegrationTest {
@@ -78,19 +76,16 @@ public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegration
private Set<Archive> archivesToClose = new HashSet<>();
- private SpyDropDownSelectionTextField<?> spyTextField;
+ private SpyDropDownSelectionTextField spyTextField;
@Before
public void setUp() throws Exception {
- System.err.println("\n\nsetUp() - " + testName.getMethodName());
+ Msg.debug(this, "\n\nsetUp() - " + testName.getMethodName());
setErrorGUIEnabled(false);
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- // trigger the mock to load
- spyTextField = new SpyDropDownSelectionTextField<>();
-
env = new TestEnv();
tool = env.getTool();
@@ -110,16 +105,40 @@ public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegration
closeUndesiredArchives();
+ createDtSelectionDialog();
+ assertNotNull(dialog);
+
+ Msg.debug(this, "\tend setUp()");
+ }
+
+ private void createDtSelectionDialog() {
+
runSwing(() -> {
+
+ //
+ // Slightly complicated overriding done to inject our spy text field
+ //
dialog = new DataTypeSelectionDialog(tool, program.getDataTypeManager(), -1,
- AllowedDataTypes.ALL);
+ AllowedDataTypes.ALL) {
+ @Override
+ protected DataTypeSelectionEditor createEditor(PluginTool pluginTool,
+ AllowedDataTypes allowedDataTypes) {
+ return new DataTypeSelectionEditor(pluginTool, allowedDataTypes) {
+
+ @Override
+ protected DropDownSelectionTextField<DataType> createDropDownSelectionTextField(
+ DataTypeDropDownSelectionDataModel model) {
+
+ spyTextField = new SpyDropDownSelectionTextField(model);
+ return spyTextField;
+ }
+ };
+ }
+ };
+
DropDownSelectionTextField<?> field = getEditorTextField(dialog);
removeFocusIssues(field);
});
-
- assertNotNull(dialog);
-
- System.err.println("\tend setUp()");
}
private void removeFocusIssues(DropDownSelectionTextField<?> field) {
@@ -348,7 +367,7 @@ public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegration
// fire the first enter key and make sure the completion window closes
triggerActionKey(getEditorTextField(dialog), 0, KeyEvent.VK_ENTER);
-// UPDATE - SCR 7176 - Enter key now closes the dialog, even if the selection window was open
+// UPDATE - SCR 7176 - Enter key now closes the dialog, even if the selection window was open
// assertTrue( "The completion window is visible after pressing the enter key.",
// !completionWindow.isVisible() );
// assertTrue( "The data type selection dialog was closed when only the completion window " +
@@ -425,7 +444,7 @@ public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegration
clearText(getEditorTextField(dialog));
//
- // Check that the tree chooser dialog is not shown when there are multiple matches, but
+ // Check that the tree chooser dialog is not shown when there are multiple matches, but
// each data type is equivalent. In this case we just pick the program's DT.
//
dataType = new CustomDataType(category.getCategoryPath(), crazyName, 100);
@@ -439,7 +458,7 @@ public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegration
"from multiple equivalent data types.");
//
- // Check that more than 2 non-equivalent data types trigger the dialog to appear.
+ // Check that more than 2 non-equivalent data types trigger the dialog to appear.
//
Category secondCategory = rootCategory.createCategory("testCategory2");
dataType = new CustomDataType(secondCategory.getCategoryPath(), crazyName, 2,
@@ -467,7 +486,7 @@ public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegration
clearText(getEditorTextField(dialog));
secondCategory.remove(dataType, new TaskMonitorAdapter());
- //
+ //
// After deleting the previous type, we have more than 2 multiple matches, all equivalent.
// (We tested this case above already)
//
@@ -582,7 +601,7 @@ public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegration
// To test this, start with 3 types in a file archive: foo and a typedef to foo
// (foo_typedef) and a pointer to the typedef.
// Then, pick the typedef * as the initial type of the dialog. After that, delete the
- // ending text to turn the typedef in to the foo type, leaving the pointer char in the
+ // ending text to turn the typedef in to the foo type, leaving the pointer char in the
// text field. Pressing OK should show you a chooser to pick the type found.
//
//
@@ -809,7 +828,7 @@ public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegration
}
private void closeDataTypeChooserTreeDialog(final DataTypeChooserDialog d) {
- // make sure there are no pending notifications from previous data changes
+ // make sure there are no pending notifications from previous data changes
// before we close the dialog, as this could lead to a disposed worker being used
waitForSwing();
program.flushEvents();
@@ -874,7 +893,7 @@ public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegration
String currentText = getText(textField);
if (!currentText.isEmpty()) {
// the key typing didn't work; try manually updating the text (we could probably just
- // use this call instead of typing text, but the typing will execute a slightly
+ // use this call instead of typing text, but the typing will execute a slightly
// different path in the code).
Msg.debug(this, "Clearing the text by typing DELETE did not work--manually clearing");
setText(textField, "");
@@ -1000,18 +1019,22 @@ public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegration
//==================================================================================================
// Inner Classes
-//==================================================================================================
+//==================================================================================================
/*
* A spy that allows us to track when the text field under test will trigger its window to
* be hidden. We need this due to focus issues encountered in parallel mode. Once we removed
* the focus-sensitive issues, we have to track being hidden using this mock method.
*/
- private class SpyDropDownSelectionTextField<T extends DropDownTextField<T>> extends MockUp<T> {
+ private class SpyDropDownSelectionTextField extends DropDownSelectionTextField<DataType> {
private volatile int hideId;
- @Mock
+ public SpyDropDownSelectionTextField(DropDownTextFieldDataModel<DataType> dataModel) {
+ super(dataModel);
+ }
+
+ @Override
protected void hideMatchingWindow() {
++hideId;
}
@@ -1175,8 +1198,7 @@ public class DataTypeSelectionDialogTest extends AbstractGhidraHeadedIntegration
new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW));
JPanel editorPanel = new JPanel(new BorderLayout());
- DataTypeSelectionEditor editor =
- new DataTypeSelectionEditor(tool, AllowedDataTypes.ALL);
+ DataTypeSelectionEditor editor = new DataTypeSelectionEditor(tool, AllowedDataTypes.ALL);
editor.setPreferredDataTypeManager(program.getDataTypeManager());
editorPanel.add(panelUpdateField, BorderLayout.SOUTH);