From 55f173324acfe0800987d1deefb08b908fe6ca61 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Fri, 21 Jan 2022 00:26:27 +0100 Subject: [PATCH] Changed select method with test for MenuManager to avoid ArrayIndexOutOfBoundsException --- src/main/java/Application/MenuManager.java | 2 ++ src/test/java/Application/MenuManagerTest.java | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/main/java/Application/MenuManager.java b/src/main/java/Application/MenuManager.java index 62944b1..4aa1be5 100644 --- a/src/main/java/Application/MenuManager.java +++ b/src/main/java/Application/MenuManager.java @@ -23,6 +23,8 @@ public class MenuManager { } public void select(int i) { + if(i < 0 || i >= this.getSize()) + return; if (currentMenu == null) this.currentMenu = menuList.get(i); else diff --git a/src/test/java/Application/MenuManagerTest.java b/src/test/java/Application/MenuManagerTest.java index 111bf13..3ad5701 100644 --- a/src/test/java/Application/MenuManagerTest.java +++ b/src/test/java/Application/MenuManagerTest.java @@ -29,8 +29,18 @@ class MenuManagerTest { @Test void select() { assertNull(mm.getCurrentMenu()); + //Negative index + mm.select(-1); + assertNull(mm.getCurrentMenu()); + mm.select(0); assertEquals(mm.getCurrentMenu().getName(), "Menu1"); + //Dose nothing when index to big + mm.select(1); + assertEquals(mm.getCurrentMenu().getName(), "Menu1"); + + mm.select(0); + assertEquals(mm.getCurrentMenu().getName(), "Menu1.1"); mm.select(0); assertEquals(mm.getCurrentMenu().getName(), "Menu1.1"); }