Skip to content

Commit 7c2f331

Browse files
author
taylor.smock
committed
Fix #23081: NoSuchElementException in ConflictDialog.ResolveToAction#actionPerformed
This occurs when one of the `Resolve to (my|their) versions` actions is called when no conflict is selected. There are two distinct bugs: * `isConflictSelected` did not check to make certain that the selection index made sense * `ResolveAction`s were not enabled/disabled when the popup menu became visible, depending upon whether the current selection was valid git-svn-id: https://josm.openstreetmap.de/svn/trunk@18783 0c6e7542-c601-0410-84e7-c038aed88b3b
1 parent 9d540bc commit 7c2f331

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/org/openstreetmap/josm/gui/dialogs/ConflictDialog.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,13 @@ public synchronized Conflict<? extends OsmPrimitive> getSelectedConflict() {
321321

322322
private synchronized boolean isConflictSelected() {
323323
final ListSelectionModel selModel = lstConflicts.getSelectionModel();
324-
return selModel.getMinSelectionIndex() >= 0 && selModel.getMaxSelectionIndex() >= selModel.getMinSelectionIndex();
324+
final int minSelectionIndex = selModel.getMinSelectionIndex();
325+
final int maxSelectionIndex = selModel.getMaxSelectionIndex();
326+
final int maxIndex = conflicts.size();
327+
// if minSelectionIndex < 0, nothing is selected
328+
// if minSelectionIndex > maxIndex, then nothing is selected (we are operating with an old selection context, most likely)
329+
// if maxSelectionIndex < minSelectionIndex, _something_ funny is going on. Or there was a typo in the original code.
330+
return minSelectionIndex >= 0 && maxIndex > minSelectionIndex && maxSelectionIndex >= minSelectionIndex;
325331
}
326332

327333
@Override
@@ -368,6 +374,8 @@ static final class ResolveButtonsPopupMenuListener implements PopupMenuListener
368374
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
369375
btnResolveMy.setVisible(ExpertToggleAction.isExpert());
370376
btnResolveTheir.setVisible(ExpertToggleAction.isExpert());
377+
((ResolveAction) btnResolveMy.getAction()).valueChanged(null);
378+
((ResolveAction) btnResolveTheir.getAction()).valueChanged(null);
371379
}
372380

373381
@Override
@@ -528,7 +536,7 @@ public void actionPerformed(ActionEvent e) {
528536
}
529537
}
530538
}
531-
UndoRedoHandler.getInstance().add(new SequenceCommand(name, commands));
539+
UndoRedoHandler.getInstance().add(new SequenceCommand(name, commands));
532540
refreshView();
533541
}
534542
}

test/unit/org/openstreetmap/josm/gui/dialogs/ConflictDialogTest.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// License: GPL. For details, see LICENSE file.
22
package org.openstreetmap.josm.gui.dialogs;
33

4+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
45
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertNotNull;
66

77
import java.awt.Color;
88
import java.awt.image.BufferedImage;
99

10-
import org.junit.jupiter.api.extension.RegisterExtension;
1110
import org.junit.jupiter.api.Test;
1211
import org.openstreetmap.josm.data.coor.LatLon;
1312
import org.openstreetmap.josm.data.osm.DataSet;
@@ -18,28 +17,21 @@
1817
import org.openstreetmap.josm.gui.MainApplication;
1918
import org.openstreetmap.josm.gui.dialogs.ConflictDialog.ConflictPainter;
2019
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
21-
import org.openstreetmap.josm.testutils.JOSMTestRules;
22-
23-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20+
import org.openstreetmap.josm.testutils.annotations.Main;
21+
import org.openstreetmap.josm.testutils.annotations.Projection;
2422

2523
/**
2624
* Unit tests of {@link ConflictDialog} class.
2725
*/
26+
@Main
27+
@Projection
2828
class ConflictDialogTest {
29-
30-
/**
31-
* Setup tests
32-
*/
33-
@RegisterExtension
34-
@SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
35-
public JOSMTestRules test = new JOSMTestRules().main().projection();
36-
3729
/**
3830
* Unit test of {@link ConflictDialog#ConflictDialog}.
3931
*/
4032
@Test
4133
void testConflictDialog() {
42-
assertNotNull(new ConflictDialog());
34+
assertDoesNotThrow(ConflictDialog::new);
4335
}
4436

4537
/**

0 commit comments

Comments
 (0)