/src/test/java/de/javauni/jarcade/control/ControlManagementTest.java

https://github.com/wabu/hank · Java · 142 lines · 98 code · 18 blank · 26 comment · 9 complexity · e6e77d90ca7cbb2142e2716fa98a10fc MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 kmochi
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package de.javauni.jarcade.control;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.util.logging.Logger;
  21. import com.google.inject.Guice;
  22. import com.google.inject.Injector;
  23. import de.javauni.jarcade.control.controlmanagement.ControlDataIsCorruptExeption;
  24. import de.javauni.jarcade.control.controlmanagement.ControlEvent;
  25. import de.javauni.jarcade.control.controlmanagement.ControlManagement;
  26. import de.javauni.jarcade.control.controlmanagement.ControlManagementImpl;
  27. import de.javauni.jarcade.control.controlmanagement.CouldNotLoadExeption;
  28. import de.javauni.jarcade.control.controlmanagement.CouldNotSaveExeption;
  29. import de.javauni.jarcade.control.playercontrol.PlayerControlMap;
  30. import de.javauni.jarcade.utils.Pair;
  31. import java.io.BufferedReader;
  32. import java.io.File;
  33. import java.io.FileInputStream;
  34. import java.io.InputStreamReader;
  35. import java.util.Set;
  36. import java.util.logging.Level;
  37. import org.junit.After;
  38. import org.junit.AfterClass;
  39. import org.junit.Before;
  40. import org.junit.BeforeClass;
  41. import org.junit.Test;
  42. import static org.junit.Assert.*;
  43. import de.javauni.yarrish.MainModelModule;
  44. /**
  45. *
  46. * @author kmochi
  47. */
  48. public class ControlManagementTest {
  49. PlayerControlMap keymap;
  50. ControlManagement manager;
  51. public ControlManagementTest() {
  52. }
  53. @BeforeClass
  54. public static void setUpClass() throws Exception {
  55. }
  56. @AfterClass
  57. public static void tearDownClass() throws Exception {
  58. }
  59. @Before
  60. // COMMENT(wabu): @Inject does not work for junit tests
  61. public void setUp() { // PlayerControlMap keymap, ControlManagement manager) {
  62. Injector inj = Guice.createInjector(new ControlModule(), new MainModelModule());
  63. this.keymap = inj.getInstance(PlayerControlMap.class);
  64. this.manager = inj.getInstance(ControlManagement.class);
  65. }
  66. @After
  67. public void tearDown() {
  68. }
  69. @Test
  70. public void loadAndSave() {
  71. keymap.put(1, new Pair<Integer, ControlEvent>(1, ControlEvent.Jump));
  72. keymap.put(2, new Pair<Integer, ControlEvent>(1, ControlEvent.MoveLeft));
  73. keymap.put(3, new Pair<Integer, ControlEvent>(1, ControlEvent.MoveRight));
  74. try {
  75. manager.save();
  76. manager.load();
  77. } catch (CouldNotLoadExeption ex) {
  78. Logger.getLogger(ControlManagementImpl.class.getName()).log(Level.SEVERE, null, ex);
  79. } catch (ControlDataIsCorruptExeption ex) {
  80. Logger.getLogger(ControlManagementImpl.class.getName()).log(Level.SEVERE, null, ex);
  81. } catch (CouldNotSaveExeption ex) {
  82. Logger.getLogger(ControlManagementImpl.class.getName()).log(Level.SEVERE, null, ex);
  83. }
  84. Set<Integer> set = keymap.keySet();
  85. for (Integer i : set) {
  86. Pair<Integer, ControlEvent> a = keymap.get(i);
  87. System.out.println(i + " " + keymap.get(i).fst + " " + keymap.get(i).snd);
  88. if (i == 1) {
  89. assertTrue((i + " " + keymap.get(i).fst + " " + keymap.get(i).snd).equals("1 1 Jump"));
  90. } else if (i == 2) {
  91. assertTrue((i + " " + keymap.get(i).fst + " " + keymap.get(i).snd).equals("2 1 MoveLeft"));
  92. } else if (i == 3) {
  93. assertTrue((i + " " + keymap.get(i).fst + " " + keymap.get(i).snd).equals("3 1 MoveRight"));
  94. }
  95. }
  96. }
  97. @Test
  98. public void save() throws IOException{
  99. keymap.put(1, new Pair<Integer, ControlEvent>(1, ControlEvent.Jump));
  100. keymap.put(2, new Pair<Integer, ControlEvent>(1, ControlEvent.MoveLeft));
  101. keymap.put(3, new Pair<Integer, ControlEvent>(1, ControlEvent.MoveRight));
  102. File file = new File("Keyboard.ini");
  103. System.out.println(file.exists());
  104. try {
  105. manager.save(file.getAbsolutePath());
  106. } catch (CouldNotSaveExeption ex) {
  107. Logger.getLogger(ControlManagementTest.class.getName()).log(Level.SEVERE, null, ex);
  108. }
  109. try {
  110. BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
  111. assertTrue(br.readLine().equals("1\t1\tJump"));
  112. assertTrue(br.readLine().equals("2\t1\tMoveLeft"));
  113. assertTrue(br.readLine().equals("3\t1\tMoveRight"));
  114. } catch (FileNotFoundException ex) {
  115. Logger.getLogger(ControlManagementTest.class.getName()).log(Level.SEVERE, null, ex);
  116. }
  117. System.out.println(keymap.hashCode());
  118. }
  119. }
  120. // TODO add test methods here.
  121. // The methods must be annotated with annotation @Test. For example:
  122. //
  123. // @Test
  124. // public void hello() {}