Ignore:
Timestamp:
2015-08-15T12:30:24+02:00 (9 years ago)
Author:
nokutu
Message:

More tests for history record system.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/history/MapillaryRecordTest.java

    r31492 r31500  
    22
    33import static org.junit.Assert.assertEquals;
     4import static org.junit.Assert.fail;
    45
    56import java.util.Arrays;
     
    910import org.openstreetmap.josm.plugins.mapillary.AbstractTest;
    1011import org.openstreetmap.josm.plugins.mapillary.MapillaryAbstractImage;
     12import org.openstreetmap.josm.plugins.mapillary.MapillaryData;
    1113import org.openstreetmap.josm.plugins.mapillary.MapillaryImage;
     14import org.openstreetmap.josm.plugins.mapillary.MapillaryLayer;
    1215import org.openstreetmap.josm.plugins.mapillary.history.MapillaryRecord;
     16import org.openstreetmap.josm.plugins.mapillary.history.commands.CommandDelete;
     17import org.openstreetmap.josm.plugins.mapillary.history.commands.CommandImport;
     18import org.openstreetmap.josm.plugins.mapillary.history.commands.CommandJoin;
    1319import org.openstreetmap.josm.plugins.mapillary.history.commands.CommandMove;
    1420import org.openstreetmap.josm.plugins.mapillary.history.commands.CommandTurn;
     21import org.openstreetmap.josm.plugins.mapillary.history.commands.CommandUnjoin;
    1522import org.openstreetmap.josm.plugins.mapillary.history.commands.MapillaryCommand;
    1623
     
    3845    this.img2 = new MapillaryImage("key2", 0.2, 0.2, 0.2);
    3946    this.img3 = new MapillaryImage("key3", 0.3, 0.3, 0.3);
     47    MapillaryLayer.getInstance().getData().getImages().clear();
    4048  }
    4149
     
    5563        0.1, 0.1);
    5664    MapillaryCommand cmd1 = new CommandMove(
    57         Arrays.asList(new MapillaryAbstractImage[] { this.img1 }),
    58         0.1, 0.1);
     65        Arrays.asList(new MapillaryAbstractImage[] { this.img1 }), 0.1, 0.1);
    5966    MapillaryCommand cmd31 = new CommandMove(
    6067        Arrays.asList(new MapillaryAbstractImage[] { this.img3, this.img1 }),
     
    98105
    99106  /**
    100    * Tests CommandMoveImage class.
     107   * Tests {@link CommandMove} class.
    101108   */
    102109  @Test
     
    132139
    133140  /**
    134    * Tests CommandTurnImage class.
     141   * Tests {@link CommandTurn} class.
    135142   */
    136143  @Test
     
    161168    assertEquals(0.1, this.img1.getCa(), 0.01);
    162169  }
     170
     171  /**
     172   * Tests {@link CommandJoin} class.
     173   */
     174  @Test
     175  public void commandJoinClass() {
     176    CommandJoin cmd1 = new CommandJoin(
     177        Arrays.asList(new MapillaryAbstractImage[] { this.img1, this.img2 }));
     178    CommandJoin cmd2 = new CommandJoin(
     179        Arrays.asList(new MapillaryAbstractImage[] { this.img2, this.img3 }));
     180
     181    this.record.addCommand(cmd1);
     182    assertEquals(2, this.img1.getSequence().getImages().size());
     183    assertEquals(this.img2, this.img1.next());
     184    this.record.undo();
     185    assertEquals(1, this.img1.getSequence().getImages().size());
     186    this.record.redo();
     187    this.record.addCommand(cmd2);
     188    assertEquals(3, this.img1.getSequence().getImages().size());
     189    assertEquals(this.img3, this.img1.next().next());
     190
     191    try {
     192      this.record.addCommand(new CommandJoin(Arrays
     193          .asList(new MapillaryAbstractImage[] { this.img1, this.img2,
     194              this.img3 })));
     195      fail();
     196    } catch (IllegalArgumentException e) {
     197      // Expected output.
     198    } catch (Exception e) {
     199      fail();
     200    }
     201  }
     202
     203  /**
     204   * Tests {@link CommandUnjoin} class.
     205   */
     206  @Test
     207  public void commandUnjoinClass() {
     208    CommandJoin join1 = new CommandJoin(
     209        Arrays.asList(new MapillaryAbstractImage[] { this.img1, this.img2 }));
     210    CommandJoin join2 = new CommandJoin(
     211        Arrays.asList(new MapillaryAbstractImage[] { this.img2, this.img3 }));
     212
     213    CommandUnjoin cmd1 = new CommandUnjoin(
     214        Arrays.asList(new MapillaryAbstractImage[] { this.img1, this.img2 }));
     215    CommandUnjoin cmd2 = new CommandUnjoin(
     216        Arrays.asList(new MapillaryAbstractImage[] { this.img2, this.img3 }));
     217
     218    this.record.addCommand(join1);
     219    this.record.addCommand(join2);
     220
     221    this.record.addCommand(cmd1);
     222    assertEquals(1, this.img1.getSequence().getImages().size());
     223    this.record.undo();
     224    assertEquals(3, this.img1.getSequence().getImages().size());
     225    this.record.redo();
     226    this.record.addCommand(cmd2);
     227    assertEquals(1, this.img1.getSequence().getImages().size());
     228    assertEquals(1, this.img2.getSequence().getImages().size());
     229
     230    try {
     231      this.record.addCommand(new CommandUnjoin(Arrays
     232          .asList(new MapillaryAbstractImage[] { this.img1, this.img2,
     233              this.img3 })));
     234      fail();
     235    } catch (IllegalArgumentException e) {
     236      // Expected output.
     237    } catch (Exception e) {
     238      fail();
     239    }
     240  }
     241
     242  /**
     243   * Test {@link CommandDelete} class.
     244   */
     245  @Test
     246  public void commandDeleteTest() {
     247    CommandJoin join1 = new CommandJoin(
     248        Arrays.asList(new MapillaryAbstractImage[] { this.img1, this.img2 }));
     249    CommandJoin join2 = new CommandJoin(
     250        Arrays.asList(new MapillaryAbstractImage[] { this.img2, this.img3 }));
     251
     252    CommandDelete cmd1 = new CommandDelete(
     253        Arrays.asList(new MapillaryAbstractImage[] { this.img1 }));
     254    CommandDelete cmd2 = new CommandDelete(
     255        Arrays.asList(new MapillaryAbstractImage[] { this.img2, this.img3 }));
     256
     257    this.record.addCommand(join1);
     258    this.record.addCommand(join2);
     259
     260    MapillaryLayer
     261        .getInstance()
     262        .getData()
     263        .add(
     264            Arrays.asList(new MapillaryAbstractImage[] { this.img1, this.img2,
     265                this.img3 }));
     266
     267    this.record.addCommand(cmd1);
     268    assertEquals(false, MapillaryLayer.getInstance().getData().getImages()
     269        .contains(this.img1));
     270    assertEquals(null, this.img2.previous());
     271    this.record.undo();
     272    assertEquals(true, MapillaryLayer.getInstance().getData().getImages()
     273        .contains(this.img1));
     274    this.record.redo();
     275    this.record.addCommand(cmd2);
     276    assertEquals(0, MapillaryLayer.getInstance().getData().size());
     277  }
     278
     279  /**
     280   * Test {@link CommandImport} class.
     281   */
     282  @Test
     283  public void commandImportTest() {
     284    MapillaryData data = MapillaryLayer.getInstance().getData();
     285    data.remove(data.getImages());
     286
     287    CommandImport cmd1 = new CommandImport(
     288        Arrays.asList(new MapillaryAbstractImage[] { this.img1 }));
     289    CommandImport cmd2 = new CommandImport(
     290        Arrays.asList(new MapillaryAbstractImage[] { this.img2, this.img3 }));
     291
     292    this.record.addCommand(cmd1);
     293    assertEquals(1, data.size());
     294    this.record.undo();
     295    assertEquals(0, data.size());
     296    this.record.redo();
     297    this.record.addCommand(cmd2);
     298    assertEquals(3, data.size());
     299  }
    163300}
Note: See TracChangeset for help on using the changeset viewer.