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