1 package com.atlassian.plugin.util;
2
3 import com.google.common.base.Function;
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.runners.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.*;
16 import static org.mockito.Mockito.*;
17
18 @RunWith(MockitoJUnitRunner.class)
19 public final class TestModuleRestricts
20 {
21 @Mock
22 private Element moduleElement;
23
24 @Test
25 public void parseModuleWithNoRestrictInformation()
26 {
27 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
28 assertTrue(Iterables.isEmpty(restricts.restricts));
29 }
30
31 @Test
32 public void parseModuleWithApplicationAsAttribute()
33 {
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 {
44 final List<Element> restrictElements = mockRestrictElements("my-app");
45 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
46
47
48 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
49
50 assertEquals(restrictElements.size(), Iterables.size(restricts.restricts));
51 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
52 }
53
54 @Test
55 public void parseModuleWithMultipleApplicationAsRestrictElement()
56 {
57 final List<Element> restrictElements = mockRestrictElements("my-app", "my-app2");
58 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
59
60 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
61
62 assertEquals(restrictElements.size(), Iterables.size(restricts.restricts));
63 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
64 assertEquals("my-app2", Iterables.get(restricts.restricts, 1).application);
65 }
66
67 @Test(expected = IllegalStateException.class)
68 public void parseModuleWithMultipleSameApplicationAsRestrictElement()
69 {
70 final List<Element> restrictElements = mockRestrictElements("my-app", "my-app");
71 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
72
73 ModuleRestricts.parse(moduleElement);
74 }
75
76 @Test
77 public void parseModuleWithApplicationAsRestrictElementWithVersionRangeAttribute()
78 {
79 final Element restrictElement = mockRestrictElement("my-app");
80 when(restrictElement.attributeValue("version")).thenReturn("[2.0]");
81 final List<Element> restrictElements = newArrayList(restrictElement);
82
83 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
84
85 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
86
87 assertEquals(restrictElements.size(), Iterables.size(restricts.restricts));
88 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
89 assertEquals(VersionRange.single("2.0"), Iterables.get(restricts.restricts, 0).version);
90 }
91
92 @Test
93 public void parseModuleWithApplicationAsRestrictElementWithVersionRangeElement()
94 {
95 final Element restrictElement = mockRestrictElement("my-app");
96 final Element versionElement = mockVersionElement("[2.0]");
97 when(restrictElement.elements("version")).thenReturn(newArrayList(versionElement));
98 final List<Element> restrictElements = newArrayList(restrictElement);
99
100 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
101
102 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
103
104 assertEquals(restrictElements.size(), Iterables.size(restricts.restricts));
105 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
106 assertEquals(VersionRange.empty().or(VersionRange.single("2.0")), Iterables.get(restricts.restricts, 0).version);
107 }
108
109 @Test
110 public void parseModuleWithApplicationAsRestrictElementWithVersionRangeElements()
111 {
112 final Element restrictElement = mockRestrictElement("my-app");
113 final Element versionElement = mockVersionElement("[2.0]");
114 final Element versionElement2 = mockVersionElement("[3.0,4.0)");
115 when(restrictElement.elements("version")).thenReturn(newArrayList(versionElement, versionElement2));
116 final List<Element> restrictElements = newArrayList(restrictElement);
117
118 when(moduleElement.elements("restrict")).thenReturn(restrictElements);
119
120 final ModuleRestricts restricts = ModuleRestricts.parse(moduleElement);
121
122 assertEquals(restrictElements.size(), Iterables.size(restricts.restricts));
123 assertEquals("my-app", Iterables.get(restricts.restricts, 0).application);
124 assertEquals(VersionRange.empty().or(VersionRange.single("2.0")).or(VersionRange.include("3.0").exclude("4.0")), Iterables.get(restricts.restricts, 0).version);
125 }
126
127 private List<Element> mockRestrictElements(String... appNames)
128 {
129 return newArrayList(transform(newArrayList(appNames), new Function<String, Element>()
130 {
131 @Override
132 public Element apply(String appName)
133 {
134 return mockRestrictElement(appName);
135 }
136 }));
137 }
138
139 private Element mockRestrictElement(String appName)
140 {
141 final Element restrictElement = mock(Element.class);
142 when(restrictElement.attributeValue("application")).thenReturn(appName);
143 return restrictElement;
144 }
145
146 private Element mockVersionElement(String range)
147 {
148 final Element versionElement = mock(Element.class);
149 when(versionElement.getText()).thenReturn(range);
150 return versionElement;
151 }
152 }