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