1 package com.atlassian.plugin.webresource;
2
3 import com.atlassian.plugin.Plugin;
4 import com.atlassian.plugin.servlet.DownloadableResource;
5 import junit.framework.TestCase;
6 import org.dom4j.DocumentException;
7 import org.mockito.Mock;
8 import org.mockito.MockitoAnnotations;
9
10 import java.util.Arrays;
11 import java.util.Collections;
12 import java.util.LinkedHashSet;
13
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 public class TestSuperBatchSubResourceBuilder extends TestCase
19 {
20 @Mock
21 private DefaultResourceDependencyResolver mockDependencyResolver;
22 @Mock
23 private DownloadableResourceFinder mockResourceFinder;
24
25 private SuperBatchSubResourceBuilder builder;
26
27 @Override
28 public void setUp() throws Exception
29 {
30 super.setUp();
31
32 MockitoAnnotations.initMocks(this);
33 builder = new SuperBatchSubResourceBuilder(mockDependencyResolver, mockResourceFinder);
34 }
35
36 @Override
37 public void tearDown() throws Exception
38 {
39 mockDependencyResolver = null;
40 mockResourceFinder = null;
41
42 builder = null;
43
44 super.tearDown();
45 }
46
47 public void testParsePluginResource() throws UrlParseException
48 {
49 String moduleKey = "testplug:testmod";
50 WebResourceModuleDescriptor mockModDesc = mock(WebResourceModuleDescriptor.class);
51 when(mockModDesc.getCompleteKey()).thenReturn(moduleKey);
52 when(mockDependencyResolver.getSuperBatchDependencies()).thenReturn(Arrays.asList(mockModDesc));
53 DownloadableResource imageResource = mock(DownloadableResource.class);
54 when(mockResourceFinder.find(moduleKey, "css/images/foo.png")).thenReturn(imageResource);
55 String path = "/download/superbatch/css/images/foo.png";
56
57 assertTrue(builder.matches(path));
58 DownloadableResource resource = builder.parse(path, Collections.<String, String>emptyMap());
59 assertSame(imageResource, resource);
60
61
62
63 }
64
65 public void testParsePluginResourceAndResolve() throws UrlParseException, DocumentException, ClassNotFoundException
66 {
67 String path = "/download/superbatch/css/images/foo.png";
68
69 String moduleKey = "super-resources";
70 Plugin testPlugin = TestUtils.createTestPlugin();
71 DownloadableResource imageResource = mock(DownloadableResource.class);
72 when(mockResourceFinder.find(moduleKey, "css/images/foo.png")).thenReturn(imageResource);
73
74 LinkedHashSet<WebResourceModuleDescriptor> superBatchDependencies = new LinkedHashSet<WebResourceModuleDescriptor>();
75 WebResourceModuleDescriptor moduleDescriptor = TestUtils.createWebResourceModuleDescriptor(moduleKey, testPlugin);
76 superBatchDependencies.add(moduleDescriptor);
77 when(mockDependencyResolver.getSuperBatchDependencies()).thenReturn(superBatchDependencies);
78
79 DownloadableResource resource = builder.parse(path, Collections.<String, String>emptyMap());
80 assertSame(imageResource, resource);
81
82 verify(mockResourceFinder).find(moduleKey, "css/images/foo.png");
83 }
84 }