Add tests for a table (minus gui reliant components)

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2019-11-19 09:47:45 -07:00
rodzic 1b4970dad8
commit ce196c8cf6
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
4 zmienionych plików z 12 dodań i 179 usunięć

Wyświetl plik

@ -17,7 +17,7 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="lib" path="/JOSM/test/lib/awaitility-3.1.5.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/JOSM-utilsplugin2"/>
<classpathentry kind="output" path="build/classes/java/main"/>

Wyświetl plik

@ -1,3 +1,4 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai;
import static org.openstreetmap.josm.tools.I18n.tr;

Wyświetl plik

@ -4,15 +4,12 @@ package org.openstreetmap.josm.plugins.mapwithai;
import static org.openstreetmap.josm.tools.I18n.tr;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;
@ -20,30 +17,19 @@ import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.swing.ButtonGroup;
import javax.swing.DefaultCellEditor;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.openstreetmap.josm.gui.ExtendedDialog;
import org.openstreetmap.josm.gui.preferences.advanced.ListEditor;
import org.openstreetmap.josm.gui.preferences.advanced.ListListEditor;
import org.openstreetmap.josm.gui.preferences.advanced.MapListEditor;
import org.openstreetmap.josm.gui.preferences.advanced.PrefEntry;
import org.openstreetmap.josm.gui.preferences.advanced.StringEditor;
import org.openstreetmap.josm.gui.widgets.JosmTextField;
import org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation.DataUrl;
import org.openstreetmap.josm.spi.preferences.ListListSetting;
import org.openstreetmap.josm.spi.preferences.ListSetting;
import org.openstreetmap.josm.spi.preferences.MapListSetting;
import org.openstreetmap.josm.spi.preferences.Setting;
import org.openstreetmap.josm.spi.preferences.StringSetting;
import org.openstreetmap.josm.tools.GBC;
/**
* Component for editing list of preferences as a table.
@ -136,10 +122,6 @@ public class MapWithAIURLPreferenceTable extends JTable {
if (editor != null) {
editor.requestFocus();
}
} else if (stg instanceof ListSetting) {
ok = doEditList(gui, (PrefEntry) e, (ListSetting) stg);
} else if (stg instanceof ListListSetting) {
ok = doEditListList(gui, (PrefEntry) e, (ListListSetting) stg);
} else if (stg instanceof MapListSetting) {
PrefEntry pref = e instanceof PrefEntry ? (PrefEntry) e : new PrefEntry("Parameters", stg, stg, false);
ok = doEditMapList(gui, pref, (MapListSetting) stg);
@ -154,7 +136,15 @@ public class MapWithAIURLPreferenceTable extends JTable {
return ok;
}
private static Map<String, Object> objectify(Map<String, String> map) {
/**
* Convert a map of Map&lt;String, String&gt; to a map of Map&lt;String,
* Object&gt;
*
* @param map The map of strings to strings to convert (e.g., "1"->int 1, "true"
* -> boolean true, etc)
* @return A converted map
*/
public static Map<String, Object> objectify(Map<String, String> map) {
Map<String, Object> returnMap = new TreeMap<>();
for (Entry<String, String> entry : map.entrySet()) {
Object obj = null;
@ -176,32 +166,6 @@ public class MapWithAIURLPreferenceTable extends JTable {
return returnMap;
}
private static boolean doEditList(final JComponent gui, final PrefEntry e, ListSetting lSetting) {
ListEditor lEditor = new ListEditor(gui, e, lSetting);
lEditor.showDialog();
if (lEditor.getValue() == 1) {
List<String> data = lEditor.getData();
if (!lSetting.equalVal(data)) {
e.setValue(new ListSetting(data));
return true;
}
}
return false;
}
private static boolean doEditListList(final JComponent gui, final PrefEntry e, ListListSetting llSetting) {
ListListEditor llEditor = new ListListEditor(gui, e, llSetting);
llEditor.showDialog();
if (llEditor.getValue() == 1) {
List<List<String>> data = llEditor.getData();
if (!llSetting.equalVal(data)) {
e.setValue(new ListListSetting(data));
return true;
}
}
return false;
}
private static boolean doEditMapList(final JComponent gui, final PrefEntry e, MapListSetting mlSetting) {
MapListEditor mlEditor = new MapListEditor(gui, e, mlSetting);
mlEditor.showDialog();
@ -215,139 +179,6 @@ public class MapWithAIURLPreferenceTable extends JTable {
return false;
}
/**
* Add new preference to the table
* @param gui - parent component for asking dialogs
* @return newly created entry or null if adding was cancelled
*/
public PrefEntry addPreference(final JComponent gui) {
JPanel p = new JPanel(new GridBagLayout());
p.add(new JLabel(tr("Key")), GBC.std().insets(0, 0, 5, 0));
JosmTextField tkey = new JosmTextField("", 50);
p.add(tkey, GBC.eop().insets(5, 0, 0, 0).fill(GridBagConstraints.HORIZONTAL));
p.add(new JLabel(tr("Select Setting Type:")), GBC.eol().insets(5, 15, 5, 0));
JRadioButton rbString = new JRadioButton(tr("Simple"));
JRadioButton rbList = new JRadioButton(tr("List"));
JRadioButton rbListList = new JRadioButton(tr("List of lists"));
JRadioButton rbMapList = new JRadioButton(tr("List of maps"));
ButtonGroup group = new ButtonGroup();
group.add(rbString);
group.add(rbList);
group.add(rbListList);
group.add(rbMapList);
p.add(rbString, GBC.eol());
p.add(rbList, GBC.eol());
p.add(rbListList, GBC.eol());
p.add(rbMapList, GBC.eol());
rbString.setSelected(true);
PrefEntry pe = null;
boolean ok = false;
if (askAddSetting(gui, p)) {
if (rbString.isSelected()) {
StringSetting sSetting = new StringSetting(null);
pe = new PrefEntry(tkey.getText(), sSetting, sSetting, false);
ok = doAddSimple(gui, pe, sSetting);
} else if (rbList.isSelected()) {
ListSetting lSetting = new ListSetting(null);
pe = new PrefEntry(tkey.getText(), lSetting, lSetting, false);
ok = doAddList(gui, pe, lSetting);
} else if (rbListList.isSelected()) {
ListListSetting llSetting = new ListListSetting(null);
pe = new PrefEntry(tkey.getText(), llSetting, llSetting, false);
ok = doAddListList(gui, pe, llSetting);
} else if (rbMapList.isSelected()) {
MapListSetting mlSetting = new MapListSetting(null);
pe = new PrefEntry(tkey.getText(), mlSetting, mlSetting, false);
ok = doAddMapList(gui, pe, mlSetting);
}
}
return ok ? pe : null;
}
private static boolean askAddSetting(JComponent gui, JPanel p) {
return new ExtendedDialog(gui, tr("Add setting"), tr("OK"), tr("Cancel"))
.setContent(p).setButtonIcons("ok", "cancel").showDialog().getValue() == 1;
}
private static boolean doAddSimple(final JComponent gui, PrefEntry pe, StringSetting sSetting) {
StringEditor sEditor = new StringEditor(gui, pe, sSetting);
sEditor.showDialog();
if (sEditor.getValue() == 1) {
String data = sEditor.getData();
if (!Objects.equals(sSetting.getValue(), data)) {
pe.setValue(new StringSetting(data));
return true;
}
}
return false;
}
private static boolean doAddList(final JComponent gui, PrefEntry pe, ListSetting lSetting) {
ListEditor lEditor = new ListEditor(gui, pe, lSetting);
lEditor.showDialog();
if (lEditor.getValue() == 1) {
List<String> data = lEditor.getData();
if (!lSetting.equalVal(data)) {
pe.setValue(new ListSetting(data));
return true;
}
}
return false;
}
private static boolean doAddListList(final JComponent gui, PrefEntry pe, ListListSetting llSetting) {
ListListEditor llEditor = new ListListEditor(gui, pe, llSetting);
llEditor.showDialog();
if (llEditor.getValue() == 1) {
List<List<String>> data = llEditor.getData();
if (!llSetting.equalVal(data)) {
pe.setValue(new ListListSetting(data));
return true;
}
}
return false;
}
private static boolean doAddMapList(final JComponent gui, PrefEntry pe, MapListSetting mlSetting) {
MapListEditor mlEditor = new MapListEditor(gui, pe, mlSetting);
mlEditor.showDialog();
if (mlEditor.getValue() == 1) {
List<Map<String, String>> data = mlEditor.getData();
if (!mlSetting.equalVal(data)) {
pe.setValue(new MapListSetting(data));
return true;
}
}
return false;
}
/**
* Reset selected preferences to their default values
* @param gui - parent component to display warning messages
*/
public void resetPreferences(final JComponent gui) {
if (getSelectedRowCount() == 0) {
JOptionPane.showMessageDialog(
gui,
tr("Please select the row to delete."),
tr("Warning"),
JOptionPane.WARNING_MESSAGE
);
return;
}
for (int row : getSelectedRows()) {
DataUrl e = displayData.get(row);
e.reset();
}
fireDataChanged();
}
final class URLTableModel extends DefaultTableModel {
private static final long serialVersionUID = 2435772137483913278L;

Wyświetl plik

@ -1,3 +1,4 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation;
import static org.openstreetmap.josm.tools.I18n.tr;