You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:58:20 UTC
[sling-org-apache-sling-resourcebuilder] 11/36: SLING-5356 -
FileRetrievalIT added, and fix nt:file bug that it found
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.resourcebuilder-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourcebuilder.git
commit f6837c333dcb11c5f66846c8c01f700b52546641
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Dec 11 14:16:12 2015 +0000
SLING-5356 - FileRetrievalIT added, and fix nt:file bug that it found
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/resourcebuilder@1719425 13f79535-47bb-0310-9956-ffa450edef68
---
.../resourcebuilder/impl/ResourceBuilderImpl.java | 18 +++---
...ResourceBuilderIT.java => FileRetrievalIT.java} | 71 +++++++++++-----------
.../resourcebuilder/it/ResourceBuilderIT.java | 57 +++++++++--------
...ResourceBuilderIT.java => TestEnvironment.java} | 50 +++------------
.../resourcebuilder/test/ResourceAssertions.java | 48 +++++++++++----
5 files changed, 126 insertions(+), 118 deletions(-)
diff --git a/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java b/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
index 51618b8..58a6aaf 100644
--- a/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
+++ b/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
@@ -44,6 +44,7 @@ public class ResourceBuilderImpl implements ResourceBuilder {
public static final String JCR_DATA = "jcr:data";
public static final String JCR_CONTENT = "jcr:content";
public static final String NT_RESOURCE = "nt:resource";
+ public static final String NT_FILE = "nt:file";
private final MimeTypeService mimeTypeService;
@@ -172,13 +173,16 @@ public class ResourceBuilderImpl implements ResourceBuilder {
if(resolver.getResource(fullPath) != null) {
throw new IllegalStateException("Resource already exists:" + fullPath);
}
- file = resolver.create(currentParent, name, null);
- final Map<String, Object> props = new HashMap<String, Object>();
- props.put(JCR_PRIMARYTYPE, NT_RESOURCE);
- props.put(JCR_MIMETYPE, getMimeType(filename, mimeType));
- props.put(JCR_LASTMODIFIED, getLastModified(lastModified));
- props.put(JCR_DATA, data);
- resolver.create(file, JCR_CONTENT, props);
+ final Map<String, Object> fileProps = new HashMap<String, Object>();
+ fileProps.put(JCR_PRIMARYTYPE, NT_FILE);
+ file = resolver.create(currentParent, name, fileProps);
+
+ final Map<String, Object> contentProps = new HashMap<String, Object>();
+ contentProps.put(JCR_PRIMARYTYPE, NT_RESOURCE);
+ contentProps.put(JCR_MIMETYPE, getMimeType(filename, mimeType));
+ contentProps.put(JCR_LASTMODIFIED, getLastModified(lastModified));
+ contentProps.put(JCR_DATA, data);
+ resolver.create(file, JCR_CONTENT, contentProps);
} catch(PersistenceException pex) {
throw new RuntimeException("Unable to create file under " + currentParent.getPath(), pex);
}
diff --git a/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java b/src/test/java/org/apache/sling/resourcebuilder/it/FileRetrievalIT.java
similarity index 53%
copy from src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java
copy to src/test/java/org/apache/sling/resourcebuilder/it/FileRetrievalIT.java
index e97d542..1991c27 100644
--- a/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java
+++ b/src/test/java/org/apache/sling/resourcebuilder/it/FileRetrievalIT.java
@@ -18,27 +18,31 @@
*/
package org.apache.sling.resourcebuilder.it;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
import java.io.IOException;
-import java.util.UUID;
+import java.io.InputStream;
+
+import javax.servlet.ServletException;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.junit.rules.TeleporterRule;
-import org.apache.sling.resourcebuilder.api.ResourceBuilder;
-import org.apache.sling.resourcebuilder.api.ResourceBuilderProvider;
import org.apache.sling.resourcebuilder.test.ResourceAssertions;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-/** Server-side integration test for the
- * ResourceBuilder, acquired via the ResourceBuilderProvider
+/** Verify that our file structure is correct,
+ * by creating a file and retrieving it via
+ * a Sling request.
*/
-public class ResourceBuilderIT {
+public class FileRetrievalIT {
@Rule
public final TeleporterRule teleporter =
@@ -46,45 +50,44 @@ public class ResourceBuilderIT {
.forClass(getClass(), "RBIT_Teleporter")
.withResources("/files/");
- private ResourceBuilder builder;
- private ResourceResolver resolver;
- private String testRootPath;
- private Resource parent;
+ private TestEnvironment E;
private ResourceAssertions A;
@Before
public void setup() throws LoginException, PersistenceException {
- testRootPath = getClass().getSimpleName() + "-" + UUID.randomUUID().toString();
- resolver = teleporter.getService(ResourceResolverFactory.class).getAdministrativeResourceResolver(null);
- final Resource root = resolver.getResource("/");
- parent = resolver.create(root, testRootPath, null);
- builder = teleporter.getService(ResourceBuilderProvider.class).getResourceBuilder(parent);
- A = new ResourceAssertions(testRootPath, resolver);
+ E = new TestEnvironment(teleporter);
+ A = new ResourceAssertions(E.testRootPath, E.resolver);
}
@After
public void cleanup() throws PersistenceException {
- if(resolver != null && parent != null) {
- resolver.delete(parent);
- resolver.commit();
- }
+ E.cleanup();
}
@Test
- public void simpleResource() {
- builder
- .resource("foo", "title", testRootPath)
- .commit();
- A.assertProperties("foo", "title", testRootPath);
- }
-
- @Test
- public void smallTreeWithFile() throws IOException {
- builder
+ public void createAndeRtrieveFile() throws IOException, ServletException {
+ final String expected = "yes, it worked";
+ final long startTime = System.currentTimeMillis();
+ final String mimeType = "application/javascript";
+
+ E.builder
.resource("somefolder")
- .file("the-model.js", getClass().getResourceAsStream("/files/models.js"), "foo", 42L)
+ .file("the-model.js", getClass().getResourceAsStream("/files/models.js"))
.commit();
- A.assertFile("somefolder/the-model.js", "foo", "yes, it worked", 42L);
+ final Resource r = A.assertFile("somefolder/the-model.js", mimeType, expected, -1L);
+
+ final ResourceMetadata meta = r.getResourceMetadata();
+ assertTrue("Expecting a last modified time >= startTime", meta.getModificationTime() >= startTime);
+ assertEquals("Expecting the correct mime-type", mimeType, meta.getContentType());
+
+ final InputStream is = r.adaptTo(InputStream.class);
+ assertNotNull("Expecting InputStream for file resource " + r.getPath(), is);
+ try {
+ final String content = A.readFully(is);
+ assertTrue("Expecting [" + expected + "] in content", content.contains(expected));
+ } finally {
+ is.close();
+ }
}
}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java b/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java
index e97d542..6c6d43a 100644
--- a/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java
+++ b/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java
@@ -18,17 +18,13 @@
*/
package org.apache.sling.resourcebuilder.it;
+import static org.junit.Assert.fail;
import java.io.IOException;
-import java.util.UUID;
+import java.util.Comparator;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.PersistenceException;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.junit.rules.TeleporterRule;
-import org.apache.sling.resourcebuilder.api.ResourceBuilder;
-import org.apache.sling.resourcebuilder.api.ResourceBuilderProvider;
import org.apache.sling.resourcebuilder.test.ResourceAssertions;
import org.junit.After;
import org.junit.Before;
@@ -46,45 +42,58 @@ public class ResourceBuilderIT {
.forClass(getClass(), "RBIT_Teleporter")
.withResources("/files/");
- private ResourceBuilder builder;
- private ResourceResolver resolver;
- private String testRootPath;
- private Resource parent;
+ private TestEnvironment E;
private ResourceAssertions A;
@Before
public void setup() throws LoginException, PersistenceException {
- testRootPath = getClass().getSimpleName() + "-" + UUID.randomUUID().toString();
- resolver = teleporter.getService(ResourceResolverFactory.class).getAdministrativeResourceResolver(null);
- final Resource root = resolver.getResource("/");
- parent = resolver.create(root, testRootPath, null);
- builder = teleporter.getService(ResourceBuilderProvider.class).getResourceBuilder(parent);
- A = new ResourceAssertions(testRootPath, resolver);
+ E = new TestEnvironment(teleporter);
+ A = new ResourceAssertions(E.testRootPath, E.resolver);
}
@After
public void cleanup() throws PersistenceException {
- if(resolver != null && parent != null) {
- resolver.delete(parent);
- resolver.commit();
- }
+ E.cleanup();
}
+
@Test
public void simpleResource() {
- builder
- .resource("foo", "title", testRootPath)
+ E.builder
+ .resource("foo", "title", E.testRootPath)
.commit();
- A.assertProperties("foo", "title", testRootPath);
+ A.assertProperties("foo", "title", E.testRootPath);
}
@Test
public void smallTreeWithFile() throws IOException {
- builder
+ E.builder
.resource("somefolder")
.file("the-model.js", getClass().getResourceAsStream("/files/models.js"), "foo", 42L)
.commit();
A.assertFile("somefolder/the-model.js", "foo", "yes, it worked", 42L);
}
+
+ @Test
+ public void fileAutoValues() throws IOException {
+ final long startTime = System.currentTimeMillis();
+ E.builder
+ .resource("a/b/c")
+ .file("model2.js", getClass().getResourceAsStream("/files/models.js"))
+ .commit();
+
+ final Comparator<Long> moreThanStartTime = new Comparator<Long>() {
+ @Override
+ public int compare(Long expected, Long fromResource) {
+ if(fromResource >= startTime) {
+ return 0;
+ }
+ fail("last-modified is not >= than current time:" + fromResource + " < " + startTime);
+ return -1;
+ }
+ };
+
+ A.assertFile("a/b/c/model2.js", "application/javascript", "yes, it worked", startTime, moreThanStartTime);
+ }
}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java b/src/test/java/org/apache/sling/resourcebuilder/it/TestEnvironment.java
similarity index 62%
copy from src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java
copy to src/test/java/org/apache/sling/resourcebuilder/it/TestEnvironment.java
index e97d542..be22432 100644
--- a/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java
+++ b/src/test/java/org/apache/sling/resourcebuilder/it/TestEnvironment.java
@@ -18,7 +18,6 @@
*/
package org.apache.sling.resourcebuilder.it;
-import java.io.IOException;
import java.util.UUID;
import org.apache.sling.api.resource.LoginException;
@@ -30,30 +29,16 @@ import org.apache.sling.junit.rules.TeleporterRule;
import org.apache.sling.resourcebuilder.api.ResourceBuilder;
import org.apache.sling.resourcebuilder.api.ResourceBuilderProvider;
import org.apache.sling.resourcebuilder.test.ResourceAssertions;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-/** Server-side integration test for the
- * ResourceBuilder, acquired via the ResourceBuilderProvider
- */
-public class ResourceBuilderIT {
-
- @Rule
- public final TeleporterRule teleporter =
- TeleporterRule
- .forClass(getClass(), "RBIT_Teleporter")
- .withResources("/files/");
+class TestEnvironment {
- private ResourceBuilder builder;
- private ResourceResolver resolver;
- private String testRootPath;
- private Resource parent;
- private ResourceAssertions A;
+ final ResourceBuilder builder;
+ final ResourceResolver resolver;
+ final String testRootPath;
+ final Resource parent;
+ final ResourceAssertions A;
- @Before
- public void setup() throws LoginException, PersistenceException {
+ TestEnvironment(TeleporterRule teleporter) throws LoginException, PersistenceException {
testRootPath = getClass().getSimpleName() + "-" + UUID.randomUUID().toString();
resolver = teleporter.getService(ResourceResolverFactory.class).getAdministrativeResourceResolver(null);
final Resource root = resolver.getResource("/");
@@ -62,29 +47,10 @@ public class ResourceBuilderIT {
A = new ResourceAssertions(testRootPath, resolver);
}
- @After
- public void cleanup() throws PersistenceException {
+ void cleanup() throws PersistenceException {
if(resolver != null && parent != null) {
resolver.delete(parent);
resolver.commit();
}
}
-
- @Test
- public void simpleResource() {
- builder
- .resource("foo", "title", testRootPath)
- .commit();
- A.assertProperties("foo", "title", testRootPath);
- }
-
- @Test
- public void smallTreeWithFile() throws IOException {
- builder
- .resource("somefolder")
- .file("the-model.js", getClass().getResourceAsStream("/files/models.js"), "foo", 42L)
- .commit();
-
- A.assertFile("somefolder/the-model.js", "foo", "yes, it worked", 42L);
- }
}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/resourcebuilder/test/ResourceAssertions.java b/src/test/java/org/apache/sling/resourcebuilder/test/ResourceAssertions.java
index 32db89e..cea2069 100644
--- a/src/test/java/org/apache/sling/resourcebuilder/test/ResourceAssertions.java
+++ b/src/test/java/org/apache/sling/resourcebuilder/test/ResourceAssertions.java
@@ -26,6 +26,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
+import java.util.Comparator;
import java.util.Map;
import org.apache.commons.io.IOUtils;
@@ -57,11 +58,40 @@ public class ResourceAssertions {
}
/** Assert that a file exists and verify its properties. */
- public void assertFile(String path, String mimeType, String expectedContent, Long lastModified) throws IOException {
+ public Resource assertFile(String path, String mimeType, String expectedContent, Long lastModified) throws IOException {
+ final Comparator<Long> defaultComparator = new Comparator<Long>() {
+ @Override
+ public int compare(Long expected, Long fromResource) {
+ if(expected == -1) {
+ return 0;
+ }
+ return expected.compareTo(fromResource);
+ }
+ };
+ return assertFile(path, mimeType, expectedContent, lastModified, defaultComparator);
+ }
+
+ public String readFully(InputStream is) throws IOException {
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ try {
+ IOUtils.copy(is, bos);
+ return new String(bos.toByteArray());
+ } finally {
+ bos.close();
+ is.close();
+ }
+ }
+
+ /** Assert that a file exists and verify its properties. */
+ public Resource assertFile(String path, String mimeType, String expectedContent, Long lastModified, Comparator<Long> lastModifiedComparator) throws IOException {
final Resource r = assertResource(path);
assertNotNull("Expecting resource to exist:" + path, r);
// Files are stored according to the standard JCR structure
+ final ValueMap fileVm = r.adaptTo(ValueMap.class);
+ assertNotNull("Expecting ValueMap for " + r.getPath(), fileVm);
+ assertEquals("Expecting an nt:file at " + r.getPath(),
+ ResourceBuilderImpl.NT_FILE, fileVm.get(ResourceBuilderImpl.JCR_PRIMARYTYPE));
final Resource jcrContent = r.getChild(ResourceBuilderImpl.JCR_CONTENT);
assertNotNull("Expecting subresource:" + ResourceBuilderImpl.JCR_CONTENT, jcrContent);
final ValueMap vm = jcrContent.adaptTo(ValueMap.class);
@@ -69,19 +99,15 @@ public class ResourceAssertions {
assertEquals("Expecting nt:Resource type for " + jcrContent.getPath(),
ResourceBuilderImpl.NT_RESOURCE, vm.get(ResourceBuilderImpl.JCR_PRIMARYTYPE));
assertEquals("Expecting the correct mime-type", mimeType, vm.get(ResourceBuilderImpl.JCR_MIMETYPE));
- assertEquals("Expecting the correct last modified", lastModified, getLastModified(vm));
+ assertEquals("Expecting the correct last modified",
+ 0, lastModifiedComparator.compare(lastModified, getLastModified(vm)));
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final InputStream is = vm.get(ResourceBuilderImpl.JCR_DATA, InputStream.class);
assertNotNull("Expecting InputStream property on nt:resource:" + ResourceBuilderImpl.JCR_DATA, is);
- IOUtils.copy(is, bos);
- try {
- final String content = new String(bos.toByteArray());
- assertTrue("Expecting content to contain " + expectedContent, content.contains(expectedContent));
- } finally {
- bos.close();
- is.close();
- }
+ final String content = readFully(is);
+ assertTrue("Expecting content to contain " + expectedContent, content.contains(expectedContent));
+
+ return r;
}
private Long getLastModified(ValueMap vm) {
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.