View Javadoc

1   /*
2    * $Id: CloseStreamOnMuleExceptionTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.test.integration.streaming;
12  
13  import org.mule.api.MuleException;
14  import org.mule.module.client.MuleClient;
15  import org.mule.module.xml.stax.DelegateXMLStreamReader;
16  import org.mule.module.xml.stax.StaxSource;
17  import org.mule.module.xml.util.XMLUtils;
18  import org.mule.tck.FunctionalTestCase;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.IOException;
22  
23  import javax.xml.stream.FactoryConfigurationError;
24  import javax.xml.stream.XMLInputFactory;
25  import javax.xml.stream.XMLStreamException;
26  import javax.xml.stream.XMLStreamReader;
27  import javax.xml.transform.Source;
28  import javax.xml.transform.sax.SAXSource;
29  import javax.xml.transform.stream.StreamSource;
30  
31  import org.xml.sax.InputSource;
32  
33  public class CloseStreamOnMuleExceptionTestCase extends FunctionalTestCase
34  {
35  
36      private String xmlText = "<test attribute=\"1\"/>";
37      private TestByteArrayInputStream inputStream;
38      MuleClient client;
39  
40      @Override
41      protected void doSetUp() throws Exception
42      {
43          super.doSetUp();
44          client = new MuleClient(muleContext);
45          inputStream = new TestByteArrayInputStream(xmlText.getBytes());
46      }
47  
48      public void testCloseStreamOnComponentException() throws MuleException, InterruptedException, IOException
49      {
50  
51          client.send("vm://inEcho?connector=vm", inputStream, null);
52          assertTrue(inputStream.isClosed());
53      }
54  
55      public void testCloseXMLInputSourceOnComponentException()
56          throws MuleException, InterruptedException, IOException
57      {
58          InputSource stream = new InputSource(inputStream);
59  
60          client.send("vm://inEcho?connector=vm", stream, null);
61  
62          assertTrue(((TestByteArrayInputStream) stream.getByteStream()).isClosed());
63      }
64  
65      public void testCloseXMLStreamSourceOnComponentException() throws FactoryConfigurationError, Exception
66      {
67          Source stream = XMLUtils.toXmlSource(XMLInputFactory.newInstance(), false, inputStream);
68  
69          client.send("vm://inEcho?connector=vm", stream, null);
70  
71          assertTrue(((TestByteArrayInputStream) ((StreamSource) stream).getInputStream()).isClosed());
72      }
73  
74      public void testCloseXMLStreamReaderOnComponentException()
75          throws MuleException, InterruptedException, IOException, XMLStreamException,
76          FactoryConfigurationError
77      {
78          TestXMLStreamReader stream = new TestXMLStreamReader(XMLInputFactory.newInstance()
79              .createXMLStreamReader(inputStream));
80  
81          client.send("vm://inEcho?connector=vm", stream, null);
82  
83          assertTrue(stream.isClosed());
84      }
85  
86      public void testCloseSaxSourceOnComponentException()
87          throws MuleException, InterruptedException, IOException, XMLStreamException,
88          FactoryConfigurationError
89      {
90          SAXSource stream = new SAXSource(new InputSource(inputStream));
91  
92          client.send("vm://inEcho?connector=vm", stream, null);
93  
94          assertTrue(((TestByteArrayInputStream) stream.getInputSource().getByteStream()).isClosed());
95      }
96  
97      public void testCloseStaxSourceOnComponentException()
98          throws MuleException, InterruptedException, IOException, XMLStreamException,
99          FactoryConfigurationError
100     {
101 
102         StaxSource stream = new StaxSource(new TestXMLStreamReader(XMLInputFactory.newInstance()
103             .createXMLStreamReader(inputStream)));
104 
105         client.send("vm://inEcho?connector=vm", stream, null);
106 
107         assertTrue(((TestXMLStreamReader) stream.getXMLStreamReader()).isClosed());
108     }
109 
110     //TODO fix
111 //    public void testCloseStreamOnDispatcherException()
112 //        throws MuleException, InterruptedException, IOException
113 //    {
114 //        client.send("vm://dispatcherExceptionBridge?connector=vm", inputStream, null);
115 //
116 //        assertTrue(inputStream.isClosed());
117 //    }
118 
119     // TODO MULE-3558 Streams are not closed if there are exceptions in the message
120     // receiver. Protocol/Transport workers should clean up after themselves if there
121     // is an error (MULE-3559) but exceptions thrown by AbstractMessageReciever will
122     // not result in stream being closed. These exceptions result in
123     // exceptionStrategy being called but because RequestContext is empty the message
124     // is not available in the AbstractExceptionListener and cannot be closed.
125 
126 //    public void testCloseStreamOnInboundFilterException()
127 //        throws MuleException, InterruptedException, IOException
128 //    {
129 //        client.dispatch("vm://inboundFilterExceptionBridge?connector=vm", inputStream, null);
130 //
131 //        Thread.sleep(200);
132 //
133 //        assertTrue(((TestByteArrayInputStream) inputStream).isClosed());
134 //    }
135 
136 
137     @Override
138     protected String getConfigResources()
139     {
140         return "org/mule/test/integration/streaming/close-stream-on-mule-exception-test.xml";
141     }
142 
143     static class TestByteArrayInputStream extends ByteArrayInputStream
144     {
145         private boolean closed;
146 
147         public boolean isClosed()
148         {
149             return closed;
150         }
151 
152         public TestByteArrayInputStream(byte[] arg0)
153         {
154             super(arg0);
155         }
156 
157         public TestByteArrayInputStream(byte[] buf, int offset, int length)
158         {
159             super(buf, offset, length);
160         }
161 
162         @Override
163         public void close() throws IOException
164         {
165             super.close();
166             closed = true;
167         }
168     }
169 
170     static class TestXMLStreamReader extends DelegateXMLStreamReader
171     {
172         private boolean closed;
173 
174         public boolean isClosed()
175         {
176             return closed;
177         }
178 
179         public TestXMLStreamReader(XMLStreamReader reader)
180         {
181             super(reader);
182         }
183 
184         @Override
185         public void close() throws XMLStreamException
186         {
187             super.close();
188             closed = true;
189         }
190     }
191 
192 }