1   /*
2    * $Id: CloseStreanOnMuleExceptionTestCase.java 12372 2008-07-17 13:23:15Z dfeist $
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.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 CloseStreanOnMuleExceptionTestCase 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();
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     public void testCloseStreamOnDispatcherException()
111         throws MuleException, InterruptedException, IOException
112     {
113         client.send("vm://dispatcherExceptionBridge?connector=vm", inputStream, null);
114 
115         assertTrue(((TestByteArrayInputStream) inputStream).isClosed());
116     }
117 
118     // TODO MULE-3558 Streams are not closed if there are exceptions in the message
119     // receiver. Protocol/Transport workers should clean up after themselves if there
120     // is an error (MULE-3559) but exceptions thrown by AbstractMessageReciever will
121     // not result in stream being closed. These exceptions result in
122     // exceptionStrategy being called but because RequestContext is empty the message
123     // is not available in the AbstractExceptionListener and cannot be closed.
124 
125 //    public void testCloseStreamOnInboundFilterException()
126 //        throws MuleException, InterruptedException, IOException
127 //    {
128 //        client.dispatch("vm://inboundFilterExceptionBridge?connector=vm", inputStream, null);
129 //
130 //        Thread.sleep(200);
131 //
132 //        assertTrue(((TestByteArrayInputStream) inputStream).isClosed());
133 //    }
134 
135 
136     // @Override
137     protected String getConfigResources()
138     {
139         return "org/mule/test/integration/streaming/close-stream-on-mule-exception-test.xml";
140     }
141 
142     static class TestByteArrayInputStream extends ByteArrayInputStream
143     {
144         private boolean closed;
145 
146         public boolean isClosed()
147         {
148             return closed;
149         }
150 
151         public TestByteArrayInputStream(byte[] arg0)
152         {
153             super(arg0);
154         }
155 
156         public TestByteArrayInputStream(byte[] buf, int offset, int length)
157         {
158             super(buf, offset, length);
159         }
160 
161         // @Override
162         public void close() throws IOException
163         {
164             super.close();
165             closed = true;
166         }
167     }
168 
169     static class TestXMLStreamReader extends DelegateXMLStreamReader
170     {
171         private boolean closed;
172 
173         public boolean isClosed()
174         {
175             return closed;
176         }
177 
178         public TestXMLStreamReader(XMLStreamReader reader)
179         {
180             super(reader);
181         }
182 
183         // @Override
184         public void close() throws XMLStreamException
185         {
186             super.close();
187             closed = true;
188         }
189     }
190 
191 }