package puzzleTests; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import junit.framework.TestCase; import model.playerModel.PlayerModel; public class Tests extends TestCase { public static PlayerModel getFakePlayerModel() { PlayerModel pm = new PlayerModel(); pm.logGame(25); pm.logGame(30); pm.logGame(26); pm.logGame(34); pm.logGame(22); pm.logGame(30); pm.logGame(60); return pm; } public void testHighScores() { PlayerModel pm = getFakePlayerModel(); int[] highScores = pm.getHighScores(); assertEquals(highScores.length, 10); assertEquals(highScores[0], 60); assertEquals(highScores[1], 34); assertEquals(highScores[2], 30); assertEquals(highScores[3], 30); assertEquals(highScores[4], 26); assertEquals(highScores[5], 25); assertEquals(highScores[6], 22); assertEquals(highScores[7], -1); assertEquals(highScores[8], -1); assertEquals(highScores[9], -1); } public void testSerializationInput() { PlayerModel pm = getFakePlayerModel(); FileOutputStream fout = null; ObjectOutputStream oout = null; FileInputStream fin = null; ObjectInputStream oin = null; try { fout = new FileOutputStream("test/PlayerModel.dat"); oout = new ObjectOutputStream(fout); oout.writeObject(pm); oout.close(); } catch (IOException ex) { ex.printStackTrace(); fail("Exception"); } pm = null; try { fin = new FileInputStream("test/PlayerModel.dat"); oin = new ObjectInputStream(fin); pm = (PlayerModel) oin.readObject(); oin.close(); int[] highScores = pm.getHighScores(); assertEquals(highScores.length, 10); assertEquals(highScores[0], 60); assertEquals(highScores[1], 34); assertEquals(highScores[2], 30); assertEquals(highScores[3], 30); assertEquals(highScores[4], 26); assertEquals(highScores[5], 25); assertEquals(highScores[6], 22); assertEquals(highScores[7], -1); assertEquals(highScores[8], -1); assertEquals(highScores[9], -1); } catch (FileNotFoundException e) { e.printStackTrace(); fail("Exception"); } catch (IOException e) { e.printStackTrace(); fail("Exception"); } catch (ClassNotFoundException e) { e.printStackTrace(); fail("Exception"); } } public void testSerializationOutput() { PlayerModel pm = getFakePlayerModel(); FileOutputStream fout = null; ObjectOutputStream oout = null; try { fout = new FileOutputStream("test/PlayerModel.dat"); oout = new ObjectOutputStream(fout); oout.writeObject(pm); oout.close(); assertTrue((new File("test/playerModel.dat")).exists()); } catch (IOException ex) { ex.printStackTrace(); fail("Exception"); } } }