Coverage Report - org.mule.component.simple.StaticComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
StaticComponent
0%
0/27
0%
0/8
1.7
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.component.simple;
 8  
 
 9  
 import org.mule.api.MuleEventContext;
 10  
 import org.mule.api.lifecycle.Callable;
 11  
 import org.mule.api.lifecycle.Initialisable;
 12  
 import org.mule.api.lifecycle.InitialisationException;
 13  
 import org.mule.util.IOUtils;
 14  
 
 15  
 import java.io.IOException;
 16  
 
 17  
 /**
 18  
  * A service that will return a static data object as a result. This is useful for
 19  
  * testing with expected results. The data returned can be read from a file or set as
 20  
  * a property on this service.
 21  
  */
 22  0
 public class StaticComponent implements Callable, Initialisable
 23  
 {
 24  
 
 25  
     private Object data;
 26  
     private String dataFile;
 27  
     private String prefix;
 28  
     private String postfix;
 29  
 
 30  
     public void initialise() throws InitialisationException
 31  
     {
 32  0
         if (dataFile != null)
 33  
         {
 34  
             try
 35  
             {
 36  0
                 data = IOUtils.getResourceAsString(dataFile, getClass());
 37  
             }
 38  0
             catch (IOException e)
 39  
             {
 40  0
                 throw new InitialisationException(e, this);
 41  0
             }
 42  
         }
 43  0
     }
 44  
 
 45  
     public Object getData()
 46  
     {
 47  0
         return data;
 48  
     }
 49  
 
 50  
     public void setData(Object data)
 51  
     {
 52  0
         this.data = data;
 53  0
     }
 54  
 
 55  
     public String getDataFile()
 56  
     {
 57  0
         return dataFile;
 58  
     }
 59  
 
 60  
     public void setDataFile(String dataFile)
 61  
     {
 62  0
         this.dataFile = dataFile;
 63  0
     }
 64  
 
 65  
     public String getPrefix()
 66  
     {
 67  0
         return prefix;
 68  
     }
 69  
 
 70  
     public void setPrefix(String prefix)
 71  
     {
 72  0
         this.prefix = prefix;
 73  0
     }
 74  
 
 75  
     public String getPostfix()
 76  
     {
 77  0
         return postfix;
 78  
     }
 79  
 
 80  
     public void setPostfix(String postfix)
 81  
     {
 82  0
         this.postfix = postfix;
 83  0
     }
 84  
 
 85  
     public Object onCall(MuleEventContext eventContext) throws Exception
 86  
     {
 87  0
         if (data != null)
 88  
         {
 89  0
             return data;
 90  
         }
 91  
 
 92  0
         String eventData = eventContext.transformMessageToString();
 93  
 
 94  0
         if (prefix != null)
 95  
         {
 96  0
             eventData = prefix + eventData;
 97  
         }
 98  
 
 99  0
         if (postfix != null)
 100  
         {
 101  0
             eventData += postfix;
 102  
         }
 103  
 
 104  0
         return eventData;
 105  
     }
 106  
 }