Changeset 12011 in josm


Ignore:
Timestamp:
2017-04-27T23:47:16+02:00 (2 years ago)
Author:
Don-vip
Message:

improve unit test coverage of Main

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/Main.java

    r12010 r12011  
    583583    protected abstract void initializeMainWindow();
    584584
    585     private static class InitializationTask implements Callable<Void> {
     585    static final class InitializationTask implements Callable<Void> {
    586586
    587587        private final String name;
     
    12311231
    12321232    /**
     1233     * Clears the network errors cache.
     1234     * @since 12011
     1235     */
     1236    public static void clearNetworkErrors() {
     1237        NETWORK_ERRORS.clear();
     1238    }
     1239
     1240    /**
    12331241     * Returns the JOSM website URL.
    12341242     * @return the josm website URL
  • trunk/test/unit/org/openstreetmap/josm/MainTest.java

    r11995 r12011  
    88import static org.junit.Assert.assertTrue;
    99
     10import java.awt.event.KeyEvent;
    1011import java.net.MalformedURLException;
     12import java.net.URL;
    1113import java.nio.file.Paths;
    1214import java.util.Collection;
    1315import java.util.List;
     16import java.util.Map;
    1417import java.util.concurrent.ExecutionException;
    1518import java.util.concurrent.Future;
     
    2023import org.junit.Test;
    2124import org.openstreetmap.josm.Main.DownloadParamType;
     25import org.openstreetmap.josm.Main.InitStatusListener;
     26import org.openstreetmap.josm.Main.InitializationTask;
     27import org.openstreetmap.josm.actions.AboutAction;
    2228import org.openstreetmap.josm.data.osm.DataSet;
     29import org.openstreetmap.josm.gui.MapFrameListener;
    2330import org.openstreetmap.josm.gui.ProgramArguments;
    2431import org.openstreetmap.josm.gui.layer.GpxLayer;
     32import org.openstreetmap.josm.io.OnlineResource;
    2533import org.openstreetmap.josm.testutils.JOSMTestRules;
     34import org.openstreetmap.josm.tools.Shortcut;
    2635
    2736import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    6473        Main.error(new Exception("exception_error"));
    6574        Main.error("Error message on one line");
     75        Main.error("Error message with {0}", "param");
    6676        Main.error("First line of error message on several lines\nline2\nline3\nline4");
    6777        Collection<String> errors = Main.getLastErrorAndWarnings();
    6878        assertTrue(errors.contains("E: java.lang.Exception: exception_error"));
     79        assertTrue(errors.contains("E: Error message with param"));
    6980        assertTrue(errors.contains("E: Error message on one line"));
    7081        assertTrue(errors.contains("E: First line of error message on several lines"));
     
    7283        // Correct behaviour with warnings
    7384        Main.warn(new Exception("exception_warn", new Exception("root_cause")));
     85        Main.warn(new Exception("exception_warn_bool"), true);
    7486        Main.warn("Warning message on one line");
    7587        Main.warn("First line of warning message on several lines\nline2\nline3\nline4");
    7688        Collection<String> warnings = Main.getLastErrorAndWarnings();
    7789        assertTrue(warnings.contains("W: java.lang.Exception: exception_warn. Cause: java.lang.Exception: root_cause"));
     90        assertTrue(warnings.contains("W: java.lang.Exception: exception_warn_bool"));
    7891        assertTrue(warnings.contains("W: Warning message on one line"));
    7992        assertTrue(warnings.contains("W: First line of warning message on several lines"));
     
    174187        TestUtils.superficialEnumCodeCoverage(DownloadParamType.class);
    175188    }
     189
     190    /**
     191     * Unit test of {@link Main#getBaseUserUrl}.
     192     */
     193    @Test
     194    public void testGetBaseUserUrl() {
     195        assertEquals("http://api06.dev.openstreetmap.org/user", Main.getBaseUserUrl());
     196    }
     197
     198    /**
     199     * Unit test of {@link Main#addNetworkError}, {@link Main#getNetworkErrors} and {@link Main#clearNetworkErrors}.
     200     * @throws MalformedURLException if any error occurs
     201     */
     202    @Test
     203    public void testNetworkErrors() throws MalformedURLException {
     204        Main.clearNetworkErrors();
     205        assertTrue(Main.getNetworkErrors().isEmpty());
     206        Main.addNetworkError("http://url1", new Exception("exception_1"));
     207        Main.addNetworkError(new URL("http://url2"), new Exception("exception_2"));
     208        Map<String, Throwable> errors = Main.getNetworkErrors();
     209        assertEquals(2, errors.size());
     210        assertEquals("exception_1", errors.get("http://url1").getMessage());
     211        assertEquals("exception_2", errors.get("http://url2").getMessage());
     212        Main.clearNetworkErrors();
     213        assertTrue(Main.getNetworkErrors().isEmpty());
     214    }
     215
     216    /**
     217     * Unit test of {@link Main#setOffline} and {@link Main#getOfflineResources}.
     218     */
     219    @Test
     220    public void testOfflineRessources() {
     221        Main.setOnline(OnlineResource.ALL);
     222        assertTrue(Main.getOfflineResources().isEmpty());
     223        assertFalse(Main.isOffline(OnlineResource.JOSM_WEBSITE));
     224        Main.setOffline(OnlineResource.JOSM_WEBSITE);
     225        assertTrue(Main.isOffline(OnlineResource.JOSM_WEBSITE));
     226        Main.setOnline(OnlineResource.JOSM_WEBSITE);
     227        assertFalse(Main.isOffline(OnlineResource.JOSM_WEBSITE));
     228        Main.setOffline(OnlineResource.ALL);
     229        assertTrue(Main.isOffline(OnlineResource.JOSM_WEBSITE));
     230        assertTrue(Main.isOffline(OnlineResource.OSM_API));
     231        Main.setOnline(OnlineResource.ALL);
     232    }
     233
     234    /**
     235     * Unit test of {@link Main#getRegisteredActionShortcut}.
     236     */
     237    @Test
     238    public void testGetRegisteredActionShortcut() {
     239        Shortcut noKeystroke = Shortcut.registerShortcut("no", "keystroke", 0, 0);
     240        assertNull(noKeystroke.getKeyStroke());
     241        assertNull(Main.getRegisteredActionShortcut(noKeystroke));
     242        Shortcut noAction = Shortcut.registerShortcut("foo", "bar", KeyEvent.VK_AMPERSAND, Shortcut.SHIFT);
     243        assertNotNull(noAction.getKeyStroke());
     244        assertNull(Main.getRegisteredActionShortcut(noAction));
     245        AboutAction about = new AboutAction();
     246        assertEquals(about, Main.getRegisteredActionShortcut(about.getShortcut()));
     247    }
     248
     249    /**
     250     * Unit test of {@link Main#addMapFrameListener} and {@link Main#removeMapFrameListener}.
     251     */
     252    @Test
     253    public void testMapFrameListener() {
     254        MapFrameListener listener = (o, n) -> { };
     255        assertTrue(Main.addMapFrameListener(listener));
     256        assertFalse(Main.addMapFrameListener(null));
     257        assertTrue(Main.removeMapFrameListener(listener));
     258        assertFalse(Main.removeMapFrameListener(null));
     259    }
     260
     261    private static class InitStatusListenerStub implements InitStatusListener {
     262
     263        boolean updated;
     264        boolean finished;
     265
     266        @Override
     267        public Object updateStatus(String event) {
     268            updated = true;
     269            return null;
     270        }
     271
     272        @Override
     273        public void finish(Object status) {
     274            finished = true;
     275        }
     276    }
     277
     278    /**
     279     * Unit test of {@link Main#setInitStatusListener}.
     280     */
     281    @Test
     282    public void testSetInitStatusListener() {
     283        InitStatusListenerStub listener = new InitStatusListenerStub();
     284        Main.setInitStatusListener(listener);
     285        assertFalse(listener.updated);
     286        assertFalse(listener.finished);
     287        new InitializationTask("", () -> { }).call();
     288        assertTrue(listener.updated);
     289        assertTrue(listener.finished);
     290    }
    176291}
Note: See TracChangeset for help on using the changeset viewer.