/**
 *
 * Copyright 2004 Hiram Chirino
 *
 * 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.activemq.itests;

import java.rmi.RemoteException;
import java.util.Hashtable;

import javax.ejb.CreateException;
import javax.jms.JMSException;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import junit.framework.TestCase;

import org.codehaus.activemq.itest.ejb.MessengerHome;
import org.codehaus.activemq.itest.ejb.MessengerObject;
import org.codehaus.activemq.message.ActiveMQQueue;

/**
 * @version $Revision: 1.2 $
 */
public class OutboundResourceAdapterTest extends TestCase {


    private static final String MESSENGERBEAN_JNDI = "org/codehaus/activemq/itest/MessengerBean";
    private static final int REPS = 5;

    public void xtestSendReceive() throws CreateException, RemoteException, NamingException, JMSException {
        InitialContext ctx = createInitialContext();
        MessengerHome home = (MessengerHome) ctx.lookup(MESSENGERBEAN_JNDI);
        MessengerObject messenger = home.create();

        String msg1 = "Hello World";
        messenger.sendMessage(msg1, REPS);
        int received = messenger.receiveMessage(10000, REPS);
        assertEquals(REPS, received);

    }

    public void testMessageAreConsumed() throws CreateException, RemoteException, NamingException, JMSException {
        InitialContext ctx = createInitialContext();
        MessengerHome home = (MessengerHome) ctx.lookup(MESSENGERBEAN_JNDI);
        MessengerObject messenger = home.create();
        ActiveMQQueue queue = new ActiveMQQueue("testMessageAreConsumed");
        
        messenger.sendMessage(queue, "Test:1");
        String msg = messenger.receiveMessage(queue, 10000);
        assertEquals("Test:1", msg);
        
        messenger.sendMessage(queue, "Test:2");
        msg = messenger.receiveMessage(queue, 10000);
        assertEquals("Test:2", msg);
    }
    
    /**
     * @throws NamingException
     */
    private InitialContext createInitialContext() throws NamingException {
        Hashtable props = new Hashtable();
        props.put("java.naming.factory.initial", "org.openejb.client.RemoteInitialContextFactory");
        props.put("java.naming.provider.url", "127.0.0.1:4201");
        props.put("java.naming.security.principal", "testuser");
        props.put("java.naming.security.credentials", "testpassword");
        return new InitialContext(props);
    }

}
