View Javadoc

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