View Javadoc

1   package com.atlassian.plugin.webresource.transformer;
2   
3   import com.atlassian.plugin.servlet.DownloadException;
4   import com.atlassian.plugin.servlet.DownloadableResource;
5   
6   import com.google.common.base.Function;
7   
8   import java.io.IOException;
9   import java.io.OutputStream;
10  import java.nio.charset.Charset;
11  
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  
15  import com.google.common.io.CharStreams;
16  import junit.framework.TestCase;
17  
18  public class TestTransformerUtils extends TestCase
19  {
20      public void testTransform() throws Exception
21      {
22          final DownloadableResource resource = new MockDownloadableResource("hideho!");
23          final StringCapture capture = new StringCapture();
24          TransformerUtils.transformAndStreamResource(resource, Charset.forName("UTF-8"), capture.out(), new Function<CharSequence, CharSequence>()
25          {
26              public CharSequence apply(final CharSequence from)
27              {
28                  return "hoodeha?";
29              }
30          });
31          assertEquals("hoodeha?", capture.toString());
32      }
33  }
34  
35  class StringCapture
36  {
37      private final StringBuilder builder = new StringBuilder();
38  
39      OutputStream out()
40      {
41          return new WriterOutputStream(CharStreams.asWriter(builder));
42      }
43  
44      @Override
45      public String toString()
46      {
47          return builder.toString();
48      }
49  }
50  
51  class MockDownloadableResource implements DownloadableResource
52  {
53      private final String content;
54  
55      MockDownloadableResource(final String content)
56      {
57          this.content = content;
58      }
59  
60      public void streamResource(final OutputStream out) throws DownloadException
61      {
62          try
63          {
64              out.write(content.getBytes());
65          }
66          catch (final IOException e)
67          {
68              throw new DownloadException(e);
69          }
70      }
71  
72      public void serveResource(final HttpServletRequest request, final HttpServletResponse response) throws DownloadException
73      {}
74  
75      public boolean isResourceModified(final HttpServletRequest request, final HttpServletResponse response)
76      {
77          return false;
78      }
79  
80      public String getContentType()
81      {
82          return null;
83      }
84  
85  }