Search in sources :

Example 1 with MockResourceResolver

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

the class TestAdaptObjectTag method init.

/**
	 * Initializes the fields for this test.
	 */
@SuppressWarnings("serial")
@Before
public void init() {
    log.info("init");
    adaptToTag = new AdaptToTag() {

        protected ClassLoader getClassLoader() {
            return TestAdaptObjectTag.class.getClassLoader();
        }
    };
    pageContext = new MockPageContext();
    adaptToTag.setPageContext(pageContext);
    ResourceResolver resolver = new MockResourceResolver();
    resource = new MockResource(resolver, "/", "test");
    log.info("init Complete");
}
Also used : MockResource(org.apache.sling.commons.testing.sling.MockResource) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) Before(org.junit.Before)

Example 2 with MockResourceResolver

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

the class TestGetResourceTag method init.

/**
	 * Initializes the fields for this test.
	 */
@SuppressWarnings("serial")
@Before
public void init() {
    log.info("init");
    final MockResourceResolver resolver = new MockResourceResolver();
    resource = new MockResource(resolver, TEST_ABSOLUTE_PATH, "test");
    resolver.addResource(resource);
    MockResource child = new MockResource(resolver, TEST_ABSOLUTE_PATH + "/" + TEST_RELATIVE_PATH, "test");
    resolver.addResource(child);
    getResourceTag = new GetResourceTag() {

        protected ResourceResolver getResourceResolver() {
            return resolver;
        }
    };
    pageContext = new MockPageContext();
    getResourceTag.setPageContext(pageContext);
    log.info("init Complete");
}
Also used : MockResource(org.apache.sling.commons.testing.sling.MockResource) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) Before(org.junit.Before)

Example 3 with MockResourceResolver

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

the class TestSlingFunctions method init.

/**
	 * Initializes the fields for this test.
	 */
@Before
public void init() {
    log.info("init");
    resolver = new MockResourceResolver() {

        @Override
        public Iterator<Resource> findResources(String query, String language) {
            if (query.equals("query") && language.equals("language")) {
                List<Resource> resources = new ArrayList<Resource>();
                resources.add(resource);
                return resources.iterator();
            } else {
                return null;
            }
        }
    };
    resource = new MockResource(resolver, TEST_PATH, "test");
    this.date = new Date();
    resource.addProperty("date", date);
    resource.addProperty("long", new Long(0L));
    resolver.addResource(resource);
    MockResource child1 = new MockResource(resolver, TEST_PATH + "/child1", "test");
    resolver.addResource(child1);
    MockResource child2 = new MockResource(resolver, TEST_PATH + "/child2", "test");
    resolver.addResource(child2);
    log.info("init Complete");
}
Also used : MockResource(org.apache.sling.commons.testing.sling.MockResource) Iterator(java.util.Iterator) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.commons.testing.sling.MockResource) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) ArrayList(java.util.ArrayList) List(java.util.List) Date(java.util.Date) Before(org.junit.Before)

Example 4 with MockResourceResolver

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

the class SlingServletResolverTest method setUp.

