1
2
3
4
5
6
7
8
9
10
11 package org.mule.module.launcher;
12
13 import org.mule.api.config.MuleProperties;
14 import org.mule.tck.AbstractMuleTestCase;
15 import org.mule.util.FileUtils;
16 import org.mule.util.StringUtils;
17 import org.mule.util.concurrent.Latch;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.URL;
22
23 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
24
25 import org.apache.commons.io.filefilter.DirectoryFileFilter;
26 import org.apache.commons.io.filefilter.SuffixFileFilter;
27
28 import static org.junit.Assert.assertArrayEquals;
29
30
31
32
33 public class DeploymentServiceTestCase extends AbstractMuleTestCase
34 {
35
36 protected static final int LATCH_TIMEOUT = 10000;
37 protected static final String[] NONE = new String[0];
38
39 protected File muleHome;
40 protected File appsDir;
41 protected DeploymentService deploymentService;
42
43 protected volatile Latch deployLatch;
44 protected volatile Latch undeployLatch;
45
46 @Override
47 protected void doSetUp() throws Exception
48 {
49 super.doSetUp();
50
51 final String tmpDir = System.getProperty("java.io.tmpdir");
52 muleHome = new File(tmpDir, getClass().getSimpleName() + System.currentTimeMillis());
53 appsDir = new File(muleHome, "apps");
54 appsDir.mkdirs();
55 System.setProperty(MuleProperties.MULE_HOME_DIRECTORY_PROPERTY, muleHome.getCanonicalPath());
56
57 new File(muleHome, "lib/shared/default").mkdirs();
58
59 deploymentService = new DeploymentService();
60 deploymentService.setDeployer(new TestDeployer());
61 deployLatch = new Latch();
62 undeployLatch = new Latch();
63 }
64
65 @Override
66 protected void doTearDown() throws Exception
67 {
68
69 FileUtils.deleteTree(muleHome);
70 if (deploymentService != null)
71 {
72 deploymentService.stop();
73 }
74 super.doTearDown();
75 }
76
77 public void testDeployZipOnStartup() throws Exception
78 {
79 final URL url = getClass().getResource("/dummy-app.zip");
80 assertNotNull("Test app file not found", url);
81 addAppArchive(url);
82
83 deploymentService.start();
84
85 assertTrue("Deployer never invoked", deployLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS));
86
87 assertAppsDir(NONE, new String[] {"dummy-app"});
88 }
89
90 public void testUpdateAppViaZip() throws Exception
91 {
92 final URL url = getClass().getResource("/dummy-app.zip");
93 assertNotNull("Test app file not found", url);
94 addAppArchive(url);
95
96 deploymentService.start();
97
98 assertTrue("Deployer never invoked", deployLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS));
99 assertAppsDir(NONE, new String[] {"dummy-app"});
100
101 deployLatch = new Latch();
102 addAppArchive(url);
103 assertTrue("Undeploy never invoked", undeployLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS));
104 assertTrue("Deployer never invoked", deployLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS));
105 assertAppsDir(NONE, new String[] {"dummy-app"});
106 }
107
108 private void assertAppsDir(String[] expectedZips, String[] expectedApps)
109 {
110 final String[] actualZips = appsDir.list(new SuffixFileFilter(".zip"));
111 assertArrayEquals("Invalid Mule application archives set", expectedZips, actualZips);
112 final String[] actualApps = appsDir.list(DirectoryFileFilter.DIRECTORY);
113 assertArrayEquals("Invalid Mule exploded applications set", expectedApps, actualApps);
114 }
115
116 private void addAppArchive(URL url) throws IOException
117 {
118
119 final String tempFileName = new File(url.getFile() + ".part").getName();
120 final File tempFile = new File(appsDir, tempFileName);
121 FileUtils.copyURLToFile(url, tempFile);
122 tempFile.renameTo(new File(StringUtils.removeEnd(tempFile.getAbsolutePath(), ".part")));
123 }
124
125
126 private class TestDeployer implements MuleDeployer
127 {
128 MuleDeployer delegate = new DefaultMuleDeployer();
129
130 public void deploy(Application app)
131 {
132 System.out.println("DeploymentServiceTestCase$TestDeployer.deploy");
133 delegate.deploy(app);
134 deployLatch.release();
135 }
136
137 public void undeploy(Application app)
138 {
139 System.out.println("DeploymentServiceTestCase$TestDeployer.undeploy");
140 delegate.undeploy(app);
141 undeployLatch.release();
142 }
143
144 public Application installFromAppDir(String packedMuleAppFileName) throws IOException
145 {
146 System.out.println("DeploymentServiceTestCase$TestDeployer.installFromAppDir");
147 return delegate.installFromAppDir(packedMuleAppFileName);
148 }
149
150 public Application installFrom(URL url) throws IOException
151 {
152 System.out.println("DeploymentServiceTestCase$TestDeployer.installFrom");
153 return delegate.installFrom(url);
154 }
155
156 }
157 }