Ticket #20823: patch-20823-3.patch

File patch-20823-3.patch, 9.1 KB (added by ljdelight, 4 years ago)

Rebased to trunk and added unit test for this case

  • src/org/openstreetmap/josm/gui/io/UploadDialog.java

    diff --git src/org/openstreetmap/josm/gui/io/UploadDialog.java src/org/openstreetmap/josm/gui/io/UploadDialog.java
    index 449ed0bd0..19b2242a6 100644
    public class UploadDialog extends AbstractUploadDialog implements PreferenceChan  
    221221                () -> tpConfigPanels.setSelectedIndex(2)
    222222        );
    223223
     224        // Set the initial state of the upload button
     225        btnUpload.setEnabled(pnlBasicUploadSettings.getUploadTextValidators()
     226                .stream().noneMatch(UploadTextComponentValidator::isUploadRejected));
     227
    224228        // Enable/disable the upload button if at least an upload validator rejects upload
    225229        pnlBasicUploadSettings.getUploadTextValidators().forEach(v -> v.addChangeListener(e -> btnUpload.setEnabled(
    226230                pnlBasicUploadSettings.getUploadTextValidators().stream().noneMatch(UploadTextComponentValidator::isUploadRejected))));
  • src/org/openstreetmap/josm/gui/io/UploadTextComponentValidator.java

    diff --git src/org/openstreetmap/josm/gui/io/UploadTextComponentValidator.java src/org/openstreetmap/josm/gui/io/UploadTextComponentValidator.java
    index 5d22d2b66..85cb959d2 100644
    abstract class UploadTextComponentValidator extends AbstractTextComponentValidat  
    8383                feedbackDisabled();
    8484                return;
    8585            }
    86             String uploadComment = getComponent().getText();
    87             if (UploadDialog.UploadAction.isUploadCommentTooShort(uploadComment)) {
     86            final String uploadComment = getComponent().getText();
     87            final String rejection = UploadDialog.UploadAction.validateUploadTag(uploadComment, "upload.comment",
     88                    Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
     89
     90            // Reject the upload if tags are required and are not in the input. If the tags exist or are not
     91            // required, then check the length of the input and warn if it's too short (a short msg is not a rejection)
     92            uploadRejected = rejection != null;
     93
     94            if (uploadRejected) {
     95                feedbackWarning(tr("Your upload comment is <i>rejected</i>.") + "<br />" + rejection);
     96            } else if (UploadDialog.UploadAction.isUploadCommentTooShort(uploadComment)) {
    8897                feedbackWarning(tr("Your upload comment is <i>empty</i>, or <i>very short</i>.<br /><br />" +
    8998                        "This is technically allowed, but please consider that many users who are<br />" +
    9099                        "watching changes in their area depend on meaningful changeset comments<br />" +
    abstract class UploadTextComponentValidator extends AbstractTextComponentValidat  
    92101                        "If you spend a minute now to explain your change, you will make life<br />" +
    93102                        "easier for many other mappers.").replace("<br />", " "));
    94103            } else {
    95                 String rejection = UploadDialog.UploadAction.validateUploadTag(uploadComment, "upload.comment",
    96                         Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
    97                 uploadRejected = rejection != null;
    98                 if (uploadRejected) {
    99                     feedbackWarning(tr("Your upload comment is <i>rejected</i>.") + "<br />" + rejection);
    100                 } else {
    101                     feedbackValid(tr("Thank you for providing a changeset comment! " +
    102                             "This gives other mappers a better understanding of your intent."));
    103                 }
     104                feedbackValid(tr("Thank you for providing a changeset comment! " +
     105                        "This gives other mappers a better understanding of your intent."));
    104106            }
    105107        }
    106108    }
    abstract class UploadTextComponentValidator extends AbstractTextComponentValidat  
    120122                feedbackDisabled();
    121123                return;
    122124            }
    123             String uploadSource = getComponent().getText();
    124             if (Utils.isStripEmpty(uploadSource)) {
     125            final String uploadSource = getComponent().getText();
     126            final String rejection = UploadDialog.UploadAction.validateUploadTag(
     127                    uploadSource, "upload.source", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
     128
     129            // Reject the upload only if tags are required and are not in the input. If the tags exist or are not
     130            // required, then check the length of the input and warn if it's too short (a short msg is not a rejection)
     131            uploadRejected = rejection != null;
     132
     133            if (uploadRejected) {
     134                feedbackWarning(tr("Your changeset source is <i>rejected</i>.") + "<br />" + rejection);
     135            } else if (Utils.isStripEmpty(uploadSource)) {
    125136                feedbackWarning(tr("You did not specify a source for your changes.<br />" +
    126137                        "It is technically allowed, but this information helps<br />" +
    127138                        "other users to understand the origins of the data.<br /><br />" +
    128139                        "If you spend a minute now to explain your change, you will make life<br />" +
    129140                        "easier for many other mappers.").replace("<br />", " "));
    130141            } else {
    131                 final String rejection = UploadDialog.UploadAction.validateUploadTag(
    132                         uploadSource, "upload.source", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
    133                 uploadRejected = rejection != null;
    134                 if (uploadRejected) {
    135                     feedbackWarning(tr("Your changeset source is <i>rejected</i>.") + "<br />" + rejection);
    136                 } else {
    137                     feedbackValid(tr("Thank you for providing the data source!"));
    138                 }
     142                feedbackValid(tr("Thank you for providing the data source!"));
    139143            }
    140144        }
    141145    }
  • test/unit/org/openstreetmap/josm/gui/io/UploadTextComponentValidatorTest.java

    diff --git test/unit/org/openstreetmap/josm/gui/io/UploadTextComponentValidatorTest.java test/unit/org/openstreetmap/josm/gui/io/UploadTextComponentValidatorTest.java
    index 16b00c90e..f37ca476c 100644
    import javax.swing.JLabel;  
    88import javax.swing.JTextField;
    99
    1010import org.junit.jupiter.api.Test;
     11import org.openstreetmap.josm.spi.preferences.Config;
    1112import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
    1213
     14import java.util.Arrays;
     15import java.util.Collections;
     16
    1317@BasicPreferences
    1418class UploadTextComponentValidatorTest {
    1519    /**
    class UploadTextComponentValidatorTest {  
    3943        textField.setText("a comment long enough");
    4044        assertThat(feedback.getText(), containsString("Thank you for providing the data source"));
    4145    }
     46
     47    /**
     48     * Unit test of {@link UploadTextComponentValidator.UploadCommentValidator} with mandatory terms
     49     */
     50    @Test
     51    void testUploadCommentWithMandatoryTerm() {
     52        testUploadWithMandatoryTermHelper("upload.comment.mandatory-terms");
     53    }
     54
     55
     56    /**
     57     * Unit test of {@link UploadTextComponentValidator.UploadSourceValidator} with mandatory terms
     58     */
     59    @Test
     60    void testUploadSourceWithMandatoryTerm() {
     61        testUploadWithMandatoryTermHelper("upload.source.mandatory-terms");
     62    }
     63
     64    void testUploadWithMandatoryTermHelper(String confPref) {
     65        Config.getPref().putList(confPref, Arrays.asList("myrequired", "xyz"));
     66        JTextField textField = new JTextField("");
     67        JLabel feedback = new JLabel();
     68
     69        if ("upload.comment.mandatory-terms".equalsIgnoreCase(confPref)) {
     70            new UploadTextComponentValidator.UploadCommentValidator(textField, feedback);
     71        } else if ("upload.source.mandatory-terms".equalsIgnoreCase(confPref)) {
     72            new UploadTextComponentValidator.UploadSourceValidator(textField, feedback);
     73        } else {
     74            assertThat("Invalid configuration pref", false);
     75        }
     76
     77        // A too-short string should fail validation
     78        textField.setText("");
     79        assertThat(feedback.getText(), containsString("The following required terms are missing: [myrequired, xyz]"));
     80
     81        // A long enough string without the mandatory terms should claim that the required terms are missing
     82        textField.setText("a string long enough but missing the mandatory term");
     83        assertThat(feedback.getText(), containsString("The following required terms are missing: [myrequired, xyz]"));
     84
     85        // A valid string should pass
     86        textField.setText("a string long enough with the mandatory term #myrequired #xyz");
     87        if ("upload.comment.mandatory-terms".equalsIgnoreCase(confPref)) {
     88            assertThat(feedback.getText(), containsString("Thank you for providing a changeset comment"));
     89        } else {
     90            assertThat(feedback.getText(), containsString("Thank you for providing the data source"));
     91        }
     92
     93        Config.getPref().putList(confPref, Collections.emptyList());
     94    }
    4295}