Changeset 9923 in josm for trunk/src/org


Ignore:
Timestamp:
2016-03-04T21:19:57+01:00 (8 years ago)
Author:
simon04
Message:

Add unit tests for OrthogonalizeAction

File:
1 edited

Legend:

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

    r9446 r9923  
    146146        }
    147147
    148         final List<Node> nodeList = new ArrayList<>();
    149         final List<WayData> wayDataList = new ArrayList<>();
    150148        final Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
    151149
    152150        try {
    153             // collect nodes and ways from the selection
    154             for (OsmPrimitive p : sel) {
    155                 if (p instanceof Node) {
    156                     nodeList.add((Node) p);
    157                 } else if (p instanceof Way) {
    158                     wayDataList.add(new WayData((Way) p));
    159                 } else
    160                     throw new InvalidUserInputException(tr("Selection must consist only of ways and nodes."));
    161             }
    162             if (wayDataList.isEmpty())
    163                 throw new InvalidUserInputException("usage");
    164             else  {
    165                 if (nodeList.size() == 2 || nodeList.isEmpty()) {
    166                     OrthogonalizeAction.rememberMovements.clear();
    167                     final Collection<Command> commands = new LinkedList<>();
    168 
    169                     if (nodeList.size() == 2) {  // fixed direction
    170                         commands.addAll(orthogonalize(wayDataList, nodeList));
    171                     } else if (nodeList.isEmpty()) {
    172                         List<List<WayData>> groups = buildGroups(wayDataList);
    173                         for (List<WayData> g: groups) {
    174                             commands.addAll(orthogonalize(g, nodeList));
    175                         }
    176                     } else
    177                         throw new IllegalStateException();
    178 
    179                     Main.main.undoRedo.add(new SequenceCommand(tr("Orthogonalize"), commands));
    180                     Main.map.repaint();
    181 
    182                 } else
    183                     throw new InvalidUserInputException("usage");
    184             }
     151            final SequenceCommand command = orthogonalize(sel);
     152            Main.main.undoRedo.add(new SequenceCommand(tr("Orthogonalize"), command));
     153            Main.map.repaint();
    185154        } catch (InvalidUserInputException ex) {
    186155            String msg;
     
    194163                    .setDuration(Notification.TIME_DEFAULT)
    195164                    .show();
     165        }
     166    }
     167
     168    /**
     169     * Rectifies the selection
     170     * @param selection the selection which should be rectified
     171     * @return a rectifying command
     172     * @throws InvalidUserInputException if the selection is invalid
     173     */
     174    static SequenceCommand orthogonalize(Iterable<OsmPrimitive> selection) throws InvalidUserInputException {
     175        final List<Node> nodeList = new ArrayList<>();
     176        final List<WayData> wayDataList = new ArrayList<>();
     177        // collect nodes and ways from the selection
     178        for (OsmPrimitive p : selection) {
     179            if (p instanceof Node) {
     180                nodeList.add((Node) p);
     181            } else if (p instanceof Way) {
     182                wayDataList.add(new WayData((Way) p));
     183            } else
     184                throw new InvalidUserInputException(tr("Selection must consist only of ways and nodes."));
     185        }
     186        if (wayDataList.isEmpty()) {
     187            throw new InvalidUserInputException("usage");
     188        } else  {
     189            if (nodeList.size() == 2 || nodeList.isEmpty()) {
     190                OrthogonalizeAction.rememberMovements.clear();
     191                final Collection<Command> commands = new LinkedList<>();
     192
     193                if (nodeList.size() == 2) {  // fixed direction
     194                    commands.addAll(orthogonalize(wayDataList, nodeList));
     195                } else if (nodeList.isEmpty()) {
     196                    List<List<WayData>> groups = buildGroups(wayDataList);
     197                    for (List<WayData> g: groups) {
     198                        commands.addAll(orthogonalize(g, nodeList));
     199                    }
     200                } else {
     201                    throw new IllegalStateException();
     202                }
     203
     204                return new SequenceCommand(tr("Orthogonalize"), commands);
     205
     206            } else {
     207                throw new InvalidUserInputException("usage");
     208            }
    196209        }
    197210    }
     
    580593     * Exception: unsuited user input
    581594     */
    582     private static class InvalidUserInputException extends Exception {
     595    protected static class InvalidUserInputException extends Exception {
    583596        InvalidUserInputException(String message) {
    584597            super(message);
     
    593606     * Exception: angle cannot be recognized as 0, 90, 180 or 270 degrees
    594607     */
    595     private static class RejectedAngleException extends Exception {
     608    protected static class RejectedAngleException extends Exception {
    596609        RejectedAngleException() {
    597610            super();
Note: See TracChangeset for help on using the changeset viewer.