Ticket #20037: 20037-fix.patch

File 20037-fix.patch, 5.8 KB (added by GerdP, 3 years ago)

refactoring + fix + unit test

  • src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java

     
    9090     */
    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);
    9696            JOptionPane.showMessageDialog(
  • src/org/openstreetmap/josm/tools/SearchCompilerQueryWizard.java

     
    2525 */
    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
    4130    }
     
    4635     * @return an Overpass QL query
    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);
    5241            if (matcher.find()) {
     
    7362                    throw new IllegalStateException(mode);
    7463                }
    7564            }
    76            
     65
    7766            final Match match = SearchCompiler.compile(search);
    7867            return constructQuery(match, "[bbox:{{bbox}}];", "");
    7968        } catch (SearchParseError | UnsupportedOperationException e) {
     
    8170        }
    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<>();
    8776        queryLines.add("[out:xml][timeout:90]" + bounds);
     
    135124                    return "[" + (negated ? "!" : "") + quote(key) + "]";
    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);
    140131                    final String valueQuery = matcher.matches()
    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:
    145138                    // special case for empty values, see https://github.com/drolbr/Overpass-API/issues/53
  • test/unit/org/openstreetmap/josm/io/OverpassDownloadReaderTest.java

     
    5858    }
    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)
    6464                .substring("interpreter?data=".length());
  • test/unit/org/openstreetmap/josm/tools/SearchCompilerQueryWizardTest.java

     
    2323    public JOSMTestRules test = new JOSMTestRules().i18n("de");
    2424
    2525    private static String constructQuery(String s) {
    26         return SearchCompilerQueryWizard.getInstance().constructQuery(s);
     26        return SearchCompilerQueryWizard.constructQuery(s);
    2727    }
    2828
    2929    private void assertQueryEquals(String expectedQueryPart, String input) {
     
    240240        assertQueryEquals("  relation[\"type\"=\"multipolygon\"][!\"landuse\"][!\"area:highway\"];\n",
    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}