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