/*
 * 
 * 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.harness.TestUtility;
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.ServiceID;
import java.rmi.RemoteException;

/** This class is used to test that a service registered with
 *  the Lookup service is assigned a service ID that conforms
 *  to the ServiceID specification.
 *
 *  @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 ServiceIDTest extends TestRegistrar {

    private ServiceItem[] srvcItems ;
    private ServiceRegistration[] srvcRegs ;
    private int nInstances = 0;

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

    public ServiceIDTest() {
    }

    /** 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.
     *  @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();
    }

    /** Executes the current QA test.
     *  @exception QATestException usually indicates test failure
     */
    public void doQATest() throws TestException {
        if (srvcRegs[0] == null) {
            throw new TestException("Service never registered");
        }    

        ServiceID serviceID = srvcRegs[0].getServiceID();
        if (serviceID == null) {
            throw new TestException("Service registered without a ServiceID");
        }    

        String reason = TestUtility.checkServiceID(serviceID);
        if (reason != null) {
            throw new TestException(
                "Service's service ID is non-conformant: " + reason);
        }    
    }


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