View Javadoc
1   package com.atlassian.plugin.servlet;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.atlassian.plugin.elements.ResourceLocation;
5   import org.junit.Before;
6   import org.junit.Rule;
7   import org.junit.Test;
8   import org.mockito.Mock;
9   import org.mockito.junit.MockitoJUnit;
10  import org.mockito.junit.MockitoRule;
11  
12  import javax.servlet.ServletContext;
13  import java.io.InputStream;
14  
15  import static org.hamcrest.CoreMatchers.equalTo;
16  import static org.hamcrest.MatcherAssert.assertThat;
17  import static org.mockito.Mockito.mock;
18  import static org.mockito.Mockito.when;
19  
20  public class TestDownloadableWebResource {
21      private DownloadableWebResource downloadableWebResource;
22  
23      @Rule
24      public final MockitoRule mockitoRule = MockitoJUnit.rule();
25  
26      @Mock
27      private Plugin plugin;
28      @Mock
29      private ResourceLocation resourceLocation;
30  
31      @Mock
32      private ServletContext servletContext;
33  
34      @Before
35      public void setUp() {
36          when(resourceLocation.getLocation()).thenReturn("resource/location");
37  
38          downloadableWebResource = new DownloadableWebResource(
39                  plugin,
40                  resourceLocation,
41                  "extraPath",
42                  servletContext,
43                  true); // disableMinification
44      }
45  
46      @Test
47      public void resourceLocationWithoutSlashInTheBeginningShouldBeFixedToHaveOne() {
48          final String resourceLocationWithoutSlash = "resource/without/slash/in/the/beginning";
49          final String fixedLocation = "/" + resourceLocationWithoutSlash;
50          testGetResourceAsStream(resourceLocationWithoutSlash, fixedLocation);
51      }
52  
53      @Test
54      public void resourceLocationWithSlashInTheBeginningShouldWorkUnchanged() {
55          final String resourceLocation = "/resource/with/slash/in/the/beginning";
56          testGetResourceAsStream(resourceLocation, resourceLocation);
57      }
58  
59      private void testGetResourceAsStream(final String passedResourceLocation, final String expectedLocation) {
60          final InputStream expectedStream = mock(InputStream.class);
61  
62          when(servletContext.getResourceAsStream(expectedLocation))
63                  .thenReturn(expectedStream);
64  
65          final InputStream resourceAsStream = downloadableWebResource.getResourceAsStream(passedResourceLocation);
66  
67          assertThat(resourceAsStream, equalTo(expectedStream));
68      }
69  }