This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.fsresource-2.0.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-fsresource.git
commit 17c73925e657b3b05ed8628188874c084e753bb1 Author: Stefan Seifert <[email protected]> AuthorDate: Fri Feb 17 17:45:13 2017 +0000 SLING-6526 Switch back to Sling API 2.11.0 add unit test coverage git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/fsresource@1783439 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 45 +++++- .../sling/fsprovider/internal/FileMonitor.java | 4 +- .../fsprovider/internal/FsResourceProvider.java | 1 + .../sling/fsprovider/internal/FileMonitorTest.java | 174 +++++++++++++++++++++ .../sling/fsprovider/internal/FilesFolderTest.java | 76 +++++++++ .../sling/fsprovider/internal/JcrMixedTest.java | 103 ++++++++++++ .../sling/fsprovider/internal/TestUtils.java | 87 +++++++++++ src/test/resources/fs-test/folder1/file1a.txt | 1 + src/test/resources/fs-test/folder1/file1b.txt | 1 + .../resources/fs-test/folder1/folder11/file11a.txt | 1 + src/test/resources/fs-test/folder2/file2a.txt | 1 + src/test/resources/simplelogger.properties | 19 +++ 12 files changed, 510 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index c7e99ac..1353c6c 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,31 @@ <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> + <executions> + <!-- Configure extra execution of 'manifest' in process-classes phase to make sure SCR metadata is generated before unit test runs --> + <execution> + <id>scr-metadata</id> + <goals> + <goal>manifest</goal> + </goals> + <configuration> + <supportIncrementalBuild>true</supportIncrementalBuild> + </configuration> + </execution> + </executions> + <configuration> + <!-- Export SCR metadata to classpath to have them available in unit tests --> + <exportScr>true</exportScr> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.rat</groupId> + <artifactId>apache-rat-plugin</artifactId> + <configuration> + <excludes> + <exclude>src/test/resources/fs-test/**</exclude> + </excludes> + </configuration> </plugin> </plugins> </build> @@ -72,7 +97,7 @@ <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.api</artifactId> - <version>2.15.0</version> + <version>2.11.0</version> </dependency> <dependency> <groupId>org.apache.sling</groupId> @@ -97,5 +122,23 @@ <version>1.0.0</version> <scope>provided</scope> </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.testing.sling-mock</artifactId> + <version>2.2.4</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.testing.logging-mock</artifactId> + <version>2.0.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.testing.hamcrest</artifactId> + <version>1.0.2</version> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/src/main/java/org/apache/sling/fsprovider/internal/FileMonitor.java b/src/main/java/org/apache/sling/fsprovider/internal/FileMonitor.java index 5729b7c..c964359 100644 --- a/src/main/java/org/apache/sling/fsprovider/internal/FileMonitor.java +++ b/src/main/java/org/apache/sling/fsprovider/internal/FileMonitor.java @@ -203,8 +203,8 @@ public class FileMonitor extends TimerTask { for(final ObserverConfiguration config : reporter.getObserverConfigurations()) { if ( config.matches(monitorable.path) ) { - final ResourceChange change = new ResourceChange(changeType, monitorable.path, false); - reporter.reportChanges(config, Collections.singleton(change), false); + final ResourceChange change = new ResourceChange(changeType, monitorable.path, false, null, null, null); + reporter.reportChanges(Collections.singleton(change), false); } } } diff --git a/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java b/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java index 4a32737..f2ed39a 100644 --- a/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java +++ b/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java @@ -154,6 +154,7 @@ public class FsResourceProvider extends ResourceProvider<Object> { /** * Returns an iterator of resources. */ + @SuppressWarnings("unchecked") @Override public Iterator<Resource> listChildren(final ResolveContext<Object> ctx, final Resource parent) { File parentFile = parent.adaptTo(File.class); diff --git a/src/test/java/org/apache/sling/fsprovider/internal/FileMonitorTest.java b/src/test/java/org/apache/sling/fsprovider/internal/FileMonitorTest.java new file mode 100644 index 0000000..3201c3f --- /dev/null +++ b/src/test/java/org/apache/sling/fsprovider/internal/FileMonitorTest.java @@ -0,0 +1,174 @@ +/* + * 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.fsprovider.internal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.FileUtils; +import org.apache.sling.api.resource.observation.ResourceChange; +import org.apache.sling.api.resource.observation.ResourceChange.ChangeType; +import org.apache.sling.api.resource.observation.ResourceChangeListener; +import org.apache.sling.testing.mock.sling.ResourceResolverType; +import org.apache.sling.testing.mock.sling.junit.SlingContext; +import org.apache.sling.testing.mock.sling.junit.SlingContextBuilder; +import org.apache.sling.testing.mock.sling.junit.SlingContextCallback; +import org.junit.Rule; +import org.junit.Test; + +/** + * Test events when changing filesystem content. + */ +public class FileMonitorTest { + + private final File tempDir; + private final ResourceListener resourceListener = new ResourceListener(); + + public FileMonitorTest() throws Exception { + tempDir = Files.createTempDirectory(getClass().getName()).toFile(); + } + + @Rule + public SlingContext context = new SlingContextBuilder(ResourceResolverType.JCR_MOCK) + .beforeSetUp(new SlingContextCallback() { + @Override + public void execute(SlingContext context) throws Exception { + // copy test content to temp. directory + tempDir.mkdirs(); + File sourceDir = new File("src/test/resources/fs-test"); + FileUtils.copyDirectory(sourceDir, tempDir); + + // mount temp. directory + context.registerInjectActivateService(new FsResourceProvider(), + "provider.file", tempDir.getPath(), + "provider.root", "/fs-test", + "provider.checkinterval", 120); + + // register resource change listener + context.registerService(ResourceChangeListener.class, resourceListener, + ResourceChangeListener.PATHS, new String[] { "/fs-test" }); + } + }) + .afterTearDown(new SlingContextCallback() { + @Override + public void execute(SlingContext context) throws Exception { + // remove temp directory + tempDir.delete(); + } + }) + .build(); + + @Test + public void testUpdateFile() throws Exception { + List<ResourceChange> changes = resourceListener.getChanges(); + assertTrue(changes.isEmpty()); + + File file1a = new File(tempDir, "folder1/file1a.txt"); + FileUtils.write(file1a, "newcontent"); + + Thread.sleep(250); + + assertEquals(1, changes.size()); + assertChange(changes, 0, "/fs-test/folder1/file1a.txt", ChangeType.CHANGED); + } + + @Test + public void testAddFile() throws Exception { + List<ResourceChange> changes = resourceListener.getChanges(); + assertTrue(changes.isEmpty()); + + File file1c = new File(tempDir, "folder1/file1c.txt"); + FileUtils.write(file1c, "newcontent"); + + Thread.sleep(250); + + assertEquals(2, changes.size()); + assertChange(changes, 0, "/fs-test/folder1", ChangeType.CHANGED); + assertChange(changes, 1, "/fs-test/folder1/file1c.txt", ChangeType.ADDED); + } + + @Test + public void testRemoveFile() throws Exception { + List<ResourceChange> changes = resourceListener.getChanges(); + assertTrue(changes.isEmpty()); + + File file1a = new File(tempDir, "folder1/file1a.txt"); + file1a.delete(); + + Thread.sleep(250); + + assertEquals(2, changes.size()); + assertChange(changes, 0, "/fs-test/folder1", ChangeType.CHANGED); + assertChange(changes, 1, "/fs-test/folder1/file1a.txt", ChangeType.REMOVED); + } + + @Test + public void testAddFolder() throws Exception { + List<ResourceChange> changes = resourceListener.getChanges(); + assertTrue(changes.isEmpty()); + + File folder3 = new File(tempDir, "folder3"); + folder3.mkdir(); + + Thread.sleep(250); + + assertEquals(2, changes.size()); + assertChange(changes, 0, "/fs-test", ChangeType.CHANGED); + assertChange(changes, 1, "/fs-test/folder3", ChangeType.ADDED); + } + + @Test + public void testRemoveFolder() throws Exception { + List<ResourceChange> changes = resourceListener.getChanges(); + assertTrue(changes.isEmpty()); + + File folder1 = new File(tempDir, "folder1"); + FileUtils.deleteDirectory(folder1); + + Thread.sleep(250); + + assertEquals(2, changes.size()); + assertChange(changes, 0, "/fs-test", ChangeType.CHANGED); + assertChange(changes, 1, "/fs-test/folder1", ChangeType.REMOVED); + } + + + private void assertChange(List<ResourceChange> changes, int index, String path, ChangeType changeType) { + ResourceChange change = changes.get(index); + assertEquals(path, change.getPath()); + assertEquals(changeType, change.getType()); + } + + static class ResourceListener implements ResourceChangeListener { + private final List<ResourceChange> allChanges = new ArrayList<>(); + @Override + public void onChange(List<ResourceChange> changes) { + allChanges.addAll(changes); + } + public List<ResourceChange> getChanges() { + return allChanges; + } + } + +} diff --git a/src/test/java/org/apache/sling/fsprovider/internal/FilesFolderTest.java b/src/test/java/org/apache/sling/fsprovider/internal/FilesFolderTest.java new file mode 100644 index 0000000..d837d6e --- /dev/null +++ b/src/test/java/org/apache/sling/fsprovider/internal/FilesFolderTest.java @@ -0,0 +1,76 @@ +/* + * 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.fsprovider.internal; + +import static org.apache.sling.fsprovider.internal.TestUtils.REGISTER_FSRESOURCE_PLUGIN; +import static org.apache.sling.fsprovider.internal.TestUtils.assertFile; +import static org.apache.sling.fsprovider.internal.TestUtils.assertFolder; +import static org.junit.Assert.assertThat; + +import org.apache.sling.api.resource.Resource; +import org.apache.sling.hamcrest.ResourceMatchers; +import org.apache.sling.testing.mock.sling.ResourceResolverType; +import org.apache.sling.testing.mock.sling.junit.SlingContext; +import org.apache.sling.testing.mock.sling.junit.SlingContextBuilder; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +/** + * Test access to files and folders from filesystem. + */ +public class FilesFolderTest { + + private Resource root; + private Resource fsroot; + + @Rule + public SlingContext context = new SlingContextBuilder(ResourceResolverType.JCR_MOCK) + .plugin(REGISTER_FSRESOURCE_PLUGIN) + .build(); + + @Before + public void setUp() { + root = context.resourceResolver().getResource("/"); + fsroot = context.resourceResolver().getResource("/fs-test"); + } + + @Test + public void testFolders() { + assertFolder(fsroot, "folder1"); + assertFolder(fsroot, "folder1/folder11"); + assertFolder(fsroot, "folder2"); + } + + @Test + public void testFiles() { + assertFile(fsroot, "folder1/file1a.txt", "file1a"); + assertFile(fsroot, "folder1/file1b.txt", "file1b"); + assertFile(fsroot, "folder1/folder11/file11a.txt", "file11a"); + assertFile(fsroot, "folder2/file2a.txt", "file2a"); + } + + @Test + public void testListChildren() { + assertThat(root, ResourceMatchers.containsChildren("fs-test")); + assertThat(fsroot, ResourceMatchers.hasChildren("folder1", "folder2")); + assertThat(fsroot.getChild("folder1"), ResourceMatchers.hasChildren("file1a.txt", "file1b.txt")); + } + +} diff --git a/src/test/java/org/apache/sling/fsprovider/internal/JcrMixedTest.java b/src/test/java/org/apache/sling/fsprovider/internal/JcrMixedTest.java new file mode 100644 index 0000000..41bdbae --- /dev/null +++ b/src/test/java/org/apache/sling/fsprovider/internal/JcrMixedTest.java @@ -0,0 +1,103 @@ +/* + * 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.fsprovider.internal; + +import static org.apache.sling.fsprovider.internal.TestUtils.REGISTER_FSRESOURCE_PLUGIN; +import static org.apache.sling.fsprovider.internal.TestUtils.assertFile; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; + +import javax.jcr.Node; +import javax.jcr.RepositoryException; + +import org.apache.sling.api.resource.Resource; +import org.apache.sling.hamcrest.ResourceMatchers; +import org.apache.sling.testing.mock.sling.ResourceResolverType; +import org.apache.sling.testing.mock.sling.junit.SlingContext; +import org.apache.sling.testing.mock.sling.junit.SlingContextBuilder; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +/** + * Test access mixed with JCR content on same path. + */ +public class JcrMixedTest { + + private Resource root; + private Resource fsroot; + + @Rule + public SlingContext context = new SlingContextBuilder(ResourceResolverType.JCR_MOCK) + .plugin(REGISTER_FSRESOURCE_PLUGIN) + .build(); + + @Before + public void setUp() throws RepositoryException { + root = context.resourceResolver().getResource("/"); + fsroot = context.resourceResolver().getResource("/fs-test"); + + // prepare mixed JCR content + Node node = root.adaptTo(Node.class); + Node fstest = node.addNode("fs-test", "nt:folder"); + // folder1 + Node folder1 = fstest.addNode("folder1", "nt:folder"); + folder1.setProperty("prop1", "value1"); + folder1.setProperty("prop2", 123L); + // folder1/file1a.txt + Node file1a = folder1.addNode("file1a.txt", "nt:file"); + file1a.setProperty("prop1", "value2"); + file1a.setProperty("prop2", 234L); + // folder1/file1c.txt + folder1.addNode("file1c.txt", "nt:file"); + // folder3 + fstest.addNode("folder3", "nt:folder"); + } + + @Test + public void testFolders() { + // expected properties from JCR for folders + Resource folder1 = fsroot.getChild("folder1"); + assertThat(folder1, ResourceMatchers.props("jcr:primaryType", "nt:folder", + "prop1", "value1", + "prop2", 123L)); + } + + @Test + public void testFiles() { + assertFile(fsroot, "folder1/file1a.txt", "file1a"); + assertFile(fsroot, "folder1/file1b.txt", "file1b"); + assertFile(fsroot, "folder1/folder11/file11a.txt", "file11a"); + assertFile(fsroot, "folder2/file2a.txt", "file2a"); + + // do not expected properties from JCR for files + Resource file1a = fsroot.getChild("folder1/file1a.txt"); + assertThat(file1a, not(ResourceMatchers.props( + "prop1", "value2", + "prop2", 234L))); + } + + @Test + public void testListChildren() { + assertThat(root, ResourceMatchers.containsChildren("fs-test")); + assertThat(fsroot, ResourceMatchers.hasChildren("folder1", "folder2", "folder3")); + assertThat(fsroot.getChild("folder1"), ResourceMatchers.hasChildren("file1a.txt", "file1b.txt", "file1c.txt")); + } + +} diff --git a/src/test/java/org/apache/sling/fsprovider/internal/TestUtils.java b/src/test/java/org/apache/sling/fsprovider/internal/TestUtils.java new file mode 100644 index 0000000..8d9785e --- /dev/null +++ b/src/test/java/org/apache/sling/fsprovider/internal/TestUtils.java @@ -0,0 +1,87 @@ +/* + * 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.fsprovider.internal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.CharEncoding; +import org.apache.commons.lang3.StringUtils; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.hamcrest.ResourceMatchers; +import org.apache.sling.testing.mock.osgi.context.AbstractContextPlugin; +import org.apache.sling.testing.mock.osgi.context.ContextPlugin; +import org.apache.sling.testing.mock.sling.context.SlingContextImpl; + +class TestUtils { + + public static ContextPlugin<SlingContextImpl> REGISTER_FSRESOURCE_PLUGIN = new AbstractContextPlugin<SlingContextImpl>() { + @Override + public void beforeSetUp(SlingContextImpl context) throws Exception { + context.registerInjectActivateService(new FsResourceProvider(), + "provider.file", "src/test/resources/fs-test", + "provider.root", "/fs-test", + "provider.checkinterval", 0); + } + }; + + public static void assertFolder(Resource resource, String path) { + Resource folder = resource.getChild(path); + assertNotNull(path, folder); + + assertThat(folder, ResourceMatchers.props("jcr:primaryType", "nt:folder")); + assertEquals("nt:folder", folder.getResourceType()); + + assertNull(folder.getResourceSuperType()); + assertEquals(folder.getName(), folder.adaptTo(File.class).getName()); + assertTrue(StringUtils.contains(folder.adaptTo(URL.class).toString(), folder.getName())); + } + + public static void assertFile(Resource resource, String path, String content) { + Resource file = resource.getChild(path); + assertNotNull(path, file); + + assertThat(file, ResourceMatchers.props("jcr:primaryType", "nt:file")); + assertEquals("nt:file", file.getResourceType()); + + try { + try (InputStream is = file.adaptTo(InputStream.class)) { + String data = IOUtils.toString(is, CharEncoding.UTF_8); + assertEquals(content, data); + } + } + catch (IOException ex) { + throw new RuntimeException(ex); + } + + assertNull(file.getResourceSuperType()); + assertEquals(file.getName(), file.adaptTo(File.class).getName()); + assertTrue(StringUtils.contains(file.adaptTo(URL.class).toString(), file.getName())); + } + +} diff --git a/src/test/resources/fs-test/folder1/file1a.txt b/src/test/resources/fs-test/folder1/file1a.txt new file mode 100644 index 0000000..9f0f26f --- /dev/null +++ b/src/test/resources/fs-test/folder1/file1a.txt @@ -0,0 +1 @@ +file1a \ No newline at end of file diff --git a/src/test/resources/fs-test/folder1/file1b.txt b/src/test/resources/fs-test/folder1/file1b.txt new file mode 100644 index 0000000..518f63d --- /dev/null +++ b/src/test/resources/fs-test/folder1/file1b.txt @@ -0,0 +1 @@ +file1b \ No newline at end of file diff --git a/src/test/resources/fs-test/folder1/folder11/file11a.txt b/src/test/resources/fs-test/folder1/folder11/file11a.txt new file mode 100644 index 0000000..4940b5d --- /dev/null +++ b/src/test/resources/fs-test/folder1/folder11/file11a.txt @@ -0,0 +1 @@ +file11a \ No newline at end of file diff --git a/src/test/resources/fs-test/folder2/file2a.txt b/src/test/resources/fs-test/folder2/file2a.txt new file mode 100644 index 0000000..e2baf26 --- /dev/null +++ b/src/test/resources/fs-test/folder2/file2a.txt @@ -0,0 +1 @@ +file2a \ No newline at end of file diff --git a/src/test/resources/simplelogger.properties b/src/test/resources/simplelogger.properties new file mode 100644 index 0000000..e62c1ea --- /dev/null +++ b/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=warn +org.slf4j.simpleLogger.log.org.apache.sling.fsprovider.internal=warn -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
