Changeset 18875 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java
r18829 r18875 43 43 import org.openstreetmap.josm.tools.RightAndLefthandTraffic; 44 44 import org.openstreetmap.josm.tools.RotationAngle; 45 import org.openstreetmap.josm.tools.RotationAngle.WayDirectionRotationAngle; 45 46 import org.openstreetmap.josm.tools.StreamUtils; 46 47 import org.openstreetmap.josm.tools.Territories; 47 48 import org.openstreetmap.josm.tools.Utils; 48 import org.openstreetmap.josm.tools.RotationAngle.WayDirectionRotationAngle;49 49 50 50 /** … … 411 411 */ 412 412 public static List<String> tag_regex(final Environment env, String keyRegex, String flags) { 413 if (env.osm == null) { 414 return Collections.emptyList(); 415 } 413 416 int f = parse_regex_flags(flags); 414 417 Pattern compiled = Pattern.compile(keyRegex, f); … … 626 629 */ 627 630 public static boolean has_tag_key(final Environment env, String key) { 628 return env.osm .hasKey(key);631 return env.osm != null ? env.osm.hasKey(key) : false; 629 632 } 630 633 … … 941 944 */ 942 945 public static long osm_id(final Environment env) { 943 return env.osm .getUniqueId();946 return env.osm != null ? env.osm.getUniqueId() : 0; 944 947 } 945 948 … … 952 955 */ 953 956 public static String osm_user_name(final Environment env) { 954 return env.osm .getUser().getName();957 return env.osm != null ? env.osm.getUser().getName() : null; 955 958 } 956 959 … … 963 966 */ 964 967 public static long osm_user_id(final Environment env) { 965 return env.osm .getUser().getId();968 return env.osm != null ? env.osm.getUser().getId() : 0; 966 969 } 967 970 … … 974 977 */ 975 978 public static int osm_version(final Environment env) { 976 return env.osm .getVersion();979 return env.osm != null ? env.osm.getVersion() : 0; 977 980 } 978 981 … … 985 988 */ 986 989 public static int osm_changeset_id(final Environment env) { 987 return env.osm .getChangesetId();990 return env.osm != null ? env.osm.getChangesetId() : 0; 988 991 } 989 992 … … 996 999 */ 997 1000 public static int osm_timestamp(final Environment env) { 998 return env.osm .getRawTimestamp();1001 return env.osm != null ? env.osm.getRawTimestamp() : 0; 999 1002 } 1000 1003 … … 1197 1200 */ 1198 1201 public static boolean is_right_hand_traffic(Environment env) { 1199 return RightAndLefthandTraffic.isRightHandTraffic(center(env)); 1202 final LatLon center = center(env); 1203 if (center != null) { 1204 return RightAndLefthandTraffic.isRightHandTraffic(center); 1205 } 1206 return false; 1200 1207 } 1201 1208 … … 1261 1268 */ 1262 1269 public static int number_of_tags(Environment env) { 1263 return env.osm .getNumKeys();1270 return env.osm != null ? env.osm.getNumKeys() : 0; 1264 1271 } 1265 1272 … … 1271 1278 */ 1272 1279 public static Object setting(Environment env, String key) { 1273 return env.source .settingValues.get(key);1280 return env.source != null ? env.source.settingValues.get(key) : null; 1274 1281 } 1275 1282 … … 1281 1288 */ 1282 1289 public static LatLon center(Environment env) { 1283 return env.osm instanceof Node ? ((Node) env.osm).getCoor() : env.osm.getBBox().getCenter(); 1290 if (env.osm instanceof ILatLon) { 1291 return new LatLon(((ILatLon) env.osm).lat(), ((ILatLon) env.osm).lon()); 1292 } else if (env.osm != null) { 1293 return env.osm.getBBox().getCenter(); 1294 } 1295 return null; 1284 1296 } 1285 1297 … … 1316 1328 */ 1317 1329 public static boolean at(Environment env, double lat, double lon) { 1318 return new LatLon(lat, lon).equalsEpsilon(center(env), ILatLon.MAX_SERVER_PRECISION); 1330 final ILatLon center = center(env); 1331 if (center != null) { 1332 return new LatLon(lat, lon).equalsEpsilon(center, ILatLon.MAX_SERVER_PRECISION); 1333 } 1334 return false; 1319 1335 } 1320 1336 -
trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/FunctionsTest.java
r18870 r18875 3 3 4 4 import static org.junit.jupiter.api.Assertions.assertAll; 5 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 5 6 import static org.junit.jupiter.api.Assertions.assertEquals; 6 7 import static org.junit.jupiter.api.Assertions.assertFalse; … … 9 10 import static org.junit.jupiter.api.Assertions.assertSame; 10 11 import static org.junit.jupiter.api.Assertions.assertTrue; 12 import static org.junit.jupiter.api.Assertions.fail; 11 13 import static org.openstreetmap.josm.data.osm.OsmPrimitiveType.NODE; 12 14 15 import java.lang.reflect.Method; 16 import java.lang.reflect.Modifier; 13 17 import java.util.Arrays; 14 18 import java.util.Collections; 15 19 import java.util.List; 16 20 import java.util.Objects; 17 21 import java.util.stream.Stream; 22 23 import org.junit.jupiter.api.AfterAll; 18 24 import org.junit.jupiter.api.Test; 25 import org.junit.jupiter.params.ParameterizedTest; 26 import org.junit.jupiter.params.provider.MethodSource; 19 27 import org.openstreetmap.josm.TestUtils; 20 28 import org.openstreetmap.josm.data.coor.LatLon; … … 30 38 import org.openstreetmap.josm.data.preferences.NamedColorProperty; 31 39 import org.openstreetmap.josm.gui.mappaint.Environment; 40 import org.openstreetmap.josm.gui.mappaint.MultiCascade; 32 41 import org.openstreetmap.josm.gui.util.GuiHelper; 33 42 import org.openstreetmap.josm.spi.preferences.Config; … … 35 44 import org.openstreetmap.josm.testutils.annotations.MapPaintStyles; 36 45 import org.openstreetmap.josm.testutils.annotations.Projection; 46 import org.openstreetmap.josm.testutils.annotations.Territories; 37 47 38 48 /** … … 62 72 return new Environment(osm); 63 73 } 74 } 75 76 private static Method[] FUNCTIONS; 77 78 static Stream<Method> getFunctions() { 79 if (FUNCTIONS == null) { 80 FUNCTIONS = Stream.of(Functions.class.getDeclaredMethods()) 81 .filter(m -> Modifier.isStatic(m.getModifiers()) && Modifier.isPublic(m.getModifiers())) 82 .toArray(Method[]::new); 83 } 84 return Stream.of(FUNCTIONS); 85 } 86 87 @AfterAll 88 static void tearDown() { 89 FUNCTIONS = null; 64 90 } 65 91 … … 231 257 assertTrue(Functions.parent_osm_primitives(env, "type2").isEmpty()); 232 258 } 259 260 /** 261 * Non-regression test for #23238: NPE when env.osm is null 262 */ 263 @ParameterizedTest 264 @MethodSource("getFunctions") 265 @Territories // needed for inside, outside, is_right_hand_traffic 266 void testNonRegression23238(Method function) { 267 if (function.getParameterCount() >= 1 && function.getParameterTypes()[0].isAssignableFrom(Environment.class) 268 && !function.getParameterTypes()[0].equals(Object.class)) { 269 Environment nullOsmEnvironment = new Environment(); 270 nullOsmEnvironment.mc = new MultiCascade(); 271 Object[] args = new Object[function.getParameterCount()]; 272 args[0] = nullOsmEnvironment; 273 for (int i = 1; i < function.getParameterCount(); i++) { 274 final Class<?> type = function.getParameterTypes()[i]; 275 if (String.class.isAssignableFrom(type)) { 276 args[i] = ""; 277 } else if (String[].class.isAssignableFrom(type)) { 278 args[i] = new String[] {"{0}", ""}; // join and tr require at least 2 arguments 279 } else if (Double.class.isAssignableFrom(type) || double.class.isAssignableFrom(type)) { 280 args[i] = 0d; 281 } else if (Object.class.isAssignableFrom(type)) { 282 args[i] = new Object[0]; 283 } else { 284 fail(type.getCanonicalName()); 285 } 286 } 287 assertDoesNotThrow(() -> function.invoke(null, args)); 288 } 289 } 233 290 }
Note:
See TracChangeset
for help on using the changeset viewer.