View Javadoc

1   package com.atlassian.plugin.loaders.classloading;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.net.URISyntaxException;
6   import java.net.URL;
7   
8   import com.atlassian.plugin.test.PluginTestUtils;
9   import com.atlassian.plugin.util.ClassLoaderUtils;
10  
11  import org.apache.commons.io.FileUtils;
12  
13  public class DirectoryPluginLoaderUtils
14  {
15      private static final String TEST_PLUGIN_DIRECTORY = "ap-plugins";
16  
17      /**
18       * Copies the test plugins to a new temporary directory and returns that directory.
19       */
20      public static File copyTestPluginsToTempDirectory() throws IOException
21      {
22          File directory = PluginTestUtils.createTempDirectory(DirectoryPluginLoaderUtils.class);
23          FileUtils.copyDirectory(getTestPluginsDirectory(), directory);
24  
25          // Clean up version control files in case we copied them by mistake.
26          FileUtils.deleteDirectory(new File(directory, "CVS"));
27          FileUtils.deleteDirectory(new File(directory, ".svn"));
28  
29          return directory;
30      }
31  
32      /**
33       * Returns the directory on the classpath where the test plugins live.
34       */
35      public static File getTestPluginsDirectory()
36      {
37          URL url = ClassLoaderUtils.getResource(TEST_PLUGIN_DIRECTORY, DirectoryPluginLoaderUtils.class);
38          try
39          {
40              return new File(url.toURI());
41          }
42          catch (URISyntaxException e)
43          {
44              // Shouldn't happen, but fallback to getFile if the above fails. getFile breaks for paths with spaces
45              // and other special characters because they get URL encoded
46              return new File(url.getFile());
47          }
48      }
49  }