View Javadoc

1   package com.atlassian.plugin.parsers;
2   
3   import com.atlassian.plugin.Application;
4   import com.atlassian.plugin.Plugin;
5   import org.dom4j.Element;
6   import org.junit.Test;
7   
8   import java.io.ByteArrayInputStream;
9   import java.util.Collections;
10  
11  import static com.atlassian.fugue.Option.none;
12  import static com.atlassian.fugue.Option.option;
13  import static org.hamcrest.MatcherAssert.assertThat;
14  import static org.hamcrest.Matchers.contains;
15  import static org.hamcrest.Matchers.emptyIterable;
16  import static org.hamcrest.Matchers.is;
17  
18  public class TestPluginInformationReader
19  {
20      @Test
21      public void getStartupReturnsNoneWhenAbsent()
22      {
23          final String xml = "<plugin-info></plugin-info>";
24          PluginInformationReader reader = getPluginInformationReader(xml);
25          assertThat(reader.getStartup(), is(none(String.class)));
26      }
27  
28      @Test
29      public void getStartupReturnsTrimmedTextWhenPresent()
30      {
31          final String xml = "<plugin-info><startup> early  </startup></plugin-info>";
32          PluginInformationReader reader = getPluginInformationReader(xml);
33          assertThat(reader.getStartup(), is(option("early")));
34      }
35  
36      @Test
37      public void getScanModulesReturnsEmptyIteratorWhenAbsent()
38      {
39          final String xml = "<plugin-info></plugin-info>";
40          PluginInformationReader reader = getPluginInformationReader(xml);
41          assertThat(reader.getModuleScanFolders(), emptyIterable());
42      }
43  
44      @Test
45      public void getScanModulesReturnsDefaultWhenPresent()
46      {
47          final String xml = "<plugin-info><scan-modules/></plugin-info>";
48          PluginInformationReader reader = getPluginInformationReader(xml);
49          assertThat(reader.getModuleScanFolders(), contains("META-INF/atlassian"));
50      }
51  
52      @Test
53      public void getScanModulesReturnsFoldersWhenPresent()
54      {
55          final String xml = "<plugin-info><scan-modules><folder>META-INF/foo</folder><folder>META-INF/foo/bar</folder></scan-modules></plugin-info>";
56          PluginInformationReader reader = getPluginInformationReader(xml);
57          assertThat(reader.getModuleScanFolders(), contains("META-INF/foo", "META-INF/foo/bar"));
58      }
59  
60      @Test
61      public void DefaultFolderCanBeSpecifiedInScanFolders()
62      {
63          final String xml = "<plugin-info><scan-modules><folder>META-INF/atlassian</folder><folder>META-INF/foo/bar</folder></scan-modules></plugin-info>";
64          PluginInformationReader reader = getPluginInformationReader(xml);
65          assertThat(reader.getModuleScanFolders(), contains("META-INF/atlassian", "META-INF/foo/bar"));
66      }
67  
68      private PluginInformationReader getPluginInformationReader(final String xml)
69      {
70          final ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes());
71          final Element element = XmlDescriptorParser.createDocument(inputStream).getRootElement();
72          return new PluginInformationReader(option(element), Collections.<Application>emptySet(), Plugin.VERSION_3);
73      }
74  }