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 2016/11/24 16:09:22 UTC
svn commit: r1771170 - in /sling/trunk/installer/providers/jcr: ./
src/test/java/org/apache/sling/installer/provider/jcr/impl/
Author: rombert
Date: Thu Nov 24 16:09:22 2016
New Revision: 1771170
URL: http://svn.apache.org/viewvc?rev=1771170&view=rev
Log:
SLING-5228 - Remove loginAdministrative() usage from
org.apache.sling.installer.provider.jcr
Since the bundle is now using loginService instead of loginAdminstrative
and the commons.testing bundle does not provide support for that I have
moved the tests to use the Sling mocks.
This allows the removal of large chunks of test code. I also took the
opportunity to consolidate more tests under the same superclass where it
made sense.
As a side effect, the code now requires Java 7 but that should be ok.
Modified:
sling/trunk/installer/providers/jcr/pom.xml
sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/FindPathsToWatchTest.java
sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderDetectionTest.java
sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java
sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java
sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/ResourceDetectionTest.java
sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/ScanningLoopTest.java
Modified: sling/trunk/installer/providers/jcr/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/pom.xml?rev=1771170&r1=1771169&r2=1771170&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/pom.xml (original)
+++ sling/trunk/installer/providers/jcr/pom.xml Thu Nov 24 16:09:22 2016
@@ -38,7 +38,8 @@
</description>
<properties>
- <jackrabbit.version>2.7.5</jackrabbit.version>
+ <jackrabbit.version>2.10.0</jackrabbit.version>
+ <sling.java.version>7</sling.java.version>
</properties>
<scm>
@@ -109,7 +110,7 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.jcr.api</artifactId>
- <version>2.0.6</version>
+ <version>2.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -175,5 +176,22 @@
</exclusion>
</exclusions>
</dependency>
+ <!--
+ Testing is done with Jackrabbit and not with Oak since the tests are not prepared
+ for multi-threaded observation event delivery which leads to checks like
+ eventCounter.waitForEvent() unblocking too early
+ -->
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.sling-mock-jackrabbit</artifactId>
+ <version>1.0.0</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
+ <version>1.7.2</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
Modified: sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/FindPathsToWatchTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/FindPathsToWatchTest.java?rev=1771170&r1=1771169&r2=1771170&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/FindPathsToWatchTest.java (original)
+++ sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/FindPathsToWatchTest.java Thu Nov 24 16:09:22 2016
@@ -18,52 +18,19 @@
*/
package org.apache.sling.installer.provider.jcr.impl;
-import java.util.Collection;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
-import javax.jcr.Session;
+import java.util.Collection;
-import org.apache.sling.commons.testing.jcr.EventHelper;
-import org.apache.sling.commons.testing.jcr.RepositoryTestBase;
-import org.apache.sling.jcr.api.SlingRepository;
+import org.junit.Test;
/** Verify that the JcrInstaller finds all folders that must
* be watched, including those created after it starts
*/
-public class FindPathsToWatchTest extends RepositoryTestBase {
-
- public static final long TIMEOUT = 5000L;
-
- SlingRepository repo;
- Session session;
- private EventHelper eventHelper;
- private ContentHelper contentHelper;
- private JcrInstaller installer;
- private MockOsgiInstaller osgiInstaller;
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- repo = getRepository();
- session = repo.loginAdministrative(repo.getDefaultWorkspace());
- eventHelper = new EventHelper(session);
- contentHelper = new ContentHelper(session);
- contentHelper.cleanupContent();
- contentHelper.setupContent();
- osgiInstaller = new MockOsgiInstaller();
- installer = MiscUtil.getJcrInstaller(repo, osgiInstaller);
- }
-
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
- contentHelper.cleanupContent();
- session.logout();
- eventHelper = null;
- contentHelper = null;
- installer.deactivate(MiscUtil.getMockComponentContext());
- MiscUtil.waitForInstallerThread(installer, TIMEOUT);
- }
-
+public class FindPathsToWatchTest extends JcrInstallTestBase {
+
private boolean isWatched(String path, Collection<WatchedFolder> wfList) {
for(WatchedFolder wf : wfList ) {
if(wf.getPath().equals(path)) {
@@ -73,6 +40,7 @@ public class FindPathsToWatchTest extend
return false;
}
+ @Test
public void testInitialFind() throws Exception {
final Collection<WatchedFolder> wf = MiscUtil.getWatchedFolders(installer);
assertEquals("activate() must find all watched folders", contentHelper.WATCHED_FOLDERS.length, wf.size());
@@ -83,6 +51,7 @@ public class FindPathsToWatchTest extend
}
}
+ @Test
public void testNewWatchedFolderDetection() throws Exception {
final String newPaths [] = {
"/libs/tnwf/install",
@@ -122,6 +91,7 @@ public class FindPathsToWatchTest extend
MiscUtil.getWatchedFolders(installer).size());
}
+ @Test
public void testDeleteWatchedFolders() throws Exception {
assertEquals("activate() must find all watched folders", contentHelper.WATCHED_FOLDERS.length, MiscUtil.getWatchedFolders(installer).size());
contentHelper.cleanupContent();
Modified: sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderDetectionTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderDetectionTest.java?rev=1771170&r1=1771169&r2=1771170&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderDetectionTest.java (original)
+++ sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderDetectionTest.java Thu Nov 24 16:09:22 2016
@@ -18,6 +18,9 @@
*/
package org.apache.sling.installer.provider.jcr.impl;
+import static org.junit.Assert.assertFalse;
+
+import org.junit.Test;
/** Test that changes in folders to watch are correctly detected,
* including when root folders are created or deleted
@@ -28,6 +31,7 @@ public class FolderDetectionTest extends
return false;
}
+ @Test
public void testCreateAndDeleteLibs() throws Exception {
final String res = "/libs/foo/install/somefile.jar";
assertRegistered("Before test", res, false);
@@ -47,6 +51,7 @@ public class FolderDetectionTest extends
assertRegistered("After deleting libs", res, false);
}
+ @Test
public void testMoveLibsToFoo() throws Exception {
final String res = "/libs/foo/install/somefile.jar";
assertRegistered("Before test", res, false);
@@ -79,6 +84,7 @@ public class FolderDetectionTest extends
MiscUtil.waitAfterContentChanges(eventHelper, installer);
}
+ @Test
public void testMoveLibsToApps() throws Exception {
final String res = "/libs/foo/install/somefile.jar";
final String appsRes = "/apps/foo/install/somefile.jar";
Modified: sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java?rev=1771170&r1=1771169&r2=1771170&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java (original)
+++ sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java Thu Nov 24 16:09:22 2016
@@ -18,28 +18,36 @@
*/
package org.apache.sling.installer.provider.jcr.impl;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
import javax.jcr.Session;
import org.apache.sling.commons.testing.jcr.EventHelper;
-import org.apache.sling.commons.testing.jcr.RepositoryTestBase;
-import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.installer.api.OsgiInstaller;
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
/** Base test class with common utilities */
-abstract class JcrInstallTestBase extends RepositoryTestBase {
+public abstract class JcrInstallTestBase {
public static final long TIMEOUT = 5000L;
+
+ @Rule
+ public final SlingContext context = new SlingContext(ResourceResolverType.JCR_JACKRABBIT);
- SlingRepository repo;
- Session session;
+ protected Session session;
protected EventHelper eventHelper;
protected ContentHelper contentHelper;
protected JcrInstaller installer;
protected MockOsgiInstaller osgiInstaller;
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- repo = getRepository();
- session = repo.loginAdministrative(repo.getDefaultWorkspace());
+ @Before
+ public void setUp() throws Exception {
+
+ session = context.resourceResolver().adaptTo(Session.class);
eventHelper = new EventHelper(session);
contentHelper = new ContentHelper(session);
contentHelper.cleanupContent();
@@ -47,21 +55,27 @@ abstract class JcrInstallTestBase extend
contentHelper.setupContent();
}
osgiInstaller = new MockOsgiInstaller();
- installer = MiscUtil.getJcrInstaller(repo, osgiInstaller);
+ context.registerService(OsgiInstaller.class, osgiInstaller);
+ context.runMode(MiscUtil.RUN_MODES);
+
+ installer = new JcrInstaller();
+ context.registerInjectActivateService(installer);
+ Thread.sleep(1000);
}
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void tearDown() throws Exception {
+
contentHelper.cleanupContent();
- session.logout();
eventHelper = null;
contentHelper = null;
- installer.deactivate(MiscUtil.getMockComponentContext());
+ installer.deactivate(context.componentContext());
MiscUtil.waitForInstallerThread(installer, TIMEOUT);
}
- protected abstract boolean needsTestContent();
+ protected boolean needsTestContent() {
+ return true;
+ }
protected void assertRegisteredPaths(String [] paths) {
for(String path : paths) {
Modified: sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java?rev=1771170&r1=1771169&r2=1771170&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java (original)
+++ sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java Thu Nov 24 16:09:22 2016
@@ -18,71 +18,16 @@
*/
package org.apache.sling.installer.provider.jcr.impl;
-import java.lang.reflect.Field;
import java.util.Collection;
-import java.util.Dictionary;
-import java.util.Hashtable;
import org.apache.sling.commons.testing.jcr.EventHelper;
-import org.apache.sling.installer.api.OsgiInstaller;
-import org.apache.sling.jcr.api.SlingRepository;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
-import org.osgi.framework.BundleContext;
-import org.osgi.service.component.ComponentContext;
/** JcrInstall test utilities */
class MiscUtil {
- static final Mockery mockery = new Mockery();
-
public static String SEARCH_PATHS [] = { "/libs/", "/apps/" };
public static String RUN_MODES [] = { "dev", "staging" };
-
- /** Set a non-public Field */
- static void setField(Object target, String fieldName, Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
- final Field f = target.getClass().getDeclaredField(fieldName);
- f.setAccessible(true);
- f.set(target, value);
- }
-
- /** Return a JcrInstaller setup for testing */
- static synchronized JcrInstaller getJcrInstaller(SlingRepository repository,
- OsgiInstaller osgiInstaller) throws Exception {
- final JcrInstaller installer = new JcrInstaller();
- setField(installer, "repository", repository);
- setField(installer, "installer", osgiInstaller);
- setField(installer, "settings", new MockSettings(RUN_MODES));
-
- installer.activate(getMockComponentContext());
- Thread.sleep(1000);
- return installer;
- }
-
- private static ComponentContext COMPONENT_CONTEXT;
- static ComponentContext getMockComponentContext() {
- if ( COMPONENT_CONTEXT == null ) {
- // Setup fake ComponentContext to allow JcrInstaller to start
- final ComponentContext cc = mockery.mock(ComponentContext.class);
- final BundleContext bc = mockery.mock(BundleContext.class);
-
- final Dictionary<String, Object> emptyDict = new Hashtable<String, Object>();
- mockery.checking(new Expectations() {{
- allowing(cc).getProperties();
- will(returnValue(emptyDict));
- allowing(cc).getBundleContext();
- will(returnValue(bc));
- allowing(bc).getProperty(with(any(String.class)));
- will(returnValue(null));
- allowing(bc).registerService(with(any(String.class)), with(any(Object.class)), with(any(Dictionary.class)));
- will(returnValue(null));
- }});
- COMPONENT_CONTEXT = cc;
- }
- return COMPONENT_CONTEXT;
- }
-
static private void waitForCycles(JcrInstaller installer, long initialCycleCount, int expectedCycles, long timeoutMsec) throws Exception {
final long endTime = System.currentTimeMillis() + timeoutMsec;
long cycles = 0;
Modified: sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/ResourceDetectionTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/ResourceDetectionTest.java?rev=1771170&r1=1771169&r2=1771170&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/ResourceDetectionTest.java (original)
+++ sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/ResourceDetectionTest.java Thu Nov 24 16:09:22 2016
@@ -18,9 +18,14 @@
*/
package org.apache.sling.installer.provider.jcr.impl;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
import javax.jcr.Node;
import org.apache.sling.installer.provider.jcr.impl.JcrInstaller;
+import org.junit.Test;
import org.osgi.service.component.ComponentContext;
/** Test that added/updated/removed resources are
@@ -29,15 +34,13 @@ import org.osgi.service.component.Compon
*/
public class ResourceDetectionTest extends JcrInstallTestBase {
+ @Test
public void testInitialResourceDetection() throws Exception {
assertRegisteredPaths(contentHelper.FAKE_RESOURCES);
assertRegisteredPaths(contentHelper.FAKE_CONFIGS);
}
- protected boolean needsTestContent() {
- return true;
- }
-
+ @Test
public void testAddFiles() throws Exception {
final String [] paths = {
"/libs/foo/bar/install/" + System.currentTimeMillis() + ".jar",
@@ -55,6 +58,7 @@ public class ResourceDetectionTest exten
assertRegisteredPaths(paths);
}
+ @Test
public void testAddConfigs() throws Exception {
final String [] paths = {
"/libs/foo/bar/install/" + System.currentTimeMillis() + ".jar",
@@ -73,6 +77,7 @@ public class ResourceDetectionTest exten
assertRegisteredPaths(paths);
}
+ @Test
public void testDeleteResources() throws Exception {
assertRegisteredPaths(contentHelper.FAKE_RESOURCES);
assertRegisteredPaths(contentHelper.FAKE_CONFIGS);
@@ -96,10 +101,11 @@ public class ResourceDetectionTest exten
2, osgiInstaller.getRecordedCalls().size());
}
+ @Test
public void testStopAndRestart() throws Exception {
assertRegisteredPaths(contentHelper.FAKE_RESOURCES);
assertRegisteredPaths(contentHelper.FAKE_CONFIGS);
- final ComponentContext cc = MiscUtil.getMockComponentContext();
+ final ComponentContext cc = context.componentContext();
// With the installer deactivated, remove two resources and add some new ones
osgiInstaller.clearRecordedCalls();
@@ -149,6 +155,7 @@ public class ResourceDetectionTest exten
}
}
+ @Test
public void testFolderRemoval() throws Exception {
assertRegisteredPaths(contentHelper.FAKE_RESOURCES);
assertRegisteredPaths(contentHelper.FAKE_CONFIGS);
@@ -164,6 +171,7 @@ public class ResourceDetectionTest exten
}
}
+ @Test
public void testFileUpdate() throws Exception {
final String path = contentHelper.FAKE_RESOURCES[0];
assertRegistered(path, true);
@@ -186,6 +194,7 @@ public class ResourceDetectionTest exten
assertRecordedCall("add", path);
}
+ @Test
public void testConfigUpdate() throws Exception {
final String path = contentHelper.FAKE_CONFIGS[0];
assertRegistered(path, true);
Modified: sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/ScanningLoopTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/ScanningLoopTest.java?rev=1771170&r1=1771169&r2=1771170&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/ScanningLoopTest.java (original)
+++ sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/ScanningLoopTest.java Thu Nov 24 16:09:22 2016
@@ -19,53 +19,38 @@
package org.apache.sling.installer.provider.jcr.impl;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
import javax.jcr.Node;
-import javax.jcr.Session;
-import org.apache.sling.commons.testing.jcr.EventHelper;
-import org.apache.sling.commons.testing.jcr.RepositoryTestBase;
-import org.apache.sling.jcr.api.SlingRepository;
import org.junit.Test;
/** Verify that JcrInstaller scans folders only when needed */
-public class ScanningLoopTest extends RepositoryTestBase {
- public static final long TIMEOUT = 5000L;
-
- private JcrInstaller installer;
- private SlingRepository repository;
- private MockOsgiInstaller osgiInstaller;
- private ContentHelper contentHelper;
- private Session session;
- private EventHelper eventHelper;
-
- @Override
- public void setUp() throws Exception {
- super.setUp();
- repository = getRepository();
- osgiInstaller = new MockOsgiInstaller();
- installer = MiscUtil.getJcrInstaller(repository, osgiInstaller);
- session = repository.loginAdministrative(repository.getDefaultWorkspace());
- eventHelper = new EventHelper(session);
- contentHelper = new ContentHelper(session);
- contentHelper.setupFolders();
- }
-
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
- installer.deactivate(MiscUtil.getMockComponentContext());
- MiscUtil.waitForInstallerThread(installer, TIMEOUT);
- installer = null;
- contentHelper.cleanupContent();
- contentHelper.deleteQuietly(JcrInstaller.PAUSE_SCAN_NODE_PATH);
- if(session != null) {
- session.logout();
- session = null;
- }
- }
+public class ScanningLoopTest extends JcrInstallTestBase {
private void assertCounter(int index, long value) {
- assertEquals("Counter " + index, value, installer.getCounters()[index]);
+
+ String label;
+ switch (index ) {
+ case JcrInstaller.RUN_LOOP_COUNTER:
+ label = "RUN_LOOP_COUNTER";
+ break;
+ case JcrInstaller.SCAN_FOLDERS_COUNTER:
+ label = "SCAN_FOLDERS_COUNTER";
+ break;
+
+ case JcrInstaller.UPDATE_FOLDERS_LIST_COUNTER:
+ label = "UPDATE_FOLDERS_LIST_COUNTER";
+ break;
+
+ default:
+ label = "Unknown (" + index +")";
+ break;
+ }
+
+ assertEquals("Counter " + label, value, installer.getCounters()[index]);
}
private void assertIdle() throws Exception {
@@ -87,10 +72,12 @@ public class ScanningLoopTest extends Re
assertIdle();
}
+ @Test
public void testDefaultScanPauseFalse() throws Exception{
assertFalse(installer.scanningIsPaused(installer.getConfiguration(), installer.getSession()));
}
+ @Test
public void testPauseScan() throws Exception{
assertFalse(installer.scanningIsPaused(installer.getConfiguration(), installer.getSession()));
@@ -127,6 +114,7 @@ public class ScanningLoopTest extends Re
assertIdle();
}
+ @Test
public void testAddContentOutside() throws Exception {
final long sf = installer.getCounters()[JcrInstaller.SCAN_FOLDERS_COUNTER];
final long uc = installer.getCounters()[JcrInstaller.UPDATE_FOLDERS_LIST_COUNTER];
@@ -140,6 +128,7 @@ public class ScanningLoopTest extends Re
assertCounter(JcrInstaller.UPDATE_FOLDERS_LIST_COUNTER, uc);
}
+ @Test
public void testDeleteFile() throws Exception {
contentHelper.setupContent();
eventHelper.waitForEvents(TIMEOUT);
@@ -156,6 +145,7 @@ public class ScanningLoopTest extends Re
assertIdle();
}
+ @Test
public void testDeleteLibsFolder() throws Exception {
contentHelper.setupContent();
eventHelper.waitForEvents(TIMEOUT);