Section 29.3. Two-Dimensional Images


29.3. Two-Dimensional Images

Section 11.3 on p. 83 introduced the use of images in a two-dimensional structure. The fixture StartSokoban for this is shown in Listing 29.3. The method board() carries out the following sequence:

1.

Collects a representation of the board, as an int[][]

2.

Maps that to the file names of corresponding images, as a String[][]

3.

Passes the resulting String[][] to a newly created ImageFixture

4.

Returns that fixture

ImageFixture takes a String[][] as an expected argument and attempts to match the elements against the files names of the images in the cells in the rest of the Fit table. If they match, the test succeeds. Otherwise, the ImageFixture adds rows to the table in the report to show the actual array of images.

The ImageFixture class is covered in Section 38.3 on p. 314.

Listing 29.3. StartSokoban.java
 public class StartSokoban extends DoFixture {    private static final String PREFIX = "/files/images/";    private Sokoban sokoban = new Sokoban();    public Fixture board() {       int[][] pieces = sokoban.ge2DArrayOfValues();       String[][]imageFileName = new String[pieces.length][];       for (int row = 0; row < pieces.length; row++) {          imageFileName[row] = new String[pieces[row].length];          for (int col = 0; col < pieces[row].length; col++)             imageFileName[row][col] = PREFIX+map(pieces[row][col]);       }       return new fit.ImageFixture(imageFileName);    }    private String map(int piece) {       switch(piece) {         case Sokoban.s: return "space.jpg";          case Sokoban.e: return "shelf.jpg";          case Sokoban.f: return "shelfwithbox.jpg";          case Sokoban.b: return "box.jpg";          case Sokoban.w: return "worker.jpg";          case Sokoban.W: return "wall.jpg";       }       throw new RuntimeException("Unknown piece");    } } 



    Fit for Developing Software. Framework for Integrated Tests
    Fit for Developing Software: Framework for Integrated Tests
    ISBN: 0321269349
    EAN: 2147483647
    Year: 2005
    Pages: 331

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net