1
2
3
4
5
6
7 package org.mule.test.integration.streaming;
8
9 import org.mule.api.MuleException;
10 import org.mule.module.client.MuleClient;
11 import org.mule.module.xml.stax.DelegateXMLStreamReader;
12 import org.mule.module.xml.stax.StaxSource;
13 import org.mule.module.xml.util.XMLUtils;
14 import org.mule.tck.junit4.FunctionalTestCase;
15
16 import java.io.ByteArrayInputStream;
17 import java.io.IOException;
18
19 import javax.xml.stream.FactoryConfigurationError;
20 import javax.xml.stream.XMLInputFactory;
21 import javax.xml.stream.XMLStreamException;
22 import javax.xml.stream.XMLStreamReader;
23 import javax.xml.transform.Source;
24 import javax.xml.transform.sax.SAXSource;
25 import javax.xml.transform.stream.StreamSource;
26
27 import org.junit.Test;
28 import org.xml.sax.InputSource;
29
30 import static org.junit.Assert.assertTrue;
31
32 public class CloseStreamOnMuleExceptionTestCase extends FunctionalTestCase
33 {
34
35 private String xmlText = "<test attribute=\"1\"/>";
36 private TestByteArrayInputStream inputStream;
37 private MuleClient client;
38
39 @Override
40 protected void doSetUp() throws Exception
41 {
42 super.doSetUp();
43 client = new MuleClient(muleContext);
44 inputStream = new TestByteArrayInputStream(xmlText.getBytes());
45 }
46
47 @Test
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 @Test
56 public void testCloseXMLInputSourceOnComponentException()
57 throws MuleException, InterruptedException, IOException
58 {
59 InputSource stream = new InputSource(inputStream);
60
61 client.send("vm://inEcho?connector=vm", stream, null);
62
63 assertTrue(((TestByteArrayInputStream) stream.getByteStream()).isClosed());
64 }
65
66 @Test
67 public void testCloseXMLStreamSourceOnComponentException() throws FactoryConfigurationError, Exception
68 {
69 Source stream = XMLUtils.toXmlSource(XMLInputFactory.newInstance(), false, inputStream);
70
71 client.send("vm://inEcho?connector=vm", stream, null);
72
73 assertTrue(((TestByteArrayInputStream) ((StreamSource) stream).getInputStream()).isClosed());
74 }
75
76 @Test
77 public void testCloseXMLStreamReaderOnComponentException()
78 throws MuleException, InterruptedException, IOException, XMLStreamException,
79 FactoryConfigurationError
80 {
81 TestXMLStreamReader stream = new TestXMLStreamReader(XMLInputFactory.newInstance()
82 .createXMLStreamReader(inputStream));
83
84 client.send("vm://inEcho?connector=vm", stream, null);
85
86 assertTrue(stream.isClosed());
87 }
88
89 @Test
90 public void testCloseSaxSourceOnComponentException()
91 throws MuleException, InterruptedException, IOException, XMLStreamException,
92 FactoryConfigurationError
93 {
94 SAXSource stream = new SAXSource(new InputSource(inputStream));
95
96 client.send("vm://inEcho?connector=vm", stream, null);
97
98 assertTrue(((TestByteArrayInputStream) stream.getInputSource().getByteStream()).isClosed());
99 }
100
101 @Test
102 public void testCloseStaxSourceOnComponentException()
103 throws MuleException, InterruptedException, IOException, XMLStreamException,
104 FactoryConfigurationError
105 {
106
107 StaxSource stream = new StaxSource(new TestXMLStreamReader(XMLInputFactory.newInstance()
108 .createXMLStreamReader(inputStream)));
109
110 client.send("vm://inEcho?connector=vm", stream, null);
111
112 assertTrue(((TestXMLStreamReader) stream.getXMLStreamReader()).isClosed());
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 @Override
145 protected String getConfigResources()
146 {
147 return "org/mule/test/integration/streaming/close-stream-on-mule-exception-test.xml";
148 }
149
150 static class TestByteArrayInputStream extends ByteArrayInputStream
151 {
152 private boolean closed;
153
154 public boolean isClosed()
155 {
156 return closed;
157 }
158
159 public TestByteArrayInputStream(byte[] arg0)
160 {
161 super(arg0);
162 }
163
164 public TestByteArrayInputStream(byte[] buf, int offset, int length)
165 {
166 super(buf, offset, length);
167 }
168
169 @Override
170 public void close() throws IOException
171 {
172 super.close();
173 closed = true;
174 }
175 }
176
177 static class TestXMLStreamReader extends DelegateXMLStreamReader
178 {
179 private boolean closed;
180
181 public boolean isClosed()
182 {
183 return closed;
184 }
185
186 public TestXMLStreamReader(XMLStreamReader reader)
187 {
188 super(reader);
189 }
190
191 @Override
192 public void close() throws XMLStreamException
193 {
194 super.close();
195 closed = true;
196 }
197 }
198
199 }