Search in sources :

Example 6 with RequestData

use of org.apache.sling.engine.impl.request.RequestData in project sling by apache.

the class RequestProcessorMBeanImplTest method test_statistics.

/**
     * Asserts that the simple standard deviation algorithm used by the
     * RequestProcessorMBeanImpl is equivalent to the Commons Math
     * SummaryStatistics implementation.
     * 
     * It also tests that resetStatistics method, actually resets all the statistics
     *
     * @throws NotCompliantMBeanException not expected
     */
@Test
public void test_statistics() throws NotCompliantMBeanException {
    final SummaryStatistics durationStats = new SummaryStatistics();
    final SummaryStatistics servletCallCountStats = new SummaryStatistics();
    final SummaryStatistics peakRecursionDepthStats = new SummaryStatistics();
    final RequestProcessorMBeanImpl bean = new RequestProcessorMBeanImpl();
    assertEquals(0l, bean.getRequestsCount());
    assertEquals(Long.MAX_VALUE, bean.getMinRequestDurationMsec());
    assertEquals(0l, bean.getMaxRequestDurationMsec());
    assertEquals(0.0, bean.getMeanRequestDurationMsec(), 0);
    assertEquals(0.0, bean.getStandardDeviationDurationMsec(), 0);
    assertEquals(Integer.MAX_VALUE, bean.getMinServletCallCount());
    assertEquals(0l, bean.getMaxServletCallCount());
    assertEquals(0.0, bean.getMeanServletCallCount(), 0);
    assertEquals(0.0, bean.getStandardDeviationServletCallCount(), 0);
    assertEquals(Integer.MAX_VALUE, bean.getMinPeakRecursionDepth());
    assertEquals(0l, bean.getMaxPeakRecursionDepth());
    assertEquals(0.0, bean.getMeanPeakRecursionDepth(), 0);
    assertEquals(0.0, bean.getStandardDeviationPeakRecursionDepth(), 0);
    final Random random = new Random(System.currentTimeMillis() / 17);
    final int num = 10000;
    final int min = 85;
    final int max = 250;
    for (int i = 0; i < num; i++) {
        final long durationValue = min + random.nextInt(max - min);
        final int callCountValue = min + random.nextInt(max - min);
        final int peakRecursionDepthValue = min + random.nextInt(max - min);
        durationStats.addValue(durationValue);
        servletCallCountStats.addValue(callCountValue);
        peakRecursionDepthStats.addValue(peakRecursionDepthValue);
        final RequestData requestData = context.mock(RequestData.class, "requestData" + i);
        context.checking(new Expectations() {

            {
                one(requestData).getElapsedTimeMsec();
                will(returnValue(durationValue));
                one(requestData).getServletCallCount();
                will(returnValue(callCountValue));
                one(requestData).getPeakRecusionDepth();
                will(returnValue(peakRecursionDepthValue));
            }
        });
        bean.addRequestData(requestData);
    }
    assertEquals("Number of points must be the same", durationStats.getN(), bean.getRequestsCount());
    assertEquals("Min Duration must be equal", (long) durationStats.getMin(), bean.getMinRequestDurationMsec());
    assertEquals("Max Duration must be equal", (long) durationStats.getMax(), bean.getMaxRequestDurationMsec());
    assertAlmostEqual("Mean Duration", durationStats.getMean(), bean.getMeanRequestDurationMsec(), num);
    assertAlmostEqual("Standard Deviation Duration", durationStats.getStandardDeviation(), bean.getStandardDeviationDurationMsec(), num);
    assertEquals("Min Servlet Call Count must be equal", (long) servletCallCountStats.getMin(), bean.getMinServletCallCount());
    assertEquals("Max Servlet Call Count must be equal", (long) servletCallCountStats.getMax(), bean.getMaxServletCallCount());
    assertAlmostEqual("Mean Servlet Call Count", servletCallCountStats.getMean(), bean.getMeanServletCallCount(), num);
    assertAlmostEqual("Standard Deviation Servlet Call Count", servletCallCountStats.getStandardDeviation(), bean.getStandardDeviationServletCallCount(), num);
    assertEquals("Min Peak Recursion Depth must be equal", (long) peakRecursionDepthStats.getMin(), bean.getMinPeakRecursionDepth());
    assertEquals("Max Peak Recursion Depth must be equal", (long) peakRecursionDepthStats.getMax(), bean.getMaxPeakRecursionDepth());
    assertAlmostEqual("Mean Peak Recursion Depth", peakRecursionDepthStats.getMean(), bean.getMeanPeakRecursionDepth(), num);
    assertAlmostEqual("Standard Deviation Peak Recursion Depth", peakRecursionDepthStats.getStandardDeviation(), bean.getStandardDeviationPeakRecursionDepth(), num);
    //check method resetStatistics 
    //In the previous test, some requests have been processed, now we reset the statistics so everything statistic is reinitialized
    bean.resetStatistics();
    //Simulate a single request 
    final long durationValue = min + random.nextInt(max - min);
    final int callCountValue = min + random.nextInt(max - min);
    final int peakRecursionDepthValue = min + random.nextInt(max - min);
    final RequestData requestData = context.mock(RequestData.class, "requestDataAfterReset");
    context.checking(new Expectations() {

        {
            one(requestData).getElapsedTimeMsec();
            will(returnValue(durationValue));
            one(requestData).getServletCallCount();
            will(returnValue(callCountValue));
            one(requestData).getPeakRecusionDepth();
            will(returnValue(peakRecursionDepthValue));
        }
    });
    bean.addRequestData(requestData);
    //As only one request has been simulated since resetStatiscts: min, max and mean statistics should be equals to the request data
    assertEquals("After resetStatistics Number of requests must be one", 1, bean.getRequestsCount());
    assertEquals("After resetStatistics Min Duration must be equal", bean.getMinRequestDurationMsec(), (long) durationValue);
    assertEquals("After resetStatistics Max Duration must be equal", bean.getMaxRequestDurationMsec(), (long) durationValue);
    assertEquals("After resetStatistics Mean Duration must be equal", bean.getMeanRequestDurationMsec(), (double) durationValue, 0d);
    assertEquals("After resetStatistics Min Servlet Call Count must be equal", bean.getMinServletCallCount(), callCountValue);
    assertEquals("After resetStatistics Max Servlet Call Count must be equal", bean.getMaxServletCallCount(), callCountValue);
    assertEquals("After resetStatistics Mean Servlet Call Count", bean.getMeanServletCallCount(), (double) callCountValue, 0d);
    assertEquals("After resetStatistics Min Peak Recursion Depth must be equal", bean.getMinPeakRecursionDepth(), peakRecursionDepthValue);
    assertEquals("After resetStatistics Max Peak Recursion Depth must be equal", bean.getMinPeakRecursionDepth(), peakRecursionDepthValue);
    assertEquals("After resetStatistics Mean Peak Recursion Depth", bean.getMeanPeakRecursionDepth(), (double) peakRecursionDepthValue, 0d);
}
Also used : Expectations(org.jmock.Expectations) Random(java.util.Random) RequestData(org.apache.sling.engine.impl.request.RequestData) SummaryStatistics(org.apache.commons.math.stat.descriptive.SummaryStatistics) Test(org.junit.Test)

