View Javadoc

1   package com.atlassian.plugin;
2   
3   import junit.framework.TestCase;
4   
5   import java.io.File;
6   import java.io.IOException;
7   
8   import org.apache.commons.io.FileUtils;
9   import com.atlassian.plugin.test.PluginJarBuilder;
10  
11  public class TestDefaultPluginArtifactFactory extends TestCase
12  {
13      File testDir;
14      File spaceTestDir;
15  
16      public void setUp() throws IOException
17      {
18          testDir = makeTempDir("test");
19          spaceTestDir = makeTempDir("space test");
20      }
21  
22      private File makeTempDir(String prefix)
23              throws IOException
24      {
25          File tempDir = File.createTempFile(prefix, "");
26          tempDir.delete();
27          tempDir.mkdir();
28  
29          return tempDir;
30      }
31  
32      public void tearDown() throws IOException
33      {
34          FileUtils.deleteDirectory(testDir);
35          FileUtils.deleteDirectory(spaceTestDir);
36  
37          testDir = null;
38          spaceTestDir = null;
39      }
40  
41      public void testCreate() throws IOException
42      {
43          doCreationTestInDirectory(testDir);
44      }
45  
46      public void testCreateWithSpaceInArtifactPath() throws IOException
47      {
48          doCreationTestInDirectory(spaceTestDir);
49      }
50  
51      private void doCreationTestInDirectory(File directory) throws IOException
52      {
53          File xmlFile = new File(directory, "foo.xml");
54          FileUtils.writeStringToFile(xmlFile, "<xml/>");
55          File jarFile = new PluginJarBuilder("jar").build(directory);
56  
57          DefaultPluginArtifactFactory factory = new DefaultPluginArtifactFactory();
58  
59          PluginArtifact jarArt = factory.create(jarFile.toURI());
60          assertNotNull(jarArt);
61          assertTrue(jarArt instanceof JarPluginArtifact);
62  
63          PluginArtifact xmlArt = factory.create(xmlFile.toURI());
64          assertNotNull(xmlArt);
65          assertTrue(xmlArt instanceof XmlPluginArtifact);
66  
67          try
68          {
69              factory.create(new File(testDir, "bob.jim").toURI());
70              fail("Should have thrown exception");
71          } catch (IllegalArgumentException ex)
72          {
73              // test passed
74          }
75      }
76  }