From fa8fbea698122e9900ee71b523184b71b2cf7473 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 7 Sep 2012 14:44:26 +0200 Subject: [PATCH] Fix compatibility of rails upgrade with sonar-runner --- sonar-server/src/dev/web.xml | 19 ++++--- .../server/plugins/BatchResourcesServlet.java | 18 ++++--- .../plugins/BatchResourcesServletTest.java | 49 ++++++++++--------- 3 files changed, 48 insertions(+), 38 deletions(-) diff --git a/sonar-server/src/dev/web.xml b/sonar-server/src/dev/web.xml index b537e1dd48a..9fc07c9f20a 100644 --- a/sonar-server/src/dev/web.xml +++ b/sonar-server/src/dev/web.xml @@ -15,18 +15,27 @@ rails.env development - - jruby.min.runtimes 1 - jruby.max.runtimes 3 + + jruby.compat.version + 1.8 + + + jruby.rack.logging + slf4j + + + ServletFilters + org.sonar.server.platform.ServletFilters + DatabaseSessionFilter org.sonar.server.ui.DatabaseSessionFilter @@ -35,10 +44,6 @@ RackFilter org.jruby.rack.RackFilter - - ServletFilters - org.sonar.server.platform.ServletFilters - DatabaseSessionFilter diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/BatchResourcesServlet.java b/sonar-server/src/main/java/org/sonar/server/plugins/BatchResourcesServlet.java index 1751c240792..1106e80859c 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/BatchResourcesServlet.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/BatchResourcesServlet.java @@ -48,8 +48,8 @@ public class BatchResourcesServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String resource = getResource(request); - if (StringUtils.isEmpty(resource)) { + String filename = filename(request); + if (StringUtils.isBlank(filename)) { PrintWriter writer = null; try { response.setContentType("text/plain"); @@ -62,19 +62,19 @@ public class BatchResourcesServlet extends HttpServlet { IOUtils.closeQuietly(writer); } } else { - response.setContentType("application/java-archive"); InputStream in = null; OutputStream out = null; try { - in = getServletContext().getResourceAsStream("/WEB-INF/lib/" + resource); + in = getServletContext().getResourceAsStream("/WEB-INF/lib/" + filename); if (in == null) { // TODO } else { + response.setContentType("application/java-archive"); out = response.getOutputStream(); IOUtils.copy(in, out); } } catch (Exception e) { - LOG.error("Unable to load batch resource '" + resource + "'", e); + LOG.error("Unable to load batch resource '" + filename + "'", e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } finally { IOUtils.closeQuietly(in); @@ -115,8 +115,12 @@ public class BatchResourcesServlet extends HttpServlet { /** * @return part of request URL after servlet path */ - String getResource(HttpServletRequest request) { - return StringUtils.substringAfter(request.getRequestURI(), request.getContextPath() + request.getServletPath() + "/"); + String filename(HttpServletRequest request) { + String filename = null; + if (StringUtils.endsWithIgnoreCase(request.getRequestURI(), "jar")) { + filename = StringUtils.substringAfterLast(request.getRequestURI(), "/"); + } + return filename; } } diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/BatchResourcesServletTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/BatchResourcesServletTest.java index ea633db07f8..bdc12799ccc 100644 --- a/sonar-server/src/test/java/org/sonar/server/plugins/BatchResourcesServletTest.java +++ b/sonar-server/src/test/java/org/sonar/server/plugins/BatchResourcesServletTest.java @@ -23,18 +23,13 @@ import com.google.common.collect.Sets; import org.junit.Before; import org.junit.Test; -import java.util.Set; - import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; +import java.util.Set; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; public class BatchResourcesServletTest { private BatchResourcesServlet servlet; @@ -47,18 +42,24 @@ public class BatchResourcesServletTest { } @Test - public void shouldDetermineResource() { + public void test_filename() { when(request.getContextPath()).thenReturn("sonar"); when(request.getServletPath()).thenReturn("/batch"); when(request.getRequestURI()).thenReturn("/sonar/batch/sonar-core-2.6.jar"); - assertThat(servlet.getResource(request), is("sonar-core-2.6.jar")); + assertThat(servlet.filename(request)).isEqualTo("sonar-core-2.6.jar"); when(request.getRequestURI()).thenReturn("/sonar/batch/"); - assertThat(servlet.getResource(request), is("")); + assertThat(servlet.filename(request)).isNull(); when(request.getRequestURI()).thenReturn("/sonar/batch"); - assertThat(servlet.getResource(request), is("")); + assertThat(servlet.filename(request)).isNull(); + + when(request.getRequestURI()).thenReturn("/sonar/batch.html"); + assertThat(servlet.filename(request)).isNull(); + + when(request.getRequestURI()).thenReturn("/sonar/batch/index.html"); + assertThat(servlet.filename(request)).isNull(); } @Test @@ -72,21 +73,21 @@ public class BatchResourcesServletTest { libs.add("/WEB-INF/lib/directory/"); when(servletContext.getResourcePaths(anyString())).thenReturn(libs); - assertThat(servlet.getLibs().size(), is(1)); - assertThat(servlet.getLibs().get(0), is("sonar-core-2.6.jar")); + assertThat(servlet.getLibs()).hasSize(1); + assertThat(servlet.getLibs().get(0)).isEqualTo("sonar-core-2.6.jar"); } @Test public void shouldIgnore() { - assertThat(BatchResourcesServlet.isIgnored("sonar-batch-2.6-SNAPSHOT.jar"), is(false)); - assertThat(BatchResourcesServlet.isIgnored("h2-1.3.166.jar"), is(true)); - assertThat(BatchResourcesServlet.isIgnored("mysql-connector-java-5.1.13.jar"), is(true)); - assertThat(BatchResourcesServlet.isIgnored("postgresql-9.0-801.jdbc3.jar"), is(true)); - assertThat(BatchResourcesServlet.isIgnored("jtds-1.2.4.jar"), is(true)); - assertThat(BatchResourcesServlet.isIgnored("jfreechart-1.0.9.jar"), is(true)); - assertThat(BatchResourcesServlet.isIgnored("eastwood-1.1.0.jar"), is(true)); - assertThat(BatchResourcesServlet.isIgnored("jetty-util-6.1.24.jar"), is(true)); - assertThat(BatchResourcesServlet.isIgnored("jruby-complete-1.5.6.jar"), is(true)); - assertThat(BatchResourcesServlet.isIgnored("jruby-rack-1.0.5.jar"), is(true)); + assertThat(BatchResourcesServlet.isIgnored("sonar-batch-2.6-SNAPSHOT.jar")).isFalse(); + assertThat(BatchResourcesServlet.isIgnored("h2-1.3.166.jar")).isTrue(); + assertThat(BatchResourcesServlet.isIgnored("mysql-connector-java-5.1.13.jar")).isTrue(); + assertThat(BatchResourcesServlet.isIgnored("postgresql-9.0-801.jdbc3.jar")).isTrue(); + assertThat(BatchResourcesServlet.isIgnored("jtds-1.2.4.jar")).isTrue(); + assertThat(BatchResourcesServlet.isIgnored("jfreechart-1.0.9.jar")).isTrue(); + assertThat(BatchResourcesServlet.isIgnored("eastwood-1.1.0.jar")).isTrue(); + assertThat(BatchResourcesServlet.isIgnored("jetty-util-6.1.24.jar")).isTrue(); + assertThat(BatchResourcesServlet.isIgnored("jruby-complete-1.5.6.jar")).isTrue(); + assertThat(BatchResourcesServlet.isIgnored("jruby-rack-1.0.5.jar")).isTrue(); } } -- 2.39.5