View Javadoc

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