/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jtests.clients.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSubscriber;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.objectweb.jonas.jtests.util.JTestCase;
import org.objectweb.jonas.jtests.beans.message.Sender;
import org.objectweb.jonas.jtests.beans.message.SenderHome;
import org.objectweb.jonas.jtests.beans.message.Sender1_1;
import org.objectweb.jonas.jtests.beans.message.Sender1_1Home;

public class F_NoBean extends JTestCase {


    public F_NoBean(String name) {
        super(name);
    }

    /**
     * init environment:
     */
    protected void setUp() {
        super.setUp();
    }


    /**
     * Test the NoLocal attribute.
     * @throws Exception
     */
    public void testNoLocal() throws Exception {
        ConnectionFactory cf = (ConnectionFactory) ictx.lookup("JCF");
        Topic topic = (Topic) ictx.lookup("sampleTopic");
        Connection tc = cf.createConnection();
        Session sess = tc.createSession(false, Session.AUTO_ACKNOWLEDGE);
        TopicSubscriber tl = sess.createDurableSubscriber(topic, "NoLocal", "", true);
        MessageProducer pd = sess.createProducer(topic);
        tc.start();
        
        // Send 2 messages on the topic
        TextMessage messageSent = null;
        messageSent = sess.createTextMessage();
        messageSent.setText("Just a test");
        messageSent.setStringProperty("TEST_NAME", "testNoLocal");
        messageSent.setBooleanProperty("lastMessage", false);
        pd.send(messageSent);
        messageSent.setStringProperty("TEST_NAME", "test");
        messageSent.setBooleanProperty("lastMessage", true);
        pd.send(messageSent);

        // Verify that tl did not receive any message
        long timeout = 2000L;
        TextMessage messageReceived = null;
        messageReceived = (TextMessage)tl.receive(timeout);
        if (messageReceived != null) {
            fail("createDurableSubscriber with NoLocal");
        }

        sess.close();
        tc.close();
    }

    /**
     * Run all the tests
     */
    public static Test suite() {
        return new TestSuite(F_NoBean.class);
    }

    public static void main (String args[]) {
        String testtorun = null;
        // Get args
        for (int argn = 0; argn < args.length; argn++) {
            String s_arg = args[argn];
            if (s_arg.equals("-n")) {
                testtorun = args[++argn];
            }
        }
        if (testtorun == null) {
            junit.textui.TestRunner.run(suite());
        } else {
            junit.textui.TestRunner.run(new F_NoBean(testtorun));
        }
    }
}
