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);
}
}
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);
}
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");
}
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());
}
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;
}
Aggregations