/*
 * 
 * Copyright 2005 Sun Microsystems, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 	http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.sun.jini.compat.test;

import java.io.IOException;
import java.rmi.RemoteException;

import com.sun.jini.compat.harness.TestUtility;
import com.sun.jini.compat.harness.Status;
import com.sun.jini.compat.harness.DefaultTest;
import com.sun.jini.compat.harness.BasicServiceAdmin;
import com.sun.jini.compat.harness.BasicAdmin;
import com.sun.jini.compat.harness.Config;

import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceItem;

/**
 * Tests whether a service registers with a lookup service (belonging 
 * to the public group) that starts after the service started.
 */
public class PublicGroupTest extends DefaultTest {
    
    private BasicServiceAdmin admin;
    private ServiceRegistrar lookup;


    /**
     * Starts up the service and waits quietTime
     * so that (hopefully) there are no 
     * further multicast requests, and then starts a lookup
     * service that belongs to the public group. The test registers 
     * with the lookup service to be notified when the service of 
     * interest registers and then waits for six times the failureTime. 
     * If no notification is received, it follows up with a query to
     * the lookup service to make sure the event wasn't lost or missed. 
     * If the service has not registered with the lookup service 
     * during that time, the service fails.
     *
     * @param args Command line arguments passed through
     * @return The status of this run (failed or passed
     *         and a comment)
     */
    public Status run(String[] args) {
	Config conf = getConfig();
	admin = (BasicServiceAdmin) conf.getAdmin();

	try {
            TestUtility.log(Config.ALL,"Test: starting program");
            admin.start();
        } catch (RemoteException re) {
            TestUtility.log(Config.ERROR,"Test: caught exception",re);
            return Status.failed(Status.ENV,"Problem starting program");
        }

        /* Try to wait through all the multicast requests so that
         * the registration is more likely to be from announcements */
        try {
            TestUtility.log(Config.ALL,"Test: waiting "
                + conf.getQuietTime() + " milliseconds for (hopefully) "
                + "all multicast requests");
            Thread.sleep(conf.getQuietTime());
        } catch (InterruptedException ie) {
            TestUtility.log(Config.ERROR,"Test: caught exception",ie);
            return Status.failed(Status.ENV,"Test interrupted");
        }

	try {
	    TestUtility.log(Config.ALL,"Test: starting lookup in public group");
	    lookup = TestUtility.startLookup(new String[] {""}, 
		conf.getQuietTime(), "PublicGroupTest_RegistrarLog", conf);
	} catch (Exception e) {
            TestUtility.log(Config.ERROR,"Test: caught exception",e);
            return Status.failed(Status.ENV,"Problem starting lookup service");
        }

	ServiceItem serviceItem = null;
	try {
            long timeout = 6 * conf.getFailureTime();
            TestUtility.log(Config.ALL,"Test: waiting up to " + timeout 
                + " milliseconds for program to register in lookup");
	    serviceItem = TestUtility.waitForRegistration(lookup,timeout,conf);
	} catch (Exception e) {
            TestUtility.log(Config.ERROR,"Test: caught exception",e);
            return Status.failed(Status.ENV,"Problem while waiting for "
                + "program to register with lookup");
        }
	    
	if (serviceItem == null) {
	    return Status.failed(Status.TEST, "Service never found");
	}    

	return Status.passed("OK");
    }    

    public void tearDown() {
        super.tearDown();

        try {
            if (lookup != null) {
                TestUtility.log(Config.ALL,"Test: stopping lookup");
                TestUtility.stopLookup(lookup, getConfig());
            }
        } catch (RemoteException ignore) {}
        try {
            if (admin != null) {
                TestUtility.log(Config.ALL,"Test: stopping program");
                admin.stop();
            }
        } catch (RemoteException ignore) {}
    }
       
    public String[] getCategories() {
	return (new String[] { BasicServiceAdmin.CATEGORY });
    }

    public Class[] getRequiredAdmins() {
	return (new Class[] { BasicAdmin.class, BasicServiceAdmin.class });
    }

    public String getDescription() {
	return "PublicGroupTest";
    }

}

