Запрашиваемая страница не существует.
+ + + diff --git a/src/main/webapp/error500.jsp b/src/main/webapp/error500.jsp new file mode 100644 index 0000000..6084a78 --- /dev/null +++ b/src/main/webapp/error500.jsp @@ -0,0 +1,24 @@ +<%-- + Created by IntelliJ IDEA. + User: rmeeo + Date: 25-Dec-25 + Time: 16:29 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ page isErrorPage="true" %> + + +Что-то пошло не так на нашей стороне.
+Мы уже работаем над исправлением проблемы.
+ ++ На главную | + Начать игру +
+ + diff --git a/src/main/webapp/game.jsp b/src/main/webapp/game.jsp new file mode 100644 index 0000000..e4f2fd7 --- /dev/null +++ b/src/main/webapp/game.jsp @@ -0,0 +1,138 @@ +<%-- + Created by IntelliJ IDEA. + User: rmeeo + Date: 25-Dec-25 + Time: 15:53 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + +Игрок: ${sessionScope.playerName}
+Сыграно игр: ${sessionScope.gamesPlayed != null ? sessionScope.gamesPlayed : 0}
++ Играть снова | + Начать сначала +
+ID сессии: ${pageContext.session.id}
+Текущий шаг: ${step.id}
++Ты очнулся в космическом порту и голова покачивается на борту +своего корабля. Разве ты не об этом мечтал? Стать капитаном +галактического судна с экипажем, который будет совершать +подвиги под твоим командованием. + +Так что вперёд! ++ +
+Когда ты поднимаешься на борт корабля, тебя приветствует девушка +с умной улыбкой в глазах: + +— Здравствуйте, командир! Я Зинаида — ваша помощница. +Видите? Там в углу пьёт кофе наш штурман - сержант Перегарный Шлейф, + под штурвалом спит наш бортмеханик Чёрный Богдан, + а фоторрафирует его Сергей Стальная Пятка - наш навигатор. + +А как обращаться к вам? ++ + + + + diff --git a/src/main/webapp/result.jsp b/src/main/webapp/result.jsp new file mode 100644 index 0000000..d1e3da7 --- /dev/null +++ b/src/main/webapp/result.jsp @@ -0,0 +1,18 @@ +<%-- +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + + +
ID сессии: ${pageContext.session.id}
+Время создания: ${pageContext.session.creationTime}
+ + +--%> \ No newline at end of file diff --git a/src/test/java/com/javarush/AppTest.java b/src/test/java/com/javarush/AppTest.java deleted file mode 100644 index 2fcbf44..0000000 --- a/src/test/java/com/javarush/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.javarush; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/src/test/java/com/javarush/quest/GameServletTest.java b/src/test/java/com/javarush/quest/GameServletTest.java new file mode 100644 index 0000000..c83f226 --- /dev/null +++ b/src/test/java/com/javarush/quest/GameServletTest.java @@ -0,0 +1,91 @@ +package com.javarush.quest; + +import com.javarush.quest.model.QuestStep; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import javax.servlet.RequestDispatcher; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import static org.mockito.Mockito.*; + +class GameServletTest { + + private GameServlet servlet; + private HttpServletRequest request; + private HttpServletResponse response; + private HttpSession session; + private RequestDispatcher dispatcher; + + @BeforeEach + void setUp() { + servlet = new GameServlet(); + request = mock(HttpServletRequest.class); + response = mock(HttpServletResponse.class); + session = mock(HttpSession.class); + dispatcher = mock(RequestDispatcher.class); + + when(request.getSession()).thenReturn(session); + when(request.getContextPath()).thenReturn(""); + when(request.getRequestDispatcher("/game.jsp")).thenReturn(dispatcher); + } + + // ---------- doGet ---------- + + @Test + void doGet_withoutStep_shouldShowStart() throws Exception { + when(request.getParameter("step")).thenReturn(null); + + servlet.doGet(request, response); + + verify(request).setAttribute(eq("step"), any(QuestStep.class)); + verify(dispatcher).forward(request, response); + } + + @Test + void doGet_unknownStep_shouldReturn404() throws Exception { + when(request.getParameter("step")).thenReturn("unknown"); + + servlet.doGet(request, response); + + verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString()); + } + + // ---------- doPost ---------- + + @Test + void doPost_invalidParams_redirectsToStart() throws Exception { + when(request.getParameter("answer")).thenReturn(null); + when(request.getParameter("currentStep")).thenReturn(null); + + servlet.doPost(request, response); + + verify(response).sendRedirect("/game?step=start"); + } + + @Test + void doPost_answer1_finalStep_incrementsGamesPlayed() throws Exception { + when(request.getParameter("answer")).thenReturn("1"); + when(request.getParameter("currentStep")).thenReturn("captain"); + when(session.getAttribute("gamesPlayed")).thenReturn(0); + + servlet.doPost(request, response); + + verify(session).setAttribute("gamesPlayed", 1); + verify(response).sendRedirect("/game?step=win"); + } + + @Test + void doPost_answer2_finalStep() throws Exception { + when(request.getParameter("answer")).thenReturn("2"); + when(request.getParameter("currentStep")).thenReturn("start"); + when(session.getAttribute("gamesPlayed")).thenReturn(null); + + servlet.doPost(request, response); + + verify(session).setAttribute("gamesPlayed", 1); + verify(response).sendRedirect("/game?step=lose1"); + } +} diff --git a/src/test/java/com/javarush/quest/StartServletTest.java b/src/test/java/com/javarush/quest/StartServletTest.java new file mode 100644 index 0000000..2f12937 --- /dev/null +++ b/src/test/java/com/javarush/quest/StartServletTest.java @@ -0,0 +1,62 @@ +package com.javarush.quest; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import javax.servlet.RequestDispatcher; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import static org.mockito.Mockito.*; + +class StartServletTest { + + private StartServlet servlet; + private HttpServletRequest request; + private HttpServletResponse response; + private HttpSession session; + private RequestDispatcher dispatcher; + + @BeforeEach + void setUp() { + servlet = new StartServlet(); + request = mock(HttpServletRequest.class); + response = mock(HttpServletResponse.class); + session = mock(HttpSession.class); + dispatcher = mock(RequestDispatcher.class); + + when(request.getSession()).thenReturn(session); + when(request.getRequestDispatcher("/index.jsp")).thenReturn(dispatcher); + when(request.getContextPath()).thenReturn(""); + } + + @Test + void doGet_shouldForwardToIndexJsp() throws Exception { + servlet.doGet(request, response); + + verify(dispatcher).forward(request, response); + } + + @Test + void doPost_withPlayerName() throws Exception { + when(request.getParameter("playerName")).thenReturn("Roman"); + + servlet.doPost(request, response); + + verify(session).setAttribute("playerName", "Roman"); + verify(session).setAttribute("gamesPlayed", 0); + verify(response).sendRedirect("/game?step=start"); + } + + @Test + void doPost_withoutPlayerName_usesDefault() throws Exception { + when(request.getParameter("playerName")).thenReturn(" "); + + servlet.doPost(request, response); + + verify(session).setAttribute("playerName", "Искатель приключений"); + verify(session).setAttribute("gamesPlayed", 0); + verify(response).sendRedirect("/game?step=start"); + } +} diff --git a/src/test/java/com/javarush/quest/service/QuestServiceTest.java b/src/test/java/com/javarush/quest/service/QuestServiceTest.java new file mode 100644 index 0000000..fb01c08 --- /dev/null +++ b/src/test/java/com/javarush/quest/service/QuestServiceTest.java @@ -0,0 +1,73 @@ +package com.javarush.quest.service; +import com.javarush.quest.service.QuestService; +import com.javarush.quest.model.QuestStep; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class QuestServiceTest { + + private QuestService questService; + + @BeforeEach + void setUp() { + questService = new QuestService(); + } + + @Test + void shouldReturnStartStep() { + QuestStep start = questService.getStep("start"); + + assertNotNull(start); + assertEquals("start", start.getId()); + assertEquals("Принять вызов", start.getOption1()); + assertEquals("Отклонить вызов", start.getOption2()); + assertEquals("bridge", start.getNextStepId1()); + assertEquals("lose1", start.getNextStepId2()); + } + + @Test + void shouldReturnBridgeStep() { + QuestStep bridge = questService.getStep("bridge"); + + assertNotNull(bridge); + assertEquals("bridge", bridge.getId()); + assertEquals("captain", bridge.getNextStepId1()); + assertEquals("lose2", bridge.getNextStepId2()); + } + + @Test + void shouldReturnCaptainStep() { + QuestStep captain = questService.getStep("captain"); + + assertNotNull(captain); + assertEquals("captain", captain.getId()); + assertEquals("win", captain.getNextStepId1()); + assertEquals("lose3", captain.getNextStepId2()); + } + + @Test + void shouldDetectFinalWinStep() { + assertTrue(questService.isFinalStep("win")); + } + + @Test + void shouldDetectFinalLoseStep() { + assertTrue(questService.isFinalStep("lose1")); + assertTrue(questService.isFinalStep("lose2")); + assertTrue(questService.isFinalStep("lose3")); + } + + @Test + void shouldReturnNullForUnknownStep() { + QuestStep step = questService.getStep("unknown"); + + assertNull(step); + } + + @Test + void unknownStepIsNotFinal() { + assertFalse(questService.isFinalStep("unknown")); + } +} \ No newline at end of file