Changeset 10067 in josm


Ignore:
Timestamp:
2016-03-28T14:23:40+02:00 (8 years ago)
Author:
Don-vip
Message:

see #12652 - rework bug report classes to improve unit tests coverage

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/bugreport/BugReportExceptionHandler.java

    r10064 r10067  
    55
    66import java.awt.Component;
     7import java.awt.GraphicsEnvironment;
    78import java.awt.GridBagConstraints;
    89import java.awt.GridBagLayout;
     
    4445    private static boolean suppressExceptionDialogs;
    4546
    46     private static class BugReporterThread extends Thread {
     47    static final class BugReporterThread extends Thread {
    4748
    4849        private final class BugReporterWorker implements Runnable {
     
    5758                // Then ask for submitting a bug report, for exceptions thrown from a plugin too, unless updated to a new version
    5859                if (pluginDownloadTask == null) {
    59                     String[] buttonTexts = new String[] {tr("Do nothing"), tr("Report Bug")};
    60                     String[] buttonIcons = new String[] {"cancel", "bug"};
    61                     int defaultButtonIdx = 1;
    62                     String message = tr("An unexpected exception occurred.<br>" +
    63                             "This is always a coding error. If you are running the latest<br>" +
    64                             "version of JOSM, please consider being kind and file a bug report."
    65                             );
    66                     // Check user is running current tested version, the error may already be fixed
    67                     int josmVersion = Version.getInstance().getVersion();
    68                     if (josmVersion != Version.JOSM_UNKNOWN_VERSION) {
    69                         try {
    70                             int latestVersion = Integer.parseInt(new WikiReader().
    71                                     read(Main.getJOSMWebsite()+"/wiki/TestedVersion?format=txt").trim());
    72                             if (latestVersion > josmVersion) {
    73                                 buttonTexts = new String[] {tr("Do nothing"), tr("Update JOSM"), tr("Report Bug")};
    74                                 buttonIcons = new String[] {"cancel", "download", "bug"};
    75                                 defaultButtonIdx = 2;
    76                                 message = tr("An unexpected exception occurred. This is always a coding error.<br><br>" +
    77                                         "However, you are running an old version of JOSM ({0}),<br>" +
    78                                         "instead of using the current tested version (<b>{1}</b>).<br><br>"+
    79                                         "<b>Please update JOSM</b> before considering to file a bug report.",
    80                                         String.valueOf(josmVersion), String.valueOf(latestVersion));
    81                             }
    82                         } catch (IOException | NumberFormatException e) {
    83                             Main.warn("Unable to detect latest version of JOSM: "+e.getMessage());
    84                         }
    85                     }
    86                     // Show dialog
    87                     ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Unexpected Exception"), buttonTexts);
    88                     ed.setButtonIcons(buttonIcons);
    89                     ed.setIcon(JOptionPane.ERROR_MESSAGE);
    90                     ed.setCancelButton(1);
    91                     ed.setDefaultButton(defaultButtonIdx);
    92                     JPanel pnl = new JPanel(new GridBagLayout());
    93                     pnl.add(new JLabel("<html>" + message + "</html>"), GBC.eol());
    94                     JCheckBox cbSuppress = null;
    95                     if (exceptionCounter > 1) {
    96                         cbSuppress = new JCheckBox(tr("Suppress further error dialogs for this session."));
    97                         pnl.add(cbSuppress, GBC.eol());
    98                     }
    99                     ed.setContent(pnl);
    100                     ed.setFocusOnDefaultButton(true);
    101                     ed.showDialog();
    102                     if (cbSuppress != null && cbSuppress.isSelected()) {
    103                         suppressExceptionDialogs = true;
    104                     }
    105                     if (ed.getValue() <= 1) {
    106                         // "Do nothing"
    107                         return;
    108                     } else if (ed.getValue() < buttonTexts.length) {
    109                         // "Update JOSM"
    110                         try {
    111                             Main.platform.openUrl(Main.getJOSMWebsite());
    112                         } catch (IOException e) {
    113                             Main.warn("Unable to access JOSM website: "+e.getMessage());
    114                         }
    115                     } else {
    116                         // "Report bug"
    117                         askForBugReport(e);
    118                     }
     60                    askForBugReport(e);
    11961                } else {
    12062                    // Ask for restart to install new plugin
     
    13173         * @param t the exception
    13274         */
    133         BugReporterThread(Throwable t) {
     75        private BugReporterThread(Throwable t) {
    13476            super("Bug Reporter");
    13577            this.e = t;
     78        }
     79
     80        static void askForBugReport(final Throwable e) {
     81            String[] buttonTexts = new String[] {tr("Do nothing"), tr("Report Bug")};
     82            String[] buttonIcons = new String[] {"cancel", "bug"};
     83            int defaultButtonIdx = 1;
     84            String message = tr("An unexpected exception occurred.<br>" +
     85                    "This is always a coding error. If you are running the latest<br>" +
     86                    "version of JOSM, please consider being kind and file a bug report."
     87                    );
     88            // Check user is running current tested version, the error may already be fixed
     89            int josmVersion = Version.getInstance().getVersion();
     90            if (josmVersion != Version.JOSM_UNKNOWN_VERSION) {
     91                try {
     92                    int latestVersion = Integer.parseInt(new WikiReader().
     93                            read(Main.getJOSMWebsite()+"/wiki/TestedVersion?format=txt").trim());
     94                    if (latestVersion > josmVersion) {
     95                        buttonTexts = new String[] {tr("Do nothing"), tr("Update JOSM"), tr("Report Bug")};
     96                        buttonIcons = new String[] {"cancel", "download", "bug"};
     97                        defaultButtonIdx = 2;
     98                        message = tr("An unexpected exception occurred. This is always a coding error.<br><br>" +
     99                                "However, you are running an old version of JOSM ({0}),<br>" +
     100                                "instead of using the current tested version (<b>{1}</b>).<br><br>"+
     101                                "<b>Please update JOSM</b> before considering to file a bug report.",
     102                                String.valueOf(josmVersion), String.valueOf(latestVersion));
     103                    }
     104                } catch (IOException | NumberFormatException ex) {
     105                    Main.warn("Unable to detect latest version of JOSM: "+ex.getMessage());
     106                }
     107            }
     108            // Build panel
     109            JPanel pnl = new JPanel(new GridBagLayout());
     110            pnl.add(new JLabel("<html>" + message + "</html>"), GBC.eol());
     111            JCheckBox cbSuppress = null;
     112            if (exceptionCounter > 1) {
     113                cbSuppress = new JCheckBox(tr("Suppress further error dialogs for this session."));
     114                pnl.add(cbSuppress, GBC.eol());
     115            }
     116            if (GraphicsEnvironment.isHeadless()) {
     117                return;
     118            }
     119            // Show dialog
     120            ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Unexpected Exception"), buttonTexts);
     121            ed.setButtonIcons(buttonIcons);
     122            ed.setIcon(JOptionPane.ERROR_MESSAGE);
     123            ed.setCancelButton(1);
     124            ed.setDefaultButton(defaultButtonIdx);
     125            ed.setContent(pnl);
     126            ed.setFocusOnDefaultButton(true);
     127            ed.showDialog();
     128            if (cbSuppress != null && cbSuppress.isSelected()) {
     129                suppressExceptionDialogs = true;
     130            }
     131            if (ed.getValue() <= 1) {
     132                // "Do nothing"
     133                return;
     134            } else if (ed.getValue() < buttonTexts.length) {
     135                // "Update JOSM"
     136                try {
     137                    Main.platform.openUrl(Main.getJOSMWebsite());
     138                } catch (IOException ex) {
     139                    Main.warn("Unable to access JOSM website: "+ex.getMessage());
     140                }
     141            } else {
     142                // "Report bug"
     143                try {
     144                    JPanel p = buildPanel(e);
     145                    JOptionPane.showMessageDialog(Main.parent, p, tr("You have encountered a bug in JOSM"), JOptionPane.ERROR_MESSAGE);
     146                } catch (Exception ex) {
     147                    Main.error(ex);
     148                }
     149            }
    136150        }
    137151
     
    182196    }
    183197
    184     private static void askForBugReport(final Throwable e) {
    185         try {
    186             JPanel p = buildPanel(e);
    187             JOptionPane.showMessageDialog(Main.parent, p, tr("You have encountered a bug in JOSM"), JOptionPane.ERROR_MESSAGE);
    188         } catch (Exception e1) {
    189             Main.error(e1);
    190         }
    191     }
    192 
    193198    static JPanel buildPanel(final Throwable e) {
    194199        StringWriter stack = new StringWriter();
  • trunk/src/org/openstreetmap/josm/tools/bugreport/BugReportSender.java

    r10055 r10067  
    4949
    5050    private final String statusText;
     51    private String errorMessage;
    5152
    5253    /**
     
    5455     * @param statusText The status text to send.
    5556     */
    56     public BugReportSender(String statusText) {
     57    protected BugReportSender(String statusText) {
    5758        super("Bug report sender");
    5859        this.statusText = statusText;
     
    101102                DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    102103                Document document = builder.parse(in);
    103                 return retriveDebugToken(document);
     104                return retrieveDebugToken(document);
    104105            }
    105106        } catch (IOException | SAXException | ParserConfigurationException | XPathExpressionException t) {
     
    108109    }
    109110
    110     private String getJOSMTicketURL() {
     111    private static String getJOSMTicketURL() {
    111112        return Main.getJOSMWebsite() + "/josmticket";
    112113    }
    113114
    114     private String retriveDebugToken(Document document) throws XPathExpressionException, BugReportSenderException {
     115    private static String retrieveDebugToken(Document document) throws XPathExpressionException, BugReportSenderException {
    115116        XPathFactory factory = XPathFactory.newInstance();
    116117        XPath xpath = factory.newXPath();
     
    134135
    135136    private void failed(String string) {
     137        errorMessage = string;
    136138        SwingUtilities.invokeLater(new Runnable() {
    137139            @Override
    138140            public void run() {
    139                 JPanel errorPanel = new JPanel();
    140                 errorPanel.setLayout(new GridBagLayout());
     141                JPanel errorPanel = new JPanel(new GridBagLayout());
    141142                errorPanel.add(new JMultilineLabel(
    142143                        tr("Opening the bug report failed. Please report manually using this website:")),
     
    149150            }
    150151        });
     152    }
     153
     154    /**
     155     * Returns the error message that could have occured during bug sending.
     156     * @return the error message, or {@code null} if successful
     157     */
     158    public final String getErrorMessage() {
     159        return errorMessage;
    151160    }
    152161
     
    164173     * Opens the bug report window on the JOSM server.
    165174     * @param statusText The status text to send along to the server.
     175     * @return bug report sender started thread
    166176     */
    167     public static void reportBug(String statusText) {
    168         new BugReportSender(statusText).start();
     177    public static BugReportSender reportBug(String statusText) {
     178        BugReportSender sender = new BugReportSender(statusText);
     179        sender.start();
     180        return sender;
    169181    }
    170182}
  • trunk/test/unit/org/openstreetmap/josm/tools/bugreport/BugReportExceptionHandlerTest.java

    r10064 r10067  
    22package org.openstreetmap.josm.tools.bugreport;
    33
     4import static org.junit.Assert.assertFalse;
    45import static org.junit.Assert.assertNotNull;
    56
     
    1819    @Before
    1920    public void setUp() {
    20         JOSMFixture.createUnitTestFixture().init();
     21        JOSMFixture.createUnitTestFixture().init(true);
    2122    }
    2223
     
    2627    @Test
    2728    public void testBuildPanel() {
    28         assertNotNull(BugReportExceptionHandler.buildPanel(new Exception()));
     29        assertNotNull(BugReportExceptionHandler.buildPanel(new Exception("testBuildPanel")));
     30    }
     31
     32    /**
     33     * Unit test for {@link BugReportExceptionHandler.BugReporterThread#askForBugReport} method.
     34     */
     35    @Test
     36    public void testAskForBugReport() {
     37        BugReportExceptionHandler.BugReporterThread.askForBugReport(new Exception("testAskForBugReport"));
     38    }
     39
     40    /**
     41     * Unit test for {@link BugReportExceptionHandler#handleException} method.
     42     */
     43    @Test
     44    public void testHandleException() {
     45        BugReportExceptionHandler.handleException(new Exception("testHandleException"));
     46        assertFalse(BugReportExceptionHandler.exceptionHandlingInProgress());
    2947    }
    3048}
Note: See TracChangeset for help on using the changeset viewer.