This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.resourceresolver-mock-1.1.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-resourceresolver-mock.git
commit 8007bcc7fe77f845fd0079b5d85a9320d82e1338 Author: Stefan Seifert <[email protected]> AuthorDate: Tue Sep 16 21:45:17 2014 +0000 SLING-3889 Emulate feature of JCR resource implementation that allows adapting to InputStream for nt:file and nt:resource nodes. git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1625410 13f79535-47bb-0310-9956-ffa450edef68 --- .../testing/resourceresolver/MockResource.java | 33 +++++++- .../resourceresolver/NtFileResourceTest.java | 96 ++++++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java index a8ed2f5..1b10a0d 100644 --- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java +++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java @@ -18,10 +18,12 @@ */ package org.apache.sling.testing.resourceresolver; +import java.io.InputStream; import java.util.Map; import org.apache.sling.api.resource.AbstractResource; import org.apache.sling.api.resource.ModifiableValueMap; +import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceMetadata; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ValueMap; @@ -39,6 +41,10 @@ public class MockResource extends AbstractResource { private final ResourceResolver resolver; static final String JCR_PRIMARYTYPE = "jcr:primaryType"; + static final String JCR_CONTENT = "jcr:content"; + static final String JCR_DATA = "jcr:data"; + static final String NT_RESOURCE = "nt:resource"; + static final String NT_FILE = "nt:file"; public MockResource(final String path, final Map<String, Object> props, @@ -83,12 +89,37 @@ public class MockResource extends AbstractResource { public <AdapterType> AdapterType adaptTo(final Class<AdapterType> type) { if ( type == ValueMap.class ) { return (AdapterType)new ValueMapDecorator(this.props); - } else if ( type == ModifiableValueMap.class ) { + } + else if ( type == ModifiableValueMap.class ) { ((MockResourceResolver)this.resolver).addChanged(this.path, this.props); return (AdapterType)new ModifiableValueMapDecorator(this.props); } + else if ( type == InputStream.class ) { + InputStream is = getFileResourceInputStream(); + if (is!=null) { + return (AdapterType)is; + } + } return super.adaptTo(type); } + + /** + * Emulate feature of JCR resource implementation that allows adapting to InputStream for nt:file and nt:resource nodes. + * @return InputStream or null if adaption not possible. + */ + private InputStream getFileResourceInputStream() { + String resourceType = getResourceType(); + if (NT_RESOURCE.equals(resourceType)) { + return getValueMap().get(JCR_DATA, InputStream.class); + } + else if (NT_FILE.equals(resourceType)) { + Resource contentResource = getChild(JCR_CONTENT); + if (contentResource != null) { + return contentResource.getValueMap().get(JCR_DATA, InputStream.class); + } + } + return null; + } @Override public ValueMap getValueMap() { diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java new file mode 100644 index 0000000..fef5bc5 --- /dev/null +++ b/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.testing.resourceresolver; + +import static org.apache.sling.testing.resourceresolver.MockResource.JCR_CONTENT; +import static org.apache.sling.testing.resourceresolver.MockResource.JCR_DATA; +import static org.apache.sling.testing.resourceresolver.MockResource.JCR_PRIMARYTYPE; +import static org.apache.sling.testing.resourceresolver.MockResource.NT_FILE; +import static org.apache.sling.testing.resourceresolver.MockResource.NT_RESOURCE; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.sling.api.resource.LoginException; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.ValueMap; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.ImmutableMap; + +/** + * Implements simple write and read resource and values test. + * Sling CRUD API is used to create the test data. + */ +public class NtFileResourceTest { + + private static final byte[] BINARY_VALUE = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + + private ResourceResolver resourceResolver; + private Resource testRoot; + + @Before + public final void setUp() throws IOException, LoginException { + resourceResolver = new MockResourceResolverFactory().getResourceResolver(null); + Resource root = resourceResolver.getResource("/"); + testRoot = resourceResolver.create(root, "test", ValueMap.EMPTY); + } + + @Test + public void testNtFile() throws IOException { + Resource file = resourceResolver.create(testRoot, "ntFile", ImmutableMap.<String, Object>builder() + .put(JCR_PRIMARYTYPE, NT_FILE) + .build()); + resourceResolver.create(file, JCR_CONTENT, ImmutableMap.<String, Object>builder() + .put(JCR_PRIMARYTYPE, NT_RESOURCE) + .put(JCR_DATA, new ByteArrayInputStream(BINARY_VALUE)) + .build()); + + String path = testRoot.getPath() + "/ntFile"; + Resource resource = resourceResolver.getResource(path); + InputStream is = resource.adaptTo(InputStream.class); + assertNotNull(is); + + assertArrayEquals(BINARY_VALUE, IOUtils.toByteArray(is)); + is.close(); + } + + @Test + public void testNtResource() throws IOException { + resourceResolver.create(testRoot, "ntResource", ImmutableMap.<String, Object>builder() + .put(JCR_PRIMARYTYPE, NT_RESOURCE) + .put(JCR_DATA, new ByteArrayInputStream(BINARY_VALUE)) + .build()); + + String path = testRoot.getPath() + "/ntResource"; + Resource resource = resourceResolver.getResource(path); + InputStream is = resource.adaptTo(InputStream.class); + assertNotNull(is); + + assertArrayEquals(BINARY_VALUE, IOUtils.toByteArray(is)); + is.close(); + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
