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