]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1709: Add unit test and improve logging for StaticResourcesServlet
authorGodin <mandrikov@gmail.com>
Mon, 27 Sep 2010 13:27:50 +0000 (13:27 +0000)
committerGodin <mandrikov@gmail.com>
Mon, 27 Sep 2010 13:27:50 +0000 (13:27 +0000)
sonar-server/src/main/java/org/sonar/server/plugins/StaticResourcesServlet.java
sonar-server/src/test/java/org/sonar/server/plugins/StaticResourcesServletTest.java [new file with mode: 0644]

index 4864f895e491c60c7579b90cf24121b6acff9917..5b5366a51c2d1711677915f740976f69845324ac 100644 (file)
@@ -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 (file)
index 0000000..e10a628
--- /dev/null
@@ -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/"));
+  }
+}