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.file.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.file.AbstractFileMoveDeleteTestCase;
19  
20  import java.io.File;
21  
22  import org.junit.Test;
23  
24  /**
25   * Verify that no inbound messages are lost when exceptions occur. The message must
26   * either make it all the way to the SEDA queue (in the case of an asynchronous
27   * inbound endpoint), or be restored/rolled back at the source. In the case of the
28   * File transport, this will cause the file to be restored to its original location
29   * from the working directory. Note that a workDirectory must be specified on the
30   * connector in order for this to succeed.
31   */
32  public class InboundMessageLossTestCase extends AbstractFileMoveDeleteTestCase
33  {
34      /** Polling mechanism to replace Thread.sleep() for testing a delayed result. */
35      protected Prober prober = new PollingProber(10000, 100);
36  
37      public InboundMessageLossTestCase(ConfigVariant variant, String configResources)
38      {
39          super(variant, configResources);
40  
41      }
42  
43      @Override
44      protected String getConfigResources()
45      {
46          return "reliability/inbound-message-loss.xml";
47      }
48  
49      @Override
50      protected void doSetUp() throws Exception
51      {
52          super.doSetUp();
53  
54          // Set SystemExceptionStrategy to redeliver messages (this can only be
55          // configured programatically for now)
56          ((DefaultSystemExceptionStrategy) muleContext.getExceptionListener()).setRollbackTxFilter(new WildcardFilter(
57              "*"));
58      }
59  
60      @Test
61      public void testNoException() throws Exception
62      {
63          tmpDir = createFolder(".mule/noException");
64          final File file = createDataFile(tmpDir, "test1.txt");
65          prober.check(new Probe()
66          {
67              @Override
68              public boolean isSatisfied()
69              {
70                  // Delivery was successful so message should be gone
71                  return !file.exists();
72              }
73  
74              @Override
75              public String describeFailure()
76              {
77                  return "File should be gone";
78              }
79          });
80      }
81  
82      @Test
83      public void testTransformerException() throws Exception
84      {
85          tmpDir = createFolder(".mule/transformerException");
86          final File file = createDataFile(tmpDir, "test1.txt");
87          prober.check(new Probe()
88          {
89              @Override
90              public boolean isSatisfied()
91              {
92                  // Delivery failed so message should have been restored at the source
93                  return file.exists();
94              }
95  
96              @Override
97              public String describeFailure()
98              {
99                  return "File should have been restored";
100             }
101         });
102     }
103 
104     @Test
105     public void testRouterException() throws Exception
106     {
107         tmpDir = createFolder(".mule/routerException");
108         final File file = createDataFile(tmpDir, "test1.txt");
109         prober.check(new Probe()
110         {
111             @Override
112             public boolean isSatisfied()
113             {
114                 // Delivery failed so message should have been restored at the source
115                 return file.exists();
116             }
117 
118             @Override
119             public String describeFailure()
120             {
121                 return "File should have been restored";
122             }
123         });
124     }
125 
126     @Test
127     public void testComponentException() throws Exception
128     {
129         tmpDir = createFolder(".mule/componentException");
130         final File file = createDataFile(tmpDir, "test1.txt");
131         prober.check(new Probe()
132         {
133             @Override
134             public boolean isSatisfied()
135             {
136                 // Component exception occurs after the SEDA queue for an
137                 // asynchronous request, so from the client's
138                 // perspective, the message has been delivered successfully.
139                 return !file.exists();
140             }
141 
142             @Override
143             public String describeFailure()
144             {
145                 return "File should be gone";
146             }
147         });
148     }
149 }