1 package com.atlassian.plugin.web.descriptors;
2
3 import com.atlassian.plugin.PluginAccessor;
4 import com.atlassian.plugin.elements.ResourceDescriptor;
5 import com.atlassian.plugin.hostcontainer.HostContainer;
6 import com.atlassian.plugin.module.ModuleFactory;
7 import com.atlassian.plugin.web.model.EmbeddedTemplateWebPanel;
8 import com.atlassian.plugin.web.model.ResourceTemplateWebPanel;
9 import com.atlassian.plugin.web.model.WebPanel;
10 import com.google.common.base.Supplier;
11 import junit.framework.TestCase;
12
13 import java.util.Arrays;
14 import java.util.List;
15
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18
19 public class TestWebPanelSupplierFactory extends TestCase
20 {
21 private WebPanelSupplierFactory webPanelSupplierFactory;
22
23 private HostContainer hostContainer = mock(HostContainer.class);
24 private ModuleFactory moduleFactory = mock(ModuleFactory.class);
25 private WebPanelModuleDescriptor descriptor = mock(WebPanelModuleDescriptor.class);
26 private PluginAccessor pluginAccessor = mock(PluginAccessor.class);
27
28 @Override
29 protected void setUp() throws Exception
30 {
31 webPanelSupplierFactory = new WebPanelSupplierFactory(descriptor, hostContainer, moduleFactory);
32 }
33
34 public void testEmbeddedTemplateWebPanel() throws Exception
35 {
36 ResourceDescriptor resourceDescriptor = mock(ResourceDescriptor.class);
37 when(resourceDescriptor.getName()).thenReturn("view");
38 when(resourceDescriptor.getType()).thenReturn("static");
39 when(resourceDescriptor.getLocation()).thenReturn(null);
40 when(resourceDescriptor.getContent()).thenReturn("Foo");
41 List<ResourceDescriptor> resourceDescriptors = Arrays.asList(resourceDescriptor);
42 when(descriptor.getResourceDescriptors()).thenReturn(resourceDescriptors);
43 when(hostContainer.create(EmbeddedTemplateWebPanel.class)).thenReturn(new EmbeddedTemplateWebPanel(pluginAccessor));
44
45 Supplier<WebPanel> panelSupplier = webPanelSupplierFactory.build(null);
46 WebPanel panel = panelSupplier.get();
47 assertEquals(EmbeddedTemplateWebPanel.class, panel.getClass());
48 }
49
50 public void testResourceTemplateWebPanel() throws Exception
51 {
52 ResourceDescriptor resourceDescriptor = mock(ResourceDescriptor.class);
53 when(resourceDescriptor.getName()).thenReturn("view");
54 when(resourceDescriptor.getType()).thenReturn("static");
55 when(resourceDescriptor.getLocation()).thenReturn("some-filename.vm");
56 List<ResourceDescriptor> resourceDescriptors = Arrays.asList(resourceDescriptor);
57 when(descriptor.getResourceDescriptors()).thenReturn(resourceDescriptors);
58 when(hostContainer.create(ResourceTemplateWebPanel.class)).thenReturn(new ResourceTemplateWebPanel(pluginAccessor));
59
60 Supplier<WebPanel> panelSupplier = webPanelSupplierFactory.build(null);
61 WebPanel panel = panelSupplier.get();
62 assertEquals(ResourceTemplateWebPanel.class, panel.getClass());
63 }
64
65 public void testSuppliedWebPanel() throws Exception
66 {
67 String moduleClassName = "com.atlassian.plugin.web.descriptors.MockWebPanel";
68 when(moduleFactory.createModule(moduleClassName, descriptor)).thenReturn(new MockWebPanel());
69
70 Supplier<WebPanel> panelSupplier = webPanelSupplierFactory.build(moduleClassName);
71 WebPanel panel = panelSupplier.get();
72 assertEquals(MockWebPanel.class, panel.getClass());
73 }
74 }