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:14 UTC
[sling-org-apache-sling-resourcebuilder] 05/36: SLING-5356 - test
default mime-type and last-modified values
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 feae532aeb12eb8fe2391ec279301d30f911f0e1
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu Dec 10 16:10:05 2015 +0000
SLING-5356 - test default mime-type and last-modified values
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/resourcebuilder@1719103 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 17 ++++-
.../resourcebuilder/impl/ResourceBuilderImpl.java | 25 +++++--
.../impl/ResourceBuilderImplTest.java | 79 +++++++++++++++++-----
3 files changed, 99 insertions(+), 22 deletions(-)
diff --git a/pom.xml b/pom.xml
index ca8feba..fc668f7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -51,7 +51,16 @@
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
</plugin>
- </plugins>
+ <plugin>
+ <groupId>org.apache.rat</groupId>
+ <artifactId>apache-rat-plugin</artifactId>
+ <configuration>
+ <excludes>
+ <exclude>src/test/resources/**</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+ </plugins>
</build>
<dependencies>
@@ -75,6 +84,12 @@
<scope>provided</scope>
</dependency>
<dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.mime</artifactId>
+ <version>2.1.2</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
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 bb278d1..51618b8 100644
--- a/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
+++ b/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
@@ -27,6 +27,7 @@ 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.ResourceUtil;
+import org.apache.sling.commons.mime.MimeTypeService;
import org.apache.sling.resourcebuilder.api.ResourceBuilder;
/** ResourceBuilder implementation */
@@ -44,7 +45,10 @@ public class ResourceBuilderImpl implements ResourceBuilder {
public static final String JCR_CONTENT = "jcr:content";
public static final String NT_RESOURCE = "nt:resource";
- public ResourceBuilderImpl(Resource parent) {
+ private final MimeTypeService mimeTypeService;
+
+ public ResourceBuilderImpl(Resource parent, MimeTypeService mts) {
+ mimeTypeService = mts;
if(parent == null) {
throw new IllegalArgumentException("Parent resource is null");
}
@@ -136,6 +140,20 @@ public class ResourceBuilderImpl implements ResourceBuilder {
}
}
+ protected String getMimeType(String filename, String userSuppliedMimeType) {
+ if(userSuppliedMimeType != null) {
+ return userSuppliedMimeType;
+ }
+ return mimeTypeService.getMimeType(filename);
+ }
+
+ protected long getLastModified(long userSuppliedValue) {
+ if(userSuppliedValue < 0) {
+ return System.currentTimeMillis();
+ }
+ return userSuppliedValue;
+ }
+
@Override
public ResourceBuilder file(String filename, InputStream data, String mimeType, long lastModified) {
Resource file = null;
@@ -157,9 +175,8 @@ public class ResourceBuilderImpl implements ResourceBuilder {
file = resolver.create(currentParent, name, null);
final Map<String, Object> props = new HashMap<String, Object>();
props.put(JCR_PRIMARYTYPE, NT_RESOURCE);
- // TODO get mime type from MimeTypeService
- props.put(JCR_MIMETYPE, mimeType);
- props.put(JCR_LASTMODIFIED, lastModified >= 0 ? lastModified : System.currentTimeMillis());
+ props.put(JCR_MIMETYPE, getMimeType(filename, mimeType));
+ props.put(JCR_LASTMODIFIED, getLastModified(lastModified));
props.put(JCR_DATA, data);
resolver.create(file, JCR_CONTENT, props);
} catch(PersistenceException pex) {
diff --git a/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java b/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java
index dbf0253..9807507 100644
--- a/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java
+++ b/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java
@@ -18,24 +18,27 @@
*/
package org.apache.sling.resourcebuilder.impl;
-import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
+import java.util.Random;
import java.util.UUID;
import org.apache.commons.io.IOUtils;
-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.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.commons.mime.MimeTypeService;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.apache.sling.testing.mock.sling.services.MockMimeTypeService;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -44,14 +47,36 @@ public class ResourceBuilderImplTest {
private String testRootPath;
private ResourceResolver resourceResolver;
+ private long lastModified;
+ private Random random = new Random(System.currentTimeMillis());
+ private static final MimeTypeService mimeTypeService = new MockMimeTypeService();
@Rule
public SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
- private ResourceBuilderImpl getBuilder(String path) throws PersistenceException {
+ private ResourceBuilderImpl getBuilder(String path) throws Exception {
final Resource root = context.resourceResolver().resolve("/");
assertNotNull("Expecting non-null root", root);
- return new ResourceBuilderImpl(resourceResolver.create(root, ResourceUtil.getName(path), null));
+ lastModified = random.nextLong();
+
+ final Resource parent = resourceResolver.create(root, ResourceUtil.getName(path), null);
+ final ResourceBuilderImpl result = new ResourceBuilderImpl(parent, mimeTypeService) {
+ @Override
+ protected long getLastModified(long userSuppliedValue) {
+ final long now = System.currentTimeMillis();
+ final long superValue = super.getLastModified(-1);
+ final long maxDelta = 60 * 1000L;
+ if(superValue < now || superValue - now > maxDelta) {
+ fail("getLastModified does not seem to use current time as its default value");
+ }
+
+ if(userSuppliedValue >= 0) {
+ return super.getLastModified(userSuppliedValue);
+ }
+ return lastModified;
+ }
+ };
+ return result;
}
@Before
@@ -113,7 +138,7 @@ public class ResourceBuilderImplTest {
}
@Test
- public void basicResource() throws PersistenceException {
+ public void basicResource() throws Exception {
getBuilder(testRootPath)
.resource("child", "title", "foo")
.commit();
@@ -123,11 +148,11 @@ public class ResourceBuilderImplTest {
}
@Test
- public void ensureResourceExists() throws PersistenceException {
+ public void ensureResourceExists() throws Exception {
class MyResourceBuilder extends ResourceBuilderImpl {
MyResourceBuilder() {
- super(resourceResolver.getResource("/"));
+ super(resourceResolver.getResource("/"), null);
}
Resource r(String path) {
@@ -142,7 +167,7 @@ public class ResourceBuilderImplTest {
}
@Test
- public void deepResource() throws PersistenceException {
+ public void deepResource() throws Exception {
getBuilder(testRootPath)
.resource("a/b/c", "title", "foo")
.commit();
@@ -154,7 +179,7 @@ public class ResourceBuilderImplTest {
}
@Test
- public void intermediatePrimaryTypes() throws PersistenceException {
+ public void intermediatePrimaryTypes() throws Exception {
getBuilder(testRootPath)
.resource("a/b/c")
.withIntermediatePrimaryType("foo")
@@ -169,7 +194,7 @@ public class ResourceBuilderImplTest {
}
@Test
- public void resetParent() throws PersistenceException {
+ public void resetParent() throws Exception {
getBuilder(testRootPath)
.resource("a/b/c")
.resetParent()
@@ -181,7 +206,7 @@ public class ResourceBuilderImplTest {
}
@Test
- public void noResetParent() throws PersistenceException {
+ public void noResetParent() throws Exception {
getBuilder(testRootPath)
.resource("a/b/c")
.resource("d/e")
@@ -192,24 +217,24 @@ public class ResourceBuilderImplTest {
}
@Test
- public void getParent() throws PersistenceException {
+ public void getParent() throws Exception {
final Resource parent = getBuilder(testRootPath).getCurrentParent();
assertNotNull(parent);
assertEquals(testRootPath, parent.getPath());
}
@Test(expected=RuntimeException.class)
- public void missingParentFails() throws PersistenceException {
- new ResourceBuilderImpl(null).resource("foo");
+ public void missingParentFails() throws Exception {
+ new ResourceBuilderImpl(null, null).resource("foo");
}
@Test(expected=IllegalArgumentException.class)
- public void absolutePathFails() throws PersistenceException {
+ public void absolutePathFails() throws Exception {
getBuilder(testRootPath).resource("/absolute");
}
@Test
- public void simpleTree() throws PersistenceException {
+ public void simpleTree() throws Exception {
getBuilder(testRootPath)
.resource("a/b/c", "title", "foo", "count", 21)
.siblingsMode()
@@ -231,7 +256,7 @@ public class ResourceBuilderImplTest {
}
@Test
- public void treeWithFiles() throws PersistenceException, IOException {
+ public void treeWithFiles() throws Exception {
getBuilder(testRootPath)
.resource("apps/myapp/components/resource")
.siblingsMode()
@@ -262,4 +287,24 @@ public class ResourceBuilderImplTest {
assertFile("apps/content/myapp.json",
"MT4", "\"sling:resourceType\":\"its/resource/type\"", 45L);
}
+
+ @Test
+ public void autoMimetype() throws Exception {
+ getBuilder(testRootPath)
+ .file("models.js", getClass().getResourceAsStream("/models.js"), null, 42)
+ .commit()
+ ;
+ assertFile("models.js",
+ "application/javascript", "function someJavascriptFunction()", 42L);
+ }
+
+ @Test
+ public void autoLastModified() throws Exception {
+ getBuilder(testRootPath)
+ .file("models.js", getClass().getResourceAsStream("/models.js"), "MT1", -1)
+ .commit()
+ ;
+ assertFile("models.js",
+ "MT1", "function someJavascriptFunction()", lastModified);
+ }
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.