/*
 * 
 * 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.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 can start up and successfully register 
 * with an already running lookup service within a certain amount 
 * of time after starting up.  Test also checks that the service's
 * service ID conforms to the ServiceID specification.
 */
public class RegistrationTest extends DefaultTest {

    private BasicServiceAdmin admin;
    private ServiceRegistrar lookup;

    /**
     * Starts up a lookup service in the public group, starts up 
     * the service being tested, and posts a notification request to 
     * the lookup service to see if this service registers.
     * If the test doesn't receive a notification within the time 
     * allotted, it performs a lookup to make sure that the remote 
     * event wasn't lost and that the service really
     * did not register. If the service still doesn't appear 
     * to be registered or if the service's service ID is
     * non-conformant, it 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 lookup in public group");
	    lookup = TestUtility.startLookup(new String[] {""}, 
		conf.getQuietTime(), "RegistrationTest_RegistrarLog", conf);
	} catch (Exception e) {
            TestUtility.log(Config.ERROR,"Test: caught exception",e);
            return Status.failed(Status.ENV,"Problem starting lookup service");
        }

	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");
        }
	    
	ServiceItem serviceItem = null;
	try {
	    long timeout = 4 * 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 registered");
	}    

	if (serviceItem.serviceID == null) {
	    return Status.failed(Status.TEST, 
                "Service registered without a ServiceID");
	}    

	String reason = TestUtility.checkServiceID(serviceItem.serviceID);
	if (reason != null) {
	    return Status.failed(Status.TEST, 
                "Service's service ID is non-conformant: " + reason);
	}    

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

}

