1 package com.atlassian.plugin.servlet.download.plugin;
2
3 import com.atlassian.plugin.ModuleDescriptor;
4 import com.atlassian.plugin.event.events.PluginModuleDisabledEvent;
5 import com.atlassian.plugin.event.events.PluginModuleEnabledEvent;
6 import com.atlassian.plugin.event.impl.DefaultPluginEventManager;
7 import com.atlassian.plugin.hostcontainer.DefaultHostContainer;
8 import com.atlassian.plugin.module.ClassPrefixModuleFactory;
9 import com.atlassian.plugin.module.ModuleFactory;
10 import com.atlassian.plugin.module.PrefixDelegatingModuleFactory;
11 import com.atlassian.plugin.servlet.DownloadException;
12 import com.atlassian.plugin.servlet.DownloadStrategy;
13 import com.atlassian.plugin.servlet.util.CapturingHttpServletResponse;
14 import org.junit.Before;
15 import org.junit.Test;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19 import java.io.IOException;
20 import java.util.Collections;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 public class TestPluggableDownloadStrategy {
29
30 private PluggableDownloadStrategy strategy;
31
32 @Before
33 public void setUp() {
34 strategy = new PluggableDownloadStrategy(new DefaultPluginEventManager());
35 }
36
37 @Test
38 public void testRegister() throws Exception {
39 strategy.register("monkey.key", new StubDownloadStrategy("/monkey", "Bananas"));
40
41 assertTrue(strategy.matches("/monkey/something"));
42
43 CapturingHttpServletResponse response = new CapturingHttpServletResponse();
44 HttpServletRequest request = mock(HttpServletRequest.class);
45 when(request.getRequestURI()).thenReturn("/monkey/something");
46
47 strategy.serveFile(request, response);
48 assertEquals("Bananas\r\n", response.toString());
49 }
50
51 @Test
52 public void testUnregister() {
53 strategy.register("monkey.key", new StubDownloadStrategy("/monkey", "Bananas"));
54 strategy.unregister("monkey.key");
55
56 assertFalse(strategy.matches("/monkey/something"));
57 }
58
59 @Test
60 public void testPluginModuleEnabled() throws Exception {
61 ModuleDescriptor module = new DownloadStrategyModuleDescriptor(getDefaultModuleClassFactory()) {
62 public String getCompleteKey() {
63 return "jungle.plugin:lion-strategy";
64 }
65
66 public DownloadStrategy getModule() {
67 return new StubDownloadStrategy("/lion", "ROAR!");
68 }
69 };
70
71 strategy.pluginModuleEnabled(new PluginModuleEnabledEvent(module));
72
73 assertTrue(strategy.matches("/lion/something"));
74
75 CapturingHttpServletResponse response = new CapturingHttpServletResponse();
76 HttpServletRequest request = mock(HttpServletRequest.class);
77 when(request.getRequestURI()).thenReturn("/lion/something");
78
79 strategy.serveFile(request, response);
80 assertEquals("ROAR!\r\n", response.toString());
81 }
82
83 @Test
84 public void testPluginModuleDisabled() {
85 ModuleDescriptor module = new DownloadStrategyModuleDescriptor(getDefaultModuleClassFactory()) {
86 public String getCompleteKey() {
87 return "jungle.plugin:lion-strategy";
88 }
89
90 public DownloadStrategy getModule() {
91 return new StubDownloadStrategy("/lion", "ROAR!");
92 }
93 };
94
95 strategy.pluginModuleEnabled(new PluginModuleEnabledEvent(module));
96 assertTrue(strategy.matches("/lion/something"));
97
98 strategy.pluginModuleDisabled(new PluginModuleDisabledEvent(module, true));
99 assertFalse(strategy.matches("/lion/something"));
100 }
101
102 @Test
103 public void testUnregisterPluginModule() {
104 ModuleDescriptor module = new DownloadStrategyModuleDescriptor(getDefaultModuleClassFactory()) {
105 public String getCompleteKey() {
106 return "jungle.plugin:lion-strategy";
107 }
108
109 public DownloadStrategy getModule() {
110 return new StubDownloadStrategy("/lion", "ROAR!");
111 }
112 };
113
114 strategy.pluginModuleEnabled(new PluginModuleEnabledEvent(module));
115 assertTrue(strategy.matches("/lion/something"));
116
117 strategy.unregister("jungle.plugin:lion-strategy");
118 assertFalse(strategy.matches("/lion/something"));
119 }
120
121 private ModuleFactory getDefaultModuleClassFactory() {
122 return new PrefixDelegatingModuleFactory(
123 Collections.singleton(new ClassPrefixModuleFactory(new DefaultHostContainer())));
124 }
125
126 private static class StubDownloadStrategy implements DownloadStrategy {
127
128 private final String urlPattern;
129 private final String output;
130
131 StubDownloadStrategy(String urlPattern, String output) {
132 this.urlPattern = urlPattern;
133 this.output = output;
134 }
135
136 public boolean matches(String urlPath) {
137 return urlPath.contains(urlPattern);
138 }
139
140 public void serveFile(HttpServletRequest request, HttpServletResponse response) throws DownloadException {
141 try {
142 response.getOutputStream().println(output);
143 } catch (IOException e) {
144 throw new DownloadException(e);
145 }
146 }
147 }
148 }