@Before
public void setUp() throws Exception {
    mockResourceResolver = new MockResourceResolver() {

        @Override
        public void close() {
        // nothing to do;
        }

        @Override
        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
            return null;
        }

        @Override
        public ResourceResolver clone(Map<String, Object> authenticationInfo) throws LoginException {
            throw new LoginException("MockResourceResolver can't be cloned - excepted for this test!");
        }

        @Override
        public void refresh() {
        // nothing to do
        }
    };
    mockResourceResolver.setSearchPath("/");
    final ResourceResolverFactory factory = new ResourceResolverFactory() {

        @Override
        public ResourceResolver getAdministrativeResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
            return mockResourceResolver;
        }

        @Override
        public ResourceResolver getResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
            return mockResourceResolver;
        }

        @Override
        public ResourceResolver getServiceResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
            return mockResourceResolver;
        }

        @Override
        public ResourceResolver getThreadResourceResolver() {
            // TODO Auto-generated method stub
            return null;
        }
    };
    servlet = new MockSlingRequestHandlerServlet();
    servletResolver = new SlingServletResolver();
    Class<?> resolverClass = servletResolver.getClass();
    // set resource resolver factory
    final Field resolverField = resolverClass.getDeclaredField("resourceResolverFactory");
    resolverField.setAccessible(true);
    resolverField.set(servletResolver, factory);
    MockBundle bundle = new MockBundle(1L);
    MockBundleContext bundleContext = new MockBundleContext(bundle) {

        @Override
        public ServiceRegistration registerService(String s, Object o, Dictionary dictionary) {
            return null;
        }

        @Override
        public ServiceRegistration registerService(String[] strings, Object o, Dictionary dictionary) {
            return null;
        }
    };
    MockServiceReference serviceReference = new MockServiceReference(bundle);
    serviceReference.setProperty(Constants.SERVICE_ID, 1L);
    serviceReference.setProperty(ServletResolverConstants.SLING_SERVLET_NAME, SERVLET_NAME);
    serviceReference.setProperty(ServletResolverConstants.SLING_SERVLET_PATHS, SERVLET_PATH);
    serviceReference.setProperty(ServletResolverConstants.SLING_SERVLET_EXTENSIONS, SERVLET_EXTENSION);
    servletResolver.bindServlet(SlingServletResolverTest.this.servlet, serviceReference);
    servletResolver.activate(bundleContext, new SlingServletResolver.Config() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return SlingServletResolver.Config.class;
        }

        @Override
        public String servletresolver_servletRoot() {
            return "0";
        }

        @Override
        public String[] servletresolver_paths() {
            return new String[] { "/" };
        }

        @Override
        public String[] servletresolver_defaultExtensions() {
            // TODO Auto-generated method stub
            return new String[] { "html" };
        }

        @Override
        public int servletresolver_cacheSize() {
            return 200;
        }
    });
    String path = "/" + MockSlingHttpServletRequest.RESOURCE_TYPE + "/" + ResourceUtil.getName(MockSlingHttpServletRequest.RESOURCE_TYPE) + ".servlet";
    MockServletResource res = new MockServletResource(mockResourceResolver, servlet, path);
    mockResourceResolver.addResource(res);
    MockResource parent = new MockResource(mockResourceResolver, ResourceUtil.getParent(res.getPath()), "nt:folder");
    mockResourceResolver.addResource(parent);
    List<Resource> childRes = new ArrayList<>();
    childRes.add(res);
    mockResourceResolver.addChildren(parent, childRes);
}
Also used : Dictionary(java.util.Dictionary) MockResource(org.apache.sling.commons.testing.sling.MockResource) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) MockBundle(org.apache.sling.commons.testing.osgi.MockBundle) MockServletResource(org.apache.sling.servlets.resolver.internal.resource.MockServletResource) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.commons.testing.sling.MockResource) MockServletResource(org.apache.sling.servlets.resolver.internal.resource.MockServletResource) Annotation(java.lang.annotation.Annotation) MockServiceReference(org.apache.sling.commons.testing.osgi.MockServiceReference) MockBundleContext(org.apache.sling.commons.testing.osgi.MockBundleContext) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) Map(java.util.Map) Before(org.junit.Before)

Example 5 with MockResourceResolver

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

the class RedirectServletTest method toRedirect.

private static String toRedirect(String basePath, String selectors, String extension, String suffix, String queryString, String targetPath) {
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(basePath, selectors, extension, suffix, queryString, basePath, TEST_SCHEME, TEST_HOST, TEST_PORT, "");
    request.setResourceResolver(new MockResourceResolver());
    return RedirectServlet.toRedirectPath(targetPath, request);
}
Also used : MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver)

Aggregations

MockResourceResolver (org.apache.sling.commons.testing.sling.MockResourceResolver)13 MockResource (org.apache.sling.commons.testing.sling.MockResource)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)7 ArrayList (java.util.ArrayList)6 Resource (org.apache.sling.api.resource.Resource)6 Before (org.junit.Before)6 Map (java.util.Map)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Calendar (java.util.Calendar)3 HashMap (java.util.HashMap)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Part (javax.servlet.http.Part)3 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)3 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)3 PersistenceException (org.apache.sling.api.resource.PersistenceException)3 ValueMap (org.apache.sling.api.resource.ValueMap)3 AbstractPostResponse (org.apache.sling.servlets.post.AbstractPostResponse)3 Modification (org.apache.sling.servlets.post.Modification)3