View Javadoc

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