Changeset 6427 in josm


Ignore:
Timestamp:
2013-11-30T22:06:02+01:00 (10 years ago)
Author:
simon04
Message:

fix #9382 - fix OpeningHourTest under Java 8

Instead of implementing yet another getList() extraction
based on jdk.nashorn.api.scripting.ScriptObjectMirror,
use JavaScript to join warnings to a String and
split that String in Java code afterwards.

Also, don't rely on JavaScript exception to obtain
test errors (due to different string representation),
but catch those exceptions in JavaScript and provide
a getErrors() function.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java

    r6415 r6427  
    55
    66import java.io.InputStreamReader;
    7 import java.lang.reflect.InvocationTargetException;
    8 import java.lang.reflect.Method;
    97import java.util.ArrayList;
    108import java.util.Arrays;
     
    5856            // fake country/state to not get errors on holidays
    5957            ENGINE.eval("var nominatiomJSON = {address: {state: 'Bayern', country_code: 'de'}};");
    60             ENGINE.eval("var oh = function (value, mode) {return new opening_hours(value, nominatiomJSON, mode);};");
     58            ENGINE.eval("" +
     59                    "var oh = function (value, mode) {" +
     60                    " try {" +
     61                    "    var r= new opening_hours(value, nominatiomJSON, mode);" +
     62                    "    r.getErrors = function() {return [];};" +
     63                    "    return r;" +
     64                    "  } catch(err) {" +
     65                    "    return {" +
     66                    "      getWarnings: function() {return [];}," +
     67                    "      getErrors: function() {return [err.toString()]}" +
     68                    "    };" +
     69                    "  }" +
     70                    "};");
    6171        } else {
    6272            Main.warn("Unable to initialize OpeningHourTest because no JavaScript engine has been found");
     
    7888
    7989    @SuppressWarnings("unchecked")
    80     protected List<Object> getList(Object obj) {
     90    protected List<Object> getList(Object obj) throws ScriptException, NoSuchMethodException {
    8191        if (obj == null || "".equals(obj)) {
    8292            return Arrays.asList();
    8393        } else if (obj instanceof String) {
    84             final Object[] strings = ((String) obj).split("\\n");
     94            final Object[] strings = ((String) obj).split("\\\\n");
    8595            return Arrays.asList(strings);
    8696        } else if (obj instanceof List) {
    8797            return (List<Object>) obj;
    88         } else if ("sun.org.mozilla.javascript.internal.NativeArray".equals(obj.getClass().getName())) {
    89             List<Object> list = new ArrayList<Object>();
    90             try {
    91                 Method getIds = obj.getClass().getMethod("getIds");
    92                 Method get = obj.getClass().getMethod("get", long.class);
    93                 Object[] ids = (Object[]) getIds.invoke(obj);
    94                 for (Object id : ids) {
    95                     list.add(get.invoke(obj, id));
    96                 }
    97             } catch (NoSuchMethodException e) {
    98                 Main.error("Unable to run OpeningHourTest because of NoSuchMethodException by reflection: "+e.getMessage());
    99             } catch (IllegalArgumentException e) {
    100                 Main.error("Unable to run OpeningHourTest because of IllegalArgumentException by reflection: "+e.getMessage());
    101             } catch (IllegalAccessException e) {
    102                 Main.error("Unable to run OpeningHourTest because of IllegalAccessException by reflection: "+e.getMessage());
    103             } catch (InvocationTargetException e) {
    104                 Main.error("Unable to run OpeningHourTest because of InvocationTargetException by reflection: "+e.getMessage());
    105             }
    106             return list;
    10798        } else {
    108             throw new IllegalArgumentException("Not expecting class " + obj.getClass());
     99            // recursively call getList() with argument converted to newline-separated string
     100            return getList(((Invocable) ENGINE).invokeMethod(obj, "join", "\\n"));
    109101        }
    110102    }
     
    168160                Main.debug(e.getMessage());
    169161            }
     162            for (final Object i : getList(((Invocable) ENGINE).invokeMethod(r, "getErrors"))) {
     163                errors.add(new OpeningHoursTestError(key + " - " + i.toString().trim(), Severity.ERROR, prettifiedValue));
     164            }
    170165            for (final Object i : getList(((Invocable) ENGINE).invokeMethod(r, "getWarnings"))) {
    171                 errors.add(new OpeningHoursTestError(i.toString(), Severity.WARNING, prettifiedValue));
     166                errors.add(new OpeningHoursTestError(i.toString().trim(), Severity.WARNING, prettifiedValue));
    172167            }
    173168            return errors;
    174         } catch (ScriptException ex) {
    175             final String message = ex.getMessage()
    176                     .replaceAll("[^:]*Exception: ", key+" - ")
    177                     .replaceAll("\\(<Unknown source.*", "")
    178                     .trim();
    179             return Arrays.asList(new OpeningHoursTestError(message, Severity.ERROR));
    180169        } catch (final Exception ex) {
    181170            throw new RuntimeException(ex);
Note: See TracChangeset for help on using the changeset viewer.