Package org.jboss.soa.esb.listeners

Source Code of org.jboss.soa.esb.listeners.ListenerManagerFileUnitTest

/*
* JBoss, Home of Professional Open Source
* Copyright 2006, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This 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 (at your option) any later version.
*
* This software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.soa.esb.listeners;

import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.util.List;
import java.util.UUID;

import org.jboss.internal.soa.esb.couriers.DeliverOnlyCourier;
import org.jboss.soa.esb.addressing.Call;
import org.jboss.soa.esb.addressing.EPR;
import org.jboss.soa.esb.addressing.eprs.FileEpr;
import org.jboss.soa.esb.couriers.CourierFactory;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycle;
import org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycleController;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.body.content.BytesBody;
import org.jboss.soa.esb.message.format.MessageFactory;

public class ListenerManagerFileUnitTest extends ListenerManagerBaseTest
{
  public ListenerManagerFileUnitTest ()
  {
    _file = "listenerFile.xml";
  }

  public void testListener ()
  {
    _logger.info("___Test for FILENAME: " + _file
          + " starting ____________________");
   
    try
    {
      oneTest();
    }
    catch (Exception ex)
    {
      _logger.error(ex.toString(), ex);
            ex.printStackTrace();
            fail();
    }
   
    _logger.info("___Test for FILENAME: " + _file
          + " FINISHED ____________________");
  }

  public void setUp() throws Exception {
    _logger.info("Writing temp files to " + TMP_DIR);

    clearMessages() ;

    // initialize registry
    runBeforeAllTests();
  }

  public void tearDown() throws Exception
  {
    clearMessages() ;
   
    runAfterAllTests();
  }
 
  // TODO more refactoring, since oneText is duplicated a lot!
 
  protected void oneTest () throws Exception
 
    // Write wome messages to EPR obtained from configuration file
    String configFile = getClass().getResource(_file).getFile();
    ConfigTree tree = ConfigTree.fromInputStream(new FileInputStream(
        configFile));
    final File tmpDir = new File(System.getProperty("java.io.tmpdir")) ;
    final String tmpDirForm = tmpDir.toURL().toExternalForm() ;

        ConfigTree eprElement = tree.getAllChildren()[0].getFirstChild("EPR");
    eprElement.setAttribute(ListenerTagNames.URL_TAG, tmpDirForm) ;
   
    EPR toEPR = ListenerUtil.assembleEpr(eprElement);
   
    if (toEPR instanceof FileEpr)
    {
      eprElement.setAttribute(FileEpr.INPUT_SUFFIX_TAG, eprElement
          .getAttribute(FileEpr.INPUT_SUFFIX_TAG));
      toEPR = ListenerUtil.assembleEpr(eprElement);
    }
    else
      fail();
   
    String THE_TEXT = "___Config=" + _file + "___ Message Content ___";

    int howMany = 10; // how many messages do you want to send before the
              // listener comes up
   
    DeliverOnlyCourier sender = CourierFactory.getCourier(toEPR);
   
    Message message = MessageFactory.getInstance().getMessage();
    message.getHeader().setCall(new Call(toEPR));
    message.getBody().add(THE_TEXT.getBytes());
   
    for (int i1 = 0; i1 < howMany; i1++)
    {
      URI uri = new URI(UUID.randomUUID().toString());
      message.getHeader().getCall().setMessageID(uri);
      sender.deliver(message);
    }

    //     launch listener manager in a child thread
    final ConfigTree newTree = ConfigTree.fromInputStream(new FileInputStream(
        configFile));
    final ConfigTree newTreeEPRElement = newTree.getAllChildren()[0].getFirstChild("EPR");
    newTreeEPRElement.setAttribute(ListenerTagNames.URL_TAG, tmpDirForm) ;
               
                final List<ManagedLifecycle> instances = LifecycleUtil.getListeners(newTree) ;
                final ManagedLifecycleController controller = new ManagedLifecycleController(instances) ;
                controller.start() ;
               
    _logger.debug(" All child listeners ready");

    // JUST FOR THIS TEST:
    // Give your listener some time to process queued messages (see howMany
    // above)
    // Time allowed, and maxThreads in config file will impact how many
    // messages
    // will be processed, and how many will remain unprocessed
   
    for (int count = 0 ; count < howMany ; count++)
    {
      final String response = getMessage(10000) ;
      assertNotNull("getMessage timeout", response) ;
      assertEquals(THE_TEXT, response);
    }

    _logger.debug("going to stop");
    controller.stop();
    _logger.debug("back from stop");
  }
 
  protected String _file;
 
}
TOP

Related Classes of org.jboss.soa.esb.listeners.ListenerManagerFileUnitTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.