1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.hamcrest.CoreMatchers.is;
|
---|
5 | import static org.hamcrest.MatcherAssert.assertThat;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
9 |
|
---|
10 | import java.util.Optional;
|
---|
11 |
|
---|
12 | import org.junit.jupiter.api.Test;
|
---|
13 |
|
---|
14 | import net.trajano.commons.testing.UtilityClassTestUtil;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Unit tests of {@link StringParser} class.
|
---|
18 | */
|
---|
19 | class 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 | }
|
---|