[felix-bundlerepository] 01/04: Imported Upstream version 2.0.4

Markus Koschany apo-guest at moszumanska.debian.org
Mon May 4 12:03:57 UTC 2015


This is an automated email from the git hooks/post-receive script.

apo-guest pushed a commit to branch master
in repository felix-bundlerepository.

commit 9df0a9b34e14205993b4cd949d7ec0c1af4fc154
Author: Markus Koschany <apo at gambaru.de>
Date:   Mon May 4 13:44:12 2015 +0200

    Imported Upstream version 2.0.4
---
 doc/changelog.txt                                  |  14 ++-
 pom.xml                                            |  10 +-
 .../impl/LazyLocalResourceImpl.java                | 120 +++++++++++++++++++++
 .../bundlerepository/impl/LocalRepositoryImpl.java |  34 +++---
 .../bundlerepository/impl/LocalResourceImpl.java   |  54 +++-------
 .../bundlerepository/impl/ResourceComparator.java  |  10 +-
 .../felix/bundlerepository/impl/ResourceImpl.java  |  74 ++++++++++++-
 .../impl/OSGiRepositoryImplTest.java               |  32 ++++++
 .../impl/OSGiRepositoryXMLTest.java                |   3 +
 .../bundlerepository/impl/RepositoryAdminTest.java |  13 ++-
 .../bundlerepository/impl/RepositoryImplTest.java  |   8 +-
 .../bundlerepository/impl/ResolverImplTest.java    |  16 ++-
 .../bundlerepository/impl/ResourceImplTest.java    |  85 +++++++++++++++
 .../bundlerepository/impl/StaxParserTest.java      |  17 +--
 14 files changed, 400 insertions(+), 90 deletions(-)

diff --git a/doc/changelog.txt b/doc/changelog.txt
index 4a624be..537c7a1 100644
--- a/doc/changelog.txt
+++ b/doc/changelog.txt
@@ -1,4 +1,16 @@
-Changes from 1.6.6 to 2.0.0
+Changes from 2.0.2 to 2.0.4
+---------------------------
+
+** Bug
+    * [FELIX-3097] - LocalRepository is not updated when bundles are
+    * [FELIX-4571] - NullPointerException when using Repository impl with Aries subsystem impl
+    * [FELIX-4616] - BundleRepository ResourceComparator violates comparison contract
+    * [FELIX-4640] - missing (&(osgi.ee=JavaSE)(version=1.8)) when embedding in org.apache.felix.framework
+
+** Improvement
+    * [FELIX-4812] - BundleRepository can be quite CPU intensive when starting a lot of bundles
+
+Changes from 1.6.6 to 2.0.2
 ---------------------------
 
 ** New Feature
diff --git a/pom.xml b/pom.xml
index 82b80ed..948e352 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,17 +28,17 @@
   <name>Apache Felix Bundle Repository</name>
   <description>Bundle repository service.</description>
   <artifactId>org.apache.felix.bundlerepository</artifactId>
-  <version>2.0.2</version>
+  <version>2.0.4</version>
   <scm>
-    <connection>scm:svn:http://svn.apache.org/repos/asf/felix/releases/org.apache.felix.bundlerepository-2.0.2</connection>
-    <developerConnection>scm:svn:https://svn.apache.org/repos/asf/felix/releases/org.apache.felix.bundlerepository-2.0.2</developerConnection>
-    <url>http://svn.apache.org/repos/asf/felix/releases/org.apache.felix.bundlerepository-2.0.2</url>
+    <connection>scm:svn:http://svn.apache.org/repos/asf/felix/releases/org.apache.felix.bundlerepository-2.0.4</connection>
+    <developerConnection>scm:svn:https://svn.apache.org/repos/asf/felix/releases/org.apache.felix.bundlerepository-2.0.4</developerConnection>
+    <url>http://svn.apache.org/repos/asf/felix/releases/org.apache.felix.bundlerepository-2.0.4</url>
   </scm>
   <dependencies>
     <dependency>
       <groupId>${project.groupId}</groupId>
       <artifactId>org.apache.felix.utils</artifactId>
-      <version>1.6.0</version>
+      <version>1.8.0</version>
       <optional>true</optional>
     </dependency>
     <dependency>
