1 package com.atlassian.plugin.util;
2
3 import com.atlassian.plugin.InstallationMode;
4 import com.google.common.collect.Iterables;
5 import org.dom4j.Element;
6 import org.junit.Test;
7 import org.junit.runner.RunWith;
8 import org.mockito.Mock;
9 import org.mockito.junit.MockitoJUnitRunner;
10
11 import java.util.List;
12
13 import static com.google.common.collect.Iterables.transform;
14 import static com.google.common.collect.Lists.newArrayList;
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.when;
20
21 @RunWith(MockitoJUnitRunner.class)
22 public final class TestModuleRestricts {
23 @Mock
24 private Element moduleElement;
25
26 @Test
27 public void parseModuleWithNoRestrictInformation() {
28 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
29 assertTrue(Iterables.isEmpty(restricts.restricts));
30 }
31
32 @Test
33 public void parseModuleWithApplicationAsAttribute() {
34 when(moduleElement.attributeValue("application")).thenReturn("my-app");
35 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
36
37 assertEquals(1, Iterables.size(restricts.restricts));
38 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
39 }
40
41 @Test
42 public void parseModuleWithApplicationAsRestrictElement() {
43 final List<Element> restrictElements = mockRestrictElements("my-app");
44 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
45
46
47 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
48
49 assertEquals(restrictElements.size(), Iterables.size(restricts.restricts));
50 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
51 }
52
53 @Test
54 public void parseModuleWithMultipleApplicationAsRestrictElement() {
55 final List<Element> restrictElements = mockRestrictElements("my-app", "my-app2");
56 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
57
58 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
59
60 assertEquals(restrictElements.size(), Iterables.size(restricts.restricts));
61 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
62 assertNull(Iterables.get(restricts.restricts, 0).mode);
63 assertEquals("my-app2", Iterables.get(restricts.restricts, 1).application);
64 assertNull(Iterables.get(restricts.restricts, 1).mode);
65 }
66
67 @Test
68 public void parseModuleWithApplicationAsRestrictElementWithInstallationModeAttribute() {
69 final Element restrictElement = mockRestrictElement("my-app");
70 when(restrictElement.attributeValue("mode")).thenReturn("local");
71 final List<Element> restrictElements = newArrayList(restrictElement);
72
73 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
74
75 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
76
77 assertEquals(restrictElements.size(), Iterables.size(restricts.restricts));
78 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
79 assertEquals(InstallationMode.LOCAL, Iterables.get(restricts.restricts, 0).mode);
80 }
81
82 @Test
83 public void parseModuleWithApplicationAsRestrictElementWithVersionRangeAttribute() {
84 final Element restrictElement = mockRestrictElement("my-app");
85 when(restrictElement.attributeValue("version")).thenReturn("[2.0]");
86 final List<Element> restrictElements = newArrayList(restrictElement);
87
88 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
89
90 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
91
92 assertEquals(restrictElements.size(), Iterables.size(restricts.restricts));
93 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
94 assertEquals(VersionRange.single("2.0"), Iterables.get(restricts.restricts, 0).version);
95 }
96
97 @Test
98 public void parseModuleWithApplicationAsRestrictElementWithVersionRangeElement() {
99 final Element restrictElement = mockRestrictElement("my-app");
100 final Element versionElement = mockVersionElement("[2.0]");
101 when(restrictElement.elements("version")).thenReturn(newArrayList(versionElement));
102 final List<Element> restrictElements = newArrayList(restrictElement);
103
104 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
105
106 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
107
108 assertEquals(restrictElements.size(), Iterables.size(restricts.restricts));
109 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
110 assertEquals(VersionRange.empty().or(VersionRange.single("2.0")), Iterables.get(restricts.restricts, 0).version);
111 }
112
113 @Test
114 public void parseModuleWithApplicationAsRestrictElementWithVersionRangeElements() {
115 final Element restrictElement = mockRestrictElement("my-app");
116 final Element versionElement = mockVersionElement("[2.0]");
117 final Element versionElement2 = mockVersionElement("[3.0,4.0)");
118 when(restrictElement.elements("version")).thenReturn(newArrayList(versionElement, versionElement2));
119 final List<Element> restrictElements = newArrayList(restrictElement);
120
121 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
122
123 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
124
125 assertEquals(restrictElements.size(), Iterables.size(restricts.restricts));
126 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
127 assertEquals(VersionRange.empty().or(VersionRange.single("2.0")).or(VersionRange.include("3.0").exclude("4.0")), Iterables.get(restricts.restricts, 0).version);
128 }
129
130 private List<Element> mockRestrictElements(String... appNames) {
131 return newArrayList(transform(newArrayList(appNames), this::mockRestrictElement));
132 }
133
134 private Element mockRestrictElement(String appName) {
135 final Element restrictElement = mock(Element.class);
136 when(restrictElement.attributeValue("application")).thenReturn(appName);
137 return restrictElement;
138 }
139
140 private Element mockVersionElement(String range) {
141 final Element versionElement = mock(Element.class);
142 when(versionElement.getText()).thenReturn(range);
143 return versionElement;
144 }
145 }