View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import com.atlassian.plugin.PluginAccessor;
4   import com.atlassian.plugin.elements.ResourceLocation;
5   import com.atlassian.plugin.servlet.DownloadException;
6   import com.atlassian.plugin.servlet.DownloadableResource;
7   import com.atlassian.plugin.webresource.transformer.AbstractStringTransformedDownloadableResource;
8   import com.atlassian.plugin.webresource.transformer.WebResourceTransformer;
9   import com.atlassian.plugin.webresource.transformer.WebResourceTransformerModuleDescriptor;
10  import junit.framework.TestCase;
11  import org.apache.commons.io.IOUtils;
12  import org.dom4j.DocumentException;
13  import org.dom4j.DocumentHelper;
14  import org.dom4j.Element;
15  
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  import java.io.*;
19  import java.util.Arrays;
20  
21  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.verify;
23  import static org.mockito.Mockito.when;
24  
25  public class TestWebResourceTransformation extends TestCase
26  {
27      public void testMatches() throws DocumentException {
28          WebResourceTransformation trans = new WebResourceTransformation(DocumentHelper.parseText(
29                  "<transformation extension=\"js\">\n" +
30                      "<transformer key=\"foo\" />\n" +
31                  "</transformation>").getRootElement());
32          ResourceLocation loc = mock(ResourceLocation.class);
33          when(loc.getName()).thenReturn("foo.js");
34          assertTrue(trans.matches(loc));
35      }
36  
37      public void testNotMatches() throws DocumentException {
38          WebResourceTransformation trans = new WebResourceTransformation(DocumentHelper.parseText(
39                  "<transformation extension=\"js\">\n" +
40                      "<transformer key=\"foo\" />\n" +
41                  "</transformation>").getRootElement());
42          ResourceLocation loc = mock(ResourceLocation.class);
43          when(loc.getName()).thenReturn("foo.cs");
44          assertFalse(trans.matches(loc));
45      }
46  
47      public void testNoExtension() throws DocumentException {
48          try
49          {
50              new WebResourceTransformation(DocumentHelper.parseText(
51                      "<transformation>\n" +
52                              "<transformer key=\"foo\" />\n" +
53                              "</transformation>").getRootElement());
54              fail("Should have forced extension");
55          }
56          catch (IllegalArgumentException ex)
57          {
58              // pass
59          }
60      }
61  
62      public void testTransformDownloadableResource() throws DocumentException {
63          Element element = DocumentHelper.parseText(
64                  "<transformation extension=\"js\">\n" +
65                          "<transformer key=\"foo\" />\n" +
66                          "</transformation>").getRootElement();
67          WebResourceTransformation trans = new WebResourceTransformation(element);
68          PluginAccessor pluginAccessor = mock(PluginAccessor.class);
69          WebResourceTransformerModuleDescriptor descriptor = mock(WebResourceTransformerModuleDescriptor.class);
70          when(descriptor.getKey()).thenReturn("foo");
71          WebResourceTransformer transformer = mock(WebResourceTransformer.class);
72          when(descriptor.getModule()).thenReturn(transformer);
73          when(pluginAccessor.getEnabledModuleDescriptorsByClass(WebResourceTransformerModuleDescriptor.class)).thenReturn(
74                  Arrays.asList(descriptor));
75          ResourceLocation loc = mock(ResourceLocation.class);
76          when(loc.getName()).thenReturn("foo.js");
77  
78          DownloadableResource originalResource = mock(DownloadableResource.class);
79          DownloadableResource transResource = mock(DownloadableResource.class);
80          when(transformer.transform(element.element("transformer"), loc, "", originalResource)).thenReturn(transResource);
81  
82          DownloadableResource testResource = trans.transformDownloadableResource(pluginAccessor, originalResource, loc, "");
83          assertEquals(transResource, testResource);
84      }
85  
86      public void testTransformTwoDownloadableResources() throws DocumentException, DownloadException
87      {
88          Element element = DocumentHelper.parseText(
89                  "<transformation extension=\"js\">\n" +
90                          "<transformer key=\"foo\" />\n" +
91                          "<transformer key=\"bar\" />\n" +
92                          "</transformation>").getRootElement();
93          WebResourceTransformation trans = new WebResourceTransformation(element);
94          PluginAccessor pluginAccessor = mock(PluginAccessor.class);
95  
96          WebResourceTransformerModuleDescriptor fooDescriptor = createTransformer("foo");
97          WebResourceTransformerModuleDescriptor barDescriptor = createTransformer("bar");
98          when(pluginAccessor.getEnabledModuleDescriptorsByClass(WebResourceTransformerModuleDescriptor.class)).thenReturn(
99                  Arrays.asList(
100                         fooDescriptor,
101                         barDescriptor));
102         ResourceLocation loc = mock(ResourceLocation.class);
103         when(loc.getName()).thenReturn("foo.js");
104 
105         DownloadableResource originalResource = new StringDownloadableResource("resource");
106 
107         DownloadableResource testResource = trans.transformDownloadableResource(pluginAccessor, originalResource, loc, "");
108         ByteArrayOutputStream bout = new ByteArrayOutputStream();
109         testResource.streamResource(bout);
110         assertEquals("bar: foo: resource", new String(bout.toByteArray()));
111     }
112 
113     private WebResourceTransformerModuleDescriptor createTransformer(String key)
114     {
115         WebResourceTransformerModuleDescriptor descriptor = mock(WebResourceTransformerModuleDescriptor.class);
116         when(descriptor.getKey()).thenReturn(key);
117         WebResourceTransformer fooTransformer = new PrefixTransformer(key + ": ");
118         when(descriptor.getModule()).thenReturn(fooTransformer);
119         return descriptor;
120     }
121 
122     private static class PrefixTransformer implements WebResourceTransformer
123     {
124 
125         private final String prefix;
126 
127         public PrefixTransformer(String prefix)
128         {
129             this.prefix = prefix;
130         }
131 
132         public DownloadableResource transform(Element configElement, ResourceLocation location, String filePath, DownloadableResource nextResource)
133         {
134             return new AbstractStringTransformedDownloadableResource(nextResource)
135             {
136                 protected String transform(String originalContent)
137                 {
138                     return prefix + originalContent;
139                 }
140             };
141         }
142     }
143 
144     private static class StringDownloadableResource implements DownloadableResource
145     {
146         final String value;
147 
148         public StringDownloadableResource(String value)
149         {
150             this.value = value;
151         }
152 
153         public boolean isResourceModified(HttpServletRequest request, HttpServletResponse response)
154         {
155             return false;
156         }
157 
158         public void serveResource(HttpServletRequest request, HttpServletResponse response) throws DownloadException
159         {
160 
161         }
162 
163         public void streamResource(OutputStream out) throws DownloadException
164         {
165             try
166             {
167                 IOUtils.write(value, out);
168             }
169             catch (IOException e)
170             {
171                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
172             }
173         }
174 
175         public String getContentType()
176         {
177             return "text/plain";
178         }
179     }
180 
181 }