Search in sources :

Example 1 with MockBundle

use of org.apache.sling.commons.testing.osgi.MockBundle 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 2 with MockBundle

use of org.apache.sling.commons.testing.osgi.MockBundle in project acs-aem-commons by Adobe-Consulting-Services.

the class StaticReferenceRewriteTransformerFactoryTest method test_with_prefix_and_matching_pattern_and_single_host.

@Test
public void test_with_prefix_and_matching_pattern_and_single_host() throws Exception {
    MockBundle bundle = new MockBundle(-1);
    MockComponentContext ctx = new MockComponentContext(bundle);
    ctx.setProperty("prefixes", new String[] { "/content/dam" });
    ctx.setProperty("attributes", new String[] { "img:srcset,src" });
    ctx.setProperty("host.pattern", "static.host.com");
    ctx.setProperty("matchingPatterns", "img:srcset;(\\/content\\/dam\\/.+?\\.(png|jpg))");
    StaticReferenceRewriteTransformerFactory factory = new StaticReferenceRewriteTransformerFactory();
    factory.activate(ctx);
    Transformer transformer = factory.createTransformer();
    transformer.setContentHandler(handler);
    AttributesImpl imageWithSrcSet = new AttributesImpl();
    imageWithSrcSet.addAttribute(null, "srcset", null, "CDATA", "/content/dam/flower.jpg 1280w,/content/dam/house.png 480w");
    transformer.startElement(null, "img", null, imageWithSrcSet);
    AttributesImpl imageWithJustSrc = new AttributesImpl();
    imageWithJustSrc.addAttribute(null, "src", null, "CDATA", "/content/dam/flower.jpg");
    transformer.startElement(null, "img", null, imageWithJustSrc);
    verify(handler, times(2)).startElement(isNull(String.class), eq("img"), isNull(String.class), attributesCaptor.capture());
    List<Attributes> values = attributesCaptor.getAllValues();
    assertEquals("//static.host.com/content/dam/flower.jpg 1280w,//static.host.com/content/dam/house.png 480w", values.get(0).getValue(0));
    assertEquals("//static.host.com/content/dam/flower.jpg", values.get(1).getValue(0));
}
Also used : MockComponentContext(org.apache.sling.commons.testing.osgi.MockComponentContext) Transformer(org.apache.sling.rewriter.Transformer) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attributes(org.xml.sax.Attributes) MockBundle(org.apache.sling.commons.testing.osgi.MockBundle) Test(org.junit.Test)

Example 3 with MockBundle

use of org.apache.sling.commons.testing.osgi.MockBundle in project acs-aem-commons by Adobe-Consulting-Services.

the class StaticReferenceRewriteTransformerFactoryTest method test_with_prefix_and_multiple_numbered_hosts.

@Test
public void test_with_prefix_and_multiple_numbered_hosts() throws Exception {
    MockBundle bundle = new MockBundle(-1);
    MockComponentContext ctx = new MockComponentContext(bundle);
    ctx.setProperty("prefixes", new String[] { "/etc/clientlib" });
    ctx.setProperty("host.pattern", "static{}.host.com");
    ctx.setProperty("host.count", 2);
    StaticReferenceRewriteTransformerFactory factory = new StaticReferenceRewriteTransformerFactory();
    factory.activate(ctx);
    Transformer transformer = factory.createTransformer();
    transformer.setContentHandler(handler);
    AttributesImpl in = new AttributesImpl();
    in.addAttribute(null, "href", null, "CDATA", "/etc/clientlib/testA.css");
    transformer.startElement(null, "link", null, in);
    verify(handler, only()).startElement(isNull(String.class), eq("link"), isNull(String.class), attributesCaptor.capture());
    Attributes out = attributesCaptor.getValue();
    assertEquals("//static2.host.com/etc/clientlib/testA.css", out.getValue(0));
}
Also used : MockComponentContext(org.apache.sling.commons.testing.osgi.MockComponentContext) Transformer(org.apache.sling.rewriter.Transformer) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attributes(org.xml.sax.Attributes) MockBundle(org.apache.sling.commons.testing.osgi.MockBundle) Test(org.junit.Test)

