use of org.apache.sling.commons.testing.osgi.MockBundleContext 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);
}
Aggregations