/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2005 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This 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 1any later version.
 *
 * This 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer: Matt Wringe
 * --------------------------------------------------------------------------
 * $Id:
 * --------------------------------------------------------------------------
 */
package org.objectweb.jonas.applications.clients.apps.mejb;

import java.net.URL;
import javax.xml.namespace.QName;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.objectweb.jonas.applications.util.JApplicationsTestCase;

/**
 * Class used to test that the monitoring endpoint (contained in
 * mejb.ear) is working properly.
 * @author Matt Wringe
 */
public class F_MonitoringEndpoint extends JApplicationsTestCase{


    /**
     * URL of the monitoring endpoint
     */
    private static final String URL_ENDPOINT = "/mejb/ManagementEndpoint/ManagementEndpoint";

    /**
     * The namespace of the monitoring endpoint.
     */
    private static final String NAMESPACE = "http://mejb.jonas.objectweb.org";

    /**
     * Call object used to invoke methods in the monitoring endpoint
     */
    private static Call call;
    
    /**
     * the username to be used with the application
     */
    private static final String USERNAME = "jonas";
    
    /**
     * the password to be used with the application
     */
    private static final String PASSWORD = "jonas";

    /**
     * Main method
     * @param args the arguments
     */
    public static void main(String[] args) {
        String testtorun = null;
        for (int argn = 0; argn < args.length; argn++) {
            String sArg = args[argn];
            if (sArg.equals("-n")) {
                testtorun = args[++argn];
            }
        }
        if (testtorun == null) {
            TestRunner.run(suite());
        } else {
            TestRunner.run(new F_MonitoringEndpoint(testtorun));
        }
    }

    /**
     * Constructor with a specified name
     * @param s name
     */
    public F_MonitoringEndpoint(String s) {
        super (s, URL_ENDPOINT, USERNAME, PASSWORD);
    }

    /**
     * Get a new TestSuite for this class
     * @return a new TestSuite for this class
     */
    public static TestSuite suite() {
        return new TestSuite(F_MonitoringEndpoint.class);
    }

    /**
     * Initial setup for the test
     * @throws Exception    If any exception occurs.
     */
    protected void setUp() throws Exception {
        super.setUp();
        useEar("mejb");
        Service service = new Service();
        call = (Call) service.createCall();
        call.setUsername(USERNAME);
        call.setPassword(PASSWORD);
        call.setTargetEndpointAddress (new URL(getAbsoluteUrl(URL_ENDPOINT)));
    }

    /**
     * Tests that the endpoint can get the mbean count for the current server
     * @throws Exception If any exception occurs
     */
    public void testGetMBeanCount() throws Exception {
        call.setOperationName (new QName(NAMESPACE, "getMBeanCount"));
        Integer res = (Integer) call.invoke (new Object[]{});
        if (res.intValue() <= 0) {
            fail ("Could not get any mbeans");
        }
    }

    /**
     * Tests if the endpoint can get the list of servers.
     * @throws Exception If any exception occurs.
     */
    public void testGetServers() throws Exception {
        call.setOperationName (new QName(NAMESPACE, "getServers"));
        String[] res = (String[]) call.invoke (new Object[]{});
        if (res.length < 1) {
            fail ("No servers found in the domain");
        }
    }

    /**
     * Test if the endopoint can get current domain.
     * Assumes the sever is started in domain 'jonas'
     * @throws Exception If any exception occurs.
     */
    public void testDefaultDomain() throws Exception {
        //Assumes server is started in domain jonas
        call.setOperationName (new QName(NAMESPACE, "getDefaultDomain"));
        String res = (String) call.invoke (new Object[]{});
        if (!res.equals("jonas")) {
            fail ("The domain name gotten through the endpoint does not match the actual domain name. Expected :jonas, received :" + res);
        }
    }

    /**
     * Test if the endpoint can get the server name from the J2EEServer mbean.
     * Assumes that the server's name is 'jonas' and started in domain 'jonas'
     * @throws Exception If any exception occurs.
     */
    public void testGetAttribute() throws Exception {
        //Assumes that the server started is named jonas and is started in domain jonas
        String serverName = "jonas";
        String domainName = "jonas";
        String objectName = domainName + ":j2eeType=J2EEServer,name=" + serverName;

        call.setOperationName (new QName(NAMESPACE, "getAttribute"));
        String[] res = (String[]) call.invoke (new Object[]{serverName, objectName, "serverName"});
        if (!res[0].equals(serverName)) {
            fail ("The attribute value received does not match its acual value. Expected :"
                   + serverName + ", received:" + res[0]);
        }
    }
}