diff --git a/src/main/java/org/apache/felix/bundlerepository/impl/LazyLocalResourceImpl.java b/src/main/java/org/apache/felix/bundlerepository/impl/LazyLocalResourceImpl.java
new file mode 100644
index 0000000..6837fc7
--- /dev/null
+++ b/src/main/java/org/apache/felix/bundlerepository/impl/LazyLocalResourceImpl.java
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.bundlerepository.impl;
+
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import org.apache.felix.bundlerepository.Capability;
+import org.apache.felix.bundlerepository.Requirement;
+import org.apache.felix.bundlerepository.Resource;
+import org.apache.felix.utils.log.Logger;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
+import org.osgi.framework.wiring.BundleRevision;
+
+public class LazyLocalResourceImpl implements Resource
+{
+    private final Bundle m_bundle;
+    private final Logger m_logger;
+    private volatile Resource m_resource = null;
+
+    LazyLocalResourceImpl(Bundle bundle, Logger logger)
+    {
+        m_bundle = bundle;
+        m_logger = logger;
+    }
+
+    public boolean isLocal()
+    {
+        return true;
+    }
+
+    public Bundle getBundle()
+    {
+        return m_bundle;
+    }
+
+    public String toString()
+    {
+        return m_bundle.toString();
+    }
+
+    private final Resource getResource() {
+        if (m_resource == null) {
+            synchronized (this) {
+                try {
+                    m_resource = new LocalResourceImpl(m_bundle);
+                } catch (InvalidSyntaxException ex) {
+                    // This should never happen since we are generating filters,
+                    // but ignore the resource if it does occur.
+                    m_logger.log(Logger.LOG_WARNING, ex.getMessage(), ex);
+                    m_resource = new ResourceImpl();
+                }
+            }
+        }
+        return m_resource;
+    }
+
+    public Map getProperties() {
+        return getResource().getProperties();
+    }
+
+    public String getId() {
+        return getResource().getId();
+    }
+
+    public String getSymbolicName() {
+        return getResource().getSymbolicName();
+    }
+
+    public Version getVersion() {
+        return getResource().getVersion();
+    }
+
+    public String getPresentationName() {
+        return getResource().getPresentationName();
+    }
+
+    public String getURI() {
+        return getResource().getURI();
+    }
+
+    public Long getSize() {
+        return getResource().getSize();
+    }
+
+    public String[] getCategories() {
+        return getResource().getCategories();
+    }
+
+    public Capability[] getCapabilities() {
+        return getResource().getCapabilities();
+    }
+
+    public Requirement[] getRequirements() {
+        return getResource().getRequirements();
+    }
+}
diff --git a/src/main/java/org/apache/felix/bundlerepository/impl/LocalRepositoryImpl.java b/src/main/java/org/apache/felix/bundlerepository/impl/LocalRepositoryImpl.java
index 5d52f3a..862ffb0 100644
--- a/src/main/java/org/apache/felix/bundlerepository/impl/LocalRepositoryImpl.java
+++ b/src/main/java/org/apache/felix/bundlerepository/impl/LocalRepositoryImpl.java
@@ -26,7 +26,6 @@ import org.osgi.framework.AllServiceListener;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
-import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.SynchronousBundleListener;
 import org.apache.felix.bundlerepository.*;
@@ -36,7 +35,7 @@ public class LocalRepositoryImpl implements Repository, SynchronousBundleListene
     private final BundleContext m_context;
     private final Logger m_logger;
     private long m_snapshotTimeStamp = 0;
