vendredi 25 septembre 2015

Problems writing Tests (working with VBox API)

I'm kinda new to the whole topic and need to write unit-tests for a project working with Virtual Box. It's meant to be kind of a wrapper for easier use of certain functions of the API. An example:

public void setStateRunning(final String type, final String environment)
{
    if (getState() == MachineState.PoweredOff || getState() == MachineState.Aborted
            || getState() == MachineState.Saved) {
        boot(type, environment);
        waitForLogin();
        LogMF.info(logger, "Started", null);
    } else if (getState() == MachineState.Paused) {
        getConsole().resume();
        LogMF.info(logger, "Resumed", null);
    } else {
        vm.lockMachine(session, LockType.Shared);
        LogMF.info(logger, "Machine was already running. Locked to enable access.", null);
    }
}

private void boot(final String type, final String environment)
{
    LogMF.info(logger, "Booting machine \"{0}\".", vm.getName());
    final IProgress prog = vm.launchVMProcess(session, type, environment);
    final long timeout = 10 * 1000L; // give the process 10 seconds
    waitForCompletion(prog, timeout);
}

private void waitForCompletion(final IProgress prog, final long timeout)
{
    final long end = System.currentTimeMillis() + timeout;
    while (!prog.getCompleted()) {
        vBox.waitForEvents(0);
        prog.waitForCompletion(50);
        if (System.currentTimeMillis() >= end) {
            break;
        }
    }
    final IVirtualBoxErrorInfo err = prog.getErrorInfo();
    if (err != null) {
        LogMF.info(logger, "prog.ResultCode={0}, completed={1}, error={2}", prog.getResultCode(),
                prog.getCompleted(), err.getText());
    }
    if (prog.getResultCode() != 0) {
        final String msg = String.format("Completion failed '%s': %d", vmId, prog.getResultCode());
        throw new RuntimeException(msg);
    }
}

private void waitForLogin()
{

    final IEventSource es = vBox.getEventSource();
    IEventListener listener = null;
    try {
        listener = es.createListener();
        es.registerListener(listener, Arrays.asList(VBoxEventType.Any), false);
        String guestPropertyName = "";
        String guestPropertyValue = "";
        while (!guestPropertyName.contains("NoLoggedInUsers")) {
            try {
                final IEvent ev = es.getEvent(listener, 500);
                if (ev != null) {
                    if (ev.getType() == VBoxEventType.OnGuestPropertyChanged) {
                        final IGuestPropertyChangedEvent gpce =
                                IGuestPropertyChangedEvent.queryInterface(ev);
                        guestPropertyName = gpce.getName();
                        guestPropertyValue = gpce.getValue();
                    }
                    // LogMF.info(logger, "GuestProperty = \"{0}\":{1}", guestPropertyName,
                    // guestPropertyValue);
                    es.eventProcessed(listener, ev);
                }
            } catch (final Exception e) {
                LogMF.warn(logger, e, "Exception(ignored)", null);
            }
        }
    } catch (final NullPointerException ex) {
        ex.printStackTrace();
    } finally {
        es.unregisterListener(listener);
    }
}

How should I test the private methods ? Should they be changed ? Am I supposed to mock everything uses by waitForLogin() ?

Aucun commentaire:

Enregistrer un commentaire