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.transformers.xml;
8   
9   import org.mule.api.transformer.Transformer;
10  import org.mule.api.transformer.TransformerException;
11  import org.mule.module.xml.transformer.XsltTransformer;
12  import org.mule.tck.junit4.AbstractMuleContextTestCase;
13  import org.mule.transformer.types.DataTypeFactory;
14  import org.mule.util.IOUtils;
15  
16  import java.util.Collection;
17  import java.util.Iterator;
18  
19  import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentLinkedQueue;
20  import org.custommonkey.xmlunit.XMLAssert;
21  import org.junit.Test;
22  
23  public class ParallelXsltTransformerTestCase extends AbstractMuleContextTestCase
24  {
25      private String srcData;
26      private String resultData;
27      private Collection actualResults = new ConcurrentLinkedQueue();
28  
29      @Override
30      protected void doSetUp() throws Exception
31      {
32          srcData = IOUtils.toString(IOUtils.getResourceAsStream("cdcatalog-utf-8.xml", getClass()), "UTF-8");
33          resultData = IOUtils.toString(IOUtils.getResourceAsStream("cdcatalog-utf-8.html", getClass()),
34              "UTF-8");
35      }
36  
37      public Transformer getTransformer() throws Exception
38      {
39          XsltTransformer transformer = new XsltTransformer();
40          transformer.setReturnDataType(DataTypeFactory.STRING);
41          transformer.setXslFile("cdcatalog.xsl");
42          transformer.setMuleContext(muleContext);
43          transformer.initialise();
44          return transformer;
45      }
46  
47      int running = 0;
48  
49      public synchronized void signalStarted()
50      {
51          ++running;
52      }
53  
54      public synchronized void signalDone()
55      {
56          if (--running == 0) this.notify();
57      }
58  
59      @Test
60      public void testParallelTransformation() throws Exception
61      {
62          final Transformer transformer = getTransformer();
63  
64          long startTime = System.currentTimeMillis();
65  
66          for (int i = 0; i < getParallelThreadCount(); ++i)
67          {
68              new Thread(new Runnable()
69              {
70                  public void run()
71                  {
72                      signalStarted();
73                      for (int j = 0; j < getCallsPerThread(); ++j)
74                      {
75                          try
76                          {
77                              actualResults.add(transformer.transform(srcData));
78                          }
79                          catch (TransformerException e)
80                          {
81                              actualResults.add(e);
82                          }
83                      }
84                      signalDone();
85                  }
86              }).start();
87          }
88  
89          synchronized (this)
90          {
91              this.wait();
92          }
93  
94          long endTime = System.currentTimeMillis();
95  
96          checkResult();
97  
98          if (logger.isDebugEnabled())
99          {
100             logger.debug("Parallel transformations in " + getParallelThreadCount() + " threads with "
101                          + getCallsPerThread() + " calls/thread took " + (endTime - startTime) + " ms.");
102         }
103     }
104 
105     protected void checkResult() throws Exception
106     {
107         Object expectedResult = resultData;
108 
109         for (Iterator it = actualResults.iterator(); it.hasNext();)
110         {
111             Object result = it.next();
112             if (result instanceof Exception) throw (Exception) result;
113 
114             if (expectedResult instanceof String && result instanceof String)
115             {
116                 XMLAssert.assertXMLEqual((String) expectedResult, (String) result);
117             }
118             else
119             {
120                 XMLAssert.assertEquals(expectedResult, result);
121             }
122         }
123     }
124 
125     private int getParallelThreadCount()
126     {
127         return 20;
128     }
129 
130     private int getCallsPerThread()
131     {
132         return 100;
133     }
134 
135 }