Changeset 12464 in josm for trunk/test


Ignore:
Timestamp:
2017-07-10T23:12:16+02:00 (2 years ago)
Author:
michael2402
Message:

Apply #14923: Adjust the search dialog to allow to search for primitives that use a preset. Patch by bafonins

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/actions/search/SearchCompilerTest.java

    r11978 r12464  
    88import static org.junit.Assert.fail;
    99
     10import java.lang.reflect.Field;
    1011import java.nio.charset.StandardCharsets;
    1112import java.nio.file.Files;
    1213import java.nio.file.Paths;
     14import java.util.Arrays;
     15import java.util.Collection;
     16import java.util.Collections;
    1317
    1418import org.junit.Rule;
     
    3135import org.openstreetmap.josm.data.osm.Way;
    3236import org.openstreetmap.josm.data.osm.WayData;
     37import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
     38import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType;
     39import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
     40import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetMenu;
     41import org.openstreetmap.josm.gui.tagging.presets.items.Key;
    3342import org.openstreetmap.josm.testutils.JOSMTestRules;
    3443import org.openstreetmap.josm.tools.date.DateUtils;
     
    491500        TestUtils.superficialEnumCodeCoverage(ExactKeyValue.Mode.class);
    492501    }
     502
     503    /**
     504     * Robustness test for preset searching. Ensures that the query 'preset:' is not accepted.
     505     * @throws ParseError always
     506     * @since 12464
     507     */
     508    @Test(expected = ParseError.class)
     509    public void testPresetSearchMissingValue() throws ParseError {
     510        SearchSetting settings = new SearchSetting();
     511        settings.text = "preset:";
     512        settings.mapCSSSearch = false;
     513
     514        TaggingPresets.readFromPreferences();
     515
     516        SearchCompiler.compile(settings);
     517    }
     518
     519    /**
     520     * Robustness test for preset searching. Validates that it is not possible to search for
     521     * non existing presets.
     522     * @throws ParseError always
     523     * @since 12464
     524     */
     525    @Test(expected = ParseError.class)
     526    public void testPresetNotExist() throws ParseError {
     527        String testPresetName = "groupnamethatshouldnotexist/namethatshouldnotexist";
     528        SearchSetting settings = new SearchSetting();
     529        settings.text = "preset:" + testPresetName;
     530        settings.mapCSSSearch = false;
     531
     532        // load presets
     533        TaggingPresets.readFromPreferences();
     534
     535        SearchCompiler.compile(settings);
     536    }
     537
     538    /**
     539     * Robustness tests for preset searching. Ensures that combined preset names (having more than
     540     * 1 word) must be enclosed with " .
     541     * @throws ParseError always
     542     * @since 12464
     543     */
     544    @Test(expected = ParseError.class)
     545    public void testPresetMultipleWords() throws ParseError {
     546        TaggingPreset testPreset = new TaggingPreset();
     547        testPreset.name = "Test Combined Preset Name";
     548        testPreset.group = new TaggingPresetMenu();
     549        testPreset.group.name = "TestGroupName";
     550
     551        String combinedPresetname = testPreset.getRawName();
     552        SearchSetting settings = new SearchSetting();
     553        settings.text = "preset:" + combinedPresetname;
     554        settings.mapCSSSearch = false;
     555
     556        // load presets
     557        TaggingPresets.readFromPreferences();
     558
     559        SearchCompiler.compile(settings);
     560    }
     561
     562
     563    /**
     564     * Ensures that correct presets are stored in the {@link org.openstreetmap.josm.actions.search.SearchCompiler.Preset}
     565     * class against which the osm primitives are tested.
     566     * @throws ParseError if an error has been encountered while compiling
     567     * @throws NoSuchFieldException if there is no field called 'presets'
     568     * @throws IllegalAccessException if cannot access the field where all matching presets are stored
     569     * @since 12464
     570     */
     571    @Test
     572    public void testPresetLookup() throws ParseError, NoSuchFieldException, IllegalAccessException {
     573        TaggingPreset testPreset = new TaggingPreset();
     574        testPreset.name = "Test Preset Name";
     575        testPreset.group = new TaggingPresetMenu();
     576        testPreset.group.name = "Test Preset Group Name";
     577
     578        String query = "preset:" +
     579                "\"" + testPreset.getRawName() + "\"";
     580        SearchSetting settings = new SearchSetting();
     581        settings.text = query;
     582        settings.mapCSSSearch = false;
     583
     584        // load presets and add the test preset
     585        TaggingPresets.readFromPreferences();
     586        TaggingPresets.addTaggingPresets(Collections.singletonList(testPreset));
     587
     588        Match match = SearchCompiler.compile(settings);
     589
     590        // access the private field where all matching presets are stored
     591        // and ensure that indeed the correct ones are there
     592        Field field = match.getClass().getDeclaredField("presets");
     593        field.setAccessible(true);
     594        Collection<TaggingPreset> foundPresets = (Collection<TaggingPreset>) field.get(match);
     595
     596        assertEquals(1, foundPresets.size());
     597        assertTrue(foundPresets.contains(testPreset));
     598    }
     599
     600    /**
     601     * Ensures that the wildcard search works and that correct presets are stored in
     602     * the {@link org.openstreetmap.josm.actions.search.SearchCompiler.Preset} class against which
     603     * the osm primitives are tested.
     604     * @throws ParseError if an error has been encountered while compiling
     605     * @throws NoSuchFieldException if there is no field called 'presets'
     606     * @throws IllegalAccessException if cannot access the field where all matching presets are stored
     607     * @since 12464
     608     */
     609    @Test
     610    public void testPresetLookupWildcard() throws ParseError, NoSuchFieldException, IllegalAccessException {
     611        TaggingPresetMenu group = new TaggingPresetMenu();
     612        group.name = "TestPresetGroup";
     613
     614        TaggingPreset testPreset1 = new TaggingPreset();
     615        testPreset1.name = "TestPreset1";
     616        testPreset1.group = group;
     617
     618        TaggingPreset testPreset2 = new TaggingPreset();
     619        testPreset2.name = "TestPreset2";
     620        testPreset2.group = group;
     621
     622        TaggingPreset testPreset3 = new TaggingPreset();
     623        testPreset3.name = "TestPreset3";
     624        testPreset3.group = group;
     625
     626        String query = "preset:" + "\"" + group.getRawName() + "/*\"";
     627        SearchSetting settings = new SearchSetting();
     628        settings.text = query;
     629        settings.mapCSSSearch = false;
     630
     631        TaggingPresets.readFromPreferences();
     632        TaggingPresets.addTaggingPresets(Arrays.asList(testPreset1, testPreset2, testPreset3));
     633
     634        Match match = SearchCompiler.compile(settings);
     635
     636        // access the private field where all matching presets are stored
     637        // and ensure that indeed the correct ones are there
     638        Field field = match.getClass().getDeclaredField("presets");
     639        field.setAccessible(true);
     640        Collection<TaggingPreset> foundPresets = (Collection<TaggingPreset>) field.get(match);
     641
     642        assertEquals(3, foundPresets.size());
     643        assertTrue(foundPresets.contains(testPreset1));
     644        assertTrue(foundPresets.contains(testPreset2));
     645        assertTrue(foundPresets.contains(testPreset3));
     646    }
     647
     648    /**
     649     * Ensures that correct primitives are matched against the specified preset.
     650     * @throws ParseError if an error has been encountered while compiling
     651     * @since 12464
     652     */
     653    @Test
     654    public void testPreset() throws ParseError {
     655        final String presetName = "Test Preset Name";
     656        final String presetGroupName = "Test Preset Group";
     657        final String key = "test_key1";
     658        final String val = "test_val1";
     659
     660        Key key1 = new Key();
     661        key1.key = key;
     662        key1.value = val;
     663
     664        TaggingPreset testPreset = new TaggingPreset();
     665        testPreset.name = presetName;
     666        testPreset.types = Collections.singleton(TaggingPresetType.NODE);
     667        testPreset.data.add(key1);
     668        testPreset.group = new TaggingPresetMenu();
     669        testPreset.group.name = presetGroupName;
     670
     671        TaggingPresets.readFromPreferences();
     672        TaggingPresets.addTaggingPresets(Collections.singleton(testPreset));
     673
     674        String query = "preset:" + "\"" + testPreset.getRawName() + "\"";
     675
     676        SearchContext ctx = new SearchContext(query);
     677        ctx.n1.put(key, val);
     678        ctx.n2.put(key, val);
     679
     680        for (OsmPrimitive osm : new OsmPrimitive[] {ctx.n1, ctx.n2}) {
     681            ctx.match(osm, true);
     682        }
     683
     684        for (OsmPrimitive osm : new OsmPrimitive[] {ctx.r1, ctx.r2, ctx.w1, ctx.w2}) {
     685            ctx.match(osm, false);
     686        }
     687    }
    493688}
     689
Note: See TracChangeset for help on using the changeset viewer.