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.4.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit 0f222f75be49bb70195954f96b2f3bedc21e2862 Author: Stefan Seifert <[email protected]> AuthorDate: Mon Jun 1 20:21:29 2015 +0000 SLING-4769 Osgi Mock: Add support for bundle headers git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1682993 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/sling/testing/mock/osgi/MapUtil.java | 12 ++++----- .../apache/sling/testing/mock/osgi/MockBundle.java | 29 ++++++++++++++++------ .../sling/testing/mock/osgi/package-info.java | 2 +- .../sling/testing/mock/osgi/MockBundleTest.java | 21 ++++++++++++---- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java index 17aad75..8a9e4a8 100644 --- a/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java +++ b/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java @@ -31,21 +31,21 @@ import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.OsgiMetadata; */ final class MapUtil { - public static Dictionary<String, Object> toDictionary(Map<String, Object> map) { + public static <T, U> Dictionary<T, U> toDictionary(Map<T, U> map) { if (map == null) { return null; } - return new Hashtable<String, Object>(map); + return new Hashtable<T, U>(map); } - public static Map<String, Object> toMap(Dictionary<String, Object> dictionary) { + public static <T, U> Map<T, U> toMap(Dictionary<T, U> dictionary) { if (dictionary == null) { return null; } - Map<String,Object> map = new HashMap<String, Object>(); - Enumeration<String> keys = dictionary.keys(); + Map<T, U> map = new HashMap<T, U>(); + Enumeration<T> keys = dictionary.keys(); while (keys.hasMoreElements()) { - String key = keys.nextElement(); + T key = keys.nextElement(); map.put(key, dictionary.get(key)); } return map; diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java index 468beaf..be17da0 100644 --- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java +++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java @@ -32,15 +32,18 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.framework.Version; +import com.google.common.collect.ImmutableMap; + /** * Mock {@link Bundle} implementation. */ -class MockBundle implements Bundle { +public final class MockBundle implements Bundle { private static volatile long bundleCounter; private final long bundleId; private final BundleContext bundleContext; + private Map<String, String> headers = ImmutableMap.<String, String>of(); /** * Constructor @@ -71,24 +74,34 @@ class MockBundle implements Bundle { return Bundle.ACTIVE; } - // --- unsupported operations --- @Override - public Enumeration<URL> findEntries(final String path, final String filePattern, final boolean recurse) { - throw new UnsupportedOperationException(); + public Dictionary<String, String> getHeaders() { + return MapUtil.toDictionary(headers); } @Override - public Enumeration<String> getEntryPaths(final String path) { - throw new UnsupportedOperationException(); + public Dictionary<String, String> getHeaders(final String locale) { + // localziation not supported, always return default headers + return getHeaders(); + } + + /** + * Set headers for mock bundle + * @param value Header map + */ + public void setHeaders(Map<String, String> value) { + this.headers = value; } + + // --- unsupported operations --- @Override - public Dictionary<String, String> getHeaders() { + public Enumeration<URL> findEntries(final String path, final String filePattern, final boolean recurse) { throw new UnsupportedOperationException(); } @Override - public Dictionary<String, String> getHeaders(final String locale) { + public Enumeration<String> getEntryPaths(final String path) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java b/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java index 76826a4..f5cd3c6 100644 --- a/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java +++ b/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java @@ -19,5 +19,5 @@ /** * Mock implementation of selected OSGi APIs. */ [email protected]("2.1") [email protected]("2.2") package org.apache.sling.testing.mock.osgi; diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java index 7402241..3ab4ebe 100644 --- a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java +++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java @@ -27,29 +27,31 @@ import org.junit.Before; import org.junit.Test; import org.osgi.framework.Bundle; +import com.google.common.collect.ImmutableMap; + public class MockBundleTest { private Bundle bundle; @Before public void setUp() { - this.bundle = MockOsgi.newBundleContext().getBundle(); + bundle = MockOsgi.newBundleContext().getBundle(); } @Test public void testBundleId() { - assertTrue(this.bundle.getBundleId() > 0); + assertTrue(bundle.getBundleId() > 0); } @Test public void testBundleContxt() { - assertNotNull(this.bundle.getBundleContext()); + assertNotNull(bundle.getBundleContext()); } @Test public void testGetEntry() { - assertNotNull(this.bundle.getEntry("/META-INF/test.txt")); - assertNull(this.bundle.getEntry("/invalid")); + assertNotNull(bundle.getEntry("/META-INF/test.txt")); + assertNull(bundle.getEntry("/invalid")); } @Test @@ -57,4 +59,13 @@ public class MockBundleTest { assertEquals(Bundle.ACTIVE, bundle.getState()); } + @Test + public void testGetHeaders() { + assertTrue(bundle.getHeaders().isEmpty()); + + ((MockBundle)bundle).setHeaders(ImmutableMap.of("prop1", "value1")); + assertEquals("value1", bundle.getHeaders().get("prop1")); + assertEquals("value1", bundle.getHeaders("en").get("prop1")); + } + } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
