From 0871928e84c916edfc21314894cc951fb94cb4aa Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Mon, 15 Jul 2019 11:00:47 +0200 Subject: [PATCH] Added IllegalArgumentException to checkInput instead of returning false, get tests and invoking methodes adapted --- .../matrixcalc/MatrixCalcController.java | 43 +++++++------------ .../ugsbo/matrixcalc/MatrixCalcIOUtils.java | 14 +++--- .../ugsbo/matrixcalc/MatrixIOUtilsTest.java | 31 ++++++++++--- 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java index dddea79..12524fe 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java +++ b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java @@ -37,7 +37,11 @@ public class MatrixCalcController { String stringMatrixA = matrixATextArea.getText(); String stringMatrixB = matrixBTextArea.getText(); - if (util.checkInput(stringMatrixA) && util.checkInput(stringMatrixB)) { + try { + + } catch (Exception e) { + //TODO: handle exception + } double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixB = util.stringToMatrix(stringMatrixB); @@ -47,7 +51,6 @@ public class MatrixCalcController { outputText.setText(DisplayableString); outputText.setTextAlignment(TextAlignment.CENTER); - } // System.out.println(matrixATextArea.getText()); }); @@ -56,7 +59,11 @@ public class MatrixCalcController { MatrixCalcIOUtils util = new MatrixCalcIOUtils(); String stringMatrixA = matrixATextArea.getText(); - if (util.checkInput(stringMatrixA)) { + try { + + } catch (Exception e) { + //TODO: handle exception + } double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] result = math.matrixTransponation(matrixA); @@ -65,7 +72,6 @@ public class MatrixCalcController { outputText.setText(DisplayableString); outputText.setTextAlignment(TextAlignment.CENTER); - } }); addButton.setOnMouseClicked((event) -> { @@ -74,7 +80,11 @@ public class MatrixCalcController { String stringMatrixA = matrixATextArea.getText(); String stringMatrixB = matrixBTextArea.getText(); - if (util.checkInput(stringMatrixA) && util.checkInput(stringMatrixB)) { + try { + + } catch (Exception e) { + //TODO: handle exception + } double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixB = util.stringToMatrix(stringMatrixB); @@ -84,7 +94,6 @@ public class MatrixCalcController { outputText.setText(DisplayableString); outputText.setTextAlignment(TextAlignment.CENTER); - } }); substractButton.setOnMouseClicked((event) -> { @@ -94,13 +103,9 @@ public class MatrixCalcController { String stringMatrixA = matrixATextArea.getText(); String stringMatrixB = matrixBTextArea.getText(); - boolean stop = false; - try { util.checkInput(stringMatrixA); } catch (Exception e) { - stop = true; - outputText.setText(e.getMessage() + "A"); outputText.setTextAlignment(TextAlignment.CENTER); } @@ -108,14 +113,10 @@ public class MatrixCalcController { try { util.checkInput(stringMatrixB); } catch (Exception e) { - stop = true; - outputText.setText(e.getMessage() + "B"); outputText.setTextAlignment(TextAlignment.CENTER); } - if (util.checkInput(stringMatrixA) && util.checkInput(stringMatrixB) && !stop) { - double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixB = util.stringToMatrix(stringMatrixB); @@ -131,10 +132,6 @@ public class MatrixCalcController { outputText.setText(e.getMessage()); outputText.setTextAlignment(TextAlignment.CENTER); } - - - - } }); DetAButton.setOnMouseClicked((event) -> { @@ -143,18 +140,14 @@ public class MatrixCalcController { String stringMatrixA = matrixATextArea.getText(); - boolean stop = false; try { util.checkInput(stringMatrixA); } catch (IllegalArgumentException e) { - stop = true; - outputText.setText(e.getMessage()); outputText.setTextAlignment(TextAlignment.CENTER); } - if (util.checkInput(stringMatrixA) && !stop) { double[][] matrixA = util.stringToMatrix(stringMatrixA); try { @@ -170,7 +163,6 @@ public class MatrixCalcController { outputText.setText(e.getMessage()); outputText.setTextAlignment(TextAlignment.CENTER); } - } }); DetBButton.setOnMouseClicked((event) -> { @@ -178,18 +170,14 @@ public class MatrixCalcController { MatrixCalcIOUtils util = new MatrixCalcIOUtils(); String stringMatrixB = matrixBTextArea.getText(); - boolean stop = false; try { util.checkInput(stringMatrixB); } catch (IllegalArgumentException e) { - stop = true; - outputText.setText(e.getMessage()); outputText.setTextAlignment(TextAlignment.CENTER); } - if (util.checkInput(stringMatrixB) && !stop) { double[][] matrixB = util.stringToMatrix(stringMatrixB); try { @@ -205,7 +193,6 @@ public class MatrixCalcController { outputText.setText(e.getMessage()); outputText.setTextAlignment(TextAlignment.CENTER); } - } }); } } \ No newline at end of file diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java index ab3ab7e..20c5130 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java +++ b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java @@ -42,8 +42,7 @@ public class MatrixCalcIOUtils { * @param matrix It is the InputMatrix * @return true if the Matrix is valid Input. */ - protected boolean checkInput(String matrix) throws IllegalArgumentException { - boolean isMatched = false; + protected void checkInput(String matrix) throws IllegalArgumentException { if (matrix.length() == 0) { throw new IllegalArgumentException("Please insert a Matrix"); } @@ -59,13 +58,10 @@ public class MatrixCalcIOUtils { Pattern p = Pattern.compile(row1 + row2 + row3); Matcher m = p.matcher(matrix); - - isMatched = m.matches(); - // TODO change the funktion to void and just throw exceptions if something went - // worng - - // System.out.println(isMatched); - return isMatched; + + if(!m.matches()){ + throw new IllegalArgumentException("A Matrix is not in the right format, Matrix "); + } } /** diff --git a/src/test/java/com/ugsbo/matrixcalc/MatrixIOUtilsTest.java b/src/test/java/com/ugsbo/matrixcalc/MatrixIOUtilsTest.java index b01e775..6f501f7 100644 --- a/src/test/java/com/ugsbo/matrixcalc/MatrixIOUtilsTest.java +++ b/src/test/java/com/ugsbo/matrixcalc/MatrixIOUtilsTest.java @@ -2,6 +2,7 @@ package com.ugsbo.matrixcalc; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Before; @@ -30,27 +31,45 @@ public class MatrixIOUtilsTest { public void checkIfA1by3MatrixIsValidInput() { String input = "1 2 3"; - boolean result = util.checkInput(input); + try { + // act + util.checkInput(input); - assertTrue("The 1 by 3 Matrix should be Matched as valid input.", result); + // assert + assertTrue("The 1 by 3 Matrix should be Matched as valid input.", true); + } catch (IllegalArgumentException e) { + assertFalse("The 1 by 3 Matrix should be Matched as valid input.", true); + } } @Test public void checkIfA2by3MatrixIsValidInput() { + // arrange String input = "1 2 3\n1 2 3"; - boolean result = util.checkInput(input); + try { + // act + util.checkInput(input); - assertTrue("The 2 by 3 Matrix should be Matched as valid input.", result); + // assert + assertTrue("The 2 by 3 Matrix should be Matched as valid input.", true); + } catch (IllegalArgumentException e) { + assertFalse("The 2 by 3 Matrix should be Matched as valid input.", true); + } } @Test public void checkIfA3by3MatrixIsValidInput() { String input = "1 2 3\n1 2 3\n1 2 3"; - boolean result = util.checkInput(input); + try { + util.checkInput(input); + + assertTrue("The 3 by 3 Matrix should be Matched as valid input.", true); + } catch (IllegalArgumentException e) { + assertFalse("The 3 by 3 Matrix should be Matched as valid input.", true); + } - assertTrue("The 3 by 3 Matrix should be Matched as valid input.", result); } @Test