1 package com.atlassian.plugin.webresource;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Set;
10
11 import org.apache.commons.lang.StringUtils;
12
13 import junit.framework.TestCase;
14
15 import com.google.common.collect.Iterables;
16
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.verifyNoMoreInteractions;
20 import static org.mockito.Mockito.when;
21
22 public class TestContextBatchOperations extends TestCase
23 {
24 private ContextBatchOperations operations;
25 private PluginResourceLocator mockPluginResourceLocator;
26 private WebResourceFilter mockWebResourceFilter;
27
28 @Override
29 protected void setUp() throws Exception
30 {
31 super.setUp();
32 mockPluginResourceLocator = mock(PluginResourceLocator.class);
33 mockWebResourceFilter = mock(WebResourceFilter.class);
34 operations = new ContextBatchOperations(mockPluginResourceLocator, mockWebResourceFilter);
35 }
36
37 public void testMergeNoBatches()
38 {
39 assertNull(operations.merge(null));
40 assertNull(operations.merge(Collections.<ContextBatch> emptySet()));
41 }
42
43 public void testMergeWithExcludedContexts()
44 {
45 ContextBatch batch1 = new ContextBatch("abc", Collections.singletonList("apple"), Collections.singletonList("pear"),
46 Collections.<WebResourceModuleDescriptor> emptySet(),
47 Collections.<PluginResourceBatchParams> emptySet());
48
49 ContextBatch batch2 = new ContextBatch("def", Collections.singletonList("plum"),
50 Collections.<WebResourceModuleDescriptor> emptySet(),
51 Collections.<PluginResourceBatchParams> emptySet());
52
53 Collection<ContextBatch> mergeBatches = new ArrayList<ContextBatch>(2);
54 mergeBatches.add(batch1);
55 mergeBatches.add(batch2);
56
57 try
58 {
59 operations.merge(mergeBatches);
60 fail("Should not be possible to merge when one or more batches contains excluded contexts.");
61 }
62 catch (IllegalArgumentException ex)
63 {
64
65 }
66 }
67
68 public void testMergeThreeContextBatches()
69 {
70 List<WebResourceModuleDescriptor> mockResources1 = createListOfMockDescriptors(3, "plugin1");
71 List<WebResourceModuleDescriptor> mockResources2 = createListOfMockDescriptors(2, "plugin2");
72 List<WebResourceModuleDescriptor> mockResources3 = createListOfMockDescriptors(2, "plugin3");
73 mockResources2.add(mockResources1.get(0));
74 List<PluginResourceBatchParams> mockParams1 = createListOfBatchParams(4);
75 List<PluginResourceBatchParams> mockParams2 = createListOfBatchParams(1);
76 List<PluginResourceBatchParams> mockParams3 = createListOfBatchParams(1);
77 mockParams3.add(mockParams2.get(0));
78
79 List<String> contexts1 = new ArrayList<String>(2);
80 contexts1.add("apple");
81 contexts1.add("sausage");
82
83 List<String> contexts2 = new ArrayList<String>(2);
84 contexts2.add("custard");
85 contexts2.add("chops");
86
87 List<String> contexts3 = new ArrayList<String>(2);
88 contexts3.add("potato");
89 contexts3.add("grape");
90
91 ContextBatch batch1 = new ContextBatch("abc", contexts1, mockResources1, mockParams1);
92 ContextBatch batch2 = new ContextBatch("def", contexts2, mockResources2, mockParams2);
93 ContextBatch batch3 = new ContextBatch("ghi", contexts3, mockResources3, mockParams3);
94
95 Collection<ContextBatch> mergeBatches = new ArrayList<ContextBatch>(3);
96 mergeBatches.add(batch1);
97 mergeBatches.add(batch2);
98 mergeBatches.add(batch3);
99
100 ContextBatch mergedBatch = operations.merge(mergeBatches);
101
102 Set<String> contextsSet = new HashSet<String>(6);
103 Iterables.addAll(contextsSet, mergedBatch.getContexts());
104
105 assertEquals(6, contextsSet.size());
106 assertTrue(contextsSet.contains("apple") && contextsSet.contains("sausage")
107 && contextsSet.contains("custard") && contextsSet.contains("chops"));
108
109 assertEquals("abc,def,ghi", mergedBatch.getKey());
110
111 Collection<WebResourceModuleDescriptor> mergedResources = new ArrayList<WebResourceModuleDescriptor>(7);
112 Iterables.addAll(mergedResources, mergedBatch.getResources());
113
114 assertEquals(7, mergedResources.size());
115
116 Collection<PluginResourceBatchParams> mergedParams = new ArrayList<PluginResourceBatchParams>(6);
117 Iterables.addAll(mergedParams, mergedBatch.getResourceParams());
118
119 assertEquals(6, mergedParams.size());
120 }
121
122 public void testSubtractNoBatches()
123 {
124 List<WebResourceModuleDescriptor> mockResources1 = createListOfMockDescriptors(1, "plugin1");
125 List<PluginResourceBatchParams> mockParams1 = createListOfBatchParams(1);
126
127 List<String> contexts1 = new ArrayList<String>(1);
128 contexts1.add("sausage");
129
130 ContextBatch batch1 = new ContextBatch("abc", contexts1, mockResources1, mockParams1);
131
132 assertTrue(batch1 == operations.subtract(batch1, null));
133
134 assertTrue(batch1 == operations.subtract(batch1, Collections.<ContextBatch>emptySet()));
135 }
136
137 public void testSubtractWithExcludedContexts()
138 {
139 ContextBatch batch1 = new ContextBatch("abc", Collections.singletonList("apple"),
140 Collections.<WebResourceModuleDescriptor> emptySet(),
141 Collections.<PluginResourceBatchParams> emptySet());
142
143 ContextBatch batch2 = new ContextBatch("def", Collections.singletonList("plum"),
144 Collections.singletonList("pear"),
145 Collections.<WebResourceModuleDescriptor> emptySet(),
146 Collections.<PluginResourceBatchParams> emptySet());
147
148 try
149 {
150 operations.subtract(batch1, Collections.singleton(batch2));
151 fail("Should not be possible to subtract when one or more subtraction batches contains excluded contexts.");
152 }
153 catch (IllegalArgumentException ex)
154 {
155
156 }
157 }
158
159 public void testSubtractTwoBatchesFromOperand()
160 {
161 List<WebResourceModuleDescriptor> mockResources1 = createListOfMockDescriptors(5, "plugin1");
162 List<WebResourceModuleDescriptor> mockResources2 = createListOfMockDescriptors(1, "plugin2");
163 List<WebResourceModuleDescriptor> mockResources3 = createListOfMockDescriptors(1, "plugin3");
164
165
166 mockResources2.add(mockResources1.get(2));
167 mockResources2.add(mockResources1.get(3));
168 mockResources3.add(mockResources2.get(0));
169 mockResources3.add(mockResources1.get(1));
170 mockResources3.add(mockResources1.get(2));
171
172 List<PluginResourceBatchParams> mockParams1 = createListOfBatchParams(4);
173 List<PluginResourceBatchParams> mockParams2 = createListOfBatchParams(1);
174 List<PluginResourceBatchParams> mockParams3 = createListOfBatchParams(1);
175
176 List<String> contexts1 = new ArrayList<String>(2);
177 contexts1.add("apple");
178 contexts1.add("sausage");
179
180 List<String> contexts2 = new ArrayList<String>(2);
181 contexts2.add("custard");
182
183 List<String> contexts3 = new ArrayList<String>(2);
184 contexts3.add("potato");
185 contexts3.add("grape");
186
187 ContextBatch batch1 = new ContextBatch("abc", contexts1, mockResources1, mockParams1);
188 ContextBatch batch2 = new ContextBatch("def", contexts2, mockResources2, mockParams2);
189 ContextBatch batch3 = new ContextBatch("ghi", contexts3, mockResources3, mockParams3);
190
191 Collection<ContextBatch> subtractionBatches = new ArrayList<ContextBatch>(2);
192 subtractionBatches.add(batch2);
193 subtractionBatches.add(batch3);
194
195 ContextBatch subtractedBatch = operations.subtract(batch1, subtractionBatches);
196
197 Set<String> contextsSet = new HashSet<String>(2);
198 Iterables.addAll(contextsSet, subtractedBatch.getContexts());
199
200 assertEquals(2, contextsSet.size());
201 assertTrue(contextsSet.contains("apple") && contextsSet.contains("sausage"));
202
203 Set<String> excludedContextsSet = new HashSet<String>(3);
204 Iterables.addAll(excludedContextsSet, subtractedBatch.getExcludedContexts());
205
206 assertEquals(3, excludedContextsSet.size());
207 assertTrue(excludedContextsSet.contains("custard") && excludedContextsSet.contains("potato") && excludedContextsSet.contains("grape"));
208
209 List<String> keyParts = Arrays.asList(StringUtils.split(subtractedBatch.getKey(), ','));
210 assertTrue(keyParts.contains("apple"));
211 assertTrue(keyParts.contains("sausage"));
212 assertTrue(keyParts.contains("-custard"));
213 assertTrue(keyParts.contains("-potato"));
214 assertTrue(keyParts.contains("-grape"));
215
216 Collection<WebResourceModuleDescriptor> completeResources = new ArrayList<WebResourceModuleDescriptor>(2);
217 Iterables.addAll(completeResources, subtractedBatch.getResources());
218
219 assertEquals(2, completeResources.size());
220
221 verify(mockPluginResourceLocator).getPluginResources("plugin1:module-1");
222 verify(mockPluginResourceLocator).getPluginResources("plugin1:module-5");
223 verifyNoMoreInteractions(mockPluginResourceLocator);
224 }
225
226 private static List<WebResourceModuleDescriptor> createListOfMockDescriptors(int size, String keyPrefix)
227 {
228 List<WebResourceModuleDescriptor> mocks = new ArrayList<WebResourceModuleDescriptor>(size);
229 for (int i = 0; i < size; i++)
230 {
231 WebResourceModuleDescriptor mockDescriptor = mock(WebResourceModuleDescriptor.class);
232 mocks.add(mockDescriptor);
233 when(mockDescriptor.getCompleteKey()).thenReturn(keyPrefix + ":module-" + (i + 1));
234 }
235 return mocks;
236 }
237
238 private static List<PluginResourceBatchParams> createListOfBatchParams(int size)
239 {
240 List<PluginResourceBatchParams> mocks = new ArrayList<PluginResourceBatchParams>(size);
241 for (int i = 0; i < size; i++)
242 {
243 mocks.add(mock(PluginResourceBatchParams.class));
244 }
245 return mocks;
246 }
247
248 }