source: josm/trunk/test/unit/org/openstreetmap/josm/tools/StringParserTest.java@ 17275

Last change on this file since 17275 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

File size: 2.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.hamcrest.CoreMatchers.is;
5import static org.hamcrest.MatcherAssert.assertThat;
6import static org.junit.jupiter.api.Assertions.assertFalse;
7import static org.junit.jupiter.api.Assertions.assertTrue;
8import static org.junit.jupiter.api.Assertions.assertThrows;
9
10import java.util.Optional;
11
12import org.junit.jupiter.api.Test;
13
14import net.trajano.commons.testing.UtilityClassTestUtil;
15
16/**
17 * Unit tests of {@link StringParser} class.
18 */
19class StringParserTest {
20
21 /**
22 * Tests that {@code Utils} satisfies utility class criteria.
23 *
24 * @throws ReflectiveOperationException if an error occurs
25 */
26 @Test
27 void testUtilityClass() throws ReflectiveOperationException {
28 UtilityClassTestUtil.assertUtilityClassWellDefined(Utils.class);
29 }
30
31 /**
32 * Test of {@link StringParser#parse}
33 */
34 @Test
35 void testParse() {
36 assertThat(StringParser.DEFAULT.parse(char.class, "josm"), is('j'));
37 assertThat(StringParser.DEFAULT.parse(short.class, "123"), is((short) 123));
38 assertThat(StringParser.DEFAULT.parse(int.class, "123456"), is(123456));
39 assertThat(StringParser.DEFAULT.parse(long.class, "1234567890123"), is(1234567890123L));
40 assertThat(StringParser.DEFAULT.tryParse(long.class, "123.456"), is(Optional.empty()));
41 assertThat(StringParser.DEFAULT.tryParse(long.class, "1234567890123"), is(Optional.of(1234567890123L)));
42 }
43
44 /**
45 * Tests that {@link StringParser#DEFAULT} is immutable.
46 */
47 @Test
48 void testDefaultImmutable() {
49 assertThrows(UnsupportedOperationException.class, () -> StringParser.DEFAULT.registerParser(String.class, String::valueOf));
50 }
51
52 /**
53 * Tests that {@link StringParser#StringParser(StringParser)} creates a new map.
54 */
55 @Test
56 void testCopyConstructor() {
57 final StringParser parser = new StringParser(StringParser.DEFAULT).registerParser(boolean.class, "JOSM"::equals);
58 assertTrue(StringParser.DEFAULT.parse(boolean.class, "true"));
59 assertFalse(StringParser.DEFAULT.parse(boolean.class, "JOSM"));
60 assertFalse(parser.parse(boolean.class, "true"));
61 assertTrue(parser.parse(boolean.class, "JOSM"));
62 assertThat(parser.parse(int.class, "123"), is(123));
63 }
64}
Note: See TracBrowser for help on using the repository browser.