Example 4 with MockBundle

use of org.apache.sling.commons.testing.osgi.MockBundle in project acs-aem-commons by Adobe-Consulting-Services.

the class StaticReferenceRewriteTransformerFactoryTest method test_with_prefix_and_single_host.

@Test
public void test_with_prefix_and_single_host() throws Exception {
    MockBundle bundle = new MockBundle(-1);
    MockComponentContext ctx = new MockComponentContext(bundle);
    ctx.setProperty("prefixes", new String[] { "/etc/clientlib" });
    ctx.setProperty("host.pattern", "static.host.com");
    StaticReferenceRewriteTransformerFactory factory = new StaticReferenceRewriteTransformerFactory();
    factory.activate(ctx);
    Transformer transformer = factory.createTransformer();
    transformer.setContentHandler(handler);
    AttributesImpl in = new AttributesImpl();
    in.addAttribute(null, "href", null, "CDATA", "/etc/clientlib/test.css");
    transformer.startElement(null, "link", null, in);
    verify(handler, only()).startElement(isNull(String.class), eq("link"), isNull(String.class), attributesCaptor.capture());
    Attributes out = attributesCaptor.getValue();
    assertEquals("//static.host.com/etc/clientlib/test.css", out.getValue(0));
}
Also used : MockComponentContext(org.apache.sling.commons.testing.osgi.MockComponentContext) Transformer(org.apache.sling.rewriter.Transformer) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attributes(org.xml.sax.Attributes) MockBundle(org.apache.sling.commons.testing.osgi.MockBundle) Test(org.junit.Test)

Example 5 with MockBundle

use of org.apache.sling.commons.testing.osgi.MockBundle in project acs-aem-commons by Adobe-Consulting-Services.

the class StaticReferenceRewriteTransformerFactoryTest method test_with_nonrewritten_element.

@Test
public void test_with_nonrewritten_element() throws Exception {
    MockBundle bundle = new MockBundle(-1);
    MockComponentContext ctx = new MockComponentContext(bundle);
    ctx.setProperty("prefixes", new String[] { "/etc/clientlib" });
    ctx.setProperty("host.pattern", "static{}.host.com");
    ctx.setProperty("host.count", 2);
    StaticReferenceRewriteTransformerFactory factory = new StaticReferenceRewriteTransformerFactory();
    factory.activate(ctx);
    Transformer transformer = factory.createTransformer();
    transformer.setContentHandler(handler);
    AttributesImpl in = new AttributesImpl();
    in.addAttribute(null, "src", null, "CDATA", "/etc/clientlib/testABC.css");
    transformer.startElement(null, "iframe", null, in);
    verify(handler, only()).startElement(isNull(String.class), eq("iframe"), isNull(String.class), attributesCaptor.capture());
    Attributes out = attributesCaptor.getValue();
    assertEquals("/etc/clientlib/testABC.css", out.getValue(0));
}
Also used : MockComponentContext(org.apache.sling.commons.testing.osgi.MockComponentContext) Transformer(org.apache.sling.rewriter.Transformer) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attributes(org.xml.sax.Attributes) MockBundle(org.apache.sling.commons.testing.osgi.MockBundle) Test(org.junit.Test)

Aggregations

MockBundle (org.apache.sling.commons.testing.osgi.MockBundle)10 MockComponentContext (org.apache.sling.commons.testing.osgi.MockComponentContext)9 Transformer (org.apache.sling.rewriter.Transformer)9 Test (org.junit.Test)9 Attributes (org.xml.sax.Attributes)9 AttributesImpl (org.xml.sax.helpers.AttributesImpl)9 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 Map (java.util.Map)1 LoginException (org.apache.sling.api.resource.LoginException)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)1 MockBundleContext (org.apache.sling.commons.testing.osgi.MockBundleContext)1 MockServiceReference (org.apache.sling.commons.testing.osgi.MockServiceReference)1 MockResource (org.apache.sling.commons.testing.sling.MockResource)1 MockResourceResolver (org.apache.sling.commons.testing.sling.MockResourceResolver)1 MockServletResource (org.apache.sling.servlets.resolver.internal.resource.MockServletResource)1