1 package com.atlassian.plugin.web.model;
2
3 import com.atlassian.plugin.Plugin;
4 import com.atlassian.plugin.PluginAccessor;
5 import com.atlassian.plugin.web.renderer.RendererException;
6 import com.atlassian.plugin.web.renderer.WebPanelRenderer;
7 import junit.framework.TestCase;
8
9 import java.io.IOException;
10 import java.io.StringWriter;
11 import java.io.Writer;
12 import java.util.Collections;
13 import java.util.Map;
14
15 import static org.mockito.Matchers.*;
16 import static org.mockito.Mockito.doThrow;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.when;
19
20 public class ResourceTemplateWebPanelTest extends TestCase
21 {
22 private static final String TEMPLATE_FILE_TEXT = "This <file> is used as web panel contents in unit tests.\n" +
23 "Do not change its contents.";
24
25 private PluginAccessor accessorMock;
26 private WebPanelRenderer rendererMock;
27 private Plugin pluginMock;
28 private Map<String, Object> emptyContext = Collections.emptyMap();
29
30 @Override
31 protected void setUp() throws Exception
32 {
33 accessorMock = mock(PluginAccessor.class);
34 rendererMock = mock(WebPanelRenderer.class);
35 pluginMock = mock(Plugin.class);
36 when(pluginMock.getClassLoader()).thenReturn(this.getClass().getClassLoader());
37 }
38
39 @Override
40 protected void tearDown() throws Exception
41 {
42 accessorMock = null;
43 rendererMock = null;
44 pluginMock = null;
45 }
46
47 public void testGetHtml()
48 {
49 String resourceFilename = "ResourceTemplateWebPanelTest.txt";
50 String resourceType = "static";
51 final ResourceTemplateWebPanel webPanel = newResourceTemplateWebPanel(resourceFilename, resourceType);
52
53 assertEquals(TEMPLATE_FILE_TEXT, webPanel.getHtml(Collections.<String, Object>emptyMap()));
54 }
55
56 public void testWriteHtml() throws IOException
57 {
58 ResourceTemplateWebPanel webPanel = newResourceTemplateWebPanel("ResourceTemplateWebPanelTest.txt", "static");
59
60 StringWriter out = new StringWriter();
61 webPanel.writeHtml(out, emptyContext);
62 assertEquals(TEMPLATE_FILE_TEXT, out.toString());
63 }
64
65 public void testErrorMessagesEscaped() throws IOException
66 {
67 WebPanelTestUtils.mockPluginAccessorReturning(accessorMock, rendererMock);
68 when(rendererMock.getResourceType()).thenReturn("cheese");
69
70 doThrow(new RendererException("<cheese>")).when(rendererMock).render(eq("<filename>.txt"), same(pluginMock), same(emptyContext), isA(Writer.class));
71
72 ResourceTemplateWebPanel webPanel = newResourceTemplateWebPanel("<filename>.txt", "cheese");
73 assertEquals("Error rendering WebPanel (<filename>.txt): <cheese>", webPanel.getHtml(emptyContext));
74 }
75
76 public void testErrorMessagesEscapedInWriteHtml() throws IOException
77 {
78 WebPanelTestUtils.mockPluginAccessorReturning(accessorMock, rendererMock);
79 when(rendererMock.getResourceType()).thenReturn("cheese");
80 doThrow(new RendererException("<cheese>")).when(rendererMock).render(eq("<filename>.txt"), same(pluginMock), same(emptyContext), isA(Writer.class));
81
82 ResourceTemplateWebPanel webPanel = newResourceTemplateWebPanel("<filename>.txt", "cheese");
83
84 StringWriter out = new StringWriter();
85 webPanel.writeHtml(out, emptyContext);
86 assertEquals("Error rendering WebPanel (<filename>.txt): <cheese>", out.toString());
87 }
88
89 public void testUnsupportedResourceType()
90 {
91 when(rendererMock.getResourceType()).thenReturn("velocity");
92 WebPanelTestUtils.mockPluginAccessorReturning(accessorMock, rendererMock);
93
94 ResourceTemplateWebPanel webPanel = newResourceTemplateWebPanel("ResourceTemplateWebPanelTest.txt", "unsupported-type");
95
96 String result = webPanel.getHtml(emptyContext);
97 assertEquals("Error rendering WebPanel (ResourceTemplateWebPanelTest.txt): No renderer found for resource type: unsupported-type", result);
98 }
99
100 private ResourceTemplateWebPanel newResourceTemplateWebPanel(String resourceFilename, String resourceType)
101 {
102 final ResourceTemplateWebPanel webPanel = new ResourceTemplateWebPanel(accessorMock);
103 webPanel.setPlugin(pluginMock);
104 webPanel.setResourceType(resourceType);
105 webPanel.setResourceFilename(resourceFilename);
106 return webPanel;
107 }
108 }