package cicaprojekt; import java.io.File; import java.io.IOException; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; public class Tester { private Player oneill; private Player jaffa; private PlayerBase replicator; Dungeon loadMap(String param) throws IOException{ Dungeon dungeon = new Dungeon(); Map playerLocations = dungeon.buildDungeon(new File(param)); oneill = new Player("ONeill", (Tile) playerLocations.get("oneill"), Direction.NORTH); jaffa = new Player("Jaffa", (Tile) playerLocations.get("jaffa"), Direction.NORTH); replicator = new PlayerBase("Replicator", (Tile) playerLocations.get("replicator"), Direction.NORTH); return dungeon; } String listPlayers() { return oneill.toString() + " " + jaffa.toString() + " " + replicator.toString(); } void move(String param) { switch (param) { case "O" : oneill.move(oneill.getFacingDirection()); break; case "J" : jaffa.move(jaffa.getFacingDirection()); break; default: //TODO error handling break; } } String boxLift(String param) { switch (param) { case "O" : oneill.boxLift(); break; case "J" : jaffa.boxLift(); break; } return "box lifted"; } String boxDrop(String param) { switch (param) { case "O" : oneill.boxDrop(); break; case "J" : jaffa.boxDrop(); break; } return "box dropped"; } void shootONeillsGun(String param) { switch (param) { case "B" : oneill.shootStargate(Stargate.blueStargate); break; case "Y" : oneill.shootStargate(Stargate.yellowStargate); break; } } String rotate(String playerParam, String directionParam) { switch (playerParam) { case "O" : switch (directionParam) { case "L" : oneill.rotateLeft(); return oneill.getFacingDirection().toString(); case "R" : oneill.rotateRight(); return oneill.getFacingDirection().toString(); default: return "Hiba, nem fordult!"; } case "J" : switch (directionParam) { case "L" : jaffa.rotateLeft(); return jaffa.getFacingDirection().toString(); case "R" : jaffa.rotateRight(); return jaffa.getFacingDirection().toString(); default: return "Hiba, nem fordult!"; } default: return "Hiba, nem létező játékos!"; } } String listStargates() { String blue = Stargate.blueStargate.toString(); String yellow = Stargate.yellowStargate.toString(); String red = Stargate.redStargate.toString(); String green = Stargate.greenStargate.toString(); return blue + " " + yellow + " " + red + " " + green; } String getConnectedGateOpen(Scale s){ if(s.getGateConnected().isOpen()) return "gate open"; else return "gate closed"; } /* custom Test annotation */ @Target(ElementType.METHOD) // it's for methods @Retention(RetentionPolicy.RUNTIME) // we want to retain annotations in runtime private @interface Test {} /* run all methods annotated with @Test */ public boolean runAllTests() throws InvocationTargetException, IllegalAccessException { boolean testspassed = true; for (Method m : this.getClass().getMethods()) // iterate over all methods of this { if (m.isAnnotationPresent(Test.class)) // if its annotated with @Test... { Boolean testresult = (Boolean) m.invoke(null); // call it! if (!testresult) { testspassed = false; } } } return testspassed; } @Test boolean moveTest() throws IOException{ boolean success = true; loadMap("moveTest.dungeon"); String listOfPlayers = listPlayers(); if(!listOfPlayers.equals("ONeill: 1, 1 Jaffa: -666, -666 Replicator: -666, -666")) success = false; move("O"); listOfPlayers = listPlayers(); if(!listOfPlayers.equals("ONeill: 1, 2 Jaffa: -666, -666 Replicator: -666, -666")) success = false; return success; } @Test boolean testStargates() throws IOException{ boolean success = true; loadMap("testStargates.dungeon"); shootONeillsGun("B"); rotate("O", "L"); shootONeillsGun("Y"); String listOfStargates = listStargates(); if(!listOfStargates.equals("BlueStargate: 5, 10 YellowStargate: 10, 5 RedStargate: not spawned GreenStargate: not spawned")) success = false; move("O"); move("O"); move("O"); String listOfPlayers = listPlayers(); if(!listOfPlayers.equals("ONeill: 5, 9 Jaffa: -666, -666 Replicator: -666, -666")) success = false; return success; } @Test boolean testScalesAndGates() throws IOException { boolean success = true; loadMap("testScalesAndGates.dungeon"); boxLift("O"); rotate("O", "L"); boxDrop("O"); String gateOpen = getConnectedGateOpen((Scale)oneill.getCurrentTile().getAdjacentTile(oneill.facingDirection)); if(!gateOpen.equals("gate open")) success = false; return success; } @Test boolean gapTest() throws IOException{ loadMap("gapTest.dungeon"); move("O"); boolean success = oneill.isDestroyed(); return success; } @Test boolean ZPMTest() throws IOException{ boolean success = true; loadMap("ZPMTest.dungeon"); move("O"); if(oneill.getZPMCount() != 1) success = false; return success; } @Test boolean testReplicatorPosition(){ String[] commands = {"loadMap testReplicatorPosition\n", "move O\n"}; String[] expectedOutputs = {}; boolean results = testOnSequenceOfCommands(commands, expectedOutputs); return results; } @Test boolean timeUpTest(){ String[] commands = {"loadMap timeUpTest\n"}; String[] expectedOutputs = {}; boolean results = testOnSequenceOfCommands(commands, expectedOutputs); return results; } @Test boolean rotationTest() throws IOException { boolean success = true; loadMap("rotationTest.dungeon"); if(!rotate("O", "L").equals("WEST")) success = false; if(!rotate("O", "R").equals("NORTH")) success = false; return success; } @Test boolean testBoxes() throws IOException{ boolean success = true; loadMap("testBoxes.dungeon"); if(!boxLift("O").equals("box lifted")) success = false; if(!boxDrop("O").equals("box dropped")) success = false; return success; } private boolean testOnSequenceOfCommands(String[] commands, String[] expectedOutputs) { return true; } }