Day 1: Monday

  • Testing concepts
  • SQL Injections in JDBC
  • MAVEN

Resources

  1. Pfleeger, Shari Lawrence. & Atlee, Joanne M. (2010) Software engineering : theory and practice. 4th ed. Upper Saddle River [N.J: Prentice Hall. (available in the library)

  2. Fowler, Martin (2018) Refactoring: Improving the Design of Existing Code. 1st edition. Addison-Wesley Professional. (available in the library)

  3. Testing types
    1. https://www.guru99.com/white-box-testing.html
    2. https://www.guru99.com/black-box-testing.html
    3. https://www.guru99.com/grey-box-testing.html

Running External Commands in Java

public Test() throws Exception{
System.out.println("Starting process");
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("find /c \"Ali\" test.txt");
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) = null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
}

A modified version that parses the output and prints only the number of lines

public Test() throws Exception{

System.out.println("Starting process");
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("find /c \"Ali\" C:\\Users\\ahmed\\Documents\\test.txt");
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
int number = 0;
while((line=input.readLine()) = null) {
System.out.println(line);

if(line.contains(":"))
{
String [] splits = line.split(" ");
number = Integer.valueOf(splits[splits.length-1]);
}
}
int exitVal = pr.waitFor();
System.out.println("# of lines: " + number);
System.out.println("Exited with error code "+exitVal);
}

=====================================================

public static Stream<Arguments> primeNumbers(){
return Stream.of(
Arguments.of(2, true),
Arguments.of(2200, false),
Arguments.of(7, true),
Arguments.of(2459, true),
Arguments.of(1093, true),
Arguments.of(3597, false),
Arguments.of(200, true)
);
}

@ParameterizedTest (name = "{index}: validatePrime({0})= {1}")
@MethodSource("primeNumbers")
public void isAPrimeTest(int inputNumber, boolean expectedResult) {
assertEquals(expectedResult, primeNumberChecker.isPrime(inputNumber));
}

Creating a set of test cases using arrays

static Stream<Arguments> create3TestCasesEachWith3ArraysAsParameters() {
return Stream.of(
Arguments.of(Arrays.asList(1, 2, 3, 4).toArray(), Arrays.asList(1, 2).toArray(), Arrays.asList(3, 4).toArray()),
Arguments.of(Arrays.asList(3, 4).toArray(), Arrays.asList(3).toArray(), Arrays.asList(4).toArray()),
Arguments.of(Arrays.asList(1, 2, 4).toArray(), Arrays.asList(1, 2).toArray(), Arrays.asList(4).toArray()),
Arguments.of(Arrays.asList(0, 1, 1, 2, 3, 4, 5, 6).toArray(), Arrays.asList(0, 1, 1, 2).toArray(), Arrays.asList(3, 4, 5, 6).toArray())

);

The Assessment

You are to write a Math utility that implements the following functions along with writing the various parametrised jUnit test cases for those functionalities:
  1. Adding two numerical values and returning the result. (10 marks)
  2. Subtracting two numerical values and returning the result. (10 marks)
  3. Multiplying two numerical values and returning the result. (10 marks)
  4. Dividing two numerical values and returning the result. (15 marks)
  5. Returning the product of two vectors (i.e. a new vector). (20 marks)
  6. Returning the product of a matrix and a vector. (15 marks)
  7. Checking if a matrix is an identity matrix (i.e. the function should return a Boolean). An identity matrix is a square matrix with ones on the diagonal from upper left to lower right and zeros elsewhere. (10 marks)
  8. Challenge: Returning the product of two matrices. (5 marks)
  9. Challenge: Returning the transpose of a matrix. (5 marks)
Rubrics:

The marks shown in front of the question are for both the correct implementation of the function and the proper design of the test cases. Implementation accounts for 50% of those marks and so does the testing.

- If you have chosen to used non-parametrised jUnit testing, the max you can get is 60% of the testing marks depending on the test cases coverage.

- If your parametrised jUnit testing is missing a fundamental test case, 5% of the testing mark will be deducted for each missing case.

- We are not expecting to help you in the challenging part of the assessment. General directions could be provided though.

When designing the functions, bear in mind that those functions could be used by another developer who may want to catch/process any exceptions raised during execution.

For some definition of those matrices functions, please have a look at https://www.mathsisfun.com/algebra/matrix-introduction.html.
Topic attachments
I Attachment Action Size Date Who Comment
2020-testingAll V2.0-Day1.pptxpptx 2020-testingAll V2.0-Day1.pptx manage 2 MB 07 Dec 2020 - 09:53 Main.ahmed  
2020-testingAll V2.0-Day2.pptxpptx 2020-testingAll V2.0-Day2.pptx manage 734 K 08 Dec 2020 - 13:01 Main.ahmed  
MyMath.javajava MyMath.java manage 535 bytes 07 Dec 2020 - 15:34 Main.ahmed  
MyMathTest.javajava MyMathTest.java manage 1 K 08 Dec 2020 - 14:44 Main.ahmed Parameterised Junit
PostgresDatabseTestApp.zipzip PostgresDatabseTestApp.zip manage 7 K 07 Dec 2020 - 10:41 Main.ahmed  
TestMyMath.javajava TestMyMath.java manage 408 bytes 07 Dec 2020 - 15:34 Main.ahmed  
testi.zipzip testi.zip manage 14 K 08 Dec 2020 - 12:03 Main.ahmed Maven Project to Test JSON in Java and PHP