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.issues;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.client.MuleClient;
12  import org.mule.module.xml.functional.AbstractXmlFunctionalTestCase;
13  
14  import org.junit.Test;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertTrue;
19  
20  
21  /**
22   * This is a simplified version of {@link org.mule.module.xml.functional.XmlTransformerFunctionalTestCase}
23   * The {@link #testObjectXmlOut()} method hangs intermittently.
24   */
25  public class MulticastRouterMule2136TestCase extends AbstractXmlFunctionalTestCase
26  {
27  
28      public static final int TEST_COUNT = 2000; // cut down from 10k messages, since it seems a little much for the continuous build
29      public static final String SERIALIZED = "<org.mule.issues.MulticastRouterMule2136TestCase_-Parent>\n" +
30                      "  <child/>\n" +
31                      "</org.mule.issues.MulticastRouterMule2136TestCase_-Parent>";
32  
33      @Override
34      protected String getConfigResources()
35      {
36          return "org/mule/issues/multicast-router-mule-2136-test.xml";
37      }
38  
39      protected MuleClient sendObject() throws MuleException
40      {
41          MuleClient client = new MuleClient(muleContext);
42          client.dispatch("object-in", new Parent(new Child()), null);
43          return client;
44      }
45  
46      @Test
47      public void testObjectOut() throws MuleException, InterruptedException
48      {
49          request(sendObject(), "object-out", Parent.class);
50          // wait a while, otherwise we pull down everything while it is still running
51          Thread.sleep(3000);
52      }
53  
54      @Test
55      public void testObjectXmlOut() throws MuleException
56      {
57          String xml = (String) request(sendObject(), "object-xml-out", String.class);
58          assertEquals(SERIALIZED, xml);
59      }
60  
61      @Test
62      public void testXmlObjectOut() throws MuleException
63      {
64          request(sendObject(), "xml-object-out", Parent.class);
65      }
66  
67      @Test
68      public void testStress() throws MuleException
69      {
70          int tenth = TEST_COUNT / 10;
71          for (int i = 0; i < TEST_COUNT; i++)
72          {
73              testObjectXmlOut();
74              
75              // Pull result from "xml-object-out" endpoint as queuing is enabled and otherwise we get
76              // OutOfMemoryExceptions during stress tests when these results build up in queue.
77              request(new MuleClient(muleContext), "xml-object-out", Parent.class);
78              
79              if (i % tenth == 0)
80              {
81                  logger.info("Iteration " + i);
82              }
83          }
84      }
85  
86      protected Object request(MuleClient client, String endpoint, Class<?> clazz) throws MuleException
87      {
88          MuleMessage message = client.request(endpoint, TIMEOUT);
89          assertNotNull(message);
90          assertNotNull(message.getPayload());
91          
92          Class<?> payloadClass = message.getPayload().getClass();
93          String assertionMessage = String.format("expected payload of type %1s but was %2s", clazz.getName(), payloadClass);
94          assertTrue(assertionMessage, clazz.isAssignableFrom(payloadClass));
95          return message.getPayload();
96      }
97  
98      public static class Parent
99      {
100         private Child child;
101 
102         public Parent()
103         {
104             this(null);
105         }
106 
107         public Parent(Child child)
108         {
109             setChild(child);
110         }
111 
112         public Child getChild()
113         {
114             return child;
115         }
116 
117         public void setChild(Child child)
118         {
119             this.child = child;
120         }
121     }
122 
123     public static class Child
124     {
125         // nothing here
126     }
127 
128 }