BrowserTest.java :  » Workflow-Engines » bpmscript » org » bpmscript » jbi » Java Open Source

Java Open Source » Workflow Engines » bpmscript 
bpmscript » org » bpmscript » jbi » BrowserTest.java
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.bpmscript.jbi;

import java.util.Enumeration;

import javax.jms.Connection;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;

import junit.framework.TestCase;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.xbean.BrokerFactoryBean;
import org.apache.log4j.BasicConfigurator;
import org.springframework.core.io.ClassPathResource;

public class BrowserTest extends TestCase {
  
  protected final transient org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
      .getLog(getClass());

  protected void setUp() throws Exception {
    super.setUp();
  }

  protected void tearDown() throws Exception {
    super.tearDown();
  }
  
  public void testSomething() throws Exception {
    BasicConfigurator.configure();
    BrokerFactoryBean bean = new BrokerFactoryBean();
    bean.setConfig(new ClassPathResource("inmemoryactivemq.xml"));
    bean.afterPropertiesSet();
    
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    Connection connection = connectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue("org.bpmscript.jbi.BrowserTestQueue");
    MessageProducer producer = session.createProducer(queue);

    try {
      ObjectMessage message = session.createObjectMessage("Hello World!");
      producer.send(message);
      QueueBrowser browser = session.createBrowser(queue);
      
      Enumeration enumeration = browser.getEnumeration();
      assertTrue(enumeration.hasMoreElements());
      
      MessageConsumer consumer = session.createConsumer(queue);
      Message amessage = consumer.receiveNoWait();
      assertNotNull(amessage);
      
    } finally {
      // clear out old messages
      MessageConsumer consumer = session.createConsumer(queue);
      Message message = null;
      while((message = consumer.receiveNoWait()) != null) {
        log.info("this shouldn't be here:" + message);
      }
    }
    
    session.close();
    connection.stop();
    connection.close();

    bean.destroy();
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.