source: josm/trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/FunctionsTest.java@ 17531

Last change on this file since 17531 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.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertTrue;
6import static org.openstreetmap.josm.data.osm.OsmPrimitiveType.NODE;
7
8import java.util.Collections;
9
10import org.junit.jupiter.api.extension.RegisterExtension;
11import org.junit.jupiter.api.Test;
12import org.openstreetmap.josm.TestUtils;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
15import org.openstreetmap.josm.data.osm.User;
16import org.openstreetmap.josm.data.preferences.NamedColorProperty;
17import org.openstreetmap.josm.gui.mappaint.Environment;
18import org.openstreetmap.josm.gui.util.GuiHelper;
19import org.openstreetmap.josm.spi.preferences.Config;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23
24/**
25 * Unit tests of {@link Functions}.
26 */
27class FunctionsTest {
28
29 /**
30 * Setup rule
31 */
32 @RegisterExtension
33 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
34 public JOSMTestRules test = new JOSMTestRules();
35
36 private static class EnvBuilder {
37 private final OsmPrimitive osm;
38
39 EnvBuilder(OsmPrimitiveType type) {
40 switch (type) {
41 case NODE : osm = TestUtils.newNode(""); break;
42 case WAY : osm = TestUtils.newWay(""); break;
43 case RELATION : osm = TestUtils.newRelation(""); break;
44 default: throw new IllegalArgumentException();
45 }
46 }
47
48 EnvBuilder setUser(User user) {
49 osm.setUser(user);
50 return this;
51 }
52
53 Environment build() {
54 return new Environment(osm);
55 }
56 }
57
58 /**
59 * Unit test of {@link Functions#osm_user_name}.
60 */
61 @Test
62 void testOsmUserName() {
63 assertEquals("<anonymous>", Functions.osm_user_name(new EnvBuilder(NODE).setUser(User.getAnonymous()).build()));
64 }
65
66 /**
67 * Unit test of {@link Functions#osm_user_id}.
68 */
69 @Test
70 void testOsmUserId() {
71 assertEquals(-1, Functions.osm_user_id(new EnvBuilder(NODE).setUser(User.getAnonymous()).build()));
72 }
73
74 /**
75 * Unit test of {@link Functions#osm_version}.
76 */
77 @Test
78 void testOsmVersion() {
79 assertEquals(0, Functions.osm_version(new EnvBuilder(NODE).build()));
80 }
81
82 /**
83 * Unit test of {@link Functions#osm_changeset_id}.
84 */
85 @Test
86 void testOsmChangesetId() {
87 assertEquals(0, Functions.osm_changeset_id(new EnvBuilder(NODE).build()));
88 }
89
90 /**
91 * Unit test of {@link Functions#osm_timestamp}.
92 */
93 @Test
94 void testOsmTimestamp() {
95 assertEquals(0, Functions.osm_timestamp(new EnvBuilder(NODE).build()));
96 }
97
98 /**
99 * Unit test of {@code Functions#to_xxx}
100 */
101 @Test
102 void testParseFunctions() {
103 assertTrue(Functions.to_boolean("true"));
104 assertEquals(1, Functions.to_byte("1"));
105 assertEquals(1, Functions.to_short("1"));
106 assertEquals(1, Functions.to_int("1"));
107 assertEquals(1L, Functions.to_long("1"));
108 assertEquals(1f, Functions.to_float("1"), 1e-10);
109 assertEquals(1d, Functions.to_double("1"), 1e-10);
110 }
111
112 /**
113 * Unit test of {@link Functions#JOSM_pref}
114 */
115 @Test
116 void testPref() {
117 String key = "Functions.JOSM_pref";
118 Config.getPref().put(key, null);
119 assertEquals("foobar", Functions.JOSM_pref(null, key, "foobar"));
120 Config.getPref().put(key, "baz");
121 GuiHelper.runInEDTAndWait(() -> {
122 // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
123 });
124 assertEquals("baz", Functions.JOSM_pref(null, key, "foobar"));
125 Config.getPref().put(key, null);
126 GuiHelper.runInEDTAndWait(() -> {
127 // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
128 });
129 assertEquals("foobar", Functions.JOSM_pref(null, key, "foobar"));
130 Config.getPref().put(key, null);
131 }
132
133 /**
134 * Unit test of {@link Functions#JOSM_pref}, color handling
135 */
136 @Test
137 void testPrefColor() {
138 String key = "Functions.JOSM_pref";
139 String colorKey = NamedColorProperty.NAMED_COLOR_PREFIX + NamedColorProperty.COLOR_CATEGORY_MAPPAINT + ".unknown." + key;
140 Config.getPref().put(colorKey, null);
141 assertEquals("#000000", Functions.JOSM_pref(null, key, "#000000"));
142 Config.getPref().putList(colorKey, Collections.singletonList("#00FF00"));
143 GuiHelper.runInEDTAndWait(() -> {
144 // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
145 });
146 assertEquals("#00FF00", Functions.JOSM_pref(null, key, "#000000"));
147 Config.getPref().put(colorKey, null);
148 GuiHelper.runInEDTAndWait(() -> {
149 // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
150 });
151 assertEquals("#000000", Functions.JOSM_pref(null, key, "#000000"));
152 Config.getPref().put(colorKey, null);
153 }
154
155}
Note: See TracBrowser for help on using the repository browser.