Changeset 16086 in josm for trunk/test


Ignore:
Timestamp:
2020-03-08T19:21:11+01:00 (5 years ago)
Author:
simon04
Message:

see #18140 - Get rid of OpeningHoursParser.OpeningHoursTestError

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/OpeningHourTestTest.java

    r15978 r16086  
    66import static org.hamcrest.CoreMatchers.not;
    77import static org.junit.Assert.assertEquals;
     8import static org.junit.Assert.assertNotNull;
    89import static org.junit.Assert.assertThat;
    910import static org.junit.Assert.assertTrue;
     
    1920import org.junit.Rule;
    2021import org.junit.Test;
     22import org.openstreetmap.josm.command.ChangePropertyCommand;
     23import org.openstreetmap.josm.data.coor.LatLon;
     24import org.openstreetmap.josm.data.osm.DataSet;
     25import org.openstreetmap.josm.data.osm.Node;
    2126import org.openstreetmap.josm.data.osm.Tag;
     27import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
    2228import org.openstreetmap.josm.data.validation.Severity;
     29import org.openstreetmap.josm.data.validation.TestError;
    2330import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
    2431import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;
     
    3643public class OpeningHourTestTest {
    3744    /**
    38      * We need prefs for this. We check strings so we need i18n.
     45     * We need preferences for this. We check strings so we need i18n.
    3946     */
    4047    @Rule
     
    5259        openingHourTest = new OpeningHourTest();
    5360        openingHourTest.initialize();
     61        ValidatorPrefHelper.PREF_OTHER.put(true);
    5462    }
    5563
     
    6169        final String key = "opening_hours";
    6270        // frequently used tags according to https://taginfo.openstreetmap.org/keys/opening_hours#values
    63         assertThat(openingHourTest.checkOpeningHourSyntax(key, "24/7"), isEmpty());
    64         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 08:30-20:00"), isEmpty());
    65         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr sunrise-sunset"), isEmpty());
    66         assertThat(openingHourTest.checkOpeningHourSyntax(key, "09:00-21:00"), isEmpty());
    67         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Su-Th sunset-24:00,04:00-sunrise; Fr-Sa sunset-sunrise"), isEmpty());
     71        assertThat(checkOpeningHourSyntax(key, "24/7"), isEmpty());
     72        assertThat(checkOpeningHourSyntax(key, "Mo-Fr 08:30-20:00"), isEmpty());
     73        assertThat(checkOpeningHourSyntax(key, "Mo-Fr sunrise-sunset"), isEmpty());
     74        assertThat(checkOpeningHourSyntax(key, "09:00-21:00"), isEmpty());
     75        assertThat(checkOpeningHourSyntax(key, "Su-Th sunset-24:00,04:00-sunrise; Fr-Sa sunset-sunrise"), isEmpty());
    6876    }
    6977
     
    7583        final String key = "opening_hours";
    7684        String value = ".";
    77         assertEquals("Vorgefunden wurde:  \".\" \". \" in Zeile 0, Zeichen 0\nErwartet wurde: <EOF> => null",
    78                 openingHourTest.checkOpeningHourSyntax(key, value, false, Locale.GERMAN).get(0).toString());
    79         assertEquals("Encountered:  \".\" \". \" at line 0, column 0\nWas expecting: <EOF> => null",
    80                 openingHourTest.checkOpeningHourSyntax(key, value, false, Locale.ENGLISH).get(0).toString());
     85        assertEquals("Vorgefunden wurde:  \".\" \". \" in Zeile 0, Zeichen 0\nErwartet wurde: <EOF>",
     86                checkOpeningHourSyntax(key, value, Locale.GERMAN).get(0).getDescription());
     87        assertEquals("Encountered:  \".\" \". \" at line 0, column 0\nWas expecting: <EOF>",
     88                checkOpeningHourSyntax(key, value, Locale.ENGLISH).get(0).getDescription());
    8189        value = "Mon-Thu 12-18";
    82         assertEquals("Wochentag mit 3 Buchstaben in Zeile 1, Zeichen 4 => Mo-Th 12:00-18:00",
    83                 openingHourTest.checkOpeningHourSyntax(key, value, false, Locale.GERMAN).get(0).toString());
    84         assertEquals("Three character weekday at line 1, column 4 => Mo-Th 12:00-18:00",
    85                 openingHourTest.checkOpeningHourSyntax(key, value, false, Locale.ENGLISH).get(0).toString());
     90        assertEquals("Wochentag mit 3 Buchstaben in Zeile 1, Zeichen 4",
     91                checkOpeningHourSyntax(key, value, Locale.GERMAN).get(0).getDescription());
     92        assertEquals("Three character weekday at line 1, column 4",
     93                checkOpeningHourSyntax(key, value, Locale.ENGLISH).get(0).getDescription());
    8694    }
    8795
     
    92100    public void testCheckOpeningHourSyntax2() {
    93101        final String key = "opening_hours";
    94         final List<OpeningHourTest.OpeningHoursTestError> errors = openingHourTest.checkOpeningHourSyntax(key, "Mo-Tue");
     102        final List<TestError> errors = checkOpeningHourSyntax(key, "Mo-Tue");
    95103        assertThat(errors, hasSize(1));
    96         assertEquals("Mo-Tu", errors.get(0).getPrettifiedValue());
    97         assertEquals("Three character weekday at line 1, column 6", errors.get(0).getMessage());
     104        assertFixEquals("Mo-Tu", errors.get(0));
     105        assertEquals("Three character weekday at line 1, column 6", errors.get(0).getDescription());
    98106        assertEquals(Severity.WARNING, errors.get(0).getSeverity());
    99107    }
     
    105113    public void testCheckOpeningHourSyntax3() {
    106114        final String key = "opening_hours";
    107         final List<OpeningHourTest.OpeningHoursTestError> errors = openingHourTest.checkOpeningHourSyntax(key, "Sa-Su 10.00-20.00");
     115        final List<TestError> errors = checkOpeningHourSyntax(key, "Sa-Su 10.00-20.00");
    108116        assertThat(errors, hasSize(1));
    109         assertEquals("Sa-Su 10:00-20:00", errors.get(0).getPrettifiedValue());
    110         assertEquals("Invalid minutes at line 1, column 12", errors.get(0).getMessage());
     117        assertFixEquals("Sa-Su 10:00-20:00", errors.get(0));
     118        assertEquals("Invalid minutes at line 1, column 12", errors.get(0).getDescription());
    111119        assertEquals(Severity.WARNING, errors.get(0).getSeverity());
    112120    }
     
    117125    @Test
    118126    public void testCheckOpeningHourSyntax4() {
    119         assertThat(openingHourTest.checkOpeningHourSyntax(null, null), isEmpty());
    120         assertThat(openingHourTest.checkOpeningHourSyntax(null, ""), isEmpty());
     127        assertThat(checkOpeningHourSyntax(null, null), isEmpty());
     128        assertThat(checkOpeningHourSyntax(null, ""), isEmpty());
    121129        assertEquals("opening_hours value can be prettified",
    122                 openingHourTest.checkOpeningHourSyntax("opening_hours", " ").get(0).getMessage());
     130                checkOpeningHourSyntax("opening_hours", " ").get(0).getDescription());
    123131        assertEquals("null value can be prettified",
    124                 openingHourTest.checkOpeningHourSyntax(null, " ").get(0).getMessage());
     132                checkOpeningHourSyntax(null, " ").get(0).getDescription());
    125133    }
    126134
     
    131139    public void testCheckOpeningHourSyntax5() {
    132140        final String key = "opening_hours";
    133         assertThat(openingHourTest.checkOpeningHourSyntax(key, "badtext"), hasSize(1));
     141        assertThat(checkOpeningHourSyntax(key, "badtext"), hasSize(1));
    134142        assertEquals("Encountered:  <UNEXPECTED_CHAR> \"b \" at line 0, column 0\nWas expecting: <EOF>",
    135                 openingHourTest.checkOpeningHourSyntax(key, "badtext").get(0).getMessage().trim());
    136         assertThat(openingHourTest.checkOpeningHourSyntax(key, "5.00 p.m-11.00 p.m"), hasSize(1));
     143                checkOpeningHourSyntax(key, "badtext").get(0).getDescription().trim());
     144        assertThat(checkOpeningHourSyntax(key, "5.00 p.m-11.00 p.m"), hasSize(1));
    137145        assertEquals("Encountered:  <UNEXPECTED_CHAR> \"p \" at line 1, column 2\nWas expecting: <EOF>",
    138                 openingHourTest.checkOpeningHourSyntax(key, "5.00 p.m-11.00 p.m").get(0).getMessage());
     146                checkOpeningHourSyntax(key, "5.00 p.m-11.00 p.m").get(0).getDescription());
    139147    }
    140148
     
    145153    public void testCheckOpeningHourSyntax6() {
    146154        final String key = "opening_hours";
    147         assertThat(openingHourTest.checkOpeningHourSyntax(key, "PH open \"always open on public holidays\""), isEmpty());
     155        assertThat(checkOpeningHourSyntax(key, "PH open \"always open on public holidays\""), isEmpty());
    148156    }
    149157
     
    154162    public void testCheckOpeningHourSyntax7() {
    155163        final String key = "opening_hours";
    156         assertThat(openingHourTest.checkOpeningHourSyntax(key, "9:00-18:00", true, Locale.getDefault()), isEmpty());
    157         assertThat(openingHourTest.checkOpeningHourSyntax(key, "9:00-18:00"), hasSize(1));
    158         assertEquals(Severity.OTHER, openingHourTest.checkOpeningHourSyntax(key, "9:00-18:00").get(0).getSeverity());
    159         assertEquals("09:00-18:00", openingHourTest.checkOpeningHourSyntax(key, "9:00-18:00").get(0).getPrettifiedValue());
     164        assertThat(checkOpeningHourSyntax(key, "9:00-18:00"), hasSize(1));
     165        assertEquals(Severity.OTHER, checkOpeningHourSyntax(key, "9:00-18:00").get(0).getSeverity());
     166        assertFixEquals("09:00-18:00", checkOpeningHourSyntax(key, "9:00-18:00").get(0));
    160167    }
    161168
     
    166173    public void testCheckOpeningHourSyntaxTicket9367() {
    167174        final String key = "opening_hours";
    168         assertEquals(Severity.WARNING, openingHourTest.checkOpeningHourSyntax(key, "Mo,Tu 04-17").get(0).getSeverity());
     175        assertEquals(Severity.WARNING, checkOpeningHourSyntax(key, "Mo,Tu 04-17").get(0).getSeverity());
    169176        assertEquals("Hours without minutes",
    170                 openingHourTest.checkOpeningHourSyntax(key, "Mo,Tu 04-17").get(0).getMessage());
     177                checkOpeningHourSyntax(key, "Mo,Tu 04-17").get(0).getDescription());
    171178    }
    172179
     
    178185        final String key = "service_times";
    179186        // frequently used tags according to https://taginfo.openstreetmap.org/keys/service_times#values
    180         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Su 10:00"), isEmpty());
    181         assertThat(openingHourTest.checkOpeningHourSyntax(key, "automatic"), not(isEmpty()));
    182         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Sa 09:00-18:00"), isEmpty());
    183         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Su 09:30; We 19:30"), isEmpty());
    184         // assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00"), isEmpty());
    185         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00"), hasSize(1));
    186         assertEquals("Mo-Fr 00:00-00:30,04:00-00:30; PH,Sa,Su 00:00-24:00",
    187                 openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00").get(0).getPrettifiedValue());
    188         assertEquals("Mo-Fr 00:00-00:30,04:00-00:30; PH,Sa,Su 00:00-24:00",
    189                 openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00").get(0).getPrettifiedValue());
     187        assertThat(checkOpeningHourSyntax(key, "Su 10:00"), isEmpty());
     188        assertThat(checkOpeningHourSyntax(key, "automatic"), not(isEmpty()));
     189        assertThat(checkOpeningHourSyntax(key, "Mo-Sa 09:00-18:00"), isEmpty());
     190        assertThat(checkOpeningHourSyntax(key, "Su 09:30; We 19:30"), isEmpty());
     191        // assertThat(checkOpeningHourSyntax(key, "Mo-Fr 00:00-00:30,04:00-00:30; Sa,Su,PH 00:00-24:00"), isEmpty());
     192        assertThat(checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00"), hasSize(1));
     193        assertFixEquals("Mo-Fr 00:00-00:30,04:00-00:30; PH,Sa,Su 00:00-24:00",
     194                checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00").get(0));
     195        assertFixEquals("Mo-Fr 00:00-00:30,04:00-00:30; PH,Sa,Su 00:00-24:00",
     196                checkOpeningHourSyntax(key, "Mo-Fr 0:00-0:30,4:00-00:30; Sa,Su,PH 0:00-24:00").get(0));
    190197    }
    191198
     
    197204        final String key = "collection_times";
    198205        // frequently used tags according to https://taginfo.openstreetmap.org/keys/collection_times#values
    199         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Sa 09:00"), isEmpty());
    200         assertThat(openingHourTest.checkOpeningHourSyntax(key, "fixme"), not(isEmpty()));
    201         assertThat(openingHourTest.checkOpeningHourSyntax(key, "daily"), not(isEmpty()));
    202         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30,17:45,19:00; Sa 15:00; Su 11:00"), isEmpty());
    203         assertThat(openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00"), hasSize(1));
     206        assertThat(checkOpeningHourSyntax(key, "Mo-Sa 09:00"), isEmpty());
     207        assertThat(checkOpeningHourSyntax(key, "fixme"), not(isEmpty()));
     208        assertThat(checkOpeningHourSyntax(key, "daily"), not(isEmpty()));
     209        assertThat(checkOpeningHourSyntax(key, "Mo-Fr 13:30,17:45,19:00; Sa 15:00; Su 11:00"), isEmpty());
     210        assertThat(checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00"), hasSize(1));
    204211        assertEquals(Severity.OTHER,
    205                 openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00").get(0).getSeverity());
    206         assertEquals("Mo-Fr 13:30,17:45,19:00; Sa 15:00; Su 11:00",
    207                 openingHourTest.checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00").get(0).getPrettifiedValue());
     212                checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00").get(0).getSeverity());
     213        assertFixEquals("Mo-Fr 13:30,17:45,19:00; Sa 15:00; Su 11:00",
     214                checkOpeningHourSyntax(key, "Mo-Fr 13:30, 17:45, 19:00; Sa 15:00; Su 11:00").get(0));
    208215    }
    209216
     
    226233        }
    227234        for (final Tag t : values) {
    228             final List<OpeningHourTest.OpeningHoursTestError> errors = openingHourTest.checkOpeningHourSyntax(t.getKey(), t.getValue());
    229             if (!errors.isEmpty() && errors.get(0).getMessage().startsWith("Holiday after weekday")) {
     235            final List<TestError> errors = checkOpeningHourSyntax(t.getKey(), t.getValue());
     236            if (!errors.isEmpty() && errors.get(0).getDescription().startsWith("Holiday after weekday")) {
    230237                continue;
    231238            }
     
    240247    public void testTicket17932() {
    241248        Logging.clearLastErrorAndWarnings();
    242         assertTrue(openingHourTest.checkOpeningHourSyntax("opening_hours", "SH off").isEmpty());
     249        assertTrue(checkOpeningHourSyntax("opening_hours", "SH off").isEmpty());
     250    }
     251
     252    private List<TestError> checkOpeningHourSyntax(final String key, final String value, final Locale... locales) {
     253        final Locale locale = locales.length > 0 ? locales[0] : Locale.ENGLISH;
     254        final Node node = new Node(LatLon.ZERO);
     255        node.put(key, value);
     256        new DataSet(node);
     257        return openingHourTest.checkOpeningHourSyntax(key, value, node, locale);
     258    }
     259
     260    private static void assertFixEquals(String value, TestError error) {
     261        assertNotNull("fix is not null", error.getFix());
     262        assertTrue("fix is ChangePropertyCommand", error.getFix() instanceof ChangePropertyCommand);
     263        final ChangePropertyCommand command = (ChangePropertyCommand) error.getFix();
     264        assertEquals(1, command.getTags().size());
     265        assertEquals(value, command.getTags().values().iterator().next());
    243266    }
    244267}
Note: See TracChangeset for help on using the changeset viewer.