Skip to content

Interview Exam - Classloader-based Factory Pattern Implementation

Estimated time to read the description: 5 minutes

You may open Google, Stack Overflow, textbook, slides, etc. However, please be aware that your time is very limited. It is better to focus on doing the tasks.

The following code is taken from a research project named WinVMJ in the Reliable Software Engineering lab at the Faculty of Computer Science Universitas Indonesia with some modifications. The said code implements a factory design pattern used for instantiating a type-compatible object dynamically using Java reflection mechanism.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// For brevity, assume all required classes have been imported.
public class FinancialReportFactory {

    private static final Logger LOGGER = Logger.getLogger(FinancialReportFactory.class.getName());

    private FinancialReportFactory() { }

    public static FinancialReport create(String fullyQualifiedName, Object... base) {
        FinancialReport obj = null;

        try {
            Class<?> clz = Class.forName(fullyQualifiedName);
            Constructor<?> constructor = clz.getDeclaredConstructors()[0];
            obj = (FinancialReport) constructor.newInstance(base);
        } catch (IllegalArgumentException e) {
            LOGGER.severe("Check the argument passed into the constructor");
            System.exit(20);
        } catch (ClassCastException e) {
            LOGGER.severe("Cannot cast the object due to incompatible interface");
            System.exit(30);
        } catch (ClassNotFoundException e) {
            LOGGER.severe("Class not found in the classloader");
            System.exit(40);
        } catch (Exception e) {
            LOGGER.severe("Unknown, unhandled issue present in the method");
            LOGGER.severe(e.toString());
            System.exit(50);
        }

        return obj;
    }
}

The factory implementation above expects two arguments: a fully qualified name (FQN) of a class and a sequence of Object that required by a constructor method. The FQN is a string that forms the identifier of a Java class in the classloader. The other argument contains one or more objects of type Object that will be passed into the constructor of a dynamically-loaded type via Java reflection mechanism.

For example, if you have a Java class named FooFinancialReport located in a package named com.example, then the FQN of FooFinancialReport class is com.example.FooFinancialReport. If the said class require two arguments, which is a string and an integer, then both argument also need to be prepared before invoking the factory. One example of the invocation is as follows:

1
2
3
4
5
6
7
public static void main(String[] args) {
    Object[] requiredArgs = new Object[] {
        "A financial report", new Integer(42)
    };

    FinancialReport example = FinancialReportFactory.create("com.example.FooFinancialReport", requiredArgs);
}

As you may have noticed, the factory also ensures that the created instance has matching type named FinancialReport. In other words, the factory can only create an object with type FinancialReport or any subtypes of it.

Part 1: Input Space Partitioning

Estimated time to complete: 5 minutes

You are asked to design an input space model for the factory method by following a functionality-based approach. The information required to develop the model can be derived by reading the code snippet and understanding the problem description. Once you have developed the model, create the test requirement and the test cases.

Your tasks are as follows:

  1. Determine the characteristics and the partition of the create() method.
  2. Based on the input space model that you have created, create the test requirement and the test cases based on certain coverage criteria chosen by the proctor.

Possible coverage criteria choices:

  • All Combinations Coverage (ACoC)
  • Each Choice Coverage (ECC)
  • Pair-Wise Coverage (PWC)
  • Base Choice Coverage (BCC)

Note: You do not have to write all test cases due to the time limit. You need to demonstrate that the subset of your test cases valid with the chosen coverage criteria.

Write your answer in a sheet of paper or Microsoft Word/Google Docs document. You may include illustrations in your answer. Please prepare to present your answer remotely via Zoom/Google Hangouts during discussion time.

Part 2: Graph Coverage

Estimated time to complete: 5 minutes

You are asked to design a control flow graph (CFG), prepare the test requirement, and create the test paths.

Your tasks are as follows:

  1. Create the CFG of the create() method.
  2. Based on the CFG that you have created, create the test requirement and the test paths based on certain coverage criteria chosen by the proctor.

Possible coverage criteria choices:

  • Node Coverage (NC)
  • Edge Coverage (EC)
  • Edge-Pair Coverage (EPC)

Note: You do not have to write all test paths due to the time limit. You need to demonstrate that the subset of your test paths valid with the chosen coverage criteria.

Write your answer in a sheet of paper or Microsoft Word/Google Docs document. You may include illustrations in your answer. Please prepare to present your answer remotely via Zoom/Google Hangouts during discussion time.

Part 3: Discussion

Estimated time: 10 minutes

You are asked to present your answers to the given problems and also to have one-on-one interview with the proctor during the discussion time.

The list of topics that might be discussed is as follows:

  • Code coverage (line coverage)
  • Test-Driven Development (TDD)
  • Test isolation
  • Writing test cases in Java (JUnit)/Python (unittest and Django)/PHP (PHPUnit)
  • Your experience in conducting SQA activities in academics and/or work environment
  • The ideas of mutation testing
  • CI/CD, especially in GitLab
  • Monitoring
  • And many more that may still related to SQA

Acknowledgements

Credits to Hanif Agung as the writer of the original code used in this problem set.


Last update: 2022-10-26 04:54:16
Created: 2022-10-26 04:54:16