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.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertThat;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 |
|
---|
9 | import java.util.Optional;
|
---|
10 |
|
---|
11 | import net.trajano.commons.testing.UtilityClassTestUtil;
|
---|
12 | import org.junit.Test;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Unit tests of {@link StringParser} class.
|
---|
16 | */
|
---|
17 | public class StringParserTest {
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Tests that {@code Utils} satisfies utility class criteria.
|
---|
21 | *
|
---|
22 | * @throws ReflectiveOperationException if an error occurs
|
---|
23 | */
|
---|
24 | @Test
|
---|
25 | public void testUtilityClass() throws ReflectiveOperationException {
|
---|
26 | UtilityClassTestUtil.assertUtilityClassWellDefined(Utils.class);
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Test of {@link StringParser#parse}
|
---|
31 | */
|
---|
32 | @Test
|
---|
33 | public void testParse() {
|
---|
34 | assertThat(StringParser.DEFAULT.parse(char.class, "josm"), is('j'));
|
---|
35 | assertThat(StringParser.DEFAULT.parse(short.class, "123"), is((short) 123));
|
---|
36 | assertThat(StringParser.DEFAULT.parse(int.class, "123456"), is(123456));
|
---|
37 | assertThat(StringParser.DEFAULT.parse(long.class, "1234567890123"), is(1234567890123L));
|
---|
38 | assertThat(StringParser.DEFAULT.tryParse(long.class, "123.456"), is(Optional.empty()));
|
---|
39 | assertThat(StringParser.DEFAULT.tryParse(long.class, "1234567890123"), is(Optional.of(1234567890123L)));
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Tests that {@link StringParser#DEFAULT} is immutable.
|
---|
44 | */
|
---|
45 | @Test(expected = UnsupportedOperationException.class)
|
---|
46 | public void testDefaultImmutable() {
|
---|
47 | StringParser.DEFAULT.registerParser(String.class, String::valueOf);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Tests that {@link StringParser#StringParser(StringParser)} creates a new map.
|
---|
52 | */
|
---|
53 | @Test
|
---|
54 | public void testCopyConstructor() {
|
---|
55 | final StringParser parser = new StringParser(StringParser.DEFAULT).registerParser(boolean.class, "JOSM"::equals);
|
---|
56 | assertTrue(StringParser.DEFAULT.parse(boolean.class, "true"));
|
---|
57 | assertFalse(StringParser.DEFAULT.parse(boolean.class, "JOSM"));
|
---|
58 | assertFalse(parser.parse(boolean.class, "true"));
|
---|
59 | assertTrue(parser.parse(boolean.class, "JOSM"));
|
---|
60 | assertThat(parser.parse(int.class, "123"), is(123));
|
---|
61 | }
|
---|
62 | }
|
---|