View Javadoc

1   /*
2    * $Id: InboundMessageLossTestCase.java 22491 2011-07-21 10:04:30Z claude.mamo $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport.ftp.reliability;
12  
13  import org.mule.exception.DefaultSystemExceptionStrategy;
14  import org.mule.routing.filters.WildcardFilter;
15  import org.mule.tck.probe.PollingProber;
16  import org.mule.tck.probe.Probe;
17  import org.mule.tck.probe.Prober;
18  import org.mule.transport.ftp.AbstractFtpServerTestCase;
19  
20  import java.util.Arrays;
21  import java.util.Collection;
22  
23  import org.junit.Test;
24  import org.junit.runners.Parameterized.Parameters;
25  
26  /**
27   * Verify that no inbound messages are lost when exceptions occur.
28   * The message must either make it all the way to the SEDA queue (in the case of
29   * an asynchronous inbound endpoint), or be restored/rolled back at the source.
30   *
31   * In the case of FTP, this will cause the postProcess() method to not be executed
32   * and therefore the source file will not be deleted.
33   */
34  public class InboundMessageLossTestCase extends AbstractFtpServerTestCase
35  {
36      /** Polling mechanism to replace Thread.sleep() for testing a delayed result. */
37      protected Prober prober = new PollingProber(10000, 100);
38  
39      public InboundMessageLossTestCase(ConfigVariant variant, String configResources)
40      {
41          super(variant, configResources);
42      }
43      
44      @Parameters
45      public static Collection<Object[]> parameters()
46      {
47          return Arrays.asList(new Object[][]{
48              {ConfigVariant.SERVICE, "reliability/inbound-message-loss.xml"}            
49          });
50      }      
51      
52      @Override
53      protected void doSetUp() throws Exception
54      {
55          super.doSetUp();
56  
57          // Set SystemExceptionStrategy to redeliver messages (this can only be configured programatically for now)
58          ((DefaultSystemExceptionStrategy) muleContext.getExceptionListener()).setRollbackTxFilter(new WildcardFilter("*"));
59  
60          // Create a separate source directory for each test case
61          createFtpServerDir("noException");
62          createFtpServerDir("transformerException");
63          createFtpServerDir("routerException");
64          createFtpServerDir("componentException");
65      }
66  
67      @Test
68      public void testNoException() throws Exception
69      {
70          createFileOnFtpServer("noException/test1");
71          prober.check(new Probe()
72          {
73              @Override
74              public boolean isSatisfied()
75              {
76                  // Delivery was successful so message should be gone
77                  return !fileExists("noException/test1");
78              }
79  
80              @Override
81              public String describeFailure()
82              {
83                  return "File should be gone";
84              }
85          });
86      }
87  
88      @Test
89      public void testTransformerException() throws Exception
90      {
91          createFileOnFtpServer("transformerException/test1");
92          prober.check(new Probe()
93          {
94              @Override
95              public boolean isSatisfied()
96              {
97                  // Delivery failed so message should have been restored at the source
98                  return fileExists("transformerException/test1");
99              }
100 
101             @Override
102             public String describeFailure()
103             {
104                 return "File should have been restored";
105             }
106         });
107     }
108 
109     @Test
110     public void testRouterException() throws Exception
111     {
112         createFileOnFtpServer("routerException/test1");
113         prober.check(new Probe()
114         {
115             @Override
116             public boolean isSatisfied()
117             {
118                 // Delivery failed so message should have been restored at the source
119                 return fileExists("routerException/test1");
120             }
121 
122             @Override
123             public String describeFailure()
124             {
125                 return "File should have been restored";
126             }
127         });
128     }
129 
130     @Test
131     public void testComponentException() throws Exception
132     {
133         createFileOnFtpServer("componentException/test1");
134         prober.check(new Probe()
135         {
136             @Override
137             public boolean isSatisfied()
138             {
139                 // Component exception occurs after the SEDA queue for an asynchronous request, so from the client's
140                 // perspective, the message has been delivered successfully.
141                 return !fileExists("componentException/test1");
142             }
143 
144             @Override
145             public String describeFailure()
146             {
147                 return "File should be gone";
148             }
149         });
150     }
151 }