View Javadoc

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