-    private Map m_localResourceList = new HashMap();
+    private Map<Long, Resource> m_localResourceList = new HashMap<Long, Resource>();
 
     public LocalRepositoryImpl(BundleContext context, Logger logger)
     {
@@ -47,11 +46,11 @@ public class LocalRepositoryImpl implements Repository, SynchronousBundleListene
 
     public void bundleChanged(BundleEvent event)
     {
-        if (event.getType() == BundleEvent.INSTALLED)
+        if (event.getType() == BundleEvent.INSTALLED || event.getType() == BundleEvent.UPDATED)
         {
             synchronized (this)
             {
-                addBundle(event.getBundle(), m_logger);
+                addBundle(event.getBundle());
                 m_snapshotTimeStamp = System.currentTimeMillis();
             }
         }
@@ -59,7 +58,7 @@ public class LocalRepositoryImpl implements Repository, SynchronousBundleListene
         {
             synchronized (this)
             {
-                removeBundle(event.getBundle(), m_logger);
+                removeBundle(event.getBundle());
                 m_snapshotTimeStamp = System.currentTimeMillis();
             }
         }
@@ -73,14 +72,14 @@ public class LocalRepositoryImpl implements Repository, SynchronousBundleListene
         {
             synchronized (this)
             {
-                removeBundle(bundle, m_logger);
-                addBundle(bundle, m_logger);
+                removeBundle(bundle);
+                addBundle(bundle);
                 m_snapshotTimeStamp = System.currentTimeMillis();
             }
         }
     }
 
-    private void addBundle(Bundle bundle, Logger logger)
+    private void addBundle(Bundle bundle)
     {
         /*
          * Concurrency note: This method MUST be called in a context which
@@ -93,19 +92,10 @@ public class LocalRepositoryImpl implements Repository, SynchronousBundleListene
         {
             return;
         }
-        try
-        {
-            m_localResourceList.put(new Long(bundle.getBundleId()), new LocalResourceImpl(bundle));
-        }
-        catch (InvalidSyntaxException ex)
-        {
-            // This should never happen since we are generating filters,
-            // but ignore the resource if it does occur.
-            m_logger.log(Logger.LOG_WARNING, ex.getMessage(), ex);
-        }
+        m_localResourceList.put(bundle.getBundleId(), new LazyLocalResourceImpl(bundle, m_logger));
     }
 
-    private void removeBundle(Bundle bundle, Logger logger)
+    private void removeBundle(Bundle bundle)
     {
         /*
          * Concurrency note: This method MUST be called in a context which
@@ -113,7 +103,7 @@ public class LocalRepositoryImpl implements Repository, SynchronousBundleListene
          * corruption.
          */
 
-        m_localResourceList.remove(new Long(bundle.getBundleId()));
+        m_localResourceList.remove(bundle.getBundleId());
     }
 
     public void dispose()
@@ -139,7 +129,7 @@ public class LocalRepositoryImpl implements Repository, SynchronousBundleListene
 
     public synchronized Resource[] getResources()
     {
-        return (Resource[]) m_localResourceList.values().toArray(new Resource[m_localResourceList.size()]);
+        return m_localResourceList.values().toArray(new Resource[m_localResourceList.size()]);
     }
 
     private void initialize()
@@ -159,7 +149,7 @@ public class LocalRepositoryImpl implements Repository, SynchronousBundleListene
             bundles = m_context.getBundles();
             for (int i = 0; (bundles != null) && (i < bundles.length); i++)
             {
-                addBundle(bundles[i], m_logger);
+                addBundle(bundles[i]);
             }
 
             m_snapshotTimeStamp = System.currentTimeMillis();
diff --git a/src/main/java/org/apache/felix/bundlerepository/impl/LocalResourceImpl.java b/src/main/java/org/apache/felix/bundlerepository/impl/LocalResourceImpl.java
index 1272b41..e3d4c27 100644
--- a/src/main/java/org/apache/felix/bundlerepository/impl/LocalResourceImpl.java
+++ b/src/main/java/org/apache/felix/bundlerepository/impl/LocalResourceImpl.java
@@ -20,6 +20,7 @@ package org.apache.felix.bundlerepository.impl;
 
 import java.util.Dictionary;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 import java.util.StringTokenizer;
 
@@ -29,6 +30,7 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.wiring.BundleRevision;
 
 public class LocalResourceImpl extends ResourceImpl
 {
@@ -87,49 +89,21 @@ public class LocalResourceImpl extends ResourceImpl
                 }
             }
 
-/* TODO: OBR - Fix system capabilities.
-            // Create a case-insensitive map.
-            Map map = new TreeMap(new Comparator() {
-                public int compare(Object o1, Object o2)
+            // Add all the OSGi capabilities from the system bundle as repo capabilities
+            BundleRevision br = m_bundle.adapt(BundleRevision.class);
+            for (org.osgi.resource.Capability cap : br.getCapabilities(null))
+            {
+                CapabilityImpl bcap = new CapabilityImpl(cap.getNamespace());
+                for (Map.Entry<String, Object> entry : cap.getAttributes().entrySet())
                 {
-                    return o1.toString().compareToIgnoreCase(o2.toString());
+                    bcap.addProperty(new FelixPropertyAdapter(entry));
                 }
-            });
-            map.put(
-                Constants.FRAMEWORK_VERSION,
-                m_context.getProperty(Constants.FRAMEWORK_VERSION));
-            map.put(
-                Constants.FRAMEWORK_VENDOR,
-                m_context.getProperty(Constants.FRAMEWORK_VENDOR));
-            map.put(
-                Constants.FRAMEWORK_LANGUAGE,
-                m_context.getProperty(Constants.FRAMEWORK_LANGUAGE));
-            map.put(
-                Constants.FRAMEWORK_OS_NAME,
-                m_context.getProperty(Constants.FRAMEWORK_OS_NAME));
-            map.put(
-                Constants.FRAMEWORK_OS_VERSION,
-                m_context.getProperty(Constants.FRAMEWORK_OS_VERSION));
-            map.put(
-                Constants.FRAMEWORK_PROCESSOR,
-                m_context.getProperty(Constants.FRAMEWORK_PROCESSOR));
-//                map.put(
-//                    FelixConstants.FELIX_VERSION_PROPERTY,
-//                    m_context.getProperty(FelixConstants.FELIX_VERSION_PROPERTY));
-            Map[] capMaps = (Map[]) bundleMap.get("capability");
-            if (capMaps == null)
-            {
-                capMaps = new Map[] { map };
-            }
-            else
-            {
-                Map[] newCaps = new Map[capMaps.length + 1];
-                newCaps[0] = map;
-                System.arraycopy(capMaps, 0, newCaps, 1, capMaps.length);
-                capMaps = newCaps;
+                for (Map.Entry<String, String> entry : cap.getDirectives().entrySet())
+                {
+                    bcap.addDirective(entry.getKey(), entry.getValue());
+                }
+                addCapability(bcap);
             }
-            bundleMap.put("capability", capMaps);
-*/
         }
     }
 
