Changeset 12562 in josm


Ignore:
Timestamp:
2017-08-03T19:57:36+02:00 (7 years ago)
Author:
Don-vip
Message:

speedup unit tests

Location:
trunk
Files:
15 edited

Legend:

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

    r12527 r12562  
    170170            return;
    171171
    172         DataSet ds = getLayerManager().getEditDataSet();
    173         List<Node> selectedNodes = new ArrayList<>(ds.getSelectedNodes());
    174         List<Way> selectedWays = new ArrayList<>(ds.getSelectedWays());
    175         selectedWays.removeIf(OsmPrimitive::isIncomplete);
    176 
    177172        try {
    178             Command cmd;
    179             // Decide what to align based on selection:
    180 
    181             if (selectedNodes.isEmpty() && !selectedWays.isEmpty()) {
    182                 // Only ways selected -> For each way align their nodes taking care of intersection
    183                 cmd = alignMultiWay(selectedWays);
    184             } else if (selectedNodes.size() == 1) {
    185                 // Only 1 node selected -> align this node relative to referers way
    186                 Node selectedNode = selectedNodes.get(0);
    187                 List<Way> involvedWays;
    188                 if (selectedWays.isEmpty())
    189                     // No selected way, all way containing this node are used
    190                     involvedWays = selectedNode.getParentWays();
    191                 else
    192                     // Selected way, use only these ways
    193                     involvedWays = selectedWays;
    194                 List<Line> lines = getInvolvedLines(selectedNode, involvedWays);
    195                 if (lines.size() > 2 || lines.isEmpty())
    196                     throw new InvalidSelection();
    197                 cmd = alignSingleNode(selectedNodes.get(0), lines);
    198             } else if (selectedNodes.size() >= 3) {
    199                 // More than 3 nodes and way(s) selected -> align selected nodes. Don't care of way(s).
    200                 cmd = alignOnlyNodes(selectedNodes);
    201             } else {
    202                 // All others cases are invalid
    203                 throw new InvalidSelection();
    204             }
    205 
    206             // Do it!
    207             Main.main.undoRedo.add(cmd);
    208 
     173            Main.main.undoRedo.add(buildCommand());
    209174        } catch (InvalidSelection except) {
    210175            Main.debug(except);
     
    212177                .setIcon(JOptionPane.INFORMATION_MESSAGE)
    213178                .show();
     179        }
     180    }
     181
     182    /**
     183     * Builds "align in line" command depending on the selected objects.
     184     * @return the resulting command to execute to perform action
     185     * @throws InvalidSelection if a polygon is selected, or if a node is used by 3 or more ways
     186     * @since 12562
     187     */
     188    public Command buildCommand() throws InvalidSelection {
     189        DataSet ds = getLayerManager().getEditDataSet();
     190        List<Node> selectedNodes = new ArrayList<>(ds.getSelectedNodes());
     191        List<Way> selectedWays = new ArrayList<>(ds.getSelectedWays());
     192        selectedWays.removeIf(OsmPrimitive::isIncomplete);
     193
     194        // Decide what to align based on selection:
     195        if (selectedNodes.isEmpty() && !selectedWays.isEmpty()) {
     196            // Only ways selected -> For each way align their nodes taking care of intersection
     197            return alignMultiWay(selectedWays);
     198        } else if (selectedNodes.size() == 1) {
     199            // Only 1 node selected -> align this node relative to referers way
     200            Node selectedNode = selectedNodes.get(0);
     201            List<Way> involvedWays;
     202            if (selectedWays.isEmpty())
     203                // No selected way, all way containing this node are used
     204                involvedWays = selectedNode.getParentWays();
     205            else
     206                // Selected way, use only these ways
     207                involvedWays = selectedWays;
     208            List<Line> lines = getInvolvedLines(selectedNode, involvedWays);
     209            if (lines.size() > 2 || lines.isEmpty())
     210                throw new InvalidSelection();
     211            return alignSingleNode(selectedNodes.get(0), lines);
     212        } else if (selectedNodes.size() >= 3) {
     213            // More than 3 nodes and way(s) selected -> align selected nodes. Don't care of way(s).
     214            return alignOnlyNodes(selectedNodes);
     215        } else {
     216            // All others cases are invalid
     217            throw new InvalidSelection();
    214218        }
    215219    }
  • trunk/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java

    r12555 r12562  
    154154    @Test
    155155    public void testRelativeRedirects() throws IOException {
    156         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/relative-redirect/5")).connect(progress);
     156        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/relative-redirect/3")).connect(progress);
    157157        assertThat(response.getResponseCode(), is(200));
    158158        assertThat(response.getContentLength() > 100, is(true));
     
    161161    @Test
    162162    public void testAbsoluteRedirects() throws IOException {
    163         final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/absolute-redirect/5")).connect(progress);
     163        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/absolute-redirect/3")).connect(progress);
    164164        assertThat(response.getResponseCode(), is(200));
    165165        assertThat(response.getContentLength() > 100, is(true));
     
    168168    @Test(expected = IOException.class)
    169169    public void testTooMuchRedirects() throws IOException {
    170         HttpClient.create(new URL("https://httpbin.org/redirect/5")).setMaxRedirects(4).connect(progress);
     170        HttpClient.create(new URL("https://httpbin.org/redirect/3")).setMaxRedirects(2).connect(progress);
    171171    }
    172172
  • trunk/test/unit/org/openstreetmap/josm/MainTest.java

    r12554 r12562  
    2020import javax.swing.UIManager;
    2121
    22 import org.junit.Before;
    2322import org.junit.Rule;
    2423import org.junit.Test;
     
    4746    @Rule
    4847    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    49     public JOSMTestRules test = new JOSMTestRules().platform().https().devAPI();
    50 
    51     /**
    52      * Setup test.
    53      */
    54     @Before
    55     public void setUp() {
    56         JOSMFixture.initContentPane();
    57         JOSMFixture.initMainPanel();
    58     }
     48    public JOSMTestRules test = new JOSMTestRules().platform().https().devAPI().main();
    5949
    6050    /**
  • trunk/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java

    r12557 r12562  
    3030    /**
    3131     * We need prefs for this. We need platform for actions and the OSM API for checking blacklist.
    32      * The timeout is set to default httpclient read timeout + connect timeout + a small delay to ignore
    33      * common but harmless network issues.
    3432     */
    3533    @Rule
    3634    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    37     public JOSMTestRules test = new JOSMTestRules().preferences().platform().fakeAPI().timeout(45500);
     35    public JOSMTestRules test = new JOSMTestRules().preferences().platform().fakeAPI();
    3836
    3937    /**
  • trunk/test/unit/org/openstreetmap/josm/actions/AlignInLineActionTest.java

    r12110 r12562  
    66
    77import org.junit.Before;
     8import org.junit.Rule;
    89import org.junit.Test;
    9 import org.openstreetmap.josm.JOSMFixture;
    1010import org.openstreetmap.josm.Main;
    1111import org.openstreetmap.josm.actions.AlignInLineAction.InvalidSelection;
     
    1717import org.openstreetmap.josm.data.osm.Way;
    1818import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     19import org.openstreetmap.josm.testutils.JOSMTestRules;
     20
     21import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1922
    2023/**
     
    2326public final class AlignInLineActionTest {
    2427
     28    /**
     29     * Setup test.
     30     */
     31    @Rule
     32    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     33    public JOSMTestRules test = new JOSMTestRules().mainMenu().projection();
     34
    2535    /** Class under test. */
    2636    private static AlignInLineAction action;
     
    3141    @Before
    3242    public void setUp() {
    33         JOSMFixture.createUnitTestFixture().init(true);
    34 
    3543        // Enable "Align in line" feature.
    3644        action = Main.main.menu.alignInLine;
     
    4351     * https://josm.openstreetmap.de/ticket/9605#comment:3. Note that in this test, after alignment, way is overlapping
    4452     * itself.
    45      */
    46     @Test
    47     public void testNodesOpenWay() {
     53     * @throws InvalidSelection never
     54     */
     55    @Test
     56    public void testNodesOpenWay() throws InvalidSelection {
    4857        DataSet dataSet = new DataSet();
    4958        OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
     
    6776            dataSet.addSelected(point1, point2, point3);
    6877
    69             action.actionPerformed(null);
     78            action.buildCommand().executeCommand();
    7079        } finally {
    7180            // Ensure we clean the place before leaving, even if test fails.
     
    8291     * Test case: only nodes selected, part of a closed way: align these nodes on the line passing through the most
    8392     * distant nodes.
    84      */
    85     @Test
    86     public void testNodesClosedWay() {
     93     * @throws InvalidSelection never
     94     */
     95    @Test
     96    public void testNodesClosedWay() throws InvalidSelection {
    8797        DataSet dataSet = new DataSet();
    8898        OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
     
    106116            dataSet.addSelected(point4, point1, point2);
    107117
    108             action.actionPerformed(null);
     118            action.buildCommand().executeCommand();
    109119        } finally {
    110120            // Ensure we clean the place before leaving, even if test fails.
     
    122132     * Test case: only nodes selected, part of multiple ways: align these nodes on the line passing through the most
    123133     * distant nodes.
    124      */
    125     @Test
    126     public void testNodesOpenWays() {
     134     * @throws InvalidSelection never
     135     */
     136    @Test
     137    public void testNodesOpenWays() throws InvalidSelection {
    127138        DataSet dataSet = new DataSet();
    128139        OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
     
    149160
    150161            // Points must align between points 1 and 4.
    151             action.actionPerformed(null);
     162            action.buildCommand().executeCommand();
    152163        } finally {
    153164            // Ensure we clean the place before leaving, even if test fails.
  • trunk/test/unit/org/openstreetmap/josm/actions/CreateCircleActionTest.java

    r12111 r12562  
    3838    @Rule
    3939    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    40     public JOSMTestRules test = new JOSMTestRules().platform().projection().commands();
     40    public JOSMTestRules test = new JOSMTestRules().platform().projection().main();
    4141
    4242    /**
  • trunk/test/unit/org/openstreetmap/josm/actions/FullscreenToggleActionTest.java

    r11278 r12562  
    1717    @Rule
    1818    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    19     public JOSMTestRules test = new JOSMTestRules().platform().commands();
     19    public JOSMTestRules test = new JOSMTestRules().platform().main();
    2020
    2121    /**
  • trunk/test/unit/org/openstreetmap/josm/actions/JoinAreasActionTest.java

    r12357 r12562  
    4747    @Rule
    4848    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    49     public JOSMTestRules test = new JOSMTestRules().commands();
     49    public JOSMTestRules test = new JOSMTestRules().platform().mainMenu().projection();
    5050
    5151    /**
  • trunk/test/unit/org/openstreetmap/josm/actions/MergeLayerActionTest.java

    r11885 r12562  
    1010import org.openstreetmap.josm.Main;
    1111import org.openstreetmap.josm.data.osm.DataSet;
     12import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    1213import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
    1314import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    2627    @Rule
    2728    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    28     public JOSMTestRules test = new JOSMTestRules().platform().commands();
     29    public JOSMTestRules test = new JOSMTestRules().platform().mainMenu();
    2930
    3031    private MergeLayerAction action;
     
    3536    @Before
    3637    public void setUp() {
     38        try {
     39            LayerListDialog.getInstance();
     40        } catch (IllegalStateException e) {
     41            LayerListDialog.createInstance(Main.getLayerManager());
     42            Main.trace(e);
     43        }
    3744        if (action == null) {
    3845            action = new MergeLayerAction();
  • trunk/test/unit/org/openstreetmap/josm/actions/MergeNodesActionTest.java

    r11110 r12562  
    2828    @Rule
    2929    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    30     public JOSMTestRules test = new JOSMTestRules().platform().commands();
     30    public JOSMTestRules test = new JOSMTestRules().platform().projection();
    3131
    3232    /**
  • trunk/test/unit/org/openstreetmap/josm/actions/PurgeActionTest.java

    r10966 r12562  
    3232    @Rule
    3333    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    34     public JOSMTestRules test = new JOSMTestRules().platform().commands();
     34    public JOSMTestRules test = new JOSMTestRules().platform().main();
    3535
    3636    /**
  • trunk/test/unit/org/openstreetmap/josm/actions/SimplifyWayActionTest.java

    r11121 r12562  
    1010import java.util.stream.Stream;
    1111
    12 import org.junit.BeforeClass;
     12import org.junit.Before;
     13import org.junit.Rule;
    1314import org.junit.Test;
    14 import org.openstreetmap.josm.JOSMFixture;
    1515import org.openstreetmap.josm.Main;
    1616import org.openstreetmap.josm.command.DeleteCommand;
     
    2121import org.openstreetmap.josm.data.osm.Way;
    2222import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     23import org.openstreetmap.josm.testutils.JOSMTestRules;
    2324import org.openstreetmap.josm.tools.Utils;
     25
     26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    2427
    2528/**
     
    3437     * Setup test.
    3538     */
    36     @BeforeClass
    37     public static void setUp() {
    38         JOSMFixture.createUnitTestFixture().init(true);
    39         action = Main.main.menu.simplifyWay;
    40         action.setEnabled(true);
     39    @Rule
     40    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     41    public JOSMTestRules test = new JOSMTestRules().mainMenu();
     42
     43    /**
     44     * Setup test.
     45     */
     46    @Before
     47    public void setUp() {
     48        if (action == null) {
     49            action = Main.main.menu.simplifyWay;
     50            action.setEnabled(true);
     51        }
    4152    }
    4253
  • trunk/test/unit/org/openstreetmap/josm/actions/SplitWayActionTest.java

    r10599 r12562  
    1111import java.util.Iterator;
    1212
    13 import org.junit.BeforeClass;
     13import org.junit.Before;
     14import org.junit.Rule;
    1415import org.junit.Test;
    15 import org.openstreetmap.josm.JOSMFixture;
    1616import org.openstreetmap.josm.Main;
    1717import org.openstreetmap.josm.actions.SplitWayAction.Strategy;
     
    2525import org.openstreetmap.josm.data.osm.Way;
    2626import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     27import org.openstreetmap.josm.testutils.JOSMTestRules;
     28
     29import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    2730
    2831/**
     
    3740     * Setup test.
    3841     */
    39     @BeforeClass
    40     public static void setUp() {
    41         JOSMFixture.createUnitTestFixture().init(true);
    42         action = Main.main.menu.splitWay;
    43         action.setEnabled(true);
     42    @Rule
     43    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     44    public JOSMTestRules test = new JOSMTestRules().mainMenu().projection();
     45
     46    /**
     47     * Setup test.
     48     */
     49    @Before
     50    public void setUp() {
     51        if (action == null) {
     52            action = Main.main.menu.splitWay;
     53            action.setEnabled(true);
     54        }
    4455    }
    4556
  • trunk/test/unit/org/openstreetmap/josm/actions/UnGlueActionTest.java

    r10436 r12562  
    55import static org.junit.Assert.assertTrue;
    66
    7 import org.junit.BeforeClass;
     7import org.junit.Before;
     8import org.junit.Rule;
    89import org.junit.Test;
    9 import org.openstreetmap.josm.JOSMFixture;
    1010import org.openstreetmap.josm.Main;
    1111import org.openstreetmap.josm.data.coor.LatLon;
     
    1414import org.openstreetmap.josm.data.osm.Way;
    1515import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     16import org.openstreetmap.josm.testutils.JOSMTestRules;
     17
     18import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1619
    1720/**
     
    2629     * Setup test.
    2730     */
    28     @BeforeClass
    29     public static void setUp() {
    30         JOSMFixture.createUnitTestFixture().init(true);
    31         action = Main.main.menu.unglueNodes;
    32         action.setEnabled(true);
     31    @Rule
     32    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     33    public JOSMTestRules test = new JOSMTestRules().mainMenu();
     34
     35    /**
     36     * Setup test.
     37     */
     38    @Before
     39    public void setUp() {
     40        if (action == null) {
     41            action = Main.main.menu.unglueNodes;
     42            action.setEnabled(true);
     43        }
    3344    }
    3445
  • trunk/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    r12558 r12562  
    213213
    214214    /**
    215      * Use the {@link Main#main} application in this test.
     215     * Use the {@link Main#main}, {@code Main.contentPanePrivate}, {@code Main.mainPanel}, {@link Main#toolbar} global variables in this test.
    216216     * @return this instance, for easy chaining
    217217     * @since 12557
     
    340340            if (main) {
    341341                new MainApplication();
     342                JOSMFixture.initContentPane();
     343                JOSMFixture.initMainPanel();
     344                JOSMFixture.initToolbar();
    342345            }
    343346
    344347            if (mainMenu) {
    345                 JOSMFixture.initContentPane();
    346                 JOSMFixture.initToolbar();
    347348                Main.main.menu = new MainMenu();
    348349            }
Note: See TracChangeset for help on using the changeset viewer.