This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.osgi-mock-2.0.2 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit 67592714f548c8ab2bc702af54dd7b889047a524 Author: Stefan Seifert <[email protected]> AuthorDate: Mon Jan 25 22:27:17 2016 +0000 SLING-5453 implement MockBundleContext.getDataFile git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1726704 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 8 ++++- .../sling/testing/mock/osgi/MockBundleContext.java | 34 ++++++++++++++++++---- .../testing/mock/osgi/MockBundleContextTest.java | 20 ++++++++++++- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 0a77138..81baf92 100644 --- a/pom.xml +++ b/pom.xml @@ -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> diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java index 3c0ad53..827ef43 100644 --- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java +++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java @@ -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.ConcurrentHashMap; 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.ServiceRegistration; 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 BundleContext { 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 BundleContext { 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 BundleContext { // ignore, no deactivate method is available then } } + if (dataFileBaseDir != null) { + try { + FileUtils.deleteDirectory(dataFileBaseDir); + } catch (IOException e) { + // ignore + } + } } // --- unsupported operations --- @@ -354,11 +383,6 @@ class MockBundleContext implements BundleContext { } @Override - public File getDataFile(final String s) { - throw new UnsupportedOperationException(); - } - - @Override public Bundle getBundle(String location) { throw new UnsupportedOperationException(); } diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java index 2b1cded..ef7e3b7 100644 --- a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java +++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java @@ -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); + } } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
