source: josm/trunk/test/unit/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelperTest.java@ 17275

Last change on this file since 17275 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.properties;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertNotNull;
7
8import java.awt.GraphicsEnvironment;
9import java.lang.reflect.Field;
10import java.lang.reflect.Method;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collections;
14import java.util.HashMap;
15import java.util.List;
16import java.util.Map;
17import java.util.function.Function;
18import java.util.stream.Collectors;
19
20import javax.swing.JTable;
21import javax.swing.table.DefaultTableModel;
22
23import org.junit.jupiter.api.extension.RegisterExtension;
24import org.junit.jupiter.api.Test;
25import org.openstreetmap.josm.TestUtils;
26import org.openstreetmap.josm.data.coor.LatLon;
27import org.openstreetmap.josm.data.osm.DataSet;
28import org.openstreetmap.josm.data.osm.Node;
29import org.openstreetmap.josm.data.osm.OsmDataManager;
30import org.openstreetmap.josm.data.osm.OsmPrimitive;
31import org.openstreetmap.josm.data.osm.Way;
32import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem;
33import org.openstreetmap.josm.gui.MainApplication;
34import org.openstreetmap.josm.gui.dialogs.properties.TagEditHelper.AddTagsDialog;
35import org.openstreetmap.josm.gui.layer.OsmDataLayer;
36import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
37import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
38import org.openstreetmap.josm.testutils.JOSMTestRules;
39import org.openstreetmap.josm.testutils.mockers.WindowMocker;
40
41import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
42
43/**
44 * Unit tests of {@link TagEditHelper} class.
45 */
46class TagEditHelperTest {
47
48 /**
49 * Setup tests
50 */
51 @RegisterExtension
52 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
53 public JOSMTestRules test = new JOSMTestRules().territories().projection();
54
55 private static TagEditHelper newTagEditHelper() {
56 DefaultTableModel propertyData = new DefaultTableModel();
57 JTable tagTable = new JTable(propertyData);
58 Map<String, Map<String, Integer>> valueCount = new HashMap<>();
59 return new TagEditHelper(tagTable, propertyData, valueCount);
60 }
61
62 /**
63 * Checks that autocompleting list items are sorted correctly.
64 */
65 @Test
66 void testAcItemComparator() {
67 List<AutoCompletionItem> list = new ArrayList<>();
68 list.add(new AutoCompletionItem("Bing Sat"));
69 list.add(new AutoCompletionItem("survey"));
70 list.add(new AutoCompletionItem("Bing"));
71 list.add(new AutoCompletionItem("digitalglobe"));
72 list.add(new AutoCompletionItem("bing"));
73 list.add(new AutoCompletionItem("DigitalGlobe"));
74 list.sort(TagEditHelper.DEFAULT_AC_ITEM_COMPARATOR);
75 assertEquals(Arrays.asList("Bing", "bing", "Bing Sat", "digitalglobe", "DigitalGlobe", "survey"),
76 list.stream().map(AutoCompletionItem::getValue).collect(Collectors.toList()));
77 }
78
79 /**
80 * Unit test of {@link TagEditHelper#containsDataKey}.
81 */
82 @Test
83 void testContainsDataKey() {
84 assertFalse(newTagEditHelper().containsDataKey("foo"));
85 // TODO: complete test
86 }
87
88 /**
89 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/18764>#18764</a>
90 *
91 * @throws Exception if any error occurs
92 */
93 @Test
94 void testTicket18764() throws Exception {
95 testIcon("*[building] ⧉ *[highway] { text: tr(\"Building crossing highway\"); }", ds -> {
96 Way way = TestUtils.newWay("", new Node(LatLon.NORTH_POLE), new Node(LatLon.SOUTH_POLE));
97 way.getNodes().forEach(ds::addPrimitive);
98 return way;
99 }, "highway", "");
100 }
101
102 /**
103 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/18798>#18798</a>
104 *
105 * @throws Exception if any error occurs
106 */
107 @Test
108 void testTicket18798() throws Exception {
109 testIcon("node:righthandtraffic[junction=roundabout] { text: tr(\"Roundabout node\"); }", ds -> {
110 Node node = new Node(LatLon.NORTH_POLE);
111 ds.addPrimitive(node);
112 return node;
113 }, "junction", "roundabout");
114 }
115
116 void testIcon(String cssString, Function<DataSet, OsmPrimitive> prepare, String key, String value) throws Exception {
117 TestUtils.assumeWorkingJMockit();
118 if (GraphicsEnvironment.isHeadless()) {
119 new WindowMocker();
120 }
121 MapCSSStyleSource css = new MapCSSStyleSource(cssString);
122 css.loadStyleSource();
123 MapPaintStyles.addStyle(css);
124 DataSet ds = new DataSet();
125 final OsmPrimitive primitive = prepare.apply(ds);
126 OsmDataManager.getInstance().setActiveDataSet(ds);
127 MainApplication.getLayerManager().addLayer(new OsmDataLayer(ds, "Test Layer", null));
128 TagEditHelper helper = newTagEditHelper();
129 Field sel = TagEditHelper.class.getDeclaredField("sel");
130 sel.set(helper, Collections.singletonList(primitive));
131 AddTagsDialog addTagsDialog = helper.getAddTagsDialog();
132 Method findIcon = TagEditHelper.AbstractTagsDialog.class.getDeclaredMethod("findIcon", String.class, String.class);
133 findIcon.setAccessible(true);
134 Object val = findIcon.invoke(addTagsDialog, key, value);
135 assertNotNull(val);
136 }
137}
Note: See TracBrowser for help on using the repository browser.