You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2018/06/05 04:17:41 UTC
[sling-whiteboard] branch master updated: Adding unit tests and
cleaning up some issues found by unit tests
This is an automated email from the ASF dual-hosted git repository.
dklco pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
The following commit(s) were added to refs/heads/master by this push:
new dfb445c Adding unit tests and cleaning up some issues found by unit tests
dfb445c is described below
commit dfb445cb20a78bec03bd9bfab9b145f50e6b5e06
Author: Dan Klco <dk...@apache.org>
AuthorDate: Tue Jun 5 00:17:20 2018 -0400
Adding unit tests and cleaning up some issues found by unit tests
---
file-optim/pom.xml | 6 +-
.../fileoptim/impl/FileOptimizerServiceImpl.java | 12 +-
.../sling/fileoptim/BaseFileOptimizerTest.java | 156 +++++++++++++++++++++
.../sling/fileoptim/impl/TestFileOptimizer.java | 77 ++++++++++
.../impl/servlets/TestFileOptimizerData.java | 85 +++++++++++
.../src/test/resources/simplelogger.properties | 19 +++
6 files changed, 349 insertions(+), 6 deletions(-)
diff --git a/file-optim/pom.xml b/file-optim/pom.xml
index 621d606..7998a6f 100644
--- a/file-optim/pom.xml
+++ b/file-optim/pom.xml
@@ -182,8 +182,10 @@
<artifactId>junit</artifactId>
</dependency>
<dependency>
- <groupId>org.jmock</groupId>
- <artifactId>jmock-junit4</artifactId>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>2.7.16</version>
+ <scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
diff --git a/file-optim/src/main/java/org/apache/sling/fileoptim/impl/FileOptimizerServiceImpl.java b/file-optim/src/main/java/org/apache/sling/fileoptim/impl/FileOptimizerServiceImpl.java
index 165d521..5e5b9eb 100644
--- a/file-optim/src/main/java/org/apache/sling/fileoptim/impl/FileOptimizerServiceImpl.java
+++ b/file-optim/src/main/java/org/apache/sling/fileoptim/impl/FileOptimizerServiceImpl.java
@@ -84,8 +84,9 @@ public class FileOptimizerServiceImpl implements FileOptimizerService, ServiceLi
@Modified
public void activate(ComponentContext context, Config config) throws InvalidSyntaxException {
bundleContext = context.getBundleContext();
- bundleContext.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + FileOptimizer.class.getName() + ")");
this.config = config;
+ this.rebuildOptimizerCache();
+ bundleContext.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + FileOptimizer.class.getName() + ")");
}
private void addOptimizer(Map<String, List<ServiceReference<FileOptimizer>>> tempCache, String metaType,
@@ -111,7 +112,8 @@ public class FileOptimizerServiceImpl implements FileOptimizerService, ServiceLi
@Override
public boolean canOptimize(Resource fileResource) {
- if (!fileResource.getName().equals(JcrConstants.JCR_CONTENT)) {
+ if (!fileResource.getName().equals(JcrConstants.JCR_CONTENT)
+ && fileResource.getChild(JcrConstants.JCR_CONTENT) != null) {
fileResource = fileResource.getChild(JcrConstants.JCR_CONTENT);
}
OptimizedFile of = fileResource.adaptTo(OptimizedFile.class);
@@ -140,7 +142,8 @@ public class FileOptimizerServiceImpl implements FileOptimizerService, ServiceLi
*/
@Override
public OptimizationResult getOptimizedContents(Resource fileResource) throws IOException {
- if (!fileResource.getName().equals(JcrConstants.JCR_CONTENT)) {
+ if (!fileResource.getName().equals(JcrConstants.JCR_CONTENT)
+ && fileResource.getChild(JcrConstants.JCR_CONTENT) != null) {
fileResource = fileResource.getChild(JcrConstants.JCR_CONTENT);
}
OptimizationResult result = new OptimizationResult(fileResource);
@@ -187,7 +190,8 @@ public class FileOptimizerServiceImpl implements FileOptimizerService, ServiceLi
@Override
public boolean isOptimized(Resource fileResource) {
- if (!fileResource.getName().equals(JcrConstants.JCR_CONTENT)) {
+ if (!fileResource.getName().equals(JcrConstants.JCR_CONTENT)
+ && fileResource.getChild(JcrConstants.JCR_CONTENT) != null) {
fileResource = fileResource.getChild(JcrConstants.JCR_CONTENT);
}
diff --git a/file-optim/src/test/java/org/apache/sling/fileoptim/BaseFileOptimizerTest.java b/file-optim/src/test/java/org/apache/sling/fileoptim/BaseFileOptimizerTest.java
new file mode 100644
index 0000000..df1d667
--- /dev/null
+++ b/file-optim/src/test/java/org/apache/sling/fileoptim/BaseFileOptimizerTest.java
@@ -0,0 +1,156 @@
+/*
+ * 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.fileoptim;
+
+import java.io.InputStream;
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.fileoptim.impl.FileOptimizerServiceImpl;
+import org.apache.sling.fileoptim.models.OptimizedFile;
+import org.apache.sling.fileoptim.optimizers.JpegFileOptimizer;
+import org.mockito.Mockito;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class BaseFileOptimizerTest {
+
+ private static final Logger log = LoggerFactory.getLogger(BaseFileOptimizerTest.class);
+ public FileOptimizerServiceImpl fileOptimizerService;
+
+ private final Map<String, InputStream> FILES = new HashMap<String, InputStream>() {
+ private static final long serialVersionUID = 1L;
+ {
+ put("jpeg", BaseFileOptimizerTest.class.getClassLoader()
+ .getResourceAsStream("valentino-funghi-41239-unsplash.jpg"));
+ put("png", BaseFileOptimizerTest.class.getClassLoader()
+ .getResourceAsStream("Screen Shot 2018-05-29 at 4.17.20 PM.png"));
+ }
+ };
+
+ public BaseFileOptimizerTest() throws InvalidSyntaxException {
+ log.trace("BaseFileOptimizerTest()");
+ fileOptimizerService = new FileOptimizerServiceImpl();
+
+ // mock up the basic OSGi stuff
+ ComponentContext context = Mockito.mock(ComponentContext.class);
+ BundleContext bundleContext = Mockito.mock(BundleContext.class);
+ Mockito.when(context.getBundleContext()).thenReturn(bundleContext);
+
+ // allow for populating the service cache
+ @SuppressWarnings("unchecked")
+ ServiceReference<FileOptimizer> ref = Mockito.mock(ServiceReference.class);
+ Mockito.when(ref.getProperty(FileOptimizer.MIME_TYPE)).thenReturn("image/jpeg");
+ Collection<ServiceReference<FileOptimizer>> references = new ArrayList<ServiceReference<FileOptimizer>>();
+ references.add(ref);
+ Mockito.when(bundleContext.getServiceReferences(FileOptimizer.class, null)).thenReturn(references);
+
+ // allow for retrieving the service
+ JpegFileOptimizer jpegFileOptimizer = new JpegFileOptimizer();
+ jpegFileOptimizer.activate(new org.apache.sling.fileoptim.optimizers.JpegFileOptimizer.Config() {
+ {
+ }
+
+ @Override
+ public Class<? extends Annotation> annotationType() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public float compressionLevel() {
+ return 0.7f;
+ }
+ });
+ Mockito.when(bundleContext.getService(ref)).thenReturn(jpegFileOptimizer);
+
+ fileOptimizerService.activate(context, new org.apache.sling.fileoptim.impl.FileOptimizerServiceImpl.Config() {
+ public Class<? extends Annotation> annotationType() {
+ return null;
+ }
+
+ public String hashAlgorithm() {
+ return "MD5";
+ }
+ });
+
+ }
+
+ private Resource getValidFile(String type) {
+ Resource fileResource = Mockito.mock(Resource.class);
+ Mockito.when(fileResource.getName()).thenReturn("file." + type);
+
+ Resource contentResource = Mockito.mock(Resource.class);
+ Mockito.when(fileResource.getChild(JcrConstants.JCR_CONTENT)).thenReturn(contentResource);
+
+ OptimizedFile of = Mockito.mock(OptimizedFile.class);
+ Mockito.when(contentResource.adaptTo(OptimizedFile.class)).thenReturn(of);
+ Mockito.when(of.getHash()).thenReturn(null);
+ Mockito.when(of.getContent()).thenReturn(FILES.get(type));
+ Mockito.when(of.getDisabled()).thenReturn(false);
+ Mockito.when(of.getMimeType()).thenReturn("image/" + type);
+ return fileResource;
+ }
+
+ public Resource getOptimizedFile() {
+ Resource fileResource = Mockito.mock(Resource.class);
+ Mockito.when(fileResource.getName()).thenReturn("file.png");
+
+ Resource contentResource = Mockito.mock(Resource.class);
+ Mockito.when(fileResource.getChild(JcrConstants.JCR_CONTENT)).thenReturn(contentResource);
+
+ OptimizedFile of = Mockito.mock(OptimizedFile.class);
+ Mockito.when(contentResource.adaptTo(OptimizedFile.class)).thenReturn(of);
+ Mockito.when(of.getHash()).thenReturn(null);
+ Mockito.when(of.getContent()).thenReturn(FILES.get("png"));
+ Mockito.when(of.getDisabled()).thenReturn(false);
+ Mockito.when(of.getMimeType()).thenReturn("image/png");
+ Mockito.when(of.getHash()).thenReturn("M8SdtVLx2VXMDtteUPLVkQ==");
+ return fileResource;
+ }
+
+ public Resource getDisabled() {
+ Resource fileResource = Mockito.mock(Resource.class);
+ Mockito.when(fileResource.getName()).thenReturn("file.jpg");
+
+ Resource contentResource = Mockito.mock(Resource.class);
+ Mockito.when(fileResource.getChild(JcrConstants.JCR_CONTENT)).thenReturn(contentResource);
+
+ OptimizedFile of = Mockito.mock(OptimizedFile.class);
+ Mockito.when(contentResource.adaptTo(OptimizedFile.class)).thenReturn(of);
+ Mockito.when(of.getDisabled()).thenReturn(true);
+ Mockito.when(of.getMimeType()).thenReturn("image/jpeg");
+ return fileResource;
+ }
+
+ public Resource getValidJpegFile() {
+ return getValidFile("jpeg");
+ }
+
+ public Resource getValidPngFile() {
+ return getValidFile("png");
+ }
+}
diff --git a/file-optim/src/test/java/org/apache/sling/fileoptim/impl/TestFileOptimizer.java b/file-optim/src/test/java/org/apache/sling/fileoptim/impl/TestFileOptimizer.java
new file mode 100644
index 0000000..92098b2
--- /dev/null
+++ b/file-optim/src/test/java/org/apache/sling/fileoptim/impl/TestFileOptimizer.java
@@ -0,0 +1,77 @@
+/*
+ * 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.fileoptim.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+
+import org.apache.sling.fileoptim.BaseFileOptimizerTest;
+import org.apache.sling.fileoptim.OptimizationResult;
+import org.junit.Test;
+import org.osgi.framework.InvalidSyntaxException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestFileOptimizer extends BaseFileOptimizerTest {
+
+ public TestFileOptimizer() throws InvalidSyntaxException {
+ super();
+ }
+
+ private static Logger log = LoggerFactory.getLogger(TestFileOptimizer.class);
+
+ @Test
+ public void testCanOptimize() {
+ log.info("testCanOptimize");
+ assertTrue(fileOptimizerService.canOptimize(getValidJpegFile()));
+ }
+
+ @Test
+ public void testCantOptimize() {
+ log.info("testCantOptimize");
+ assertFalse(fileOptimizerService.canOptimize(getValidPngFile()));
+ }
+
+ @Test
+ public void testDisabled() {
+ log.info("testDisabled");
+ assertFalse(fileOptimizerService.canOptimize(getDisabled()));
+ }
+
+
+ @Test
+ public void isOptimized() {
+ log.info("isOptimized");
+ assertTrue(fileOptimizerService.isOptimized(super.getOptimizedFile()));
+ }
+
+ @Test
+ public void testGetOptimizedContents() throws IOException {
+ log.info("testGetOptimizedContents");
+ OptimizationResult res = fileOptimizerService.getOptimizedContents(getValidJpegFile());
+ assertNotNull(res);
+ assertTrue(res.isOptimized());
+ assertNotNull(res.getOptimizedContents());
+ assertEquals(res.getOptimizedContents().length, res.getOptimizedSize());
+ assertTrue(res.getSavings() > 0.0 && res.getSavings() < 1.0);
+ }
+
+}
diff --git a/file-optim/src/test/java/org/apache/sling/fileoptim/impl/servlets/TestFileOptimizerData.java b/file-optim/src/test/java/org/apache/sling/fileoptim/impl/servlets/TestFileOptimizerData.java
new file mode 100644
index 0000000..c39a22a
--- /dev/null
+++ b/file-optim/src/test/java/org/apache/sling/fileoptim/impl/servlets/TestFileOptimizerData.java
@@ -0,0 +1,85 @@
+/*
+ * 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.fileoptim.impl.servlets;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.lang.reflect.Field;
+
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.fileoptim.BaseFileOptimizerTest;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.InvalidSyntaxException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestFileOptimizerData extends BaseFileOptimizerTest {
+
+ public TestFileOptimizerData() throws InvalidSyntaxException {
+ super();
+ log.trace("TestFileOptimizerData()");
+ }
+
+ private static final Logger log = LoggerFactory.getLogger(TestFileOptimizerData.class);
+ private static final String PATH = "/content/file.jpg";
+ private StringWriter writer;
+ private SlingHttpServletRequest request;
+ private SlingHttpServletResponse response;
+ private FileOptimizerData servlet;
+
+ @Before
+ public void initTest() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException,
+ IllegalAccessException {
+ log.trace("initTest");
+ request = Mockito.mock(SlingHttpServletRequest.class);
+ Mockito.when(request.getParameter("path")).thenReturn(PATH);
+ ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
+ Mockito.when(request.getResourceResolver()).thenReturn(resolver);
+ Resource jpegResource = getValidJpegFile();
+ Mockito.when(resolver.getResource(PATH)).thenReturn(jpegResource);
+
+ this.writer = new StringWriter();
+ response = Mockito.mock(SlingHttpServletResponse.class);
+ Mockito.when(response.getWriter()).thenReturn(new PrintWriter(writer));
+
+ servlet = new FileOptimizerData();
+ Field fo = FileOptimizerData.class.getDeclaredField("fileOptimizer");
+ fo.setAccessible(true);
+ fo.set(servlet, this.fileOptimizerService);
+ }
+
+ @Test
+ public void testServlet() throws ServletException, IOException {
+
+ servlet.doGet(request, response);
+
+ String response = writer.toString();
+ assertNotNull(response);
+ log.info(response);
+
+ }
+}
diff --git a/file-optim/src/test/resources/simplelogger.properties b/file-optim/src/test/resources/simplelogger.properties
new file mode 100644
index 0000000..e6cb9e8
--- /dev/null
+++ b/file-optim/src/test/resources/simplelogger.properties
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+org.slf4j.simpleLogger.defaultLogLevel=trace
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
dklco@apache.org.