/*
   * Test method for 'br.org.archimedes.model.commands.MoveElementCommand.doIt(Drawing)'
   */
  @Test
  public void testDoIt() throws Exception {

    Selection selection = createOriginalSelection();
    Command moveElement = new MoveCommand(getPoints(selection), vector);

    try {
      moveElement.doIt(drawing);
      Assert.fail("Should throw an IllegalActionException since nothing is on the drawing");
    } catch (IllegalActionException e) {
    } catch (NullArgumentException e) {
      Assert.fail("Should not throw a NullArgumentException");
    }

    addsSelectionToDrawing(selection);
    moveElement.doIt(drawing);
    Collection<Element> expected = createOriginalExpected();
    assertCollectionTheSame(expected, selection.getSelectedElements());

    removesSelectionToDrawing(selection);

    try {
      moveElement.doIt(drawing);
      Assert.fail("Should throw an IllegalActionException");
    } catch (IllegalActionException e) {
    } catch (NullArgumentException e) {
      Assert.fail("Should not throw a NullArgumentException");
    }

    addsSelectionToDrawing(selection);
    drawing.removeElement(circle);

    try {
      moveElement.doIt(drawing);
      Assert.fail("Should throw an IllegalActionException");
    } catch (IllegalActionException e) {
      // Should throw an exception for the circle
    } catch (NullArgumentException e) {
      Assert.fail("Should not throw a NullArgumentException");
    }

    expected = new ArrayList<Element>();
    expected.add(new Line(20, 20, 120, 120));
    expected.add(new InfiniteLine(30, 30, 70, 70));
    expected.add(new Semiline(30, 10, 70, 70));
    expected.add(circle);
    expected.add(new Arc(new Point(-80, 20), new Point(20, 120), new Point(120, 20)));

    assertCollectionTheSame(expected, selection.getSelectedElements());
  }
  /*
   * Test method for 'br.org.archimedes.model.commands.MoveElementCommand.undoIt(Drawing)'
   */
  @Test
  public void testUndoIt() throws Exception {
    Selection selection = createOriginalSelection();
    UndoableCommand moveElement = new MoveCommand(getPoints(selection), vector);

    try {
      moveElement.undoIt(null);
      Assert.fail("Should throw a NullArgumentException");
    } catch (IllegalActionException e) {
      Assert.fail("Should not throw any exception");
    } catch (NullArgumentException e) {
    }

    try {
      moveElement.undoIt(drawing);
      Assert.fail("Should throw an IllegalActionException");
    } catch (IllegalActionException e) {
    } catch (NullArgumentException e) {
      Assert.fail("Should throw an IllegalActionException");
    }

    addsSelectionToDrawing(selection);
    moveElement.doIt(drawing);
    moveElement.undoIt(drawing);

    Collection<Element> expected = createOriginalSelection().getSelectedElements();
    assertCollectionTheSame(expected, selection.getSelectedElements());

    selection = createOriginalSelection();
    drawing.addLayer(new Layer(Constant.BLACK, "Layer2", LineStyle.CONTINUOUS, 1.0));
    drawing.setCurrentLayer(1);
    addsSelectionToDrawing(selection);

    moveElement = new MoveCommand(getPoints(selection), vector);
    moveElement.doIt(drawing);
    moveElement.doIt(drawing);
    moveElement.undoIt(drawing);

    expected = createOriginalExpected();
    assertCollectionTheSame(expected, selection.getSelectedElements());
  }
  /**
   * @param selection The selection with the elements that should be considered
   * @return The collection of points or null if null was passsed.
   */
  private Map<Element, Collection<Point>> getPoints(Selection selection) {

    if (selection == null) {
      return null;
    }

    Map<Element, Collection<Point>> pointsToMove = new HashMap<Element, Collection<Point>>();
    for (Element element : selection.getSelectedElements()) {
      pointsToMove.put(element, element.getPoints());
    }

    return pointsToMove;
  }
  private Selection createOriginalSelection() throws Exception {

    Selection selection = new Selection();
    selection.add(new Line(0, 0, 100, 100));
    selection.add(new InfiniteLine(10, 10, 50, 50));
    selection.add(new Semiline(10, -10, 50, 50));
    selection.add(new Arc(new Point(-100, 0), new Point(0, 100), new Point(100, 0)));
    circle = new Circle(new Point(0, 0), 50.0);
    selection.add(circle);
    return selection;
  }
  /**
   * Removes the selection from the drawing.
   *
   * @param selection The selection to remove
   */
  private void removesSelectionToDrawing(Selection selection) throws Exception {

    for (Element element : selection.getSelectedElements()) {
      drawing.removeElement(element);
    }
  }
  /**
   * Adds the selection to the drawing.
   *
   * @param selection The selection to add
   */
  private void addsSelectionToDrawing(Selection selection) throws Exception {

    for (Element element : selection.getSelectedElements()) {
      drawing.putElement(element, drawing.getCurrentLayer());
    }
  }