View Javadoc

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