/**
 * 
 * Copyright 2004 Protique Ltd
 * 
 * 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 org.codehaus.activemq.transport;

import junit.framework.TestCase;
import org.codehaus.activecluster.ClusterException;

import javax.jms.JMSException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version $Revision: 1.3 $
 */
public abstract class DiscoveryAgentTestSupport extends TestCase implements DiscoveryListener {
    protected boolean waitForever = false;

    protected DiscoveryAgent agent;
    protected Map localDetails = new HashMap();
    protected String type = "_activemq_testcase.";

    public void testDiscovery() throws Exception {
        localDetails.put("cheese", "Cheddar");
        localDetails.put("location", "London");

        agent.addDiscoveryListener(this);
        agent.start();
        agent.registerService(createName(), localDetails);

        if (waitForever) {
            synchronized (this) {
                this.wait();
            }
        }

        agent.stop();
    }

    protected String createName() {
        return getName() + "@" + System.currentTimeMillis();
    }

    protected void setUp() throws Exception {
        super.setUp();
        agent = createDiscoveryAgent();
        if (System.getProperty("waitForever") != null) {
            waitForever = true;
        }
    }

    protected abstract DiscoveryAgent createDiscoveryAgent() throws JMSException, ClusterException;

    public void addService(DiscoveryEvent event) {
        System.out.println("Added service: " + event + " with details: " + event.getServiceDetails() + " name: " + event.getServiceName());
    }

    public void removeService(DiscoveryEvent event) {
        System.out.println("Removed service: " + event);
    }
}
