View Javadoc

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