1 package com.atlassian.plugin.test;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URI;
6 import java.net.URISyntaxException;
7 import java.util.Random;
8
9 import org.apache.commons.io.FileUtils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
14 public class PluginTestUtils
15 {
16 public static final String PROJECT_VERSION;
17 public static final String SIMPLE_TEST_JAR;
18 public static final String INNER1_TEST_JAR;
19 public static final String INNER2_TEST_JAR;
20 public static final String FILTER_TEST_JAR;
21
22 static
23 {
24 PROJECT_VERSION = System.getProperty("project.version");
25 SIMPLE_TEST_JAR = "atlassian-plugins-simpletest-" + PROJECT_VERSION + ".jar";
26 INNER1_TEST_JAR = "atlassian-plugins-innerjarone-" + PROJECT_VERSION + ".jar";
27 INNER2_TEST_JAR = "atlassian-plugins-innerjartwo-" + PROJECT_VERSION + ".jar";
28 FILTER_TEST_JAR = "atlassian-plugins-filtertest-" + PROJECT_VERSION + ".jar";
29 }
30
31 public static File getFileForResource(final String resourceName) throws URISyntaxException
32 {
33 return new File(new URI(PluginTestUtils.class.getClassLoader().getResource(resourceName).toString()));
34 }
35
36 public static File createTempDirectory(Class<?> source) throws IOException
37 {
38 return createTempDirectory(source.getName());
39 }
40
41 private static Random random = new Random();
42
43
44 public static File createTempDirectory(String name) throws IOException
45 {
46 File tmpBase = new File("target", "tmp");
47
48 File tmpDir = new File(tmpBase, name);
49
50 if (tmpDir.exists())
51 {
52 try
53 {
54 FileUtils.cleanDirectory(tmpDir);
55 }
56 catch (IOException ioe)
57 {
58 String unique = Long.toString(random.nextLong() >>> 1, 26);
59 File movedAsideName = new File(tmpBase, name + "-" + unique);
60 if (tmpDir.renameTo(movedAsideName))
61 {
62 tmpDir.mkdirs();
63 }
64 else
65 {
66 throw new IOException("Unable to clean up temporary directory: " + tmpDir);
67 }
68 }
69 }
70 else
71 {
72 tmpDir.mkdirs();
73 }
74
75 assertTrue(tmpDir.exists());
76 assertTrue(tmpDir.isDirectory());
77 assertEquals(0, tmpDir.list().length);
78
79 return tmpDir.getAbsoluteFile();
80 }
81 }