diff --git a/src/main/java/org/apache/felix/bundlerepository/impl/ResourceComparator.java b/src/main/java/org/apache/felix/bundlerepository/impl/ResourceComparator.java
index 35f7439..2dfc892 100644
--- a/src/main/java/org/apache/felix/bundlerepository/impl/ResourceComparator.java
+++ b/src/main/java/org/apache/felix/bundlerepository/impl/ResourceComparator.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -28,10 +28,14 @@ class ResourceComparator implements Comparator
     {
         Resource r1 = (Resource) o1;
         Resource r2 = (Resource) o2;
-        String name1 = (String) r1.getPresentationName();
-        String name2 = (String) r2.getPresentationName();
+        String name1 = r1.getPresentationName();
+        String name2 = r2.getPresentationName();
         if (name1 == null)
         {
+            if (name2 == null)
+            {
+                return 0;
+            }
             return -1;
         }
         else if (name2 == null)
diff --git a/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java b/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java
index f896f4d..554aea5 100644
--- a/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java
+++ b/src/main/java/org/apache/felix/bundlerepository/impl/ResourceImpl.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -18,8 +18,19 @@
  */
 package org.apache.felix.bundlerepository.impl;
 
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
-import java.util.*;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
 
 import org.apache.felix.bundlerepository.Capability;
 import org.apache.felix.bundlerepository.Property;
@@ -119,7 +130,62 @@ public class ResourceImpl implements Resource
 
     public Long getSize()
     {
-        return ((Long) m_map.get(Resource.SIZE));
+        Object sz = m_map.get(Resource.SIZE);
+        if (sz instanceof Long)
+            return ((Long) sz);
+
+        long size = findResourceSize();
+        m_map.put(Resource.SIZE, size);
+        return size;
+    }
+
+    private long findResourceSize()
+    {
+        String uri = getURI();
+        if (uri != null) {
+            try
+            {
+                URL url = new URL(uri);
+                if ("file".equals(url.getProtocol()))
+                    return new File(url.getFile()).length();
+                else
+                    return findResourceSize(url);
+            }
+            catch (Exception e)
+            {
+                // TODO should really log this...
+            }
+        }
+        return -1L;
+    }
+
+    private long findResourceSize(URL url) throws IOException
+    {
+        byte[] bytes = new byte[8192];
+
+        // Not a File URL, stream the whole thing through to find out the size
+        InputStream is = null;
+        long fileSize = 0;
+        try
+        {
+            is = url.openStream();
+
+            int length = 0;
+            while ((length = is.read(bytes)) != -1)
+            {
+                fileSize += length;
+            }
+        }
+        catch (Exception ex)
+        {
+            // should really log this...
+        }
+        finally
+        {
+            if (is != null)
+                is.close();
+        }
+        return fileSize;
     }
 
     public Requirement[] getRequirements()
@@ -169,7 +235,7 @@ public class ResourceImpl implements Resource
     }
 
     /**
-     * Default setter method when setting parsed data from the XML file. 
+     * Default setter method when setting parsed data from the XML file.
      **/
     public Object put(Object key, Object value)
     {
diff --git a/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java b/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java
index 7bc728a..7f95a08 100644
--- a/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java
+++ b/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java
@@ -29,6 +29,8 @@ import java.util.Set;
 
 import junit.framework.TestCase;
 
+import org.apache.felix.bundlerepository.Reason;
+import org.apache.felix.bundlerepository.Resolver;
 import org.apache.felix.utils.log.Logger;
 import org.mockito.Mockito;
 import org.osgi.framework.Bundle;
@@ -37,6 +39,7 @@ import org.osgi.framework.Version;
 import org.osgi.framework.namespace.BundleNamespace;
 import org.osgi.framework.namespace.IdentityNamespace;
 import org.osgi.framework.namespace.PackageNamespace;
+import org.osgi.framework.wiring.BundleRevision;
 import org.osgi.resource.Capability;
 import org.osgi.resource.Requirement;
 import org.osgi.resource.Resource;
@@ -198,11 +201,40 @@ public class OSGiRepositoryImplTest extends TestCase
         assertTrue(Arrays.equals(expectedBytes, actualBytes));
     }
 
