1 package com.atlassian.plugin.webresource;
2
3 import junit.framework.TestCase;
4 import com.mockobjects.dynamic.Mock;
5 import com.mockobjects.dynamic.C;
6 import com.atlassian.plugin.PluginAccessor;
7 import com.atlassian.plugin.Plugin;
8
9 import java.util.List;
10 import java.util.ArrayList;
11 import java.util.LinkedHashSet;
12 import java.util.Collections;
13 import java.util.Arrays;
14
15 public class TestDefaultResourceDependencyResolver extends TestCase
16 {
17 private Mock mockWebResourceIntegration;
18 private Mock mockPluginAccessor;
19 private ResourceDependencyResolver dependencyResolver;
20
21 private Plugin testPlugin;
22 private List<String> superBatchKeys = new ArrayList<String>();
23 private ResourceBatchingConfiguration batchingConfiguration = new ResourceBatchingConfiguration() {
24
25 public boolean isSuperBatchingEnabled()
26 {
27 return true;
28 }
29
30 public List<String> getSuperBatchModuleCompleteKeys()
31 {
32 return superBatchKeys;
33 }
34 };
35
36 @Override
37 protected void setUp() throws Exception
38 {
39 super.setUp();
40
41 testPlugin = TestUtils.createTestPlugin();
42
43 mockPluginAccessor = new Mock(PluginAccessor.class);
44 mockWebResourceIntegration = new Mock(WebResourceIntegration.class);
45 mockWebResourceIntegration.matchAndReturn("getPluginAccessor", mockPluginAccessor.proxy());
46
47 dependencyResolver = new DefaultResourceDependencyResolver((WebResourceIntegration) mockWebResourceIntegration.proxy(), batchingConfiguration);
48 }
49
50 @Override
51 protected void tearDown() throws Exception
52 {
53 dependencyResolver = null;
54 mockWebResourceIntegration = null;
55 mockPluginAccessor = null;
56
57 testPlugin = null;
58
59 super.tearDown();
60 }
61
62 public void testSuperBatchingNotEnabled()
63 {
64 dependencyResolver = new DefaultResourceDependencyResolver((WebResourceIntegration) mockWebResourceIntegration.proxy(), new ResourceBatchingConfiguration() {
65
66 public boolean isSuperBatchingEnabled()
67 {
68 return false;
69 }
70
71 public List<String> getSuperBatchModuleCompleteKeys()
72 {
73 return null;
74 }
75 });
76
77 assertTrue(dependencyResolver.getSuperBatchDependencies().isEmpty());
78 }
79
80 public void testGetSuperBatchDependenciesInOrder()
81 {
82 String superBatchResource1 = "plugin.key:resource1";
83 String superBatchResource2 = "plugin.key:resource2";
84 String superBatchResource3 = "plugin.key:resource3";
85
86 superBatchKeys.add(superBatchResource1);
87 superBatchKeys.add(superBatchResource2);
88 superBatchKeys.add(superBatchResource3);
89
90 mockWebResourceIntegration.matchAndReturn("getSuperBatchVersion", "1.0");
91
92 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(superBatchResource1)),
93 TestUtils.createWebResourceModuleDescriptor(superBatchResource1, testPlugin));
94 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(superBatchResource2)),
95 TestUtils.createWebResourceModuleDescriptor(superBatchResource2, testPlugin));
96 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(superBatchResource3)),
97 TestUtils.createWebResourceModuleDescriptor(superBatchResource3, testPlugin));
98
99 LinkedHashSet<String> resources = dependencyResolver.getSuperBatchDependencies();
100 assertNotNull(resources);
101 assertOrder(resources, superBatchResource1, superBatchResource2, superBatchResource3);
102 }
103
104 public void testGetSuperBatchDependenciesWithCylicDependency()
105 {
106 String superBatchResource1 = "plugin.key:resource1";
107 String superBatchResource2 = "plugin.key:resource2";
108
109 superBatchKeys.add(superBatchResource1);
110 superBatchKeys.add(superBatchResource2);
111
112 mockWebResourceIntegration.matchAndReturn("getSuperBatchVersion", "1.0");
113
114 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(superBatchResource1)),
115 TestUtils.createWebResourceModuleDescriptor(superBatchResource1, testPlugin, Collections.EMPTY_LIST, Arrays.asList(superBatchResource2)));
116 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(superBatchResource2)),
117 TestUtils.createWebResourceModuleDescriptor(superBatchResource2, testPlugin, Collections.EMPTY_LIST, Arrays.asList(superBatchResource1)));
118
119 LinkedHashSet<String> resources = dependencyResolver.getSuperBatchDependencies();
120 assertNotNull(resources);
121 assertOrder(resources, superBatchResource2, superBatchResource1);
122 }
123
124 public void testGetSuperBatchDependenciesWithDependencies()
125 {
126 String superBatchResource1 = "test.atlassian:super1";
127 String superBatchResource2 = "test.atlassian:super2";
128
129 superBatchKeys.add(superBatchResource1);
130 superBatchKeys.add(superBatchResource2);
131 mockWebResourceIntegration.matchAndReturn("getSuperBatchVersion", "1.0");
132
133
134 String resourceA = "test.atlassian:a";
135 String resourceB = "test.atlassian:b";
136 String resourceC = "test.atlassian:c";
137 String resourceD = "test.atlassian:d";
138
139
140 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(resourceA)),
141 TestUtils.createWebResourceModuleDescriptor(resourceA, testPlugin));
142 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(resourceB)),
143 TestUtils.createWebResourceModuleDescriptor(resourceB, testPlugin));
144 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(superBatchResource1)),
145 TestUtils.createWebResourceModuleDescriptor(superBatchResource1, testPlugin, Collections.EMPTY_LIST, Arrays.asList(resourceA, resourceB)));
146
147
148 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(resourceD)),
149 TestUtils.createWebResourceModuleDescriptor(resourceD, testPlugin));
150 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(resourceC)),
151 TestUtils.createWebResourceModuleDescriptor(resourceC, testPlugin, Collections.EMPTY_LIST, Arrays.asList(resourceD)));
152 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(superBatchResource2)),
153 TestUtils.createWebResourceModuleDescriptor(superBatchResource2, testPlugin, Collections.EMPTY_LIST, Collections.singletonList(resourceC)));
154
155 LinkedHashSet<String> resources = dependencyResolver.getSuperBatchDependencies();
156 assertNotNull(resources);
157 assertOrder(resources, resourceA, resourceB, superBatchResource1, resourceD, resourceC, superBatchResource2);
158 }
159
160 public void testGetDependenciesExcludesSuperBatch()
161 {
162 String superBatchResource1 = "test.atlassian:super1";
163 String superBatchResource2 = "test.atlassian:super2";
164 String moduleKey = "test.atlassian:foo";
165
166 superBatchKeys.add(superBatchResource1);
167 superBatchKeys.add(superBatchResource2);
168 mockWebResourceIntegration.matchAndReturn("getSuperBatchVersion", "1.0");
169
170
171 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(moduleKey)),
172 TestUtils.createWebResourceModuleDescriptor(moduleKey, testPlugin, Collections.EMPTY_LIST, Arrays.asList(superBatchResource1)));
173 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(superBatchResource1)),
174 TestUtils.createWebResourceModuleDescriptor(superBatchResource1, testPlugin));
175 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(superBatchResource2)),
176 TestUtils.createWebResourceModuleDescriptor(superBatchResource2, testPlugin));
177
178 LinkedHashSet<String> resources = dependencyResolver.getDependencies(moduleKey, true);
179 assertNotNull(resources);
180 assertOrder(resources, moduleKey);
181 }
182
183 public void testGetDependenciesIncludesSuperBatch()
184 {
185 String superBatchResource1 = "test.atlassian:super1";
186 String superBatchResource2 = "test.atlassian:super2";
187 String moduleKey = "test.atlassian:foo";
188
189 superBatchKeys.add(superBatchResource1);
190 superBatchKeys.add(superBatchResource2);
191 mockWebResourceIntegration.matchAndReturn("getSuperBatchVersion", "1.0");
192
193
194 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(moduleKey)),
195 TestUtils.createWebResourceModuleDescriptor(moduleKey, testPlugin, Collections.EMPTY_LIST, Arrays.asList(superBatchResource1)));
196 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(superBatchResource1)),
197 TestUtils.createWebResourceModuleDescriptor(superBatchResource1, testPlugin));
198 mockPluginAccessor.matchAndReturn("getEnabledPluginModule", C.args(C.eq(superBatchResource2)),
199 TestUtils.createWebResourceModuleDescriptor(superBatchResource2, testPlugin));
200
201 LinkedHashSet<String> resources = dependencyResolver.getDependencies(moduleKey, false);
202 assertNotNull(resources);
203 assertOrder(resources, superBatchResource1, moduleKey);
204 }
205
206 private void assertOrder(LinkedHashSet<String> resources, String... expectedResources)
207 {
208 assertEquals(resources.size(), expectedResources.length);
209
210 int i = 0;
211 for(String resource : resources)
212 {
213 assertEquals(expectedResources[i], resource);
214 i++;
215 }
216 }
217 }