/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 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 any 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
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jtests.clients.service;

import org.objectweb.jonas.jtests.util.JWebServicesTestCase;

import junit.framework.Test;
import junit.framework.TestSuite;

import com.meterware.httpunit.*;

/**
 * test case for port component link (service-ref on a colocated webservices instance)
 * Client doesn't know the ws location in advance.
 */
public class F_PortComponentLink extends JWebServicesTestCase {

    protected static final String CONTEXT = "/portlink/";

    public F_PortComponentLink(String name) {
        super(name, CONTEXT);
    }

    public static Test suite() {
        return new TestSuite(F_PortComponentLink.class);
    }

    public void setUp() throws Exception {
        super.setUp();
        useEar("port-component-link");
    }

    public void tearDown() throws Exception {
        super.tearDown();
    }

    public void testConfiguration() throws Exception {
        WebResponse wr = wc.getResponse(url + "index.jsp");

        // check endpoint URL
        WebLink loc = wr.getLinkWithID("location");
        assertNotNull(loc);
        String link = loc.asText();
        String url = "http://localhost:"
            + System.getProperty("http.port")
            + "/endpoint/addressbook";
        int index = link.indexOf(url);
        System.out.println("expected URL :" + url);
        System.out.println("current  URL :" + link);
        assertTrue("endpoint not updated", -1 != index);

        // check Stub creation
        WebLink stub = wr.getLinkWithID("stub");
        assertNotNull("no stub", stub);
        link = stub.asText();
        index = link.indexOf("'true'");
        assertTrue("stub not returned", -1 != index);

    }

    public void testResults() throws Exception {
        WebResponse wr = wc.getResponse(url + "index.jsp");

        // check return type
        WebLink type = wr.getLinkWithID("classname");
        assertNotNull(type);
        String link = type.asText();
        int index = link.indexOf("org.objectweb.jonas.jtests.servlets.portlink.Address");
        assertTrue("bad return type", -1 != index);

        // check getAddress return
        WebLink get = wr.getLinkWithID("get");
        assertNotNull(get);
        link = get.asText();
        index = link.indexOf("name : 'JOnAS'");
        assertTrue("bad name returned", -1 != index);
        index = link.indexOf("company : 'ObjectWeb Consortium'");
        assertTrue("bad company returned", -1 != index);
        index = link.indexOf("version : '4.0'");
        assertTrue("bad version returned", -1 != index);

        // check getAddresses return
        WebLink getAll = wr.getLinkWithID("getAll");
        assertNotNull(getAll);
        link = getAll.asText();
        index = link.indexOf("getAddresses() length : 2");
        assertTrue("bad length", -1 != index);

        // check isPresent return
        WebLink pres = wr.getLinkWithID("present");
        assertNotNull(pres);
        link = pres.asText();
        index = link.indexOf("isPresent(\"JOnAS-Team\") : 'true'");
        assertTrue("JOnAS Team not found", -1 != index);

    }

    public static void main (String args[]) {
        String testtorun = null;
        // Get args
        for (int argn = 0; argn < args.length; argn++) {
            String s_arg = args[argn];
            if (s_arg.equals("-n")) {
                testtorun = args[++argn];
            }
        }
        if (testtorun == null) {
            junit.textui.TestRunner.run(suite());
        } else {
            junit.textui.TestRunner.run(new F_PortComponentLink(testtorun));
        }
    }

}
