1   /*
2    * Copyright 2004-2005 Alvaro Gonzalez de Paz (kortsoft)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package kortsoft.kmx.deployer;
17  
18  import java.io.File;
19  import java.io.FilenameFilter;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import kortsoft.kmx.AbstractTestCase;
26  import kortsoft.kmx.deployer.KmxDaemon;
27  
28  /***
29   * <a href="KmxDaemonTest.java.html"> <b><i>View Source </i> </b> </a>
30   * 
31   * @author Alvaro
32   * @version $Revision: 1.2 $
33   * 
34   */
35  public class KmxDaemonTest extends AbstractTestCase {
36      private KmxDaemon daemon;
37  
38      private File deployDirectory;
39  
40      public KmxDaemonTest() {
41          super("Test KmxDaemon");
42      }
43  
44      public void testCheckDirectoryChanges() throws Exception {
45          File notModifiedFile = new File(deployDirectory, "notModifiedFile");
46          notModifiedFile.createNewFile();
47          File modifiedFile = new File(deployDirectory, "modifiedFile");
48          modifiedFile.createNewFile();
49          File newFile = new File(deployDirectory, "newFile");
50          newFile.createNewFile();
51          File newDirectory = new File(deployDirectory, "newDirectory");
52          newDirectory.mkdir();
53          Map<File, Long> lastModifications = new HashMap<File, Long>();
54          lastModifications.put(notModifiedFile, new Long(notModifiedFile
55                  .lastModified() + 1));
56          lastModifications.put(modifiedFile, new Long(notModifiedFile
57                  .lastModified() - 1));
58          List<File> modifies = daemon.checkDirectoryChanges(deployDirectory,
59                  lastModifications);
60          assertLisContains("Modified file not detected", modifiedFile, modifies);
61          assertLisContains("New file not detected", newFile, modifies);
62          assertLisContains("Modified directory not detected", newDirectory,
63                  modifies);
64          assertLisNotContains("Not Modified file detected as changed",
65                  notModifiedFile, modifies);
66      }
67  
68      public void testLoadDefaultDeployers() throws Exception {
69          File jarFile = new File(deployDirectory, "testJar.jar");
70          jarFile.createNewFile();
71          File componentDirectory = new File(deployDirectory, "component");
72          componentDirectory.mkdir();
73          File simpleFile = new File(deployDirectory, "simple,file");
74          simpleFile.createNewFile();
75          Deployer actualDeployer = daemon.loadDeployer(jarFile);
76          assertNotNull("Jar Deployer was null", actualDeployer);
77          assertEquals("Deployer not suitable", JarDeployer.class, actualDeployer
78                  .getClass());
79          actualDeployer = daemon.loadDeployer(componentDirectory);
80          assertNotNull("Dir Deployer was null", actualDeployer);
81          assertEquals("Deployer not suitable", ComponentDirDeployer.class,
82                  actualDeployer.getClass());
83          actualDeployer = daemon.loadDeployer(simpleFile);
84          assertNotNull("Simple File Deployer was null", actualDeployer);
85          assertEquals("Deployer not suitable", SimpleFileDeployer.class,
86                  actualDeployer.getClass());
87      }
88  
89      public void testLoadExtraDeployers() throws Exception {
90          addExtDeployer();
91          File extraFile = new File(deployDirectory, "file.ext");
92          extraFile.createNewFile();
93          Deployer actualDeployer = daemon.loadDeployer(extraFile);
94          assertNotNull("Mock Deployer was null", actualDeployer);
95          assertEquals("Deployer not suitable", MockDeployer.class,
96                  actualDeployer.getClass());
97          assertEquals(Thread.currentThread().getContextClassLoader(),MockDeployer.parentClassLoader);
98      }
99  
100     // public void testInvokeDeployer() {
101     // daemon.addExtraDeployer(MockDeployer.class,);
102     // fail("Test not implemented");
103     // }
104 
105     public void testRunDaemon() throws Exception{
106         List<File> watchDirectories = new ArrayList<File>();
107         watchDirectories.add(deployDirectory);
108         daemon.setWatchDirectories(watchDirectories);
109         addExtDeployer();
110         File extraFile = new File(deployDirectory, "file.ext");
111         extraFile.createNewFile();
112         File jarFile = new File(deployDirectory, "jarFile.other");
113         jarFile.createNewFile();
114         daemon.runDaemon();
115         assertEquals("Incorrect deployer expected calls to deploy()",1,MockDeployer.numDeployCalled);
116         assertLisContains("Ext file not deployed: "+extraFile,extraFile,MockDeployer.files);
117         assertEquals("Deployed more files than expected",1,MockDeployer.files.size());
118         MockDeployer.files=new ArrayList<File>();
119         daemon.runDaemon();
120         assertEquals("Incorrect deployer expected calls to deploy(). File was not modified",1,MockDeployer.numDeployCalled);
121         extraFile.setLastModified(System.currentTimeMillis());
122         MockDeployer.files=new ArrayList<File>();
123         daemon.runDaemon();
124         assertEquals("Incorrect deployer expected calls to deploy(). File was modified",2,MockDeployer.numDeployCalled);
125         assertLisContains("Ext file not deployed: "+extraFile,extraFile,MockDeployer.files);
126         assertEquals("Deployed more files than expected",1,MockDeployer.files.size());
127         assertEquals(Thread.currentThread().getContextClassLoader(),MockDeployer.parentClassLoader);
128     }
129 
130     /***
131      * 
132      */
133     private void addExtDeployer() {
134         FilenameFilter filter = new FilenameFilter() {
135             public boolean accept(File dir, String name) {
136                 return name.matches(".*//.ext");
137             }
138         };
139         daemon.addExtraDeployer(MockDeployer.class, filter);
140     }
141 
142     public void setUp() {
143         daemon = new KmxDaemon();
144         daemon.setBaseClassLoader(Thread.currentThread().getContextClassLoader());
145         deployDirectory = new File(getTestDirectory(), "deploy");
146         deployDirectory.mkdir();
147     }
148 
149     public void tearDown() {
150         deleteDirectory(deployDirectory);
151         
152     }
153 
154     protected boolean deleteDirectory(File directory) {
155         String[] files = directory.list();
156         boolean allDeleted = true;
157         for (int i = 0; i < files.length; i++) {
158             String fileName = files[i];
159             File file = new File(directory, fileName);
160             boolean deleted = false;
161             if (file.isDirectory())
162                 deleted = deleteDirectory(file);
163             else
164                 deleted = file.delete();
165             allDeleted = allDeleted && deleted;
166         }
167         return allDeleted && directory.delete();
168     }
169 
170 }