Coverage Report - org.mule.tck.functional.FunctionalStreamingTestComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
FunctionalStreamingTestComponent
0%
0/65
0%
0/24
2.857
 
 1  
 /*
 2  
  * $Id: FunctionalStreamingTestComponent.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.tck.functional;
 12  
 
 13  
 import org.mule.impl.model.streaming.StreamingService;
 14  
 import org.mule.umo.UMOEventContext;
 15  
 import org.mule.util.ClassUtils;
 16  
 import org.mule.util.StringMessageUtils;
 17  
 
 18  
 import java.io.InputStream;
 19  
 import java.io.OutputStream;
 20  
 
 21  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 
 25  
 /**
 26  
  * A component that can be used by streaming functional tests. This component accepts an
 27  
  * EventCallback that can be used to assert the state of the current event.  To access the
 28  
  * component when embedded in an (XML) model, make sure that the descriptor sets the
 29  
  * singleton attribute true - see uses in TCP and FTP.
 30  
  *
 31  
  * Note that although this implements the full StreamingService interface, nothing is
 32  
  * written to the output stream - this is intended as a final sink.
 33  
  *
 34  
  * @see org.mule.tck.functional.EventCallback
 35  
  */
 36  
 
 37  
 public class FunctionalStreamingTestComponent implements StreamingService
 38  
 {
 39  0
     protected transient Log logger = LogFactory.getLog(getClass());
 40  
 
 41  0
     private static AtomicInteger count = new AtomicInteger(0);
 42  0
     private int number = count.incrementAndGet();
 43  
 
 44  
     public static final int STREAM_SAMPLE_SIZE = 4;
 45  
     public static final int STREAM_BUFFER_SIZE = 4096;
 46  
     private EventCallback eventCallback;
 47  0
     private String summary = null;
 48  0
     private long targetSize = -1;
 49  
 
 50  
     public FunctionalStreamingTestComponent()
 51  0
     {
 52  0
         logger.debug("creating " + toString());
 53  0
     }
 54  
 
 55  
     public void setEventCallback(EventCallback eventCallback, long targetSize)
 56  
     {
 57  0
         logger.debug("setting callback: " + eventCallback + " in " + toString());
 58  0
         this.eventCallback = eventCallback;
 59  0
         this.targetSize = targetSize;
 60  0
     }
 61  
 
 62  
     public String getSummary()
 63  
     {
 64  0
         return summary;
 65  
     }
 66  
 
 67  
     public int getNumber()
 68  
     {
 69  0
         return number;
 70  
     }
 71  
 
 72  
     public void call(InputStream in, OutputStream unused, UMOEventContext context) throws Exception
 73  
     {
 74  
         try
 75  
         {
 76  0
             logger.debug("arrived at " + toString());
 77  0
             byte[] startData = new byte[STREAM_SAMPLE_SIZE];
 78  0
             int startDataSize = 0;
 79  0
             byte[] endData = new byte[STREAM_SAMPLE_SIZE]; // ring buffer
 80  0
             int endDataSize = 0;
 81  0
             int endRingPointer = 0;
 82  0
             long streamLength = 0;
 83  0
             byte[] buffer = new byte[STREAM_BUFFER_SIZE];
 84  
 
 85  
             // throw data on the floor, but keep a record of size, start and end values
 86  0
             int bytesRead = 0;
 87  0
             while (bytesRead >= 0)
 88  
             {
 89  0
                 bytesRead = in.read(buffer);
 90  0
                 if (bytesRead > 0)
 91  
                 {
 92  0
                     if (logger.isDebugEnabled())
 93  
                     {
 94  0
                         logger.debug("read " + bytesRead + " bytes");
 95  
                     }
 96  0
                     streamLength += bytesRead;
 97  0
                     int startOfEndBytes = 0;
 98  0
                     for (int i = 0; startDataSize < STREAM_SAMPLE_SIZE && i < bytesRead; ++i)
 99  
                     {
 100  0
                         startData[startDataSize++] = buffer[i];
 101  0
                         ++startOfEndBytes; // skip data included in startData
 102  
                     }
 103  0
                     startOfEndBytes = Math.max(startOfEndBytes, bytesRead - STREAM_SAMPLE_SIZE);
 104  0
                     for (int i = startOfEndBytes; i < bytesRead; ++i)
 105  
                     {
 106  0
                         ++endDataSize;
 107  0
                         endData[endRingPointer++ % STREAM_SAMPLE_SIZE] = buffer[i];
 108  
                     }
 109  0
                     if (streamLength >= targetSize)
 110  
                     {
 111  0
                         doCallback(startData, startDataSize,
 112  
                                 endData, endDataSize, endRingPointer,
 113  
                                 streamLength, context);
 114  
                     }
 115  0
                 }
 116  
             }
 117  
 
 118  
         }
 119  0
         catch (Exception e)
 120  
         {
 121  0
             if (logger.isDebugEnabled())
 122  
             {
 123  0
                 logger.debug(e);
 124  
             }
 125  0
             throw e;
 126  0
         }
 127  0
     }
 128  
 
 129  
     private void doCallback(byte[] startData, int startDataSize,
 130  
                             byte[] endData, int endDataSize, int endRingPointer,
 131  
                             long streamLength, UMOEventContext context) throws Exception
 132  
     {
 133  
         // make a nice summary of the data
 134  0
         StringBuffer result = new StringBuffer("Received stream");
 135  0
         result.append("; length: ");
 136  0
         result.append(streamLength);
 137  0
         result.append("; '");
 138  
 
 139  0
         for (int i = 0; i < startDataSize; ++i)
 140  
         {
 141  0
             result.append((char) startData[i]);
 142  
         }
 143  
 
 144  0
         int endSize = Math.min(endDataSize, STREAM_SAMPLE_SIZE);
 145  0
         if (endSize > 0)
 146  
         {
 147  0
             result.append("...");
 148  0
             for (int i = 0; i < endSize; ++i)
 149  
             {
 150  0
                 result.append((char) endData[(endRingPointer + i) % STREAM_SAMPLE_SIZE]);
 151  
             }
 152  
         }
 153  0
         result.append("'");
 154  
 
 155  0
         summary = result.toString();
 156  
 
 157  0
         String msg = StringMessageUtils.getBoilerPlate("Message Received in component: "
 158  
                 + context.getComponentDescriptor().getName() + ". " + summary
 159  
                 + "\n callback: " + eventCallback,
 160  
                 '*', 80);
 161  
 
 162  0
         logger.info(msg);
 163  
 
 164  0
         if (eventCallback != null)
 165  
         {
 166  0
             eventCallback.eventReceived(context, this);
 167  
         }
 168  0
     }
 169  
 
 170  
     public String toString()
 171  
     {
 172  0
         return ClassUtils.getSimpleName(getClass()) + "/" + number;
 173  
     }
 174  
 
 175  
 }