/**
 * Copyright (c) 2012-2013 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see <http://www.gnu.org/licenses/>
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.admin.jmx;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.ow2.petals.admin.api.ContainerAdministration;
import org.ow2.petals.admin.api.PetalsAdministrationFactory;
import org.ow2.petals.admin.api.exception.ContainerAdministrationException;
import org.ow2.petals.admin.api.exception.DuplicatedServiceException;
import org.ow2.petals.admin.api.exception.MissingServiceException;
import org.ow2.petals.jmx.api.api.PetalsJmxApiFactory;
import org.ow2.petals.jmx.api.mock.PetalsJmxApiFactoryMock;

/**
 * Unit tests of {@link JMXContainerAdministration}
 * 
 * @author Christophe DENEUX - Linagora
 * 
 */
public class JMXContainerAdministrationTest {

    @Before
    public void setup() {
        System.setProperty(PetalsJmxApiFactory.PROPERTY_NAME,
                PetalsJmxApiFactoryMock.class.getName());
    }

    @After
    public void tearDown() {
        System.clearProperty(PetalsJmxApiFactory.PROPERTY_NAME);
    }

    /**
     * <p>
     * Check the
     * {@link JMXContainerAdministration#connect(String, int, String, String)}
     * when the JMX connection is succeeded.
     * </p>
     * <p>
     * Expected results: No error occurs.
     * </p>
     */
    @Test
    public void testConnect_00() throws DuplicatedServiceException, MissingServiceException,
            ContainerAdministrationException {
        final ContainerAdministration containerAdm = PetalsAdministrationFactory.newInstance()
                .newContainerAdministration();
        containerAdm.connect("localhost", 7700, "petals", "petals");
    }

    /**
     * <p>
     * Check the
     * {@link JMXContainerAdministration#connect(String, int, String, String)}
     * when the JMX connection failed.
     * </p>
     * <p>
     * Expected results: The exception {@link ContainerAdministrationException}
     * occurs.
     * </p>
     */
    @Test(expected = ContainerAdministrationException.class)
    public void testConnect_01() throws DuplicatedServiceException, MissingServiceException,
            ContainerAdministrationException {
        final ContainerAdministration containerAdm = PetalsAdministrationFactory.newInstance()
                .newContainerAdministration();
        containerAdm.connect(PetalsJmxApiFactoryMock.HOST_ERROR_CREATING_JMX_CLIENT, 7700,
                "petals", "petals");
    }

    /**
     * <p>
     * Check the {@link JMXContainerAdministration#disconnect()} when:
     * <ul>
     * <li>the JMX connection is succeeded,</li>
     * <li>the JMX disconnection is succeeded,</li>
     * </ul>
     * </p>
     * <p>
     * Expected results: No error occurs.
     * </p>
     */
    @Test
    public void testDisconnect_00() throws DuplicatedServiceException, MissingServiceException,
            ContainerAdministrationException {
        final ContainerAdministration containerAdm = PetalsAdministrationFactory.newInstance()
                .newContainerAdministration();
        containerAdm.connect("localhost", 7700, "petals", "petals");
        containerAdm.disconnect();
    }

    /**
     * <p>
     * Check the {@link JMXContainerAdministration#disconnect()} when no JMX
     * connection is established.
     * </p>
     * <p>
     * Expected results: The exception {@link ContainerAdministrationException}
     * occurs.
     * </p>
     */
    @Test(expected = ContainerAdministrationException.class)
    public void testDisconnect_01() throws DuplicatedServiceException, MissingServiceException,
            ContainerAdministrationException {
        final ContainerAdministration containerAdm = PetalsAdministrationFactory.newInstance()
                .newContainerAdministration();
        containerAdm.disconnect();
    }

}
