Subversion Repositories sokoban

Rev

Blame | Last modification | View Log | RSS feed

  1. package gdi.ws0809.test;
  2.  
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertEquals;
  5. import static org.junit.Assert.assertTrue;
  6. import static org.junit.Assert.fail;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.File;
  10. import java.io.FileReader;
  11. import java.io.IOException;
  12. import java.net.URISyntaxException;
  13.  
  14. import org.junit.Before;
  15. import org.junit.BeforeClass;
  16. import org.junit.Test;
  17.  
  18. /**
  19. * Public tests for the 2nd extended Sokoban implementation level
  20. *
  21. * @author Steven Arzt, Oren Avni
  22. * @version 1.0
  23. */
  24. public class SokobanTestExtended2 {
  25.  
  26.                 /**
  27.                  * the tested Sokoban implementation
  28.                  */
  29.                 private SokobanTest testee = null;
  30.                
  31.                
  32.                 private static File validLevel;
  33.                 private static final String validLevelString = "####\n# .#\n#  ###\n#*@  #\n#  $ #\n#  ###\n####\n";
  34.                 private static final String validLevelMovedString = "####\n# .#\n#  ###\n#* @ #\n#  $ #\n#  ###\n####\n";
  35.                 private static File highScoreFile;
  36.  
  37.                 private static File levelDir;
  38.                 private static File saveFile;
  39.  
  40.                
  41.                 @BeforeClass
  42.                 public static void init() {
  43.                         try {
  44.                                 validLevel = new File(ClassLoader.getSystemClassLoader().getResource("valid.txt").toURI());
  45.                                 levelDir = new File(ClassLoader.getSystemClassLoader().getResource("testlevel").toURI());
  46.                                 saveFile = new File(levelDir, "save.sok");
  47.                                 highScoreFile = new File(levelDir, "highscore.txt");
  48.                         } catch (final URISyntaxException e) {
  49.                                 e.printStackTrace();
  50.                         }
  51.                         SokobanTestAdapter.init();
  52.                 }
  53.                
  54.                 @Before
  55.                 public void before() {
  56.                         testee = new SokobanTestAdapter();
  57.                 }
  58.                
  59.                
  60.                 /**
  61.                  * Requirement "undo/redo"
  62.                  */
  63.                 @Test
  64.                 public void undo() throws Exception {
  65.                         testee.loadLevel(validLevel);
  66.                         testee.moveWorker('R');
  67.                         testee.undoLastMove();
  68.                         assertEquals("Level should be in initial state after undo.",validLevelString,testee.currentLevelToString());
  69.                 }
  70.                
  71.                 @Test(expected=Exception.class)
  72.                 public void undoFail() throws Exception {
  73.                         testee.loadLevel(validLevel);
  74.                         testee.undoLastMove();
  75.                 }
  76.                
  77.                 @Test
  78.                 public void redo() throws Exception {
  79.                         testee.loadLevel(validLevel);
  80.                         testee.moveWorker('R');
  81.                         testee.undoLastMove();
  82.                         testee.redoLastUndoneMove();
  83.                         assertEquals("Move should be performed after undo / redo.",validLevelMovedString,testee.currentLevelToString());
  84.                 }
  85.                
  86.                 private String textFileToString
  87.                         (final File file)
  88.                 {
  89.                         BufferedReader rdr = null;
  90.                         try
  91.                         {
  92.                                 try
  93.                                 {
  94.                                         rdr = new BufferedReader (new FileReader (file));
  95.                                         String s = "", p = "";
  96.                                         while ((p = rdr.readLine()) != null)
  97.                                                 s += p + "\n";
  98.                                         return s;
  99.                                 }
  100.                                 finally
  101.                                 {
  102.                                         if (rdr != null)
  103.                                                 rdr.close();
  104.                                 }
  105.                         }
  106.                         catch (final IOException ex)
  107.                         {
  108.                                 fail ("Could not read text file");
  109.                                 return null;
  110.                         }
  111.                 }
  112.                
  113.                 /**
  114.                  * Requirement "saveGame" & "loadGame"
  115.                  * @throws Exception
  116.                  */
  117.                 @Test
  118.                 public void saveGame() throws Exception {
  119.                         testee.setLevelDir(levelDir);
  120.                         testee.startNextLevel();
  121.                         testee.saveGame(saveFile);
  122.                         testee.moveWorker('R');
  123.                         final long timestamp = saveFile.lastModified();
  124.                         final String data = textFileToString (saveFile);
  125.                         testee.saveGame(saveFile);
  126.                         assertFalse("Nothing has been written to save file", timestamp == 0L);
  127.                         assertFalse("Saved level data not changed",
  128.                                         textFileToString (saveFile).equals (data));
  129.                 }
  130.  
  131.                 @Test
  132.                 public void loadGame() throws Exception {
  133.                         testee.setLevelDir(levelDir);
  134.                         testee.startNextLevel();
  135.                         testee.moveWorker('R');
  136.                         testee.setPlayerName("playername");
  137.                         testee.saveGame(saveFile);
  138.                         testee.loadLevel(validLevel);
  139.                         testee.loadGame(saveFile);
  140.                         assertEquals("Grid state properly loaded.",validLevelMovedString,testee.currentLevelToString());
  141.                         assertEquals("Step count not properly loaded.", 1, testee.getStepsInCurrentLevel());
  142.                 }
  143.  
  144.                 /**
  145.                  * Requirement "highscoreTime"
  146.                  */
  147.                 @Test
  148.                 public void solutionTime() throws Exception {
  149.                         // delete the old highscore file
  150.                         if (highScoreFile.exists())
  151.                                 highScoreFile.delete();
  152.                        
  153.                         // load the level directory and skip the first two levels
  154.                         // in the middle, set the player name
  155.                         testee.setLevelDir(levelDir);
  156.                         testee.startNextLevel();
  157.                         testee.setPlayerName("testplayer");
  158.                         testee.startNextLevel();
  159.                        
  160.                         // load the third level, solve it 1.5 seconds and take the exact
  161.                         // time. Afterwards, write the highscore file to disk.
  162.                         testee.startNextLevel();
  163.                         final long timestamp = System.currentTimeMillis();
  164.                         Thread.sleep(1500);
  165.                         testee.moveWorker('R');
  166.                         final long delta = System.currentTimeMillis() - timestamp;
  167.                         try {
  168.                                 testee.startNextLevel();
  169.                         } catch(final Exception e) {
  170.                         }
  171.                         testee.writeHighScoreFile();
  172.  
  173.                         // Check that a file has actually been written
  174.                         assertTrue ("No highscore file written", highScoreFile.exists());
  175.  
  176.                         // Read in the file and check for empty data
  177.                         final BufferedReader br = new BufferedReader(new FileReader(highScoreFile));
  178.                         final String line = br.readLine();
  179.                         assertFalse ("Empty highscore file", line == null);
  180.                         assertFalse ("Empty line in highscore file", line.equals (""));
  181.                        
  182.                         // Get the data and close the file
  183.                         final String[] parts = line.split("\t");
  184.                         br.close();
  185.                        
  186.                         // Compare the time recorded in the highscore file with the measured
  187.                         // one
  188.                         final int timeTaken = Integer.parseInt(parts[3]);
  189.                         final float diff = Math.abs ((timeTaken * 1000) - delta);
  190.                         assertTrue("Measured time differs more than 1s (" + diff + "ms) from actual passed time.", diff < 1000.0);
  191.                 }
  192.                
  193.                 @Test
  194.                 public void moveSequence() throws Exception {
  195.                         testee.setLevelDir(levelDir);
  196.                         testee.startNextLevel();
  197.                         testee.moveWorkerSequence("U");
  198.                 }
  199.                        
  200.        
  201. }
  202.