Browse Source

Added IllegalArgumentException to checkInput instead of returning false, get tests and invoking methodes adapted

featureMatrixCalculator
Lukas Reichwein 5 years ago
parent
commit
0871928e84
  1. 43
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java
  2. 14
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java
  3. 31
      src/test/java/com/ugsbo/matrixcalc/MatrixIOUtilsTest.java

43
src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java

@ -37,7 +37,11 @@ public class MatrixCalcController {
String stringMatrixA = matrixATextArea.getText(); String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.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[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB); double[][] matrixB = util.stringToMatrix(stringMatrixB);
@ -47,7 +51,6 @@ public class MatrixCalcController {
outputText.setText(DisplayableString); outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
}
// System.out.println(matrixATextArea.getText()); // System.out.println(matrixATextArea.getText());
}); });
@ -56,7 +59,11 @@ public class MatrixCalcController {
MatrixCalcIOUtils util = new MatrixCalcIOUtils(); MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixA = matrixATextArea.getText(); String stringMatrixA = matrixATextArea.getText();
if (util.checkInput(stringMatrixA)) {
try {
} catch (Exception e) {
//TODO: handle exception
}
double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] result = math.matrixTransponation(matrixA); double[][] result = math.matrixTransponation(matrixA);
@ -65,7 +72,6 @@ public class MatrixCalcController {
outputText.setText(DisplayableString); outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
}
}); });
addButton.setOnMouseClicked((event) -> { addButton.setOnMouseClicked((event) -> {
@ -74,7 +80,11 @@ public class MatrixCalcController {
String stringMatrixA = matrixATextArea.getText(); String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.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[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB); double[][] matrixB = util.stringToMatrix(stringMatrixB);
@ -84,7 +94,6 @@ public class MatrixCalcController {
outputText.setText(DisplayableString); outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
}
}); });
substractButton.setOnMouseClicked((event) -> { substractButton.setOnMouseClicked((event) -> {
@ -94,13 +103,9 @@ public class MatrixCalcController {
String stringMatrixA = matrixATextArea.getText(); String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText(); String stringMatrixB = matrixBTextArea.getText();
boolean stop = false;
try { try {
util.checkInput(stringMatrixA); util.checkInput(stringMatrixA);
} catch (Exception e) { } catch (Exception e) {
stop = true;
outputText.setText(e.getMessage() + "A"); outputText.setText(e.getMessage() + "A");
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} }
@ -108,14 +113,10 @@ public class MatrixCalcController {
try { try {
util.checkInput(stringMatrixB); util.checkInput(stringMatrixB);
} catch (Exception e) { } catch (Exception e) {
stop = true;
outputText.setText(e.getMessage() + "B"); outputText.setText(e.getMessage() + "B");
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} }
if (util.checkInput(stringMatrixA) && util.checkInput(stringMatrixB) && !stop) {
double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixA = util.stringToMatrix(stringMatrixA);
double[][] matrixB = util.stringToMatrix(stringMatrixB); double[][] matrixB = util.stringToMatrix(stringMatrixB);
@ -131,10 +132,6 @@ public class MatrixCalcController {
outputText.setText(e.getMessage()); outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} }
}
}); });
DetAButton.setOnMouseClicked((event) -> { DetAButton.setOnMouseClicked((event) -> {
@ -143,18 +140,14 @@ public class MatrixCalcController {
String stringMatrixA = matrixATextArea.getText(); String stringMatrixA = matrixATextArea.getText();
boolean stop = false;
try { try {
util.checkInput(stringMatrixA); util.checkInput(stringMatrixA);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
stop = true;
outputText.setText(e.getMessage()); outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} }
if (util.checkInput(stringMatrixA) && !stop) {
double[][] matrixA = util.stringToMatrix(stringMatrixA); double[][] matrixA = util.stringToMatrix(stringMatrixA);
try { try {
@ -170,7 +163,6 @@ public class MatrixCalcController {
outputText.setText(e.getMessage()); outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} }
}
}); });
DetBButton.setOnMouseClicked((event) -> { DetBButton.setOnMouseClicked((event) -> {
@ -178,18 +170,14 @@ public class MatrixCalcController {
MatrixCalcIOUtils util = new MatrixCalcIOUtils(); MatrixCalcIOUtils util = new MatrixCalcIOUtils();
String stringMatrixB = matrixBTextArea.getText(); String stringMatrixB = matrixBTextArea.getText();
boolean stop = false;
try { try {
util.checkInput(stringMatrixB); util.checkInput(stringMatrixB);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
stop = true;
outputText.setText(e.getMessage()); outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} }
if (util.checkInput(stringMatrixB) && !stop) {
double[][] matrixB = util.stringToMatrix(stringMatrixB); double[][] matrixB = util.stringToMatrix(stringMatrixB);
try { try {
@ -205,7 +193,6 @@ public class MatrixCalcController {
outputText.setText(e.getMessage()); outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER); outputText.setTextAlignment(TextAlignment.CENTER);
} }
}
}); });
} }
} }

14
src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java

@ -42,8 +42,7 @@ public class MatrixCalcIOUtils {
* @param matrix It is the InputMatrix * @param matrix It is the InputMatrix
* @return true if the Matrix is valid Input. * @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) { if (matrix.length() == 0) {
throw new IllegalArgumentException("Please insert a Matrix"); throw new IllegalArgumentException("Please insert a Matrix");
} }
@ -59,13 +58,10 @@ public class MatrixCalcIOUtils {
Pattern p = Pattern.compile(row1 + row2 + row3); Pattern p = Pattern.compile(row1 + row2 + row3);
Matcher m = p.matcher(matrix); 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 ");
}
} }
/** /**

31
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.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Before; import org.junit.Before;
@ -30,27 +31,45 @@ public class MatrixIOUtilsTest {
public void checkIfA1by3MatrixIsValidInput() { public void checkIfA1by3MatrixIsValidInput() {
String input = "1 2 3"; 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 @Test
public void checkIfA2by3MatrixIsValidInput() { public void checkIfA2by3MatrixIsValidInput() {
// arrange
String input = "1 2 3\n1 2 3"; 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 @Test
public void checkIfA3by3MatrixIsValidInput() { public void checkIfA3by3MatrixIsValidInput() {
String input = "1 2 3\n1 2 3\n1 2 3"; 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 @Test

Loading…
Cancel
Save