Changeset 15864 in josm for trunk/test


Ignore:
Timestamp:
2020-02-16T15:46:03+01:00 (4 years ago)
Author:
simon04
Message:

UtilsTest: extend

Based on test coverage and mutation testing using Pitest.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r15862 r15864  
    77import static org.junit.Assert.assertTrue;
    88
     9import java.awt.Color;
     10import java.io.File;
    911import java.io.IOException;
    1012import java.util.Arrays;
     
    1214import java.util.List;
    1315import java.util.Locale;
     16import java.util.regex.Pattern;
    1417
    1518import org.junit.Rule;
     
    131134    public void testPositionListString() {
    132135        assertEquals("1", Utils.getPositionListString(Arrays.asList(1)));
     136        assertEquals("1-2", Utils.getPositionListString(Arrays.asList(1, 2)));
    133137        assertEquals("1-3", Utils.getPositionListString(Arrays.asList(1, 2, 3)));
    134138        assertEquals("1-3", Utils.getPositionListString(Arrays.asList(3, 1, 2)));
     
    143147    public void testDurationString() {
    144148        I18n.set("en");
     149        assertEquals("0 ms", Utils.getDurationString(0));
    145150        assertEquals("123 ms", Utils.getDurationString(123));
     151        assertEquals("1.0 s", Utils.getDurationString(1000));
    146152        assertEquals("1.2 s", Utils.getDurationString(1234));
    147153        assertEquals("57.0 s", Utils.getDurationString(57 * 1000));
     154        assertEquals("1 min 0 s", Utils.getDurationString(60 * 1000));
    148155        assertEquals("8 min 27 s", Utils.getDurationString(507 * 1000));
     156        assertEquals("1 h 0 min", Utils.getDurationString(60 * 60 * 1000));
    149157        assertEquals("8 h 24 min", Utils.getDurationString((long) (8.4 * 60 * 60 * 1000)));
     158        assertEquals("1 day 0 h", Utils.getDurationString(24 * 60 * 60 * 1000));
    150159        assertEquals("1 day 12 h", Utils.getDurationString((long) (1.5 * 24 * 60 * 60 * 1000)));
    151160        assertEquals("8 days 12 h", Utils.getDurationString((long) (8.5 * 24 * 60 * 60 * 1000)));
     
    153162
    154163    /**
     164     * Test of {@link Utils#getDurationString} method.
     165     */
     166    @Test(expected = IllegalArgumentException.class)
     167    public void testDurationStringNegative() {
     168        Utils.getDurationString(-1);
     169    }
     170
     171    /**
    155172     * Test of {@link Utils#escapeReservedCharactersHTML} method.
    156173     */
     
    161178
    162179    /**
     180     * Test of {@link Utils#shortenString} method.
     181     */
     182    @Test
     183    public void testShortenString() {
     184        assertNull(Utils.shortenString(null, 3));
     185        assertEquals("...", Utils.shortenString("123456789", 3));
     186        assertEquals("1...", Utils.shortenString("123456789", 4));
     187        assertEquals("12...", Utils.shortenString("123456789", 5));
     188        assertEquals("123...", Utils.shortenString("123456789", 6));
     189        assertEquals("1234...", Utils.shortenString("123456789", 7));
     190        assertEquals("12345...", Utils.shortenString("123456789", 8));
     191        assertEquals("123456789", Utils.shortenString("123456789", 9));
     192    }
     193
     194    /**
     195     * Test of {@link Utils#shortenString} method.
     196     */
     197    @Test(expected = IllegalArgumentException.class)
     198    public void testShortenStringTooShort() {
     199        Utils.shortenString("123456789", 2);
     200    }
     201
     202    /**
    163203     * Test of {@link Utils#restrictStringLines} method.
    164204     */
    165205    @Test
    166206    public void testRestrictStringLines() {
     207        assertNull(Utils.restrictStringLines(null, 2));
    167208        assertEquals("1\n...", Utils.restrictStringLines("1\n2\n3", 2));
    168209        assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 3));
    169210        assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 4));
     211    }
     212
     213    /**
     214     * Test of {@link Utils#limit} method.
     215     */
     216    @Test
     217    public void testLimit() {
     218        assertNull(Utils.limit(null, 2, "..."));
     219        assertEquals(Arrays.asList("1", "..."), Utils.limit(Arrays.asList("1", "2", "3"), 2, "..."));
     220        assertEquals(Arrays.asList("1", "2", "3"), Utils.limit(Arrays.asList("1", "2", "3"), 3, "..."));
     221        assertEquals(Arrays.asList("1", "2", "3"), Utils.limit(Arrays.asList("1", "2", "3"), 4, "..."));
    170222    }
    171223
     
    179231        assertEquals("1023 B", Utils.getSizeString(1023, Locale.ENGLISH));
    180232        assertEquals("1.00 kB", Utils.getSizeString(1024, Locale.ENGLISH));
     233        assertEquals("10.00 kB", Utils.getSizeString(10 * 1024, Locale.ENGLISH));
     234        assertEquals("10.0 kB", Utils.getSizeString(10 * 1024 + 1, Locale.ENGLISH));
     235        assertEquals("100.0 kB", Utils.getSizeString(100 * 1024, Locale.ENGLISH));
     236        assertEquals("100 kB", Utils.getSizeString(100 * 1024 + 1, Locale.ENGLISH));
    181237        assertEquals("11.7 kB", Utils.getSizeString(12024, Locale.ENGLISH));
     238        assertEquals("1024 kB", Utils.getSizeString(1024 * 1024 - 1, Locale.ENGLISH));
     239        assertEquals("1.00 MB", Utils.getSizeString(1024 * 1024, Locale.ENGLISH));
     240        assertEquals("1024 MB", Utils.getSizeString(1024 * 1024 * 1024 - 1, Locale.ENGLISH));
     241        assertEquals("1.00 GB", Utils.getSizeString(1024 * 1024 * 1024, Locale.ENGLISH));
    182242        assertEquals("8.00 EB", Utils.getSizeString(Long.MAX_VALUE, Locale.ENGLISH));
    183243    }
     
    310370        assertEquals(3, Utils.getLevenshteinDistance("foo", "bar"));
    311371        assertEquals(1, Utils.getLevenshteinDistance("bar", "baz"));
     372        assertEquals(3, Utils.getLevenshteinDistance("foo", ""));
     373        assertEquals(3, Utils.getLevenshteinDistance("", "baz"));
     374        assertEquals(2, Utils.getLevenshteinDistance("ABjoYZ", "ABsmYZ"));
    312375    }
    313376
     
    334397                        "<td align=\"center\" valign=\"center\">55 m</td></tr></table>"));
    335398    }
     399
     400    /**
     401     * Test of {@link Utils#firstNonNull}
     402     */
     403    @Test
     404    public void testFirstNonNull() {
     405        assertNull(Utils.firstNonNull());
     406        assertNull(Utils.firstNonNull(null, null));
     407        assertEquals("foo", Utils.firstNonNull(null, "foo", null));
     408    }
     409
     410    /**
     411     * Test of {@link Utils#toString}
     412     */
     413    @Test
     414    public void testToString() {
     415        assertEquals("null", Utils.toString(null));
     416        assertEquals("#ff0000", Utils.toString(Color.red));
     417        assertEquals("#345678(alpha=18)", Utils.toString(new Color(0x12345678, true)));
     418    }
     419
     420    /**
     421     * Test of {@link Utils#colorFloat2int}
     422     */
     423    @Test
     424    public void testColorFloat2int() {
     425        assertNull(Utils.colorFloat2int(null));
     426        assertEquals(255, (int) Utils.colorFloat2int(-1.0f));
     427        assertEquals(0, (int) Utils.colorFloat2int(-0.0f));
     428        assertEquals(0, (int) Utils.colorFloat2int(0.0f));
     429        assertEquals(64, (int) Utils.colorFloat2int(0.25f));
     430        assertEquals(128, (int) Utils.colorFloat2int(0.5f));
     431        assertEquals(255, (int) Utils.colorFloat2int(1.0f));
     432        assertEquals(255, (int) Utils.colorFloat2int(2.0f));
     433    }
     434
     435    /**
     436     * Test of {@link Utils#colorInt2float}
     437     */
     438    @Test
     439    public void testColorInt2float() {
     440        assertNull(Utils.colorInt2float(null));
     441        assertEquals(1.0f, Utils.colorInt2float(-1), 1e-3);
     442        assertEquals(0.0f, Utils.colorInt2float(0), 1e-3);
     443        assertEquals(0.25f, Utils.colorInt2float(64), 1e-3);
     444        assertEquals(0.502f, Utils.colorInt2float(128), 1e-3);
     445        assertEquals(0.753f, Utils.colorInt2float(192), 1e-3);
     446        assertEquals(1.0f, Utils.colorInt2float(255), 1e-3);
     447        assertEquals(1.0f, Utils.colorInt2float(1024), 1e-3);
     448    }
     449
     450    /**
     451     * Test of {@link Utils#alphaMultiply}
     452     */
     453    @Test
     454    public void testAlphaMultiply() {
     455        final Color color = new Color(0x12345678, true);
     456        assertEquals(new Color(0x12345678, true), Utils.alphaMultiply(color, 1f));
     457        assertEquals(new Color(0x24345678, true), Utils.alphaMultiply(color, 2f));
     458    }
     459
     460    /**
     461     * Test of {@link Utils#complement}
     462     */
     463    @Test
     464    public void testComplement() {
     465        assertEquals(Color.cyan, Utils.complement(Color.red));
     466        assertEquals(Color.red, Utils.complement(Color.cyan));
     467        assertEquals(Color.magenta, Utils.complement(Color.green));
     468        assertEquals(Color.green, Utils.complement(Color.magenta));
     469        assertEquals(Color.yellow, Utils.complement(Color.blue));
     470        assertEquals(Color.blue, Utils.complement(Color.yellow));
     471    }
     472
     473    /**
     474     * Test of {@link Utils#getMatches}
     475     */
     476    @Test
     477    public void testGetMatches() {
     478        final Pattern pattern = Pattern.compile("(foo)x(bar)y(baz)");
     479        assertNull(Utils.getMatches(pattern.matcher("")));
     480        assertEquals(Arrays.asList("fooxbarybaz", "foo", "bar", "baz"), Utils.getMatches(pattern.matcher("fooxbarybaz")));
     481    }
     482
     483    /**
     484     * Test of {@link Utils#encodeUrl}
     485     */
     486    @Test
     487    public void testEncodeUrl() {
     488        assertEquals("%C3%A4%C3%B6%C3%BC%C3%9F", Utils.encodeUrl("äöüß"));
     489    }
     490
     491    /**
     492     * Test of {@link Utils#encodeUrl}
     493     */
     494    @Test(expected = NullPointerException.class)
     495    public void testEncodeUrlNull() {
     496        Utils.encodeUrl(null);
     497    }
     498
     499    /**
     500     * Test of {@link Utils#decodeUrl}
     501     */
     502    @Test
     503    public void testDecodeUrl() {
     504        assertEquals("äöüß", Utils.decodeUrl("%C3%A4%C3%B6%C3%BC%C3%9F"));
     505    }
     506
     507    /**
     508     * Test of {@link Utils#decodeUrl}
     509     */
     510    @Test(expected = NullPointerException.class)
     511    public void testDecodeUrlNull() {
     512        Utils.decodeUrl(null);
     513    }
     514
     515    /**
     516     * Test of {@link Utils#clamp}
     517     */
     518    @Test
     519    public void testClamp() {
     520        assertEquals(3, Utils.clamp(2, 3, 5));
     521        assertEquals(3, Utils.clamp(3, 3, 5));
     522        assertEquals(4, Utils.clamp(4, 3, 5));
     523        assertEquals(5, Utils.clamp(5, 3, 5));
     524        assertEquals(5, Utils.clamp(6, 3, 5));
     525        assertEquals(3., Utils.clamp(2., 3., 5.), 1e-3);
     526        assertEquals(3., Utils.clamp(3., 3., 5.), 1e-3);
     527        assertEquals(4., Utils.clamp(4., 3., 5.), 1e-3);
     528        assertEquals(5., Utils.clamp(5., 3., 5.), 1e-3);
     529        assertEquals(5., Utils.clamp(6., 3., 5.), 1e-3);
     530    }
     531
     532    /**
     533     * Test of {@link Utils#clamp}
     534     */
     535    @Test(expected = IllegalArgumentException.class)
     536    public void testClampIAE1() {
     537        Utils.clamp(0, 5, 4);
     538    }
     539
     540    /**
     541     * Test of {@link Utils#clamp}
     542     */
     543    @Test(expected = IllegalArgumentException.class)
     544    public void testClampIAE2() {
     545        Utils.clamp(0., 5., 4.);
     546    }
     547
     548    /**
     549     * Test of {@link Utils#hasExtension}
     550     */
     551    @Test
     552    public void testHasExtension() {
     553        assertFalse(Utils.hasExtension("JOSM.txt"));
     554        assertFalse(Utils.hasExtension("JOSM.txt", "jpg"));
     555        assertFalse(Utils.hasExtension("JOSM.txt", ".jpg", ".txt"));
     556        assertTrue(Utils.hasExtension("JOSM.txt", "jpg", "txt"));
     557        assertTrue(Utils.hasExtension(new File("JOSM.txt"), "jpg", "txt"));
     558    }
    336559}
Note: See TracChangeset for help on using the changeset viewer.