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

Last change on this file since 16181 was 16181, checked in by simon04, 4 years ago

see #18954, see #18864 - Introduce StringParser for parsing various types

File size: 2.2 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.junit.Assert.assertFalse;
6import static org.junit.Assert.assertThat;
7import static org.junit.Assert.assertTrue;
8
9import java.util.Optional;
10
11import net.trajano.commons.testing.UtilityClassTestUtil;
12import org.junit.Test;
13
14/**
15 * Unit tests of {@link StringParser} class.
16 */
17public 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}
Note: See TracBrowser for help on using the repository browser.