Example 7 with RequestData

use of org.apache.sling.engine.impl.request.RequestData in project sling by apache.

the class SlingHttpServletRequestImplTest method getUserPrincipal_test3.

@Test
public void getUserPrincipal_test3() {
    final HttpServletRequest servletRequest = context.mock(HttpServletRequest.class);
    context.checking(new Expectations() {

        {
            one(servletRequest).getServletPath();
            will(returnValue("/path"));
            allowing(servletRequest).getPathInfo();
            will(returnValue("/path"));
        }
    });
    final RequestData requestData = context.mock(RequestData.class, "requestData");
    final ResourceResolver resourceResolver = context.mock(ResourceResolver.class);
    final Principal principal = context.mock(Principal.class);
    context.checking(new Expectations() {

        {
            allowing(requestData).getResourceResolver();
            will(returnValue(resourceResolver));
            allowing(resourceResolver).adaptTo(Principal.class);
            will(returnValue(principal));
        }
    });
    slingHttpServletRequestImpl = new SlingHttpServletRequestImpl(requestData, servletRequest);
    Assert.assertEquals(principal, slingHttpServletRequestImpl.getUserPrincipal());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Expectations(org.jmock.Expectations) RequestData(org.apache.sling.engine.impl.request.RequestData) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Principal(java.security.Principal) Test(org.junit.Test)

Example 8 with RequestData

use of org.apache.sling.engine.impl.request.RequestData in project sling by apache.

the class SlingHttpServletRequestImplTest method getUserPrincipal_test.

@Test
public void getUserPrincipal_test() {
    final HttpServletRequest servletRequest = context.mock(HttpServletRequest.class);
    context.checking(new Expectations() {

        {
            one(servletRequest).getServletPath();
            will(returnValue("/path"));
            allowing(servletRequest).getPathInfo();
            will(returnValue("/path"));
            allowing(servletRequest).getRemoteUser();
            will(returnValue("remoteUser"));
        }
    });
    final RequestData requestData = context.mock(RequestData.class, "requestData");
    final ResourceResolver resourceResolver = context.mock(ResourceResolver.class);
    context.checking(new Expectations() {

        {
            allowing(requestData).getResourceResolver();
            will(returnValue(resourceResolver));
            allowing(resourceResolver).adaptTo(Principal.class);
            will(returnValue(null));
        }
    });
    slingHttpServletRequestImpl = new SlingHttpServletRequestImpl(requestData, servletRequest);
    Assert.assertEquals("UserPrincipal: remoteUser", slingHttpServletRequestImpl.getUserPrincipal().toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Expectations(org.jmock.Expectations) RequestData(org.apache.sling.engine.impl.request.RequestData) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Principal(java.security.Principal) Test(org.junit.Test)

Example 9 with RequestData

use of org.apache.sling.engine.impl.request.RequestData in project sling by apache.

the class SlingRequestProcessorImpl method dispatchRequest.

// ---------- Generic Content Request processor ----------------------------
/**
     * Dispatches the request on behalf of the
     * {@link org.apache.sling.engine.impl.request.SlingRequestDispatcher}.
     */
public void dispatchRequest(ServletRequest request, ServletResponse response, Resource resource, RequestPathInfo resolvedURL, boolean include) throws IOException, ServletException {
    // we need a SlingHttpServletRequest/SlingHttpServletResponse tupel
    // to continue
    SlingHttpServletRequest cRequest = RequestData.toSlingHttpServletRequest(request);
    SlingHttpServletResponse cResponse = RequestData.toSlingHttpServletResponse(response);
    // get the request data (and btw check the correct type)
    final RequestData requestData = RequestData.getRequestData(cRequest);
    final ContentData oldContentData = requestData.getContentData();
    final ContentData contentData = requestData.setContent(resource, resolvedURL);
    try {
        // resolve the servlet
        Servlet servlet = servletResolver.resolveServlet(cRequest);
        contentData.setServlet(servlet);
        FilterChainType type = include ? FilterChainType.INCLUDE : FilterChainType.FORWARD;
        processComponent(cRequest, cResponse, type);
    } finally {
        requestData.resetContent(oldContentData);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ContentData(org.apache.sling.engine.impl.request.ContentData) RequestData(org.apache.sling.engine.impl.request.RequestData) Servlet(javax.servlet.Servlet) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) FilterChainType(org.apache.sling.engine.impl.filter.ServletFilterManager.FilterChainType)

Aggregations

RequestData (org.apache.sling.engine.impl.request.RequestData)9 Expectations (org.jmock.Expectations)4 Test (org.junit.Test)4 Principal (java.security.Principal)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)2 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)2 RequestProgressTracker (org.apache.sling.api.request.RequestProgressTracker)2 IOException (java.io.IOException)1 AccessControlException (java.security.AccessControlException)1 Random (java.util.Random)1 FilterChain (javax.servlet.FilterChain)1 Servlet (javax.servlet.Servlet)1 UnavailableException (javax.servlet.UnavailableException)1 SummaryStatistics (org.apache.commons.math.stat.descriptive.SummaryStatistics)1 SlingException (org.apache.sling.api.SlingException)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)1 ServletResolver (org.apache.sling.api.servlets.ServletResolver)1