View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.tcp.protocols;
8   
9   import org.mule.tck.junit4.AbstractMuleTestCase;
10  
11  import java.io.ByteArrayInputStream;
12  import java.io.IOException;
13  import java.io.InputStream;
14  
15  import org.junit.Before;
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertNull;
21  
22  /**
23   * Test by reading characters from a fixed StringBuffer instead of a TCP port.
24   */
25  public class XmlMessageProtocolTestCase extends AbstractMuleTestCase
26  {
27      private XmlMessageProtocol xmp;
28  
29      protected void setProtocol(XmlMessageProtocol xmp)
30      {
31          this.xmp = xmp;
32      }
33  
34      protected byte[] read(InputStream is) throws IOException
35      {
36          return(byte[]) xmp.read(is);
37      }
38  
39      @Before
40      public void doSetUp()
41      {
42          setProtocol(new XmlMessageProtocol());
43      }
44  
45      protected void doTearDown() throws Exception
46      {
47          xmp = null;
48      }
49  
50      @Test
51      public void testSingleMessage() throws Exception
52      {
53          String msgData = "<?xml version=\"1.0\"?><data>hello</data>";
54  
55          ByteArrayInputStream bais = new ByteArrayInputStream(msgData.getBytes());
56  
57          byte[] result = read(bais);
58          assertNotNull(result);
59          assertEquals(msgData, new String(result));
60  
61          assertNull(read(bais));
62      }
63  
64      @Test
65      public void testTwoMessages() throws Exception
66      {
67          String[] msgData = {"<?xml version=\"1.0\"?><data>hello</data>",
68              "<?xml version=\"1.0\"?><data>goodbye</data>"};
69  
70          ByteArrayInputStream bais = new ByteArrayInputStream((msgData[0] + msgData[1]).getBytes());
71  
72          byte[] result = read(bais);
73          assertNotNull(result);
74          assertEquals(msgData[0], new String(result));
75  
76          result = read(bais);
77          assertNotNull(result);
78          assertEquals(msgData[1], new String(result));
79  
80          assertNull(read(bais));
81      }
82  
83      @Test
84      public void testMultipleMessages() throws Exception
85      {
86          String[] msgData = {"<?xml version=\"1.0\"?><data>1</data>",
87              "<?xml version=\"1.0\"?><data>22</data>", "<?xml version=\"1.0\"?><data>333</data>",
88              "<?xml version=\"1.0\"?><data>4444</data>",
89              "<?xml version=\"1.0\"?><data>55555</data>",
90              "<?xml version=\"1.0\"?><data>666666</data>",
91              "<?xml version=\"1.0\"?><data>7777777</data>",
92              "<?xml version=\"1.0\"?><data>88888888</data>",
93              "<?xml version=\"1.0\"?><data>999999999</data>",
94              "<?xml version=\"1.0\"?><data>aaaaaaaaaa</data>",
95              "<?xml version=\"1.0\"?><data>bbbbbbbbbbb</data>",
96              "<?xml version=\"1.0\"?><data>cccccccccccc</data>",
97              "<?xml version=\"1.0\"?><data>ddddddddddddd</data>",
98              "<?xml version=\"1.0\"?><data>eeeeeeeeeeeeee</data>",
99              "<?xml version=\"1.0\"?><data>fffffffffffffff</data>"};
100 
101         StringBuffer allMsgData = new StringBuffer();
102 
103         for (int i = 0; i < msgData.length; i++)
104         {
105             allMsgData.append(msgData[i]);
106         }
107 
108         ByteArrayInputStream bais = new ByteArrayInputStream(allMsgData.toString().getBytes());
109 
110         byte[] result;
111 
112         for (int i = 0; i < msgData.length; i++)
113         {
114             result = read(bais);
115             assertNotNull(result);
116             assertEquals(msgData[i], new String(result));
117         }
118 
119         assertNull(read(bais));
120     }
121 
122     @Test
123     public void testSlowStream() throws Exception
124     {
125         String msgData = "<?xml version=\"1.0\"?><data>hello</data>";
126 
127         SlowInputStream bais = new SlowInputStream(msgData.getBytes());
128 
129         byte[] result = read(bais);
130         assertNotNull(result);
131         // only get the first character!  use XmlMessageEOFProtocol instead
132         assertEquals(msgData.substring(0, 1), new String(result));
133     }
134 
135 }