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.
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1771170 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index f2090a1..b6980c3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -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>
diff --git a/src/test/java/org/apache/sling/installer/provider/jcr/impl/FindPathsToWatchTest.java b/src/test/java/org/apache/sling/installer/provider/jcr/impl/FindPathsToWatchTest.java
index c2a7170..d50d63b 100644
--- a/src/test/java/org/apache/sling/installer/provider/jcr/impl/FindPathsToWatchTest.java
+++ b/src/test/java/org/apache/sling/installer/provider/jcr/impl/FindPathsToWatchTest.java
@@ -18,52 +18,19 @@
*/
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 java.util.Collection;
-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 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 @@
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 @@
}
}
+ @Test
public void testNewWatchedFolderDetection() throws Exception {
final String newPaths [] = {
"/libs/tnwf/install",
@@ -122,6 +91,7 @@
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();
diff --git a/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderDetectionTest.java b/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderDetectionTest.java
index 7a56da7..73d9367 100644
--- a/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderDetectionTest.java
+++ b/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderDetectionTest.java
@@ -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 @@
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 @@
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 @@
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";
diff --git a/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java b/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java
index 07f0b2f..ba0c4bc 100644
--- a/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java
+++ b/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java
@@ -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 @@
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) {
diff --git a/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java b/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java
index 60e3ac8..c052ff3 100644
--- a/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java
+++ b/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java
@@ -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;
diff --git a/src/test/java/org/apache/sling/installer/provider/jcr/impl/ResourceDetectionTest.java b/src/test/java/org/apache/sling/installer/provider/jcr/impl/ResourceDetectionTest.java
index d43a043..7fbfeea 100644
--- a/src/test/java/org/apache/sling/installer/provider/jcr/impl/ResourceDetectionTest.java
+++ b/src/test/java/org/apache/sling/installer/provider/jcr/impl/ResourceDetectionTest.java
@@ -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 @@
*/
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 @@
assertRegisteredPaths(paths);
}
+ @Test
public void testAddConfigs() throws Exception {
final String [] paths = {
"/libs/foo/bar/install/" + System.currentTimeMillis() + ".jar",
@@ -73,6 +77,7 @@
assertRegisteredPaths(paths);
}
+ @Test
public void testDeleteResources() throws Exception {
assertRegisteredPaths(contentHelper.FAKE_RESOURCES);
assertRegisteredPaths(contentHelper.FAKE_CONFIGS);
@@ -96,10 +101,11 @@
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 @@
}
}
+ @Test
public void testFolderRemoval() throws Exception {
assertRegisteredPaths(contentHelper.FAKE_RESOURCES);
assertRegisteredPaths(contentHelper.FAKE_CONFIGS);
@@ -164,6 +171,7 @@
}
}
+ @Test
public void testFileUpdate() throws Exception {
final String path = contentHelper.FAKE_RESOURCES[0];
assertRegistered(path, true);
@@ -186,6 +194,7 @@
assertRecordedCall("add", path);
}
+ @Test
public void testConfigUpdate() throws Exception {
final String path = contentHelper.FAKE_CONFIGS[0];
assertRegistered(path, true);
diff --git a/src/test/java/org/apache/sling/installer/provider/jcr/impl/ScanningLoopTest.java b/src/test/java/org/apache/sling/installer/provider/jcr/impl/ScanningLoopTest.java
index 5af9965..d832029 100644
--- a/src/test/java/org/apache/sling/installer/provider/jcr/impl/ScanningLoopTest.java
+++ b/src/test/java/org/apache/sling/installer/provider/jcr/impl/ScanningLoopTest.java
@@ -19,53 +19,38 @@
package org.apache.sling.installer.provider.jcr.impl;
-import javax.jcr.Node;
-import javax.jcr.Session;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
-import org.apache.sling.commons.testing.jcr.EventHelper;
-import org.apache.sling.commons.testing.jcr.RepositoryTestBase;
-import org.apache.sling.jcr.api.SlingRepository;
+import javax.jcr.Node;
+
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 @@
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 @@
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 @@
assertCounter(JcrInstaller.UPDATE_FOLDERS_LIST_COUNTER, uc);
}
+ @Test
public void testDeleteFile() throws Exception {
contentHelper.setupContent();
eventHelper.waitForEvents(TIMEOUT);
@@ -156,6 +145,7 @@
assertIdle();
}
+ @Test
public void testDeleteLibsFolder() throws Exception {
contentHelper.setupContent();
eventHelper.waitForEvents(TIMEOUT);