sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-testing-osgi-mock] 05/09: SLING-5453 implement MockBundleContext.getDataFile
Date Tue, 07 Nov 2017 10:20:06 GMT
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-1.7.2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit 52b14aa99155a6f3567946f4bf1ffa72144cd5d0
Author: Stefan Seifert <sseifert@apache.org>
AuthorDate: Mon Jan 25 22:32:37 2016 +0000

    SLING-5453 implement MockBundleContext.getDataFile
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/branches/testing/mocks/osgi-mock-1.x@1726706
13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  8 ++++-
 .../sling/testing/mock/osgi/MockBundleContext.java | 34 ++++++++++++++++++----
 .../testing/mock/osgi/MockBundleContextTest.java   | 18 ++++++++++++
 3 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/pom.xml b/pom.xml
index f5ac4b5..8dee54b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -80,7 +80,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 786fad7..003a304 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;
@@ -52,6 +54,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.
@@ -63,6 +66,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);
@@ -304,6 +308,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.
      */
@@ -316,6 +338,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 ---
@@ -334,11 +363,6 @@ class MockBundleContext implements BundleContext {
         throw new UnsupportedOperationException();
     }
 
-    @Override
-    public File getDataFile(final String s) {
-        throw new UnsupportedOperationException();
-    }
-
     // this is part of org.osgi.core 6.0.0
     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 092ab81..b2c78e6 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,9 +29,11 @@ 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.Dictionary;
 import java.util.Hashtable;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -57,6 +59,11 @@ public class MockBundleContextTest {
         bundleContext = MockOsgi.newBundleContext();
     }
 
+    @After
+    public void tearDown() {
+        MockOsgi.shutdown(bundleContext);
+    }
+
     @Test
     public void testBundle() {
         assertNotNull(bundleContext.getBundle());
@@ -200,4 +207,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
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message