1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.hamcrest.CoreMatchers.containsString;
|
---|
5 | import static org.hamcrest.MatcherAssert.assertThat;
|
---|
6 |
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.function.BiFunction;
|
---|
9 | import java.util.stream.Stream;
|
---|
10 |
|
---|
11 | import javax.swing.JLabel;
|
---|
12 | import javax.swing.JTextField;
|
---|
13 |
|
---|
14 | import org.junit.jupiter.api.Test;
|
---|
15 | import org.junit.jupiter.params.ParameterizedTest;
|
---|
16 | import org.junit.jupiter.params.provider.Arguments;
|
---|
17 | import org.junit.jupiter.params.provider.MethodSource;
|
---|
18 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
19 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
20 |
|
---|
21 | @BasicPreferences
|
---|
22 | class UploadTextComponentValidatorTest {
|
---|
23 | /**
|
---|
24 | * Unit test of {@link UploadTextComponentValidator.UploadCommentValidator}
|
---|
25 | */
|
---|
26 | @Test
|
---|
27 | void testUploadCommentValidator() {
|
---|
28 | JTextField textField = new JTextField();
|
---|
29 | JLabel feedback = new JLabel();
|
---|
30 | new UploadTextComponentValidator.UploadCommentValidator(textField, feedback);
|
---|
31 | assertThat(feedback.getText(), containsString("Your upload comment is <i>empty</i>, or <i>very short</i>"));
|
---|
32 | textField.setText("a comment long enough");
|
---|
33 | assertThat(feedback.getText(), containsString("Thank you for providing a changeset comment"));
|
---|
34 | textField.setText("a");
|
---|
35 | assertThat(feedback.getText(), containsString("Your upload comment is <i>empty</i>, or <i>very short</i>"));
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Unit test of {@link UploadTextComponentValidator.UploadSourceValidator}
|
---|
40 | */
|
---|
41 | @Test
|
---|
42 | void testUploadSourceValidator() {
|
---|
43 | JTextField textField = new JTextField();
|
---|
44 | JLabel feedback = new JLabel();
|
---|
45 | new UploadTextComponentValidator.UploadSourceValidator(textField, feedback);
|
---|
46 | assertThat(feedback.getText(), containsString("You did not specify a source for your changes"));
|
---|
47 | textField.setText("a comment long enough");
|
---|
48 | assertThat(feedback.getText(), containsString("Thank you for providing the data source"));
|
---|
49 | }
|
---|
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 | }
|
---|
88 | }
|
---|