+    public void testSystemBundleCapabilities() throws Exception {
+        RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
+        Resolver resolver = repoAdmin.resolver();
+        RequirementImpl req = new RequirementImpl("some.system.cap");
+        req.setFilter("(sys.cap=something)");
+        resolver.add(req);
+        ResourceImpl res = new ResourceImpl();
+        res.addRequire(req);
+
+        resolver.add(res);
+        assertTrue(resolver.resolve());
+
+        // This should add the system bundle repo to the resolved set.
+        org.apache.felix.bundlerepository.Resource sysBundleRes = repoAdmin.getSystemRepository().getResources()[0];
+        Reason[] reason = resolver.getReason(sysBundleRes);
+        assertTrue(reason.length >= 1);
+        assertEquals(req, reason[0].getRequirement());
+    }
+
     private RepositoryAdminImpl createRepositoryAdmin() throws Exception
     {
         Bundle sysBundle = Mockito.mock(Bundle.class);
         Mockito.when(sysBundle.getHeaders()).thenReturn(new Hashtable<String, String>());
 
+        BundleRevision br = Mockito.mock(BundleRevision.class);
+        Mockito.when(sysBundle.adapt(BundleRevision.class)).thenReturn(br);
+        Capability cap1 = new OSGiCapabilityImpl("some.system.cap",
+                Collections.<String, Object>singletonMap("sys.cap", "something"),
+                Collections.singletonMap("x", "y"));
+        Capability cap2 = new OSGiCapabilityImpl("some.system.cap",
+                Collections.<String, Object>singletonMap("sys.cap", "somethingelse"),
+                Collections.<String, String>emptyMap());
+        Mockito.when(br.getCapabilities(null)).thenReturn(Arrays.asList(cap1, cap2));
+
         BundleContext bc = Mockito.mock(BundleContext.class);
         Mockito.when(bc.getBundle(0)).thenReturn(sysBundle);
         Mockito.when(sysBundle.getBundleContext()).thenReturn(bc);
diff --git a/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java b/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java
index a70fe17..631f061 100644
--- a/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java
+++ b/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java
@@ -35,6 +35,7 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Version;
 import org.osgi.framework.namespace.IdentityNamespace;
+import org.osgi.framework.wiring.BundleRevision;
 import org.osgi.resource.Capability;
 import org.osgi.resource.Requirement;
 import org.osgi.resource.Resource;
@@ -135,6 +136,8 @@ public class OSGiRepositoryXMLTest extends TestCase
     {
         Bundle sysBundle = Mockito.mock(Bundle.class);
         Mockito.when(sysBundle.getHeaders()).thenReturn(new Hashtable<String, String>());
+        BundleRevision br = Mockito.mock(BundleRevision.class);
+        Mockito.when(sysBundle.adapt(BundleRevision.class)).thenReturn(br);
 
         BundleContext bc = Mockito.mock(BundleContext.class);
         Mockito.when(bc.getBundle(0)).thenReturn(sysBundle);
diff --git a/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java b/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java
index 70e38b7..df6f099 100644
--- a/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java
+++ b/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java
@@ -19,9 +19,11 @@
 package org.apache.felix.bundlerepository.impl;
 
 import java.net.URL;
+import java.util.Collections;
 import java.util.Hashtable;
 
 import junit.framework.TestCase;
+
 import org.apache.felix.bundlerepository.Resource;
 import org.apache.felix.utils.filter.FilterImpl;
 import org.apache.felix.utils.log.Logger;
@@ -33,6 +35,8 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleListener;
 import org.osgi.framework.ServiceListener;
+import org.osgi.framework.wiring.BundleRevision;
+import org.osgi.resource.Capability;
 
 public class RepositoryAdminTest extends TestCase
 {
@@ -54,8 +58,9 @@ public class RepositoryAdminTest extends TestCase
 
     private RepositoryAdminImpl createRepositoryAdmin() throws Exception
     {
-        BundleContext bundleContext = (BundleContext) EasyMock.createMock(BundleContext.class);
-        Bundle systemBundle = (Bundle) EasyMock.createMock(Bundle.class);
+        BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
+        Bundle systemBundle = EasyMock.createMock(Bundle.class);
+        BundleRevision systemBundleRevision = EasyMock.createMock(BundleRevision.class);
 
         Activator.setContext(bundleContext);
         EasyMock.expect(bundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes();
@@ -64,6 +69,8 @@ public class RepositoryAdminTest extends TestCase
         EasyMock.expect(systemBundle.getRegisteredServices()).andReturn(null);
         EasyMock.expect(new Long(systemBundle.getBundleId())).andReturn(new Long(0)).anyTimes();
         EasyMock.expect(systemBundle.getBundleContext()).andReturn(bundleContext);
+        EasyMock.expect(systemBundleRevision.getCapabilities(null)).andReturn(Collections.<Capability>emptyList());
+        EasyMock.expect(systemBundle.adapt(BundleRevision.class)).andReturn(systemBundleRevision);
         bundleContext.addBundleListener((BundleListener) EasyMock.anyObject());
         bundleContext.addServiceListener((ServiceListener) EasyMock.anyObject());
         EasyMock.expect(bundleContext.getBundles()).andReturn(new Bundle[] { systemBundle });
@@ -73,7 +80,7 @@ public class RepositoryAdminTest extends TestCase
                 return FilterImpl.newInstance((String) c.getValue());
             }
         }).anyTimes();
-        EasyMock.replay(new Object[] { bundleContext, systemBundle });
+        EasyMock.replay(new Object[] { bundleContext, systemBundle, systemBundleRevision });
 
         RepositoryAdminImpl repoAdmin = new RepositoryAdminImpl(bundleContext, new Logger(bundleContext));
 
diff --git a/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryImplTest.java b/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryImplTest.java
index 5e4b459..d46d8cb 100644
--- a/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryImplTest.java
+++ b/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryImplTest.java
@@ -19,6 +19,7 @@
 package org.apache.felix.bundlerepository.impl;
 
 import java.net.URL;
+import java.util.Collections;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.Map;
@@ -35,6 +36,8 @@ import org.osgi.framework.BundleListener;
 import org.osgi.framework.Filter;
 import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.wiring.BundleRevision;
+import org.osgi.resource.Capability;
 
 public class RepositoryImplTest extends TestCase
 {
@@ -96,6 +99,7 @@ public class RepositoryImplTest extends TestCase
     {
         BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
         Bundle systemBundle = EasyMock.createMock(Bundle.class);
+        BundleRevision systemBundleRevision = EasyMock.createMock(BundleRevision.class);
 
         Activator.setContext(bundleContext);
         EasyMock.expect(bundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes();
@@ -104,6 +108,8 @@ public class RepositoryImplTest extends TestCase
         EasyMock.expect(systemBundle.getRegisteredServices()).andReturn(null);
         EasyMock.expect(new Long(systemBundle.getBundleId())).andReturn(new Long(0)).anyTimes();
         EasyMock.expect(systemBundle.getBundleContext()).andReturn(bundleContext);
+        EasyMock.expect(systemBundleRevision.getCapabilities(null)).andReturn(Collections.<Capability>emptyList());
+        EasyMock.expect(systemBundle.adapt(BundleRevision.class)).andReturn(systemBundleRevision);
         bundleContext.addBundleListener((BundleListener) EasyMock.anyObject());
         bundleContext.addServiceListener((ServiceListener) EasyMock.anyObject());
         EasyMock.expect(bundleContext.getBundles()).andReturn(new Bundle[] { systemBundle });
@@ -121,7 +127,7 @@ public class RepositoryImplTest extends TestCase
                 return true;
             }
         }).anyTimes();
-        EasyMock.replay(new Object[] { bundleContext, systemBundle });
+        EasyMock.replay(new Object[] { bundleContext, systemBundle, systemBundleRevision });
 
         RepositoryAdminImpl repoAdmin = new RepositoryAdminImpl(bundleContext, new Logger(bundleContext));
 
diff --git a/src/test/java/org/apache/felix/bundlerepository/impl/ResolverImplTest.java b/src/test/java/org/apache/felix/bundlerepository/impl/ResolverImplTest.java
index 7bd8f6b..3ec9288 100644
--- a/src/test/java/org/apache/felix/bundlerepository/impl/ResolverImplTest.java
+++ b/src/test/java/org/apache/felix/bundlerepository/impl/ResolverImplTest.java
@@ -19,11 +19,14 @@
 package org.apache.felix.bundlerepository.impl;
 
 import java.net.URL;
+import java.util.Collections;
 import java.util.Hashtable;
 
 import junit.framework.TestCase;
 
+import org.apache.felix.bundlerepository.Repository;
 import org.apache.felix.bundlerepository.Requirement;
+import org.apache.felix.bundlerepository.Resolver;
 import org.apache.felix.bundlerepository.Resource;
 import org.apache.felix.utils.filter.FilterImpl;
 import org.apache.felix.utils.log.Logger;
@@ -35,8 +38,8 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleListener;
 import org.osgi.framework.ServiceListener;
-import org.apache.felix.bundlerepository.Repository;
-import org.apache.felix.bundlerepository.Resolver;
+import org.osgi.framework.wiring.BundleRevision;
+import org.osgi.resource.Capability;
 
 public class ResolverImplTest extends TestCase
 {
@@ -138,8 +141,9 @@ public class ResolverImplTest extends TestCase
 
     private RepositoryAdminImpl createRepositoryAdmin() throws Exception
     {
-        BundleContext bundleContext = (BundleContext) EasyMock.createMock(BundleContext.class);
-        Bundle systemBundle = (Bundle) EasyMock.createMock(Bundle.class);
+        BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
+        Bundle systemBundle = EasyMock.createMock(Bundle.class);
+        BundleRevision systemBundleRevision = EasyMock.createMock(BundleRevision.class);
 
         Activator.setContext(bundleContext);
         EasyMock.expect(bundleContext.getProperty(RepositoryAdminImpl.REPOSITORY_URL_PROP))
@@ -150,6 +154,8 @@ public class ResolverImplTest extends TestCase
         EasyMock.expect(systemBundle.getRegisteredServices()).andReturn(null);
         EasyMock.expect(new Long(systemBundle.getBundleId())).andReturn(new Long(0)).anyTimes();
         EasyMock.expect(systemBundle.getBundleContext()).andReturn(bundleContext);
+        EasyMock.expect(systemBundleRevision.getCapabilities(null)).andReturn(Collections.<Capability>emptyList());
+        EasyMock.expect(systemBundle.adapt(BundleRevision.class)).andReturn(systemBundleRevision);
         bundleContext.addBundleListener((BundleListener) EasyMock.anyObject());
         bundleContext.addServiceListener((ServiceListener) EasyMock.anyObject());
         EasyMock.expect(bundleContext.getBundles()).andReturn(new Bundle[] { systemBundle });
@@ -159,7 +165,7 @@ public class ResolverImplTest extends TestCase
                 return FilterImpl.newInstance((String) c.getValue());
             }
         }).anyTimes();
-        EasyMock.replay(new Object[] { bundleContext, systemBundle });
+        EasyMock.replay(new Object[] { bundleContext, systemBundle, systemBundleRevision });
 
         RepositoryAdminImpl repoAdmin = new RepositoryAdminImpl(bundleContext, new Logger(bundleContext));
 
diff --git a/src/test/java/org/apache/felix/bundlerepository/impl/ResourceImplTest.java b/src/test/java/org/apache/felix/bundlerepository/impl/ResourceImplTest.java
new file mode 100644
index 0000000..8f9dd3a
--- /dev/null
+++ b/src/test/java/org/apache/felix/bundlerepository/impl/ResourceImplTest.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.bundlerepository.impl;
+
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+import org.apache.felix.bundlerepository.Property;
+import org.apache.felix.bundlerepository.Repository;
+
+public class ResourceImplTest extends TestCase
+{
+    public void testGetSizeFileResource() {
+        ResourceImpl res = new ResourceImpl();
+        res.put(Property.URI, "repo_files/test_file_3.jar");
+
+        final URL dir = getClass().getResource("/repo_files");
+        Repository repo = new RepositoryImpl() {
+            { setURI(dir.toExternalForm()); }
+        };
+        res.setRepository(repo);
+
+        assertEquals("Should have obtained the file size", 3, (long) res.getSize());
+    }
+
+    public void testGetSizeNonExistentFileResource() {
+        ResourceImpl res = new ResourceImpl();
+        res.put(Property.URI, "repo_files/test_file_3_garbage.jar");
+
+        final URL dir = getClass().getResource("/repo_files");
+        Repository repo = new RepositoryImpl() {
+            { setURI(dir.toExternalForm()); }
+        };
+        res.setRepository(repo);
+
+        assertEquals("File size should be reported as 0", 0, (long) res.getSize());
+    }
+
+    public void testGetSizeNonFileResource() {
+        final URL testFile4 = getClass().getResource("/repo_files/test_file_4.jar");
+
+        ResourceImpl res = new ResourceImpl();
+        res.put(Property.URI, "jar:" + testFile4.toExternalForm() + "!/blah.txt");
+
+        final URL dir = getClass().getResource("/repo_files");
+        Repository repo = new RepositoryImpl() {
+            { setURI(dir.toExternalForm()); }
+        };
+        res.setRepository(repo);
+
+        assertEquals("Should have obtained the file size", 5, (long) res.getSize());
+    }
+
+    public void testGetSizeNonExistentResource() {
+        final URL testFile4 = getClass().getResource("/repo_files/test_file_4.jar");
+
+        ResourceImpl res = new ResourceImpl();
+        res.put(Property.URI, "jar:" + testFile4.toExternalForm() + "!/blah_xyz.txt");
+
+        final URL dir = getClass().getResource("/repo_files");
+        Repository repo = new RepositoryImpl() {
+            { setURI(dir.toExternalForm()); }
+        };
+        res.setRepository(repo);
+
+        assertEquals("File size should be reported as 0", 0, (long) res.getSize());
+    }
+}
diff --git a/src/test/java/org/apache/felix/bundlerepository/impl/StaxParserTest.java b/src/test/java/org/apache/felix/bundlerepository/impl/StaxParserTest.java
index e2e5725..54a3a91 100644
--- a/src/test/java/org/apache/felix/bundlerepository/impl/StaxParserTest.java
+++ b/src/test/java/org/apache/felix/bundlerepository/impl/StaxParserTest.java
@@ -19,13 +19,14 @@
 package org.apache.felix.bundlerepository.impl;
 
 import java.net.URL;
+import java.util.Collections;
 import java.util.Hashtable;
 
 import junit.framework.TestCase;
-import org.apache.felix.bundlerepository.impl.PullParser;
+
+import org.apache.felix.bundlerepository.Repository;
 import org.apache.felix.bundlerepository.Resolver;
 import org.apache.felix.bundlerepository.Resource;
-import org.apache.felix.bundlerepository.impl.StaxParser;
 import org.apache.felix.utils.filter.FilterImpl;
 import org.apache.felix.utils.log.Logger;
 import org.easymock.Capture;
@@ -36,7 +37,8 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleListener;
 import org.osgi.framework.ServiceListener;
-import org.apache.felix.bundlerepository.Repository;
+import org.osgi.framework.wiring.BundleRevision;
+import org.osgi.resource.Capability;
 
 public class StaxParserTest extends TestCase
 {
@@ -138,8 +140,9 @@ public class StaxParserTest extends TestCase
 
     private RepositoryAdminImpl createRepositoryAdmin(Class repositoryParser) throws Exception
     {
-        BundleContext bundleContext = (BundleContext) EasyMock.createMock(BundleContext.class);
-        Bundle systemBundle = (Bundle) EasyMock.createMock(Bundle.class);
+        BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
+        Bundle systemBundle = EasyMock.createMock(Bundle.class);
+        BundleRevision systemBundleRevision = EasyMock.createMock(BundleRevision.class);
 
         Activator.setContext(bundleContext);
         EasyMock.expect(bundleContext.getProperty(RepositoryAdminImpl.REPOSITORY_URL_PROP))
@@ -152,6 +155,8 @@ public class StaxParserTest extends TestCase
         EasyMock.expect(systemBundle.getRegisteredServices()).andReturn(null);
         EasyMock.expect(new Long(systemBundle.getBundleId())).andReturn(new Long(0)).anyTimes();
         EasyMock.expect(systemBundle.getBundleContext()).andReturn(bundleContext);
+        EasyMock.expect(systemBundleRevision.getCapabilities(null)).andReturn(Collections.<Capability>emptyList());
+        EasyMock.expect(systemBundle.adapt(BundleRevision.class)).andReturn(systemBundleRevision);
         bundleContext.addBundleListener((BundleListener) EasyMock.anyObject());
         bundleContext.addServiceListener((ServiceListener) EasyMock.anyObject());
         EasyMock.expect(bundleContext.getBundles()).andReturn(new Bundle[] { systemBundle });
@@ -161,7 +166,7 @@ public class StaxParserTest extends TestCase
                 return FilterImpl.newInstance((String) c.getValue());
             }
         }).anyTimes();
-        EasyMock.replay(new Object[] { bundleContext, systemBundle });
+        EasyMock.replay(new Object[] { bundleContext, systemBundle, systemBundleRevision });
 
         RepositoryAdminImpl repoAdmin = new RepositoryAdminImpl(bundleContext, new Logger(bundleContext));
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/felix-bundlerepository.git



More information about the pkg-java-commits mailing list