Author: sseifert
Date: Tue Sep 16 21:45:17 2014
New Revision: 1625410
URL: http://svn.apache.org/r1625410
Log:
SLING-3889 Emulate feature of JCR resource implementation that allows adapting
to InputStream for nt:file and nt:resource nodes.
Added:
sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java
(with props)
Modified:
sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
Modified:
sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java?rev=1625410&r1=1625409&r2=1625410&view=diff
==============================================================================
---
sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
(original)
+++
sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
Tue Sep 16 21:45:17 2014
@@ -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 Abstra
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 Abstra
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() {
Added:
sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java?rev=1625410&view=auto
==============================================================================
---
sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java
(added)
+++
sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java
Tue Sep 16 21:45:17 2014
@@ -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();
+ }
+
+}
Propchange:
sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Tue Sep 16 21:45:17 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange:
sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain