aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server
diff options
context:
space:
mode:
authorGodin <mandrikov@gmail.com>2010-09-27 13:27:50 +0000
committerGodin <mandrikov@gmail.com>2010-09-27 13:27:50 +0000
commitf854c3c8f0c684e27e2543425ae5a1b60f4ad295 (patch)
treea5ca5e10dee8b56307e412408f325e344bfac7cf /sonar-server
parent6ab9363b7bc65621673bbf0fda658536e0aaf73a (diff)
downloadsonarqube-f854c3c8f0c684e27e2543425ae5a1b60f4ad295.tar.gz
sonarqube-f854c3c8f0c684e27e2543425ae5a1b60f4ad295.zip
SONAR-1709: Add unit test and improve logging for StaticResourcesServlet
Diffstat (limited to 'sonar-server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/plugins/StaticResourcesServlet.java25
-rw-r--r--sonar-server/src/test/java/org/sonar/server/plugins/StaticResourcesServletTest.java51
2 files changed, 71 insertions, 5 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/StaticResourcesServlet.java b/sonar-server/src/main/java/org/sonar/server/plugins/StaticResourcesServlet.java
index 4864f895e49..5b5366a51c2 100644
--- a/sonar-server/src/main/java/org/sonar/server/plugins/StaticResourcesServlet.java
+++ b/sonar-server/src/main/java/org/sonar/server/plugins/StaticResourcesServlet.java
@@ -22,17 +22,15 @@ public class StaticResourcesServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
- String path = StringUtils.substringAfter(request.getRequestURI(), request.getContextPath() + request.getServletPath() + "/");
- String pluginKey = StringUtils.substringBefore(path, "/");
- String resource = "/static/" + StringUtils.substringAfter(path, "/");
+ String pluginKey = getPluginKey(request);
+ String resource = getResourcePath(request);
PluginClassLoaders pluginClassLoaders = Platform.getInstance().getContainer().getComponent(PluginClassLoaders.class);
-
ClassLoader classLoader = pluginClassLoaders.getClassLoader(pluginKey);
if (classLoader == null) {
+ LOG.error("Plugin not found: " + pluginKey);
return;
}
-
InputStream in = null;
OutputStream out = null;
try {
@@ -40,6 +38,8 @@ public class StaticResourcesServlet extends HttpServlet {
if (in != null) {
out = response.getOutputStream();
IOUtils.copy(in, out);
+ } else {
+ LOG.error("Unable to find resource '" + resource + "' in plugin '" + pluginKey + "'");
}
} catch (Exception e) {
LOG.error("Unable to load static resource '" + resource + "' from plugin '" + pluginKey + "'", e);
@@ -48,4 +48,19 @@ public class StaticResourcesServlet extends HttpServlet {
IOUtils.closeQuietly(out);
}
}
+
+ /**
+ * @return part of request URL after servlet path
+ */
+ protected String getPluginKeyAndResourcePath(HttpServletRequest request) {
+ return StringUtils.substringAfter(request.getRequestURI(), request.getContextPath() + request.getServletPath() + "/");
+ }
+
+ protected String getPluginKey(HttpServletRequest request) {
+ return StringUtils.substringBefore(getPluginKeyAndResourcePath(request), "/");
+ }
+
+ protected String getResourcePath(HttpServletRequest request) {
+ return "/static/" + StringUtils.substringAfter(getPluginKeyAndResourcePath(request), "/");
+ }
}
diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/StaticResourcesServletTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/StaticResourcesServletTest.java
new file mode 100644
index 00000000000..e10a628ae5d
--- /dev/null
+++ b/sonar-server/src/test/java/org/sonar/server/plugins/StaticResourcesServletTest.java
@@ -0,0 +1,51 @@
+package org.sonar.server.plugins;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.servlet.http.HttpServletRequest;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class StaticResourcesServletTest {
+
+ private StaticResourcesServlet servlet;
+ private HttpServletRequest request;
+
+ @Before
+ public void setUp() throws Exception {
+ servlet = new StaticResourcesServlet();
+ request = mock(HttpServletRequest.class);
+ }
+
+ @Test
+ public void shouldDeterminePluginKey() {
+ when(request.getContextPath()).thenReturn("/");
+ when(request.getServletPath()).thenReturn("static");
+ when(request.getRequestURI()).thenReturn("/static/myplugin/image.png");
+ assertThat(servlet.getPluginKey(request), is("myplugin"));
+
+ when(request.getRequestURI()).thenReturn("/static/myplugin/images/image.png");
+ assertThat(servlet.getPluginKey(request), is("myplugin"));
+
+ when(request.getRequestURI()).thenReturn("/static/myplugin/");
+ assertThat(servlet.getPluginKey(request), is("myplugin"));
+ }
+
+ @Test
+ public void shouldDetermineResourcePath() {
+ when(request.getContextPath()).thenReturn("/");
+ when(request.getServletPath()).thenReturn("static");
+ when(request.getRequestURI()).thenReturn("/static/myplugin/image.png");
+ assertThat(servlet.getResourcePath(request), is("/static/image.png"));
+
+ when(request.getRequestURI()).thenReturn("/static/myplugin/images/image.png");
+ assertThat(servlet.getResourcePath(request), is("/static/images/image.png"));
+
+ when(request.getRequestURI()).thenReturn("/static/myplugin/");
+ assertThat(servlet.getResourcePath(request), is("/static/"));
+ }
+}