You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2016/01/25 23:27:17 UTC
svn commit: r1726704 - in /sling/trunk/testing/mocks/osgi-mock: pom.xml
src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
Author: sseifert
Date: Mon Jan 25 22:27:17 2016
New Revision: 1726704
URL: http://svn.apache.org/viewvc?rev=1726704&view=rev
Log:
SLING-5453 implement MockBundleContext.getDataFile
Modified:
sling/trunk/testing/mocks/osgi-mock/pom.xml
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
Modified: sling/trunk/testing/mocks/osgi-mock/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/pom.xml?rev=1726704&r1=1726703&r2=1726704&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/pom.xml (original)
+++ sling/trunk/testing/mocks/osgi-mock/pom.xml Mon Jan 25 22:27:17 2016
@@ -90,7 +90,13 @@
<version>3.0.1</version>
<scope>compile</scope>
</dependency>
-
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.4</version>
+ <scope>compile</scope>
+ </dependency>
+
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java?rev=1726704&r1=1726703&r2=1726704&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java (original)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java Mon Jan 25 22:27:17 2016
@@ -19,6 +19,7 @@
package org.apache.sling.testing.mock.osgi;
import java.io.File;
+import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Dictionary;
@@ -32,6 +33,7 @@ import java.util.concurrent.ConcurrentHa
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListSet;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.framework.FilterImpl;
import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.Reference;
@@ -53,6 +55,7 @@ import org.osgi.framework.ServiceRegistr
import org.osgi.service.cm.ConfigurationAdmin;
import com.google.common.collect.ImmutableList;
+import com.google.common.io.Files;
/**
* Mock {@link BundleContext} implementation.
@@ -64,6 +67,7 @@ class MockBundleContext implements Bundl
private final Map<ServiceListener, Filter> serviceListeners = new ConcurrentHashMap<ServiceListener, Filter>();
private final Queue<BundleListener> bundleListeners = new ConcurrentLinkedQueue<BundleListener>();
private final ConfigurationAdmin configAdmin = new MockConfigurationAdmin();
+ private File dataFileBaseDir;
public MockBundleContext() {
this.bundle = new MockBundle(this);
@@ -323,6 +327,24 @@ class MockBundleContext implements Bundl
return null;
}
+ @Override
+ public File getDataFile(final String path) {
+ if (path == null) {
+ throw new IllegalArgumentException("Invalid path: " + path);
+ }
+ synchronized (this) {
+ if (dataFileBaseDir == null) {
+ dataFileBaseDir = Files.createTempDir();
+ }
+ }
+ if (path.isEmpty()) {
+ return dataFileBaseDir;
+ }
+ else {
+ return new File(dataFileBaseDir, path);
+ }
+ }
+
/**
* Deactivates all bundles registered in this mocked bundle context.
*/
@@ -335,6 +357,13 @@ class MockBundleContext implements Bundl
// ignore, no deactivate method is available then
}
}
+ if (dataFileBaseDir != null) {
+ try {
+ FileUtils.deleteDirectory(dataFileBaseDir);
+ } catch (IOException e) {
+ // ignore
+ }
+ }
}
// --- unsupported operations ---
@@ -353,11 +382,6 @@ class MockBundleContext implements Bundl
throw new UnsupportedOperationException();
}
- @Override
- public File getDataFile(final String s) {
- throw new UnsupportedOperationException();
- }
-
@Override
public Bundle getBundle(String location) {
throw new UnsupportedOperationException();
Modified: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java?rev=1726704&r1=1726703&r2=1726704&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java (original)
+++ sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java Mon Jan 25 22:27:17 2016
@@ -29,10 +29,12 @@ import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
+import java.io.File;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Hashtable;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -55,7 +57,12 @@ public class MockBundleContextTest {
@Before
public void setUp() {
- bundleContext = MockOsgi.newBundleContext();
+ bundleContext = (MockBundleContext)MockOsgi.newBundleContext();
+ }
+
+ @After
+ public void tearDown() {
+ MockOsgi.shutdown(bundleContext);
}
@Test
@@ -215,4 +222,15 @@ public class MockBundleContextTest {
assertFalse(filter.match(serviceRegistration.getReference()));
}
+
+ @Test
+ public void testGetDataFile() {
+ File rootFile = bundleContext.getDataFile("");
+ assertNotNull(rootFile);
+
+ File childFile = bundleContext.getDataFile("child");
+ assertNotNull(childFile);
+
+ assertEquals(childFile.getParentFile(), rootFile);
+ }
}