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