Merge remote-tracking branch 'origin/main' into Eversmann

Eversmann
dustineversmann 2025-01-07 12:52:08 +01:00
commit 885bc6d25c
4 changed files with 240 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package de.deversmann.domain;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class HighscoreEntryTest {
@Test
void testHighscoreEntryProperties() {
HighscoreEntry entry = new HighscoreEntry("Alice", 120, 3);
assertEquals("Alice", entry.getPlayerName());
assertEquals(120, entry.getTimeSeconds());
assertEquals(3, entry.getErrorCount());
}
@Test
void testToString() {
HighscoreEntry entry = new HighscoreEntry("Alice", 120, 3);
assertEquals("Alice - 120s (Fehler: 3)", entry.toString());
}
}

View File

@ -0,0 +1,22 @@
package de.deversmann.domain;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ZeiterfassungTest {
@Test
void testZeiterfassungStartStop() throws InterruptedException {
Zeiterfassung timer = new Zeiterfassung();
timer.start();
Thread.sleep(1000); // 1 Sekunde warten
timer.stop();
long elapsedTime = timer.getElapsedTimeInSeconds();
assertTrue(elapsedTime >= 1 && elapsedTime < 2, "Elapsed time should be approximately 1 second.");
}
}

View File

@ -0,0 +1,153 @@
package de.deversmann.gui;
import de.deversmann.facade.Facade;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import javax.swing.*;
import java.io.IOException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class GameViewTest {
private Facade facadeMock;
private GameView gameView;
@BeforeEach
void setup() {
facadeMock = mock(Facade.class);
gameView = new GameView(facadeMock);
}
@Test
void testInitComponents() {
assertNotNull(gameView.cbSpielfelder);
assertNotNull(gameView.btnLoad);
assertNotNull(gameView.btnRandom);
assertNotNull(gameView.btnCheckSolution);
assertNotNull(gameView.btnShowSolution);
assertNotNull(gameView.btnReset);
assertNotNull(gameView.timeLabel);
assertNotNull(gameView.spielfeldGUI);
}
@Test
void testLoadPuzzleActionWithRealData() throws Exception {
doNothing().when(facadeMock).ladeSpielfeld("4x4.csv");
gameView.ladeSpielfeld("4x4.csv");
verify(facadeMock, times(1)).ladeSpielfeld("4x4.csv");
}
@Test
void testLoadInvalidPuzzle() throws IOException {
doThrow(new IOException("Invalid file")).when(facadeMock).ladeSpielfeld("invalid.csv");
gameView.ladeSpielfeld("invalid.csv");
verify(facadeMock, times(1)).ladeSpielfeld("invalid.csv");
}
@Test
void testCheckSolutionWithCorrectData() {
var blackCells = List.of(new int[]{0, 0});
var solutionCoordinates = List.of(new int[]{1, 1});
when(facadeMock.getLoesungsKoordinaten()).thenReturn(solutionCoordinates);
when(facadeMock.getAktuellesPuzzle()).thenReturn(new int[][]{{1, 2}});
SpielfeldGUI spielfeldGUIMock = mock(SpielfeldGUI.class);
when(spielfeldGUIMock.getBlackCells()).thenReturn(blackCells);
gameView.spielfeldGUI = spielfeldGUIMock;
gameView.checkSolution();
verify(spielfeldGUIMock, never()).markCellAsError(anyInt(), anyInt());
verify(facadeMock, times(1)).stopZeiterfassung();
verify(facadeMock, times(1)).addHighscoreForCurrentPuzzle(anyString(), anyLong(), anyInt());
}
@Test
void testCheckSolutionWithErrors() {
var blackCells = List.of(new int[]{0, 1});
var solutionCoordinates = List.of(new int[]{1, 1});
when(facadeMock.getLoesungsKoordinaten()).thenReturn(solutionCoordinates);
when(facadeMock.getAktuellesPuzzle()).thenReturn(new int[][]{{1, 2}});
SpielfeldGUI spielfeldGUIMock = mock(SpielfeldGUI.class);
when(spielfeldGUIMock.getBlackCells()).thenReturn(blackCells);
gameView.spielfeldGUI = spielfeldGUIMock;
gameView.checkSolution();
verify(spielfeldGUIMock, times(1)).markCellAsError(0, 1);
verify(facadeMock, times(1)).incrementErrorCount();
}
@Test
void testShowSolution() {
int[][] mockGrid = {{1, 2}, {3, 4}};
List<int[]> solution = List.of(new int[]{1, 1}, new int[]{2, 2});
when(facadeMock.getAktuellesPuzzle()).thenReturn(mockGrid);
when(facadeMock.getLoesungsKoordinaten()).thenReturn(solution);
SpielfeldGUI spielfeldGUIMock = mock(SpielfeldGUI.class);
gameView.spielfeldGUI = spielfeldGUIMock;
gameView.showSolution();
verify(spielfeldGUIMock, times(1)).updateGrid(mockGrid);
verify(spielfeldGUIMock, times(1)).setCellBlack(0, 0); // Lösung wird schwarz markiert
verify(spielfeldGUIMock, times(1)).setCellBlack(1, 1);
verify(spielfeldGUIMock, times(1)).setAllNonBlackToWhite();
}
@Test
void testResetField() {
int[][] mockGrid = {{1, 2}, {3, 4}};
when(facadeMock.getAktuellesPuzzle()).thenReturn(mockGrid);
SpielfeldGUI spielfeldGUIMock = mock(SpielfeldGUI.class);
gameView.spielfeldGUI = spielfeldGUIMock;
gameView.resetField();
verify(spielfeldGUIMock, times(1)).updateGrid(mockGrid);
assertEquals("Time: 0s", gameView.timeLabel.getText());
}
@Test
void testTimerUpdatesTimeLabel() {
when(facadeMock.getElapsedTimeSoFar()).thenReturn(42L);
Timer timer = gameView.timer;
assertNotNull(timer);
timer.getActionListeners()[0].actionPerformed(null);
JLabel timeLabel = gameView.timeLabel;
assertEquals("Time: 42s", timeLabel.getText());
}
@Test
void testRandomPuzzleLoading() throws Exception {
doNothing().when(facadeMock).ladeSpielfeld(anyString());
gameView.btnRandom.doClick();
verify(facadeMock, atLeastOnce()).ladeSpielfeld(anyString());
}
@Test
void testContainsCell() {
List<int[]> blackCells = List.of(new int[]{1, 1}, new int[]{2, 2});
assertTrue(gameView.containsCell(blackCells, "1,1"));
assertFalse(gameView.containsCell(blackCells, "3,3"));
}
}

View File

@ -0,0 +1,41 @@
package de.deversmann.gui;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class SpielfeldGUITest {
@Test
void testUpdateGrid() {
SpielfeldGUI gui = new SpielfeldGUI(2, 2);
int[][] grid = {
{1, 2},
{3, 4}
};
gui.updateGrid(grid);
assertNotNull(gui.getBlackCells());
assertEquals(0, gui.getBlackCells().size());
}
@Test
void testSetCellBlack() {
SpielfeldGUI gui = new SpielfeldGUI(2, 2);
gui.setCellBlack(0, 1);
assertEquals(1, gui.getBlackCells().size());
assertArrayEquals(new int[]{0, 1}, gui.getBlackCells().get(0));
}
@Test
void testMarkCellAsError() {
SpielfeldGUI gui = new SpielfeldGUI(2, 2);
gui.markCellAsError(0, 1);
// Fehlerhafte Zelle wird rot markiert (Border)
assertNotNull(gui.getBlackCells());
}
}