View Javadoc

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