View Javadoc
1   package com.atlassian.plugin;
2   
3   import com.atlassian.plugin.test.PluginJarBuilder;
4   import junit.framework.TestCase;
5   import org.apache.commons.io.FileUtils;
6   
7   import java.io.File;
8   import java.io.IOException;
9   
10  import static com.atlassian.plugin.ReferenceMode.FORBID_REFERENCE;
11  import static com.atlassian.plugin.ReferenceMode.PERMIT_REFERENCE;
12  import static org.hamcrest.MatcherAssert.assertThat;
13  import static org.hamcrest.Matchers.is;
14  
15  public class TestDefaultPluginArtifactFactory extends TestCase {
16      File testDir;
17      File spaceTestDir;
18      File file;
19  
20      public void setUp() throws IOException {
21          testDir = makeTempDir("test");
22          spaceTestDir = makeTempDir("space test");
23          file = new File(testDir, "some.jar");
24      }
25  
26      private File makeTempDir(String prefix)
27              throws IOException {
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          FileUtils.deleteDirectory(testDir);
37          FileUtils.deleteDirectory(spaceTestDir);
38  
39          testDir = null;
40          spaceTestDir = null;
41      }
42  
43      public void testCreate() throws IOException {
44          doCreationTestInDirectory(testDir);
45      }
46  
47      public void testCreateWithSpaceInArtifactPath() throws IOException {
48          doCreationTestInDirectory(spaceTestDir);
49      }
50  
51      private void doCreationTestInDirectory(File directory) throws IOException {
52          File xmlFile = new File(directory, "foo.xml");
53          FileUtils.writeStringToFile(xmlFile, "<xml/>");
54          File jarFile = new PluginJarBuilder("jar").build(directory);
55  
56          DefaultPluginArtifactFactory factory = new DefaultPluginArtifactFactory();
57  
58          PluginArtifact jarArt = factory.create(jarFile.toURI());
59          assertNotNull(jarArt);
60          assertTrue(jarArt instanceof JarPluginArtifact);
61  
62          PluginArtifact xmlArt = factory.create(xmlFile.toURI());
63          assertNotNull(xmlArt);
64          assertTrue(xmlArt instanceof XmlPluginArtifact);
65  
66          try {
67              factory.create(new File(testDir, "bob.jim").toURI());
68              fail("Should have thrown exception");
69          } catch (IllegalArgumentException ex) {
70              // test passed
71          }
72      }
73  
74      public void testReferenceModeForbidReferenceDoesNotAllowReference() {
75          final DefaultPluginArtifactFactory defaultPluginArtifactFactory = new DefaultPluginArtifactFactory(FORBID_REFERENCE);
76          final PluginArtifact pluginArtifact = defaultPluginArtifactFactory.create(file.toURI());
77          verifyAllowsReference(pluginArtifact, false);
78          assertThat(pluginArtifact.getReferenceMode(), is(FORBID_REFERENCE));
79      }
80  
81      public void testReferenceModePermitReferenceAllowsReference() {
82          final DefaultPluginArtifactFactory defaultPluginArtifactFactory = new DefaultPluginArtifactFactory(PERMIT_REFERENCE);
83          final PluginArtifact pluginArtifact = defaultPluginArtifactFactory.create(file.toURI());
84          verifyAllowsReference(pluginArtifact, true);
85          assertThat(pluginArtifact.getReferenceMode(), is(PERMIT_REFERENCE));
86      }
87  
88      public void testDefaultDoesNotAllowsReference() {
89          final DefaultPluginArtifactFactory defaultPluginArtifactFactory = new DefaultPluginArtifactFactory();
90          final PluginArtifact pluginArtifact = defaultPluginArtifactFactory.create(file.toURI());
91          verifyAllowsReference(pluginArtifact, false);
92          assertThat(pluginArtifact.getReferenceMode(), is(FORBID_REFERENCE));
93      }
94  
95      public void testLegacyReferenceModeForbidReferenceDoesNotAllowReference() {
96          final DefaultPluginArtifactFactory defaultPluginArtifactFactory = new DefaultPluginArtifactFactory(
97                  PluginArtifact.AllowsReference.ReferenceMode.FORBID_REFERENCE);
98          final PluginArtifact pluginArtifact = defaultPluginArtifactFactory.create(file.toURI());
99          verifyAllowsReference(pluginArtifact, false);
100     }
101 
102     public void testLegacyReferenceModePermitReferenceAllowsReference() {
103         final DefaultPluginArtifactFactory defaultPluginArtifactFactory = new DefaultPluginArtifactFactory(
104                 PluginArtifact.AllowsReference.ReferenceMode.PERMIT_REFERENCE);
105         final PluginArtifact pluginArtifact = defaultPluginArtifactFactory.create(file.toURI());
106         verifyAllowsReference(pluginArtifact, true);
107     }
108 
109     private void verifyAllowsReference(PluginArtifact pluginArtifact, boolean allowsReference) {
110         // Modern interface via getReferenceMode
111         assertThat(pluginArtifact.getReferenceMode().allowsReference(), is(allowsReference));
112         // Legacy support
113         // The interface is always provided now
114         assertTrue(pluginArtifact instanceof PluginArtifact.AllowsReference);
115         // And the query should report what we specified
116         assertEquals(((PluginArtifact.AllowsReference) pluginArtifact).allowsReference(), allowsReference);
117         // And the convenience interface should likewise report this
118         assertEquals(PluginArtifact.AllowsReference.Default.allowsReference(pluginArtifact), allowsReference);
119     }
120 }