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.module.sxc;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  
14  import org.apache.commons.io.IOUtils;
15  import org.junit.Test;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.fail;
19  
20  public class SxcFilterTestCase extends FunctionalTestCase
21  {
22      int finished = 0;
23  
24      @Test
25      public void testBasicXPath() throws Exception
26      {
27          final MuleClient client = new MuleClient(muleContext);
28  
29          final String testData = IOUtils.toString(getClass().getResourceAsStream("/purchase-order.xml"));
30  
31          MuleMessage res = client.send("vm://in", testData, null);
32          assertEquals(Boolean.TRUE, res.getPayload());
33      }
34  
35      @Test
36      public void testAndFilter() throws Exception
37      {
38          final MuleClient client = new MuleClient(muleContext);
39  
40          final String testData = IOUtils.toString(getClass().getResourceAsStream("/purchase-order.xml"));
41  
42          MuleMessage res = client.send("vm://and-filter", testData, null);
43  
44          assertEquals(Boolean.TRUE, res.getPayload());
45      }
46  
47      @Test
48      public void testOrFilter() throws Exception
49      {
50          final MuleClient client = new MuleClient(muleContext);
51  
52          final String testData = IOUtils.toString(getClass().getResourceAsStream("/purchase-order.xml"));
53  
54          MuleMessage res = client.send("vm://or-filter", testData, null);
55  
56          assertEquals(Boolean.TRUE, res.getPayload());
57      }
58  
59      @Test
60      public void testNotFilter() throws Exception
61      {
62          final MuleClient client = new MuleClient(muleContext);
63  
64          final String testData = IOUtils.toString(getClass().getResourceAsStream("/purchase-order.xml"));
65  
66          MuleMessage res = client.send("vm://not-filter", testData, null);
67  
68          assertEquals(Boolean.TRUE, res.getPayload());
69      }
70  
71      public void xtestBenchmark() throws Exception
72      {
73          final MuleClient client = new MuleClient(muleContext);
74  
75          final String testData = IOUtils.toString(getClass().getResourceAsStream("/purchase-order.xml"));
76  
77          System.out.println("Warmup");
78          fire(client, testData, 1500);
79  
80          System.out.println("Running....");
81  
82          fire(client, testData, 1000);
83  
84          Thread.sleep(1000);
85      }
86  
87      private void fire(final MuleClient client, final String testData, final int count)
88          throws InterruptedException
89      {
90          long time = System.currentTimeMillis();
91          finished = 0;
92          for (int i = 0; i < 10; i++)
93          {
94              new Thread(new Runnable()
95              {
96                  public void run()
97                  {
98                      for (int j = 0; j < count; j++)
99                      {
100                         try
101                         {
102                             client.send("vm://in", testData, null);
103                         }
104                         catch (MuleException e)
105                         {
106                             fail("Exception in worker thread");
107                         }
108                     }
109                     finished++;
110                 }
111             }).start();
112         }
113 
114         while (finished < 10)
115         {
116             Thread.sleep(100);
117         }
118         System.out.println("elapsed " + (System.currentTimeMillis() - time));
119 
120     }
121 
122     @Override
123     protected String getConfigResources()
124     {
125         return "xpath-filter-conf.xml";
126     }
127 
128 }