I have a class named ManagementServiceHandler that implements an interface. In this class, there are a few variables and methods, and among them there is a private static map and two static methods:
public class ManagementServiceHandler implements ManagementService.Iface {
private static Map<NodeManifest, Integer> nodePorts;
... other methods and variables
...
...
public static Map<NodeManifest, Integer> getNodePorts() {
return nodePorts;
}
public static void setNodePorts(Map<NodeManifest, Integer> nodePorts) {
ManagementServiceHandler.nodePorts = nodePorts;
}
The Object NodeManifest is something like:
class NodeManifest {
String hostName;
List<String> servicelist;
}
Other methods handle registration and de-registration of objects NodeManifest into the Map nodePorts, and requests from a NodeManifest of a particular service present in the Map. If a service requested is present, then is is used by the NodeManifest that asked for it.
I have a junit test file containing this:
public class ManagementServiceTest {
public static class ManagementServer implements Runnable {
code for ManagementServer
}
...
...
public static class ArithmeticServer implements Runnable {
code for ArithmeticServer
}
...
...
public void testArithmeticServerCompletion() throws Exception {
class ArithmeticServiceDispatcher {
public long executeOperation(String opName, int num1, int num2) throws TException {
long result = 0;
for(Map.Entry<NodeManifest, Integer> pair : ManagementServiceHandler.getNodePorts().entrySet()) {
List<String> operations = pair.getKey().serviceList;
if(operations.contains(opName)) {
switch(opName) {
case "addition":
result = arithmeticClient.add(num1, num2);
break;
case "multiplication":
result = arithmeticClient.multiply(num1, num2);
break;
case "substraction":
result = arithmeticClient.substract(num1, num2);
break;
case "division":
result = arithmeticClient.divide(num1, num2);
break;
}
break;
}
}
return result;
}
}
My problem is that right now, either the Map contained in the class ManagementServiceHandler and the two methods getNodePorts() and setNodePorts() must be static if I want the method executeOperation in the test file to access the correct Map containing the NodeManifests registered.
I was told that it's not correct, in this case, to use static methods and variables; however, if I make those non-static, then I need to change something in executeOperations() in order for it to access the correct Map nodePorts that contains all the NodePorts registered. I cannot use
ManagementServiceHandler.getNodePorts()
but I still need to access data in the Map nodePorts filled with NodeManifest objects in order to make executeOperation() work as intended.
So my questions are two:
- why i cannot use static in this case?
- how to modify executeOperation() in order for it to access the Map nodePorts created by ManagementServiceHandler when it's not empty?