/*
 * 
 * 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 does not register with a lookup service 
 * that only belongs to groups that are not of interest to the service.
 */
public class NonPublicGroupTest extends DefaultTest {

    private BasicAdmin admin;
    private ServiceRegistrar lookup;

    /**
     * Starts up the service and waits quietTime
     * so that (hopefully) there are no further multicast 
     * requests. The test then starts a lookup service that belongs 
     * to a group whose name is eight randomly generated
     * non-letter characters. The test registers with the 
     * lookup service to be notified when a service of interest 
     * registers and then the test waits. If no notification is
     * received during the value of the quietTime, 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 passes.
     *
     * @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 {
	    String group = TestUtility.randomString(10);
	    TestUtility.log(Config.ALL,"Test: starting lookup in "
		+ group + " group");
	    lookup = TestUtility.startLookup(new String[] {group}, 
		conf.getQuietTime(), "NonPublicGroupTest_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 = conf.getQuietTime();
            TestUtility.log(Config.ALL,"Test: waiting up to " + timeout 
                + " milliseconds to see if program registers 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 registered in "
		+ "lookup when it shouldn't have");
	}

	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 "NonPublicGroupTest";
    }

}

