Search in sources :

Example 6 with MockResource

use of org.apache.sling.commons.testing.sling.MockResource in project sling by apache.

the class ScriptSelectionTest method assertScript.

/** Given a list of available scripts and the request method, selectors
     *  and extension, check that the expected script is selected.
     *  The resource type is foo:bar, set by HelperTestBase
     *
     *  @param method the HTTP method of the simulated request
     *  @param selectors the selectors of the simulated request
     *  @param extension the extension of the simulated request
     *  @param scripts the list of scripts that would be available in the repository
     *  @param expectedScript the script that we expect to be selected
     */
protected void assertScript(String method, String selectors, String extension, String[] scripts, String expectedScript) {
    // Add given scripts to our mock resource resolver
    for (String script : scripts) {
        final MockResource r = new MockResource(resourceResolver, script, "nt:file");
        resourceResolver.addResource(r);
    }
    // Create mock request and get scripts from ResourceCollector
    final MockSlingHttpServletRequest req = makeRequest(method, selectors, extension);
    final ResourceCollector u = ResourceCollector.create(req, null, new String[] { "html" });
    final Collection<Resource> s = u.getServlets(req.getResourceResolver());
    if (expectedScript == null) {
        assertFalse("No script must be found", s.iterator().hasNext());
    } else {
        // Verify that the expected script is the first in the list of candidates
        assertTrue("A script must be found", s.iterator().hasNext());
        final String scriptPath = s.iterator().next().getPath();
        assertEquals("First script is the expected one", expectedScript, scriptPath);
    }
}
Also used : MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest) MockResource(org.apache.sling.commons.testing.sling.MockResource) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.commons.testing.sling.MockResource)

Example 7 with MockResource

use of org.apache.sling.commons.testing.sling.MockResource in project sling by apache.

the class ResourceIteratorInputStreamTest method test.

@Test
public void test() throws IOException {
    List<Resource> resources = new ArrayList<Resource>();
    for (int i = 0; i < 10; i++) {
        final int initialState = i;
        final InputStream in = new InputStream() {

            private int state = initialState;

            @Override
            public int read() throws IOException {
                return state--;
            }
        };
        resources.add(new MockResource(null, null, null) {

            @Override
            public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
                if (InputStream.class.equals(type)) {
                    return (AdapterType) in;
                }
                return super.adaptTo(type);
            }

            @Override
            public String getName() {
                return "chunk-" + (initialState * 100) + "-" + (((initialState + 1) * 100) - 1);
            }
        });
    }
    ResourceIteratorInputStream resourceIteratorInputStream = new ResourceIteratorInputStream(resources.iterator());
    int expected = 0;
    int cycle = 0;
    for (int i = resourceIteratorInputStream.read(); i >= 0; i = resourceIteratorInputStream.read()) {
        Assert.assertEquals(expected, i);
        if (expected == 0) {
            cycle++;
            expected = cycle;
        } else {
            expected--;
        }
    }
    Assert.assertEquals(10, cycle);
}
Also used : InputStream(java.io.InputStream) MockResource(org.apache.sling.commons.testing.sling.MockResource) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.commons.testing.sling.MockResource) Test(org.junit.Test)

Example 8 with MockResource

use of org.apache.sling.commons.testing.sling.MockResource in project sling by apache.

the class HelperTestBase method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    resourceResolver = new MockResourceResolver();
    resourceResolver.setSearchPath("/apps", "/libs");
    resourceType = "foo:bar";
    resourceTypePath = ResourceUtil.resourceTypeToPath(resourceType);
    resourcePath = "/content/page";
    resource = new MockResource(resourceResolver, resourcePath, resourceType);
    resourceResolver.addResource(resource);
    request = makeRequest("GET", "print.a4", "html");
}
Also used : MockResource(org.apache.sling.commons.testing.sling.MockResource) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver)

Example 9 with MockResource

use of org.apache.sling.commons.testing.sling.MockResource in project sling by apache.

the class LocationIteratorTest method testCircularResourceTypeHierarchy.

public void testCircularResourceTypeHierarchy() {
    final String root1 = "/libs";
    resourceResolver.setSearchPath(root1);
    // resource type and super type for start resource
    final String resourceType = "foo/bar";
    final String resourceSuperType = "foo/check1";
    final String resourceSuperType2 = "foo/check2";
    final Resource resource2 = new MockResource(resourceResolver, root1 + '/' + resourceSuperType, resourceType, resourceSuperType2);
    resourceResolver.addResource(resource2);
    final Resource resource3 = new MockResource(resourceResolver, root1 + '/' + resourceSuperType2, resourceType, resourceType);
    resourceResolver.addResource(resource3);
    LocationIterator li = getLocationIterator(resourceType, resourceSuperType);
    // 1. /libs/foo/bar
    assertTrue(li.hasNext());
    assertEquals(root1 + '/' + resourceType, li.next());
    // 1. /libs/foo/check1
    assertTrue(li.hasNext());
    assertEquals(root1 + "/" + resourceSuperType, li.next());
    // 3. /libs/foo/check2
    assertTrue(li.hasNext());
    assertEquals(root1 + "/" + resourceSuperType2, li.next());
    // 4. /libs/sling/servlet/default
    assertTrue(li.hasNext());
    assertEquals(root1 + "/" + DEFAULT_RESOURCE_TYPE, li.next());
    // 5. finished
    assertFalse(li.hasNext());
}
Also used : MockResource(org.apache.sling.commons.testing.sling.MockResource) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.commons.testing.sling.MockResource)

Example 10 with MockResource

use of org.apache.sling.commons.testing.sling.MockResource in project sling by apache.

the class RequestUtilTest method getMockRequest.

protected SlingHttpServletRequest getMockRequest(final long modificationTime, final long ifModifiedSince) {
    final String resourcePath = "foo";
    final MockSlingHttpServletRequest r = new MockSlingHttpServletRequest(resourcePath, null, null, null, null) {

        @Override
        public long getDateHeader(String name) {
            return ifModifiedSince;
        }
    };
    final String path = "/foo/node";
    final MockResource mr = new MockResource(null, path, null) {
    };
    mr.getResourceMetadata().setModificationTime(modificationTime);
    r.setResource(mr);
    return r;
}
Also used : MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest) MockResource(org.apache.sling.commons.testing.sling.MockResource)

Aggregations

MockResource (org.apache.sling.commons.testing.sling.MockResource)13 MockResourceResolver (org.apache.sling.commons.testing.sling.MockResourceResolver)8 Resource (org.apache.sling.api.resource.Resource)6 Before (org.junit.Before)6 ArrayList (java.util.ArrayList)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 MockSlingHttpServletRequest (org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest)2 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Date (java.util.Date)1 Dictionary (java.util.Dictionary)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 LoginException (org.apache.sling.api.resource.LoginException)1 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)1 MockBundle (org.apache.sling.commons.testing.osgi.MockBundle)1 MockBundleContext (org.apache.sling.commons.testing.osgi.MockBundleContext)1 MockServiceReference (org.apache.sling.commons.testing.osgi.MockServiceReference)1