Changeset 11986 in josm


Ignore:
Timestamp:
2017-04-23T19:14:12+02:00 (7 years ago)
Author:
Don-vip
Message:

add unit test for Main.postConstructorProcessCmdLine

Location:
trunk
Files:
8 edited

Legend:

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

    r11925 r11986  
    1818import java.util.Arrays;
    1919import java.util.Collection;
     20import java.util.Collections;
    2021import java.util.EnumSet;
    2122import java.util.HashMap;
     
    826827    }
    827828
     829    /**
     830     * Handle command line instructions after GUI has been initialized.
     831     * @param args program arguments
     832     */
    828833    protected static void postConstructorProcessCmdLine(ProgramArguments args) {
     834        List<Future<?>> tasks = new ArrayList<>();
    829835        List<File> fileList = new ArrayList<>();
    830836        for (String s : args.get(Option.DOWNLOAD)) {
    831             DownloadParamType.paramType(s).download(s, fileList);
     837            tasks.addAll(DownloadParamType.paramType(s).download(s, fileList));
    832838        }
    833839        if (!fileList.isEmpty()) {
    834             OpenFileAction.openFiles(fileList, true);
     840            tasks.add(OpenFileAction.openFiles(fileList, true));
    835841        }
    836842        for (String s : args.get(Option.DOWNLOADGPS)) {
    837             DownloadParamType.paramType(s).downloadGps(s);
     843            tasks.addAll(DownloadParamType.paramType(s).downloadGps(s));
     844        }
     845        // Make sure all download tasks complete before handling selection arguments
     846        for (Future<?> task : tasks) {
     847            try {
     848                task.get();
     849            } catch (InterruptedException | ExecutionException e) {
     850                error(e);
     851            }
    838852        }
    839853        for (String s : args.get(Option.SELECTION)) {
     
    868882    }
    869883
     884    /**
     885     * Shutdown JOSM.
     886     */
    870887    protected void shutdown() {
    871888        if (!GraphicsEnvironment.isHeadless()) {
     
    897914        httpUrl {
    898915            @Override
    899             void download(String s, Collection<File> fileList) {
    900                 new OpenLocationAction().openUrl(false, s);
     916            List<Future<?>> download(String s, Collection<File> fileList) {
     917                return new OpenLocationAction().openUrl(false, s);
    901918            }
    902919
    903920            @Override
    904             void downloadGps(String s) {
     921            List<Future<?>> downloadGps(String s) {
    905922                final Bounds b = OsmUrlToBounds.parse(s);
    906923                if (b == null) {
     
    911928                            JOptionPane.WARNING_MESSAGE
    912929                    );
    913                     return;
     930                    return Collections.emptyList();
    914931                }
    915                 downloadFromParamBounds(true, b);
     932                return downloadFromParamBounds(true, b);
    916933            }
    917934        }, fileUrl {
    918935            @Override
    919             void download(String s, Collection<File> fileList) {
     936            List<Future<?>> download(String s, Collection<File> fileList) {
    920937                File f = null;
    921938                try {
     
    933950                    fileList.add(f);
    934951                }
     952                return Collections.emptyList();
    935953            }
    936954        }, bounds {
     
    940958             * @param rawGps Flag to download raw GPS tracks
    941959             * @param s The bounds parameter
     960             * @return the complete download task (including post-download handler), or {@code null}
    942961             */
    943             private void downloadFromParamBounds(final boolean rawGps, String s) {
     962            private List<Future<?>> downloadFromParamBounds(final boolean rawGps, String s) {
    944963                final StringTokenizer st = new StringTokenizer(s, ",");
    945964                if (st.countTokens() == 4) {
    946                     Bounds b = new Bounds(
     965                    return Main.downloadFromParamBounds(rawGps, new Bounds(
    947966                            new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())),
    948967                            new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()))
    949                     );
    950                     Main.downloadFromParamBounds(rawGps, b);
     968                    ));
    951969                }
     970                return Collections.emptyList();
    952971            }
    953972
    954973            @Override
    955             void download(String param, Collection<File> fileList) {
    956                 downloadFromParamBounds(false, param);
     974            List<Future<?>> download(String param, Collection<File> fileList) {
     975                return downloadFromParamBounds(false, param);
    957976            }
    958977
    959978            @Override
    960             void downloadGps(String param) {
    961                 downloadFromParamBounds(true, param);
     979            List<Future<?>> downloadGps(String param) {
     980                return downloadFromParamBounds(true, param);
    962981            }
    963982        }, fileName {
    964983            @Override
    965             void download(String s, Collection<File> fileList) {
     984            List<Future<?>> download(String s, Collection<File> fileList) {
    966985                fileList.add(new File(s));
     986                return Collections.emptyList();
    967987            }
    968988        };
     
    972992         * @param param represents the object to be downloaded
    973993         * @param fileList files which shall be opened, should be added to this collection
     994         * @return the download task, or {@code null}
    974995         */
    975         abstract void download(String param, Collection<File> fileList);
     996        abstract List<Future<?>> download(String param, Collection<File> fileList);
    976997
    977998        /**
    978999         * Performs the GPS download
    9791000         * @param param represents the object to be downloaded
     1001         * @return the download task, or {@code null}
    9801002         */
    981         void downloadGps(String param) {
     1003        List<Future<?>> downloadGps(String param) {
    9821004            JOptionPane.showMessageDialog(
    9831005                    Main.parent,
     
    9861008                    JOptionPane.WARNING_MESSAGE
    9871009            );
     1010            return Collections.emptyList();
    9881011        }
    9891012
     
    10081031     * @param rawGps Flag to download raw GPS tracks
    10091032     * @param b The bounds value
    1010      */
    1011     private static void downloadFromParamBounds(final boolean rawGps, Bounds b) {
     1033     * @return the complete download task (including post-download handler)
     1034     */
     1035    private static List<Future<?>> downloadFromParamBounds(final boolean rawGps, Bounds b) {
    10121036        DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
    10131037        // asynchronously launch the download task ...
    10141038        Future<?> future = task.download(true, b, null);
    10151039        // ... and the continuation when the download is finished (this will wait for the download to finish)
    1016         Main.worker.execute(new PostDownloadHandler(task, future));
     1040        return Collections.singletonList(Main.worker.submit(new PostDownloadHandler(task, future)));
    10171041    }
    10181042
  • trunk/src/org/openstreetmap/josm/actions/OpenFileAction.java

    r11848 r11986  
    2222import java.util.List;
    2323import java.util.Set;
     24import java.util.concurrent.Future;
    2425import java.util.regex.Matcher;
    2526import java.util.regex.Pattern;
     
    8485     * Filenames will not be saved in history.
    8586     * @param fileList A list of files
    86      */
    87     public static void openFiles(List<File> fileList) {
    88         openFiles(fileList, false);
     87     * @return the future task
     88     * @since 11986 (return task)
     89     */
     90    public static Future<?> openFiles(List<File> fileList) {
     91        return openFiles(fileList, false);
    8992    }
    9093
     
    9396     * @param fileList A list of files
    9497     * @param recordHistory {@code true} to save filename in history (default: false)
    95      */
    96     public static void openFiles(List<File> fileList, boolean recordHistory) {
     98     * @return the future task
     99     * @since 11986 (return task)
     100     */
     101    public static Future<?> openFiles(List<File> fileList, boolean recordHistory) {
    97102        OpenFileTask task = new OpenFileTask(fileList, null);
    98103        task.setRecordHistory(recordHistory);
    99         Main.worker.submit(task);
     104        return Main.worker.submit(task);
    100105    }
    101106
  • trunk/src/org/openstreetmap/josm/actions/OpenLocationAction.java

    r11531 r11986  
    5757     */
    5858    private static final BooleanProperty USE_NEW_LAYER = new BooleanProperty("download.newlayer", false);
     59    /**
     60     * the list of download tasks
     61     */
    5962    protected final transient List<Class<? extends DownloadTask>> downloadTasks;
    6063
     
    200203     * @param newLayer true if the URL needs to be opened in a new layer, false otherwise
    201204     * @param url The URL to open
    202      */
    203     public void openUrl(boolean newLayer, String url) {
    204         realOpenUrl(newLayer, url);
     205     * @return the list of tasks that have been started successfully (can be empty).
     206     * @since 11986 (return type)
     207     */
     208    public List<Future<?>> openUrl(boolean newLayer, String url) {
     209        return realOpenUrl(newLayer, url);
    205210    }
    206211
     
    208213     * Open the given URL. This class checks the {@link #USE_NEW_LAYER} preference to check if a new layer should be used.
    209214     * @param url The URL to open
    210      * @return <code>true</code> if at least one task was started successfully.
    211      * @since 11279
    212      */
    213     public boolean openUrl(String url) {
     215     * @return the list of tasks that have been started successfully (can be empty).
     216     * @since 11986 (return type)
     217     */
     218    public List<Future<?>> openUrl(String url) {
    214219        return realOpenUrl(USE_NEW_LAYER.get(), url);
    215220    }
    216221
    217     private boolean realOpenUrl(boolean newLayer, String url) {
     222    private List<Future<?>> realOpenUrl(boolean newLayer, String url) {
    218223        Collection<DownloadTask> tasks = findDownloadTasks(url, false);
    219224
     
    222227        } else if (tasks.isEmpty()) {
    223228            warnNoSuitableTasks(url);
    224             return false;
     229            return Collections.emptyList();
    225230        }
    226231
    227232        PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download Data"));
    228233
    229         boolean hadAnySuccess = false;
     234        List<Future<?>> result = new ArrayList<>();
    230235        for (final DownloadTask task : tasks) {
    231236            try {
    232                 Future<?> future = task.loadUrl(newLayer, url, monitor);
    233                 Main.worker.submit(new PostDownloadHandler(task, future));
    234                 hadAnySuccess = true;
     237                result.add(Main.worker.submit(new PostDownloadHandler(task, task.loadUrl(newLayer, url, monitor))));
    235238            } catch (IllegalArgumentException e) {
    236239                Main.error(e);
    237240            }
    238241        }
    239         return hadAnySuccess;
     242        return result;
    240243    }
    241244
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java

    r11849 r11986  
    267267                //
    268268                final OsmDataLayer layer = createNewLayer(newLayerName);
    269                 if (Main.main != null)
    270                     Main.getLayerManager().addLayer(layer, zoomAfterDownload);
     269                Main.getLayerManager().addLayer(layer, zoomAfterDownload);
    271270                return layer;
    272271            }
  • trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java

    r11978 r11986  
    1010import java.awt.Dimension;
    1111import java.awt.FlowLayout;
     12import java.awt.GraphicsEnvironment;
    1213import java.awt.GridBagLayout;
    1314import java.awt.event.ActionEvent;
     
    596597                    msg = null;
    597598                }
    598                 Main.map.statusLine.setHelpText(msg);
    599                 JOptionPane.showMessageDialog(
    600                         Main.parent,
    601                         msg,
    602                         tr("Warning"),
    603                         JOptionPane.WARNING_MESSAGE
    604                 );
    605             } else {
     599                if (Main.map != null) {
     600                    Main.map.statusLine.setHelpText(msg);
     601                }
     602                if (!GraphicsEnvironment.isHeadless()) {
     603                    JOptionPane.showMessageDialog(
     604                            Main.parent,
     605                            msg,
     606                            tr("Warning"),
     607                            JOptionPane.WARNING_MESSAGE
     608                    );
     609                }
     610            } else if (Main.map != null) {
    606611                Main.map.statusLine.setHelpText(tr("Found {0} matches", foundMatches));
    607612            }
  • trunk/src/org/openstreetmap/josm/gui/ProgramArguments.java

    r11747 r11986  
    136136                addOption(opt, g.getOptarg());
    137137            } else
    138                 throw new IllegalArgumentException("Invalid option: "+c);
     138                throw new IllegalArgumentException("Invalid option: "+ (char) c);
    139139        }
    140140        // positional arguments are a shortcut for the --download ... option
  • trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/OsmLinkPaster.java

    r11713 r11986  
    5151
    5252        String transferData = (String) support.getTransferable().getTransferData(df);
    53         if (new NoWarnOpenLocationAction().openUrl(transferData)) {
     53        if (!new NoWarnOpenLocationAction().openUrl(transferData).isEmpty()) {
    5454            return true;
    5555        }
  • trunk/test/unit/org/openstreetmap/josm/MainTest.java

    r11925 r11986  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertFalse;
    56import static org.junit.Assert.assertNotNull;
    67import static org.junit.Assert.assertNull;
     
    89
    910import java.util.Collection;
     11import java.util.List;
    1012
    1113import javax.swing.UIManager;
     
    1416import org.junit.Test;
    1517import org.openstreetmap.josm.Main.DownloadParamType;
     18import org.openstreetmap.josm.data.osm.DataSet;
     19import org.openstreetmap.josm.gui.ProgramArguments;
     20import org.openstreetmap.josm.gui.layer.GpxLayer;
    1621import org.openstreetmap.josm.testutils.JOSMTestRules;
    1722
     
    2833    @Rule
    2934    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    30     public JOSMTestRules test = new JOSMTestRules().platform();
     35    public JOSMTestRules test = new JOSMTestRules().platform().devAPI();
    3136
    3237    /**
     
    8186        assertNotNull(Main.toolbar);
    8287    }
     88
     89    /**
     90     * Unit test of {@link Main#postConstructorProcessCmdLine} - empty case.
     91     */
     92    @Test
     93    public void testPostConstructorProcessCmdLineEmpty() {
     94        // Check the method accepts no arguments
     95        Main.postConstructorProcessCmdLine(new ProgramArguments(new String[0]));
     96    }
     97
     98    /**
     99     * Unit test of {@link Main#postConstructorProcessCmdLine} - nominal cases.
     100     * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
     101     */
     102    @Test
     103    public void testPostConstructorProcessCmdLineNominal() {
     104        assertNull(Main.getLayerManager().getEditDataSet());
     105        Main.postConstructorProcessCmdLine(new ProgramArguments(new String[]{
     106                "--download=0.01,0.01,0.05,0.05",
     107                "--downloadgps=51.35,-0.4,51.60,0.2",
     108                "--selection=type: node"}));
     109        DataSet ds = Main.getLayerManager().getEditDataSet();
     110        assertNotNull(ds);
     111        assertFalse(ds.getSelected().isEmpty());
     112        Main.getLayerManager().removeLayer(Main.getLayerManager().getEditLayer());
     113        List<GpxLayer> gpxLayers = Main.getLayerManager().getLayersOfType(GpxLayer.class);
     114        assertEquals(1, gpxLayers.size());
     115        Main.getLayerManager().removeLayer(gpxLayers.iterator().next());
     116    }
     117
     118    /**
     119     * Unit test of {@link DownloadParamType} enum.
     120     */
     121    @Test
     122    public void testEnumDownloadParamType() {
     123        TestUtils.superficialEnumCodeCoverage(DownloadParamType.class);
     124    }
    83125}
Note: See TracChangeset for help on using the changeset viewer.