/*
 * 
 * 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 com.sun.jini.compat.test.lookup.TestImpl;
import com.sun.jini.compat.test.lookup.TestRegistrar;
import com.sun.jini.compat.test.lookup.TestUtils;
import com.sun.jini.compat.test.lookup.TestException;
import com.sun.jini.compat.test.lookup.TestFailedException;
import com.sun.jini.compat.test.lookup.TestUnresolvedException;

import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceItem;
import net.jini.core.lookup.ServiceRegistration;
import net.jini.core.lookup.ServiceMatches;
import net.jini.core.lookup.ServiceTemplate;
import java.rmi.RemoteException;

import com.sun.jini.compat.lease.LeaseRenewalManager;

/** This class is used to test that every service item registered with
 *  the Lookup service can be successfully looked up using only its service ID.
 *
 *  @see com.sun.jini.compat.test.lookup.QATest
 *  @see com.sun.jini.compat.test.lookup.QATestImpl
 *  @see com.sun.jini.compat.test.lookup.QATestRegistrar
 *  @see com.sun.jini.compat.test.lookup.QATestUtils
 */
public class LookupByServiceIDTest extends TestRegistrar {

    private ServiceItem[] srvcItems ;
    private ServiceRegistration[] srvcRegs ;
    private ServiceTemplate[] srvcIDTmpls;
    private ServiceRegistrar proxy;
    private int nInstances = 0;
    private LeaseRenewalManager lrm;

    /** the expected number of matches when testing lookup by ID */
    private static int EXPECTED_N_MATCHES = 1;

    public LookupByServiceIDTest() {
    }

    /** Performs actions necessary to prepare for execution of the 
     *  current QA test.
     *
     *  Creates the lookup service. Loads and instantiates all service 
     *  classes; then registers each service class instance with the maximum
     *  service lease duration. Creates an array of ServiceTemplates in 
     *  which each element contains the service ID of one of the registered
     *  service items.
     *  @exception QATestException will usually indicate an "unresolved"
     *  condition because at this point the test has not yet begun.
     */
    public void setupQATest() throws TestException {
	super.setupQATest();
        nInstances = super.getNInstances();

	srvcItems = super.createServiceItems(TEST_SRVC_CLASSES);
	srvcRegs = super.registerAll();
	proxy = super.getProxy();

	srvcIDTmpls = new ServiceTemplate[nInstances];
	for(int i=0; i<srvcIDTmpls.length; i++) {
	    srvcIDTmpls[i] = new ServiceTemplate(srvcRegs[i].getServiceID(),
                                                 null,null);
	}
	lrm = new LeaseRenewalManager();
        for(int i=0; i<srvcRegs.length; i++) {
            lrm.renewUntil(srvcRegs[i].getLease(),
                         Long.MAX_VALUE, null);
        }

    }

    /** Executes the current QA test.
     *
     *  For each service registered:  
     *      1. Performs a simple lookup using the corresponding template 
     *         created during setup and then tests that the object returned 
     *         equals the service item that was registered with the 
     *         corresponding service ID.
     *      2. Performs a match lookup using the corresponding template 
     *         created during setup and then tests that the number of matches
     *         found equals 1; and that the object returned equals the 
     *         service item that was registered with the corresponding 
     *         service ID.
     *  @exception QATestException usually indicates test failure
     */
    public void doQATest() throws TestException {
        TestUtils.doLookup(srvcItems,srvcIDTmpls,proxy);
    }


    /** Performs cleanup actions necessary to achieve a graceful exit of 
     *  the current QA test.
     *  @exception QATestException will usually indicate an "unresolved"
     *  condition because at this point the test has completed.
     */
    public void cleanupQATest() throws TestException {
	super.cleanupQATest();
	lrm.clear();
    }
   
    public String getDescription() {
	return "LookupByServiceIDTest";
    }
}
