Changeset 18491 in josm


Ignore:
Timestamp:
2022-06-13T23:40:17+02:00 (22 months ago)
Author:
taylor.smock
Message:

Fix #20823: Reject uploads that do not follow either comment policy or source policy (patch by ljdelight)

This fixes an issue where a the length of the comment or source was checked first,
and a warning instead of a rejection was generated when the upload policy would
have otherwise rejected the comment or source.

This also performs the validation check first, prior to the user being able to
make changes in the upload dialog UI, so that the initial dialog state matches
the upload policy.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/UploadDialog.java

    r18472 r18491  
    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(
  • trunk/src/org/openstreetmap/josm/gui/io/UploadTextComponentValidator.java

    r17752 r18491  
    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 />" +
     
    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        }
     
    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 />" +
     
    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        }
  • trunk/test/unit/org/openstreetmap/josm/gui/io/UploadTextComponentValidatorTest.java

    r18037 r18491  
    55import static org.hamcrest.MatcherAssert.assertThat;
    66
     7import java.util.Arrays;
     8import java.util.function.BiFunction;
     9import java.util.stream.Stream;
     10
    711import javax.swing.JLabel;
    812import javax.swing.JTextField;
    913
    1014import org.junit.jupiter.api.Test;
     15import org.junit.jupiter.params.ParameterizedTest;
     16import org.junit.jupiter.params.provider.Arguments;
     17import org.junit.jupiter.params.provider.MethodSource;
     18import org.openstreetmap.josm.spi.preferences.Config;
    1119import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
    1220
     
    4048        assertThat(feedback.getText(), containsString("Thank you for providing the data source"));
    4149    }
     50
     51    static Stream<Arguments> testUploadWithMandatoryTerm() {
     52        return Stream.of(Arguments.of("upload.comment.mandatory-terms", "Thank you for providing a changeset comment",
     53                    (BiFunction<JTextField, JLabel, ? extends UploadTextComponentValidator>)
     54                            UploadTextComponentValidator.UploadCommentValidator::new),
     55                Arguments.of("upload.source.mandatory-terms", "Thank you for providing the data source",
     56                    (BiFunction<JTextField, JLabel, ? extends UploadTextComponentValidator>)
     57                            UploadTextComponentValidator.UploadSourceValidator::new)
     58        );
     59    }
     60
     61    /**
     62     * Unit test of {@link UploadTextComponentValidator.UploadCommentValidator} and
     63     * {@link UploadTextComponentValidator.UploadSourceValidator} with mandatory terms
     64     */
     65    @BasicPreferences
     66    @ParameterizedTest
     67    @MethodSource
     68    void testUploadWithMandatoryTerm(String confPref, String expectedText,
     69            BiFunction<JTextField, JLabel, ? extends UploadTextComponentValidator> validatorSupplier) {
     70        Config.getPref().putList(confPref, Arrays.asList("myrequired", "xyz"));
     71        JTextField textField = new JTextField("");
     72        JLabel feedback = new JLabel();
     73
     74        validatorSupplier.apply(textField, feedback);
     75
     76        // A too-short string should fail validation
     77        textField.setText("");
     78        assertThat(feedback.getText(), containsString("The following required terms are missing: [myrequired, xyz]"));
     79
     80        // A long enough string without the mandatory terms should claim that the required terms are missing
     81        textField.setText("a string long enough but missing the mandatory term");
     82        assertThat(feedback.getText(), containsString("The following required terms are missing: [myrequired, xyz]"));
     83
     84        // A valid string should pass
     85        textField.setText("a string long enough with the mandatory term #myrequired #xyz");
     86        assertThat(feedback.getText(), containsString(expectedText));
     87    }
    4288}
  • trunk/test/unit/org/openstreetmap/josm/testutils/annotations/BasicPreferences.java

    r18037 r18491  
    5757            // Disable saving on put, just to avoid overwriting pref files
    5858            pref.enableSaveOnPut(false);
     59            pref.resetToDefault();
    5960            Config.setPreferencesInstance(pref);
    6061            Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance());
Note: See TracChangeset for help on using the changeset viewer.