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  
77      public void testAllowsReferenceTrue()
78      {
79          final DefaultPluginArtifactFactory defaultPluginArtifactFactory = new DefaultPluginArtifactFactory(true);
80          verifyAllowsReference(defaultPluginArtifactFactory, true);
81      }
82  
83      public void testAllowsReferenceFalse()
84      {
85          final DefaultPluginArtifactFactory defaultPluginArtifactFactory = new DefaultPluginArtifactFactory(false);
86          verifyAllowsReference(defaultPluginArtifactFactory, false);
87      }
88  
89      public void testDefaultDoesNotAllowsReference()
90      {
91          final DefaultPluginArtifactFactory defaultPluginArtifactFactory = new DefaultPluginArtifactFactory();
92          verifyAllowsReference(defaultPluginArtifactFactory, false);
93      }
94  
95      private void verifyAllowsReference(DefaultPluginArtifactFactory defaultPluginArtifactFactory, boolean allowsReference)
96      {
97          final File file = new File(testDir, "some.jar");
98          final PluginArtifact pluginArtifact = defaultPluginArtifactFactory.create(file.toURI());
99          // The interface is always provided now
100         assertTrue(pluginArtifact instanceof PluginArtifact.AllowsReference);
101         // And the query should report what we specified
102         assertEquals(((PluginArtifact.AllowsReference) pluginArtifact).allowsReference(), allowsReference);
103         // And the convenience interface should likewise report this
104         assertEquals(PluginArtifact.AllowsReference.Default.allowsReference(pluginArtifact), allowsReference);
105     }
106 }