1
2
3
4
5
6
7
8
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.Collections;
20 import java.util.Iterator;
21 import java.util.LinkedList;
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 = Collections.synchronizedCollection(new LinkedList());
30
31
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 checkResult();
89
90 long endTime = System.currentTimeMillis();
91
92 if (logger.isDebugEnabled())
93 {
94 logger.debug("Parallel transformations in " + getParallelThreadCount() + " threads with "
95 + getCallsPerThread() + " calls/thread took " + (endTime - startTime) + " ms.");
96 }
97 }
98
99 private synchronized void checkResult() throws Exception
100 {
101 this.wait();
102 Object expectedResult = resultData;
103 for (Iterator it = actualResults.iterator(); it.hasNext();)
104 {
105 Object result = it.next();
106 if (result instanceof Exception) throw (Exception)result;
107
108 if (expectedResult instanceof String && result instanceof String)
109 {
110 XMLAssert.assertXMLEqual((String)expectedResult, (String)result);
111 }
112 else
113 {
114 XMLAssert.assertEquals(expectedResult, result);
115 }
116 }
117 }
118
119 private int getParallelThreadCount()
120 {
121 return 10;
122 }
123
124 private int getCallsPerThread()
125 {
126 return 50;
127 }
128
129 }