Changeset 14486 in josm for trunk/src


Ignore:
Timestamp:
2018-12-02T13:59:27+01:00 (5 years ago)
Author:
Don-vip
Message:

see #17058 - fix unit tests

Location:
trunk/src/org/openstreetmap/josm/data
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/OsmUtils.java

    r14484 r14486  
    115115     * Creates a new OSM primitive around (0,0) according to the given assertion. Originally written for unit tests,
    116116     * this can also be used in another places like validation of local MapCSS validator rules.
     117     * Ways and relations created using this method are empty.
    117118     * @param assertion The assertion describing OSM primitive (ex: "way name=Foo railway=rail")
    118119     * @return a new OSM primitive according to the given assertion
     
    121122     */
    122123    public static OsmPrimitive createPrimitive(String assertion) {
    123         return createPrimitive(assertion, LatLon.ZERO);
     124        return createPrimitive(assertion, LatLon.ZERO, false);
    124125    }
    125126
     
    129130     * @param assertion The assertion describing OSM primitive (ex: "way name=Foo railway=rail")
    130131     * @param around the coordinate at which the primitive will be located
     132     * @param enforceLocation if {@code true}, ways and relations will not be empty to force a physical location
    131133     * @return a new OSM primitive according to the given assertion
    132134     * @throws IllegalArgumentException if assertion is null or if the primitive type cannot be deduced from it
    133      * @since 14484
    134      */
    135     public static OsmPrimitive createPrimitive(String assertion, LatLon around) {
     135     * @since 14486
     136     */
     137    public static OsmPrimitive createPrimitive(String assertion, LatLon around, boolean enforceLocation) {
    136138        CheckParameterUtil.ensureParameterNotNull(assertion, "assertion");
    137139        final String[] x = assertion.split("\\s+", 2);
    138140        final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0])
    139                 ? new Node(around)
     141                ? newNode(around)
    140142                : "w".equals(x[0]) || "way".equals(x[0]) || /*for MapCSS related usage*/ "area".equals(x[0])
    141                 ? newWay(around)
     143                ? newWay(around, enforceLocation)
    142144                : "r".equals(x[0]) || "relation".equals(x[0])
    143                 ? newRelation(around)
     145                ? newRelation(around, enforceLocation)
    144146                : null;
    145147        if (p == null) {
     
    158160    }
    159161
    160     private static Way newWay(LatLon around) {
     162    private static Way newWay(LatLon around, boolean enforceLocation) {
    161163        Way w = new Way();
    162         w.addNode(newNode(new LatLon(around.lat()+0.1, around.lon())));
    163         w.addNode(newNode(new LatLon(around.lat()-0.1, around.lon())));
     164        if (enforceLocation) {
     165            w.addNode(newNode(new LatLon(around.lat()+0.1, around.lon())));
     166            w.addNode(newNode(new LatLon(around.lat()-0.1, around.lon())));
     167        }
    164168        return w;
    165169    }
    166170
    167     private static Relation newRelation(LatLon around) {
     171    private static Relation newRelation(LatLon around, boolean enforceLocation) {
    168172        Relation r = new Relation();
    169         r.addMember(new RelationMember(null, newNode(around)));
     173        if (enforceLocation) {
     174            r.addMember(new RelationMember(null, newNode(around)));
     175        }
    170176        return r;
    171177    }
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r14484 r14486  
    4444import org.openstreetmap.josm.data.osm.OsmPrimitive;
    4545import org.openstreetmap.josm.data.osm.OsmUtils;
     46import org.openstreetmap.josm.data.osm.Relation;
    4647import org.openstreetmap.josm.data.osm.Tag;
     48import org.openstreetmap.josm.data.osm.Way;
    4749import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
    4850import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
     
    10241026            for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) {
    10251027                Logging.debug("- Assertion: {0}", i);
    1026                 final OsmPrimitive p = OsmUtils.createPrimitive(i.getKey(), getLocation(check, insideMethod));
     1028                final OsmPrimitive p = OsmUtils.createPrimitive(i.getKey(), getLocation(check, insideMethod), true);
    10271029                // Build minimal ordered list of checks to run to test the assertion
    10281030                List<Set<TagCheck>> checksToRun = new ArrayList<>();
     
    10331035                checksToRun.add(Collections.singleton(check));
    10341036                // Add primitive to dataset to avoid DataIntegrityProblemException when evaluating selectors
    1035                 ds.addPrimitive(p);
     1037                addPrimitive(ds, p);
    10361038                final Collection<TestError> pErrors = getErrorsForPrimitive(p, true, checksToRun);
    10371039                Logging.debug("- Errors: {0}", pErrors);
     
    10471049        }
    10481050        return assertionErrors;
     1051    }
     1052
     1053    private static void addPrimitive(DataSet ds, OsmPrimitive p) {
     1054        if (p instanceof Way) {
     1055            ((Way) p).getNodes().forEach(n -> addPrimitive(ds, n));
     1056        } else if (p instanceof Relation) {
     1057            ((Relation) p).getMembers().forEach(m -> addPrimitive(ds, m.getMember()));
     1058        }
     1059        ds.addPrimitive(p);
    10491060    }
    10501061
Note: See TracChangeset for help on using the changeset viewer.