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  import static com.atlassian.plugin.PluginArtifact.AllowsReference.ReferenceMode.FORBID_REFERENCE;
12  import static com.atlassian.plugin.PluginArtifact.AllowsReference.ReferenceMode.PERMIT_REFERENCE;
13  
14  public class TestDefaultPluginArtifactFactory extends TestCase
15  {
16      File testDir;
17      File spaceTestDir;
18  
19      public void setUp() throws IOException
20      {
21          testDir = makeTempDir("test");
22          spaceTestDir = makeTempDir("space test");
23      }
24  
25      private File makeTempDir(String prefix)
26              throws IOException
27      {
28          File tempDir = File.createTempFile(prefix, "");
29          tempDir.delete();
30          tempDir.mkdir();
31  
32          return tempDir;
33      }
34  
35      public void tearDown() throws IOException
36      {
37          FileUtils.deleteDirectory(testDir);
38          FileUtils.deleteDirectory(spaceTestDir);
39  
40          testDir = null;
41          spaceTestDir = null;
42      }
43  
44      public void testCreate() throws IOException
45      {
46          doCreationTestInDirectory(testDir);
47      }
48  
49      public void testCreateWithSpaceInArtifactPath() throws IOException
50      {
51          doCreationTestInDirectory(spaceTestDir);
52      }
53  
54      private void doCreationTestInDirectory(File directory) throws IOException
55      {
56          File xmlFile = new File(directory, "foo.xml");
57          FileUtils.writeStringToFile(xmlFile, "<xml/>");
58          File jarFile = new PluginJarBuilder("jar").build(directory);
59  
60          DefaultPluginArtifactFactory factory = new DefaultPluginArtifactFactory();
61  
62          PluginArtifact jarArt = factory.create(jarFile.toURI());
63          assertNotNull(jarArt);
64          assertTrue(jarArt instanceof JarPluginArtifact);
65  
66          PluginArtifact xmlArt = factory.create(xmlFile.toURI());
67          assertNotNull(xmlArt);
68          assertTrue(xmlArt instanceof XmlPluginArtifact);
69  
70          try
71          {
72              factory.create(new File(testDir, "bob.jim").toURI());
73              fail("Should have thrown exception");
74          } catch (IllegalArgumentException ex)
75          {
76              // test passed
77          }
78      }
79  
80      public void testReferenceModeForbidReferenceDoesNotAllowReference()
81      {
82          final DefaultPluginArtifactFactory defaultPluginArtifactFactory = new DefaultPluginArtifactFactory(FORBID_REFERENCE);
83          verifyAllowsReference(defaultPluginArtifactFactory, false);
84      }
85  
86      public void testReferenceModePermitReferenceAllowsReference()
87      {
88          final DefaultPluginArtifactFactory defaultPluginArtifactFactory = new DefaultPluginArtifactFactory(PERMIT_REFERENCE);
89          verifyAllowsReference(defaultPluginArtifactFactory, true);
90      }
91  
92      public void testDefaultDoesNotAllowsReference()
93      {
94          final DefaultPluginArtifactFactory defaultPluginArtifactFactory = new DefaultPluginArtifactFactory();
95          verifyAllowsReference(defaultPluginArtifactFactory, false);
96      }
97  
98      private void verifyAllowsReference(DefaultPluginArtifactFactory defaultPluginArtifactFactory, boolean allowsReference)
99      {
100         final File file = new File(testDir, "some.jar");
101         final PluginArtifact pluginArtifact = defaultPluginArtifactFactory.create(file.toURI());
102         // The interface is always provided now
103         assertTrue(pluginArtifact instanceof PluginArtifact.AllowsReference);
104         // And the query should report what we specified
105         assertEquals(((PluginArtifact.AllowsReference) pluginArtifact).allowsReference(), allowsReference);
106         // And the convenience interface should likewise report this
107         assertEquals(PluginArtifact.AllowsReference.Default.allowsReference(pluginArtifact), allowsReference);
108     }
109 }