This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Writing a Test Case for Multiple Server Startup

This is a complete example of applying the multiple server startup feature in integration tests. You can find the implementation of the multiple server startup logic under the initialize() method. Startup properties, -DportOffset and -DosgiConsole are provided as the input to the carbon test server manager. Setting the port offset will allow you to start multiple server instances on the same host without any port binding errors.

public class OSGIServerBundleStatusTest {
    private static final Log log = LogFactory.getLog(OSGIServerBundleStatusTest.class);
    private static int telnetPort = 2000;
    private TelnetClient telnet = new TelnetClient();
    private ArrayList<String> arrList = new ArrayList<String>();
    private ArrayList<String> unsatisfiedList = new ArrayList<String>();
    private HashMap<String, String> serverPropertyMap = new HashMap<String, String>();
    private MultipleServersManager manager = new MultipleServersManager();
    private PrintStream out;

     @BeforeClass(alwaysRun = true)
    public void initialize() throws Exception {
        serverPropertyMap.put("-DportOffset", "1");
        serverPropertyMap.put("-DosgiConsole", Integer.toString(telnetPort));
        CarbonTestServerManager server = new CarbonTestServerManager(System.getProperty("carbon.zip"),
                serverPropertyMap);
        manager.startServers(server);
    }

     @AfterClass(alwaysRun = true)
    public void stopServers() throws Exception {
        disconnect();  // telnet disconnection
        manager.stopAllServers();
    }

     @Test(groups = "wso2.all", description = "Identifying and storing unsatisfied OSGI components")
    public void testOSGIUnsatisfiedComponents() throws Exception {
        telnet.connect(InetAddress.getLocalHost().getHostAddress(), telnetPort);
        telnet.setSoTimeout(10000);
        ArrayList<String> arr = retrieveUnsatisfiedComponentsList("ls");
        for (int x = 0; x < arr.size(); x++) {
            unsatisfiedList.add(arrList.get(x).split("\t")[3]);
            log.info(unsatisfiedList.get(x));
        }
        assertTrue(unsatisfiedList.size() == 0, "Unsatisfied components detected" +
                " in server startup");
    }

     private ArrayList<String> retrieveUnsatisfiedComponentsList(String command) throws IOException {
        writeInputCommand(command);
        try {
            readResponse();
        } catch (SocketTimeoutException e) {
            log.error("Socket timeout Exception " + e);
        }
        return arrList;
    }

     private void writeInputCommand(String value) {
        out = new PrintStream(telnet.getOutputStream());
        out.println(value);
        out.flush();
        log.info(value);
    }

     private void readResponse() throws IOException {
        InputStream in = telnet.getInputStream();
        BufferedReader inBuff = new BufferedReader(new InputStreamReader(in));
        String inputLine;
        while ((inputLine = inBuff.readLine()) != null)
            if (inputLine.contains("Unsatisfied")) {  // filtering Unsatisfied components
                arrList.add(inputLine);
                log.info(inputLine);
            }
        inBuff.close();
        out.close();
    }

     private void disconnect() {
        try {
            telnet.disconnect();
        } catch (IOException e) {
            log.error("Error occured while telnet disconnection " + e);
        }
    }
}