1   /*
2    * $Id: MulticastRouterMule2136TestCase.java 10969 2008-02-22 21:57:26Z holger $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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 = 10000;
27      public static final String SERIALIZED = "<org.mule.issues.MulticastRouterMule2136TestCase_-Parent>\n" +
28                      "  <child/>\n" +
29                      "</org.mule.issues.MulticastRouterMule2136TestCase_-Parent>";
30  
31      protected String getConfigResources()
32      {
33          return "org/mule/issues/multicast-router-mule-2136-test.xml";
34      }
35  
36      protected MuleClient sendObject() throws MuleException
37      {
38          MuleClient client = new MuleClient();
39          client.dispatch("object-in", new Parent(new Child()), null);
40          return client;
41      }
42  
43      public void testObjectOut() throws MuleException, InterruptedException
44      {
45          request(sendObject(), "object-out", Parent.class);
46          // wait a while, otherwise we pull down everything while it is still running
47          Thread.sleep(3000);
48      }
49  
50      public void testObjectXmlOut() throws MuleException
51      {
52          String xml = (String) request(sendObject(), "object-xml-out", String.class);
53          assertEquals(SERIALIZED, xml);
54      }
55  
56      public void testXmlObjectOut() throws MuleException
57      {
58          request(sendObject(), "xml-object-out", Parent.class);
59      }
60  
61      public void testStress() throws MuleException
62      {
63          int tenth = TEST_COUNT / 10;
64          for (int i = 0; i < TEST_COUNT; i++)
65          {
66              testObjectXmlOut();
67              
68              // Pull result from "xml-object-out" endpoint as queuing is enabled and otherwise we get
69              // OutOfMemoryExceptions during stress tests when these results build up in queue.
70              request(new MuleClient(), "xml-object-out", Parent.class);
71              
72              if (i % tenth == 0)
73              {
74                  logger.info("Iteration " + i);
75              }
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          assertTrue(message.getPayload().getClass().getName(), clazz.isAssignableFrom(message.getPayload().getClass()));
86          return message.getPayload();
87      }
88  
89  
90      public static class Parent
91      {
92          private Child child;
93  
94          public Parent()
95          {
96              this(null);
97          }
98  
99          public Parent(Child child)
100         {
101             setChild(child);
102         }
103 
104         public Child getChild()
105         {
106             return child;
107         }
108 
109         public void setChild(Child child)
110         {
111             this.child = child;
112         }
113     }
114 
115     public static class Child
116     {
117         // nothing here
118     }
119 
120 }