1 package com.atlassian.plugin.parsers;
2
3 import com.atlassian.plugin.Plugin;
4 import org.dom4j.Element;
5 import org.junit.Test;
6
7 import java.io.ByteArrayInputStream;
8 import java.util.Collections;
9 import java.util.Optional;
10
11 import static java.util.Optional.empty;
12 import static java.util.Optional.of;
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.equalTo;
17 import static org.hamcrest.Matchers.is;
18
19 public class TestPluginInformationReader {
20 @Test
21 public void getStartupReturnsNoneWhenAbsent() {
22 final String xml = "<plugin-info></plugin-info>";
23 PluginInformationReader reader = getPluginInformationReader(xml);
24 assertThat(reader.getStartup(), is(empty()));
25 }
26
27 @Test
28 public void getStartupReturnsTrimmedTextWhenPresent() {
29 final String xml = "<plugin-info><startup> early </startup></plugin-info>";
30 PluginInformationReader reader = getPluginInformationReader(xml);
31 assertThat(reader.getStartup(), is(of("early")));
32 }
33
34 @Test
35 public void getScanModulesReturnsEmptyIteratorWhenAbsent() {
36 final String xml = "<plugin-info></plugin-info>";
37 PluginInformationReader reader = getPluginInformationReader(xml);
38 assertThat(reader.getModuleScanFolders(), emptyIterable());
39 }
40
41 @Test
42 public void getScanModulesReturnsDefaultWhenPresent() {
43 final String xml = "<plugin-info><scan-modules/></plugin-info>";
44 PluginInformationReader reader = getPluginInformationReader(xml);
45 assertThat(reader.getModuleScanFolders(), contains("META-INF/atlassian"));
46 }
47
48 @Test
49 public void getScanModulesReturnsFoldersWhenPresent() {
50 final String xml = "<plugin-info><scan-modules><folder>META-INF/foo</folder><folder>META-INF/foo/bar</folder></scan-modules></plugin-info>";
51 PluginInformationReader reader = getPluginInformationReader(xml);
52 assertThat(reader.getModuleScanFolders(), contains("META-INF/foo", "META-INF/foo/bar"));
53 }
54
55 @Test
56 public void getDefaultFolderCanBeSpecifiedInScanFolders() {
57 final String xml = "<plugin-info><scan-modules><folder>META-INF/atlassian</folder><folder>META-INF/foo/bar</folder></scan-modules></plugin-info>";
58 PluginInformationReader reader = getPluginInformationReader(xml);
59 assertThat(reader.getModuleScanFolders(), contains("META-INF/atlassian", "META-INF/foo/bar"));
60 }
61
62 @Test
63 public void getScopeKeyWhenMissing() {
64 final String xml = "<plugin-info></plugin-info>";
65 PluginInformationReader reader = getPluginInformationReader(xml);
66 assertThat(reader.getScopeKey(), equalTo(empty()));
67 }
68
69 @Test
70 public void getScopeKeyWhenPresent() {
71 final String xml = "<plugin-info><scope key = \"/license/jira-service-desk\"/></plugin-info>";
72 PluginInformationReader reader = getPluginInformationReader(xml);
73 assertThat(reader.getScopeKey(), equalTo(Optional.of("/license/jira-service-desk")));
74 }
75
76 @Test(expected = IllegalArgumentException.class)
77 public void getBlankScopeKey() {
78 getPluginInformationReader("<plugin-info><scope key = \"\"/></plugin-info>").getScopeKey();
79 }
80
81 private PluginInformationReader getPluginInformationReader(final String xml) {
82 final ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes());
83 final Element element = XmlDescriptorParser.createDocument(inputStream).getRootElement();
84 return new PluginInformationReader(element, Collections.emptySet(), Plugin.VERSION_3);
85 }
86 }