1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.EnumSet;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.junit.BeforeClass;
|
---|
14 | import org.junit.Test;
|
---|
15 | import org.openstreetmap.josm.JOSMFixture;
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
19 | import org.openstreetmap.josm.data.osm.Way;
|
---|
20 | import org.openstreetmap.josm.tools.Utils;
|
---|
21 | import org.xml.sax.SAXException;
|
---|
22 |
|
---|
23 | public class PresetClassificationsTest {
|
---|
24 |
|
---|
25 | final static TaggingPresetSelector.PresetClassifications classifications = new TaggingPresetSelector.PresetClassifications();
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Setup test.
|
---|
29 | */
|
---|
30 | @BeforeClass
|
---|
31 | public static void setUp() throws IOException, SAXException {
|
---|
32 | JOSMFixture.createUnitTestFixture().init();
|
---|
33 | final Collection<TaggingPreset> presets = TaggingPresetReader.readAll("resource://data/defaultpresets.xml", true);
|
---|
34 | classifications.loadPresets(presets);
|
---|
35 | }
|
---|
36 |
|
---|
37 | private List<TaggingPresetSelector.PresetClassification> getMatchingPresets(String searchText, OsmPrimitive w) {
|
---|
38 | return classifications.getMatchingPresets(searchText, true, true, EnumSet.of(TaggingPresetType.forPrimitive(w)), Collections.singleton(w));
|
---|
39 | }
|
---|
40 |
|
---|
41 | private List<String> getMatchingPresetNames(String searchText, OsmPrimitive w) {
|
---|
42 | return Utils.transform(getMatchingPresets(searchText, w), new Utils.Function<TaggingPresetSelector.PresetClassification, String>() {
|
---|
43 | @Override
|
---|
44 | public String apply(TaggingPresetSelector.PresetClassification x) {
|
---|
45 | return x.preset.name;
|
---|
46 | }
|
---|
47 | });
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Test
|
---|
51 | public void testBuilding() throws Exception {
|
---|
52 | final Way w = new Way();
|
---|
53 | final Node n1 = new Node();
|
---|
54 | w.addNode(n1);
|
---|
55 | w.addNode(new Node());
|
---|
56 | w.addNode(new Node());
|
---|
57 | assertFalse("unclosed way should not match building preset", getMatchingPresetNames("building", w).contains("Building"));
|
---|
58 | w.addNode(n1);
|
---|
59 | assertTrue("closed way should match building preset", getMatchingPresetNames("building", w).contains("Building"));
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Test
|
---|
63 | public void testRelationsForTram() {
|
---|
64 | final OsmPrimitive tram = OsmUtils.createPrimitive("way railway=tram");
|
---|
65 | assertTrue("railway=tram should match 'Railway Route' for relation creation", getMatchingPresetNames("route", tram).contains("Railway Route"));
|
---|
66 | assertTrue("railway=tram should match 'Public Transport Route' for relation creation", getMatchingPresetNames("route", tram).contains("Public Transport Route"));
|
---|
67 | assertFalse("railway=tram should not match 'Bus route'", getMatchingPresetNames("route", tram).contains("Bus route"));
|
---|
68 | }
|
---|
69 | }
|
---|