SE450: How to test a class in isolation [14/16] Previous pageContentsNext page

file:myhw3/command/CommandHistoryTEST.java [source] [doc-public] [doc-private]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
package myhw3.command;

import static org.junit.Assert.*;
import org.junit.Test;

public class CommandHistoryTEST {
  @Test
  public void testEmptyExceptions() {
    CommandHistoryObj h = new CommandHistoryObj();
    assertSame(null, h.topUndoCommand());
    assertSame(null, h.topRedoCommand());
    assertFalse(h.undo());
    assertFalse(h.redo());
  }

  private void checkStacks(CommandHistoryObj h, Command topUndo, Command topRedo) {
    assertSame(topUndo, h.topUndoCommand());
    assertSame(topRedo, h.topRedoCommand());
  }

  @Test
  public void topIsSetByAddUndoAndRedo() {
    CommandHistoryObj h = new CommandHistoryObj();

    class CmdSuccess implements Command {
      public boolean run() { return true; }
      public void undo() { }
      public void redo() { }
    }

    Command x1 = new CmdSuccess();
    Command x2 = new CmdSuccess();
    Command x3 = new CmdSuccess();

    h.add(x1);          checkStacks(h, x1,   null);
    h.undo();  checkStacks(h, null, x1);
    h.redo();  checkStacks(h, x1,   null);

    h.add(x2);          checkStacks(h, x2,   null);
    h.undo();  checkStacks(h, x1,   x2);
    h.undo();  checkStacks(h, null, x1);
    h.redo();  checkStacks(h, x1,   x2);
    h.redo();  checkStacks(h, x2,   null);

    h.undo();  checkStacks(h, x1,   x2);
    h.add(x3);          checkStacks(h, x3,   null);
    h.undo();  checkStacks(h, x1,   x3);
    h.undo();  checkStacks(h, null, x1);
    h.redo();  checkStacks(h, x1,   x3);
    h.redo();  checkStacks(h, x3,   null);

    h = new CommandHistoryObj();
    h.add(x1);          checkStacks(h, x1,   null);
    h.add(x2);          checkStacks(h, x2,   null);
    h.undo();  checkStacks(h, x1,   x2);
    h.redo();  checkStacks(h, x2,   null);
    h.add(x3);          checkStacks(h, x3,   null);
    h.undo();  checkStacks(h, x2,   x3);
    h.undo();  checkStacks(h, x1,   x2);
  }

  // these must be fields so that they can be changed by the instances
  // of the inner class TestThatMethodsArePerformed.
  private boolean didRun;
  private boolean didUndo;
  private boolean didRedo;

  @Test
  public void methodsArePerformed() {
    CommandHistoryObj h = new CommandHistoryObj();

    class MockCommand implements Command {
      // Using "CommandHistoryTEST.this" to make references to
      // outer class instance explicit
      public boolean run() {
        CommandHistoryTEST.this.didRun = true;
        return true;
      }
      public void undo() {
        CommandHistoryTEST.this.didUndo = true;
      }
      public void redo() {
        CommandHistoryTEST.this.didRedo = true;
      }
    }

    Command x = new MockCommand();

    didRun = didUndo = didRedo = false;
    h.add(x);
    assertTrue(!didRun && !didUndo && !didRedo);

    didRun = didUndo = didRedo = false;
    h.undo();
    assertTrue(!didRun && didUndo && !didRedo);

    didRun = didUndo = didRedo = false;
    h.redo();
    assertTrue(!didRun && !didUndo && didRedo);
  }
}

Previous pageContentsNext page