Changeset 17336 in josm


Ignore:
Timestamp:
2020-11-23T19:59:45+01:00 (5 months ago)
Author:
GerdP
Message:

fix #20037: Overpass query wizard generates wrong query with "*=value"

  • remove obsolete getInstance()
  • fix bug and add unit test
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java

    r16359 r17336  
    9191    private Optional<String> tryParseSearchTerm(String searchTerm) {
    9292        try {
    93             return Optional.of(SearchCompilerQueryWizard.getInstance().constructQuery(searchTerm));
     93            return Optional.of(SearchCompilerQueryWizard.constructQuery(searchTerm));
    9494        } catch (UncheckedParseException | IllegalStateException ex) {
    9595            Logging.error(ex);
  • trunk/src/org/openstreetmap/josm/tools/SearchCompilerQueryWizard.java

    r16358 r17336  
    2626public final class SearchCompilerQueryWizard {
    2727
    28     private static final SearchCompilerQueryWizard instance = new SearchCompilerQueryWizard();
    29 
    30     /**
    31      * Replies the unique instance of this class.
    32      *
    33      * @return the unique instance of this class
    34      */
    35     public static SearchCompilerQueryWizard getInstance() {
    36         return instance;
    37     }
    38 
    3928    private SearchCompilerQueryWizard() {
    4029        // private constructor for utility class
     
    4736     * @throws UncheckedParseException when the parsing fails
    4837     */
    49     public String constructQuery(final String search) {
     38    public static String constructQuery(final String search) {
    5039        try {
    5140            Matcher matcher = Pattern.compile("\\s+GLOBAL\\s*$", Pattern.CASE_INSENSITIVE).matcher(search);
     
    7463                }
    7564            }
    76            
     65
    7766            final Match match = SearchCompiler.compile(search);
    7867            return constructQuery(match, "[bbox:{{bbox}}];", "");
     
    8271    }
    8372
    84     private String constructQuery(final Match match, final String bounds, final String queryLineSuffix) {
     73    private static String constructQuery(final Match match, final String bounds, final String queryLineSuffix) {
    8574        final List<Match> normalized = normalizeToDNF(match);
    8675        final List<String> queryLines = new ArrayList<>();
     
    136125                case EXACT:
    137126                    return "[" + quote(key) + (negated ? "!=" : "=") + quote(value) + "]";
     127                case ANY_KEY: // *=value
     128                    // fall through
    138129                case EXACT_REGEXP:
    139130                    final Matcher matcher = Pattern.compile("/(?<regex>.*)/(?<flags>i)?").matcher(value);
     
    141132                            ? quote(matcher.group("regex")) + Optional.ofNullable(matcher.group("flags")).map(f -> "," + f).orElse("")
    142133                            : quote(value);
     134                    if (mode == SearchCompiler.ExactKeyValue.Mode.ANY_KEY)
     135                        return "[~\"^.*$\"" + (negated ? "!~" : "~") + valueQuery + "]";
    143136                    return "[" + quote(key) + (negated ? "!~" : "~") + valueQuery + "]";
    144137                case MISSING_KEY:
  • trunk/test/unit/org/openstreetmap/josm/io/OverpassDownloadReaderTest.java

    r17333 r17336  
    5959
    6060    private String getExpandedQuery(String search) {
    61         final String query = SearchCompilerQueryWizard.getInstance().constructQuery(search);
     61        final String query = SearchCompilerQueryWizard.constructQuery(search);
    6262        final String request = new OverpassDownloadReader(new Bounds(1, 2, 3, 4), null, query)
    6363                .getRequestForBbox(1, 2, 3, 4)
  • trunk/test/unit/org/openstreetmap/josm/tools/SearchCompilerQueryWizardTest.java

    r17275 r17336  
    2424
    2525    private static String constructQuery(String s) {
    26         return SearchCompilerQueryWizard.getInstance().constructQuery(s);
     26        return SearchCompilerQueryWizard.constructQuery(s);
    2727    }
    2828
     
    241241                "type:relation and type=multipolygon and -landuse=* and -\"area:highway\"=*");
    242242    }
     243
     244    /**
     245     * Test for ticket <a href="https://josm.openstreetmap.de/ticket/20037>#20037</a>
     246     */
     247    @Test
     248    void testTicket20037() {
     249        assertQueryEquals(
     250                "  node[~\"^.*$\"~\"forward\"];\n" +
     251                "  node[~\"^.*$\"~\"backward\"];\n",
     252                "type:node AND (*=forward OR *=backward)");
     253    }
    243254}
Note: See TracChangeset for help on using the changeset viewer.