Ticket #24975: patch-Fix-Sonar-issues-in-HelpContentReader-and-Help.patch

File patch-Fix-Sonar-issues-in-HelpContentReader-and-Help.patch, 3.6 KB (added by DanProgs <Abenteurer.2901@…>, 39 hours ago)
  • src/org/openstreetmap/josm/gui/help/HelpContentReader.java

    ### Eclipse Workspace Patch 1.0
    #P josm
    diff --git src/org/openstreetmap/josm/gui/help/HelpContentReader.java src/org/openstreetmap/josm/gui/help/HelpContentReader.java
    index ce605ca..cebd6e4 100644
     
    44import java.io.BufferedReader;
    55import java.io.IOException;
    66import java.net.MalformedURLException;
     7import java.net.URI;
     8import java.net.URISyntaxException;
    79import java.net.URL;
    810
    911import org.openstreetmap.josm.tools.HttpClient;
    … …  
    3739     * @param dotest if {@code true}, checks if help content is empty
    3840     * @return the content, filtered and transformed for being displayed in the internal help browser
    3941     * @throws HelpContentReaderException if problem occurs
    40      * @throws MissingHelpContentException if this helpTopicUrl doesn't point to an existing Wiki help page
    4142     */
    4243    public String fetchHelpTopicContent(String helpTopicUrl, boolean dotest) throws HelpContentReaderException {
    43         if (helpTopicUrl == null)
    44             throw new MissingHelpContentException("helpTopicUrl is null");
     44        if (helpTopicUrl == null || helpTopicUrl.isBlank()) {
     45            throw new HelpContentReaderException(new IllegalArgumentException("helpTopicUrl is null or blank"), 0);
     46        }
    4547        HttpClient.Response con = null;
    4648        try {
    47             URL u = new URL(helpTopicUrl);
     49            final URL u = new URI(helpTopicUrl).toURL();
    4850            con = HttpClient.create(u).connect();
     51           
    4952            try (BufferedReader in = con.getContentReader()) {
    5053                return prepareHelpContent(in, dotest, u);
    5154            }
    52         } catch (MalformedURLException e) {
     55        } catch (MalformedURLException | URISyntaxException e) {
    5356            throw new HelpContentReaderException(e, 0);
    5457        } catch (IOException e) {
    55             throw new HelpContentReaderException(e, con != null ? con.getResponseCode() : 0);
     58            int responseCode = 0;
     59            if (con != null) {
     60                try {
     61                    responseCode = con.getResponseCode();
     62                } catch (Exception ignored) {
     63                    // do nothing
     64                }
     65            }
     66            throw new HelpContentReaderException(e, responseCode);
     67        } finally {
     68            if (con instanceof AutoCloseable) {
     69                try {
     70                    ((AutoCloseable) con).close();
     71                } catch (Exception ignored) {
     72                    // do nothing
     73                }
     74            }
    5675        }
    5776    }
    5877
    … …  
    7897        } catch (IOException e) {
    7998            throw new HelpContentReaderException(e, 0);
    8099        }
    81         if (dotest && s.isEmpty())
    82             throw new MissingHelpContentException(s);
     100        if (dotest && s.isEmpty()) {
     101            throw new MissingHelpContentException(s);
     102        }
    83103        return s;
    84104    }
    85105}
  • test/unit/org/openstreetmap/josm/gui/help/HelpContentReaderTest.java

    diff --git test/unit/org/openstreetmap/josm/gui/help/HelpContentReaderTest.java test/unit/org/openstreetmap/josm/gui/help/HelpContentReaderTest.java
    index f8f3299..2fc268b 100644
     
    2626     */
    2727    @Test
    2828    void testFetchHelpTopicContentNominal() throws HelpContentReaderException {
    29         String res = new HelpContentReader(HelpUtil.getWikiBaseUrl()).fetchHelpTopicContent(HelpBrowserTest.URL_1, false);
     29        final String res = new HelpContentReader(HelpUtil.getWikiBaseUrl()).fetchHelpTopicContent(HelpBrowserTest.URL_1, false);
    3030        assertFalse(res.isBlank());
    3131    }
    3232}