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.components.script.refreshable;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  import org.mule.util.IOUtils;
13  
14  import java.io.FileWriter;
15  import java.io.IOException;
16  import java.net.URL;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  
21  public abstract class AbstractRefreshableBeanTestCase extends FunctionalTestCase
22  {
23  
24      protected static final int WAIT_TIME = 1000;
25      
26      protected void writeScript(String src, String path) throws IOException
27      {
28          FileWriter scriptFile = new FileWriter(path, false);
29          scriptFile.write(src);
30          scriptFile.flush();
31          scriptFile.close();
32      }
33  
34      protected String nameToPath(String name)
35      {
36          URL url = IOUtils.getResourceAsUrl(name, getClass());
37          String path = url.getFile();
38          logger.info(url + " -> " + path);
39          return path;
40      }
41  
42      // this is a bit of a messy hack.  if it fails check you don't have more than one copy
43      // of the files on your classpath
44      protected void runScriptTest(String script, String name, String endpoint, String payload, String result) throws Exception
45      {
46          // we overwrite the existing resource on the classpath...
47          writeScript(script, nameToPath(name));
48          Thread.sleep(WAIT_TIME); // wait for bean to refresh
49          MuleClient client = new MuleClient(muleContext);
50          MuleMessage m = client.send(endpoint, payload, null);
51          assertNotNull(m);
52          assertEquals(payload + result, m.getPayloadAsString());
53      }
54  
55  }
56  
57