This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.commons.osgi-2.0.6
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-osgi.git

commit d475594fad742fdf8bfd3470fae1831982b124e6
Author: Bertrand Delacretaz <[email protected]>
AuthorDate: Thu Jan 7 13:51:44 2010 +0000

    SLING-1278 - Utilities for bundle version extraction and comparison
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/osgi@896880 
13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  53 ++++++++++
 .../bundleversion/BundleBundleVersionInfo.java     |  68 ++++++++++++
 .../bundleversion/BundleVersionComparator.java     |  87 ++++++++++++++++
 .../osgi/bundleversion/BundleVersionInfo.java      |  58 +++++++++++
 .../osgi/bundleversion/FileBundleVersionInfo.java  |  91 ++++++++++++++++
 .../bundleversion/BundleBundleVersionInfoTest.java |  99 ++++++++++++++++++
 .../bundleversion/BundleVersionComparatorTest.java | 114 +++++++++++++++++++++
 .../bundleversion/FileBundleVersionInfoTest.java   |  68 ++++++++++++
 .../osgi/bundleversion/MockBundleVersionInfo.java  |  72 +++++++++++++
 9 files changed, 710 insertions(+)

diff --git a/pom.xml b/pom.xml
index b647995..2b6edae 100644
--- a/pom.xml
+++ b/pom.xml
@@ -39,6 +39,10 @@
         
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/osgi</developerConnection>
         
<url>http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi</url>
     </scm>
+    
+    <properties>
+        
<test.jars.folder>${project.build.directory}/testjars</test.jars.folder>
+    </properties>
 
     <build>
         <plugins>
@@ -54,6 +58,37 @@
                     </instructions>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-dependency-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>copy-test-bundles</id>
+                        <goals>
+                            <goal>copy-dependencies</goal>
+                        </goals>
+                        <configuration>
+                            
<includeArtifactIds>org.apache.sling.api,jcr</includeArtifactIds>
+                            <excludeTransitive>true</excludeTransitive>
+                            
<outputDirectory>${test.jars.folder}</outputDirectory>
+                            <overWriteReleases>true</overWriteReleases>
+                            <overWriteSnapshots>true</overWriteSnapshots>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <configuration>
+                    <systemProperties>
+                        <property>
+                            <name>test.jars.folder</name>
+                            <value>${test.jars.folder}</value>
+                        </property>
+                    </systemProperties>
+                </configuration>
+            </plugin>                        
         </plugins>
     </build>
 
@@ -72,6 +107,24 @@
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.jmock</groupId>
+            <artifactId>jmock-junit4</artifactId>
+        </dependency>
+               <dependency>
+                       <!-- Not used by our code, but need a non-bundle jar 
for unit tests -->
+                       <groupId>javax.jcr</groupId>
+                       <artifactId>jcr</artifactId>
+                       <version>1.0</version>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
+                       <!-- Not used by our code, but need a bundle jar for 
unit tests -->
+                       <groupId>org.apache.sling</groupId>
+                       <artifactId>org.apache.sling.api</artifactId>
+                       <version>2.0.6</version>
+                       <scope>test</scope>
+               </dependency>
     </dependencies>
 
 </project>
diff --git 
a/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java
 
b/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java
new file mode 100644
index 0000000..8bb868f
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java
@@ -0,0 +1,68 @@
+/*
+ * 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.sling.commons.osgi.bundleversion;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Version;
+
+/** BundleVersionInfo based on a Bundle object */
+public class BundleBundleVersionInfo implements BundleVersionInfo<Bundle> {
+
+    private Bundle source;
+    private final long lastModified;
+    
+    public BundleBundleVersionInfo(Bundle b) {
+        source = b;
+        
+        long lastMod = BND_LAST_MODIFIED_MISSING;
+        final String mod = (String)source.getHeaders().get(BND_LAST_MODIFIED);
+        if(mod != null) {
+            try {
+                lastMod = Long.parseLong(mod);
+            } catch(NumberFormatException ignore) {
+            }
+        }
+        lastModified = lastMod;
+    }
+    
+    public long getBundleLastModified() {
+        return lastModified;
+    }
+
+    public String getBundleSymbolicName() {
+        return source.getSymbolicName();
+    }
+
+    public Bundle getSource() {
+        return source;
+    }
+
+    public Version getVersion() {
+        return (Version)source.getHeaders().get(Constants.BUNDLE_VERSION);
+    }
+
+    public boolean isBundle() {
+        return true;
+    }
+
+    public boolean isSnapshot() {
+        return getVersion().toString().contains(SNAPSHOT_MARKER);
+    }
+}
diff --git 
a/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java
 
b/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java
new file mode 100644
index 0000000..f23ff71
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java
@@ -0,0 +1,87 @@
+/*
+ * 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.sling.commons.osgi.bundleversion;
+
+import java.util.Comparator;
+
+import org.osgi.framework.Version;
+
+/** Compares BundleVersionInfo */
+public class BundleVersionComparator implements 
Comparator<BundleVersionInfo<?>>{
+
+    private static final int A_GREATER = 1; 
+    private static final int B_GREATER = -1;
+    private static final int EQUAL = 0;
+    
+    public int compare(BundleVersionInfo<?> a, BundleVersionInfo<?> b) {
+        
+        // Handle null values
+        if(a == null) {
+            throw new IllegalArgumentException("a is null, cannot compare");
+        }
+        if(b == null) {
+            throw new IllegalArgumentException("b is null, cannot compare");
+        }
+        
+        // Handle non-bundles: we don't want them!
+        if(!a.isBundle()) {
+            throw new IllegalArgumentException("Not a bundle, cannot compare:" 
+ a);
+        }
+        if(!b.isBundle()) {
+            throw new IllegalArgumentException("Not a bundle, cannot compare:" 
+ b);
+        }
+        
+        // First compare symbolic names
+        int result = 
a.getBundleSymbolicName().compareTo(b.getBundleSymbolicName());
+        
+        // Then compare versions
+        if(result == EQUAL) {
+            final Version va = a.getVersion();
+            final Version vb = b.getVersion();
+            if(va == null && vb == null) {
+                // result = EQUAL
+            } else if(vb == null) {
+                result = A_GREATER;
+            } else if(va == null) {
+                result = B_GREATER;
+            } else {
+                result = va.compareTo(vb);
+            }
+            
+            // more recent ones must come before older ones
+            result = -result;
+        }
+        
+        // Then, if snapshots, compare modification times, more recent comes 
first
+        if(result == EQUAL && a.isSnapshot()) {
+            final long ma = a.getBundleLastModified();
+            final long mb = b.getBundleLastModified();
+            if(ma > mb) {
+                result = A_GREATER;
+            } else if(mb > ma) {
+                result = B_GREATER;
+            }
+            
+            // more recent ones must come before older ones
+            result = -result;
+        }
+        
+        return result;
+    }
+}
diff --git 
a/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java
 
b/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java
new file mode 100644
index 0000000..c9bc28c
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java
@@ -0,0 +1,58 @@
+/*
+ * 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.sling.commons.osgi.bundleversion;
+
+import org.osgi.framework.Version;
+
+/** Provides bundle version information, which can be
+ *  extracted from bundle files or Bundle objects.
+ */
+public interface BundleVersionInfo<T> {
+    /** Marker used by Maven to identify snapshots */
+    String SNAPSHOT_MARKER = "SNAPSHOT";
+    
+    /** Name of the BND attribute that provides the bundle's last modified 
timestamp */
+    String BND_LAST_MODIFIED = "Bnd-LastModified";
+    
+    /** Value for {@link #getBundleLastModified} if corresponding header
+     *  is not present
+     */
+    long BND_LAST_MODIFIED_MISSING = -1L;
+    
+    /** Return the source of information: underlying File or Bundle */
+    T getSource();
+    
+    /** True if the provided data is a valid bundle */
+    boolean isBundle();
+    
+    /** Return the bundle symbolic name, null if not available */
+    String getBundleSymbolicName();
+    
+    /** Return the bundle version, null if not available */
+    Version getVersion();
+    
+    /** True if the bundle version indicates a snapshot */
+    boolean isSnapshot();
+    
+    /** Return the bundle last modification time, based on the 
BND_LAST_MODIFIED 
+     *  manifest header, if available. This is *not* the 
Bundle.getLastModified()
+     *  value, which refers to actions in the OSGi framework.
+     *  @return BND_LAST_MODIFIED_MISSING if header not supplied */
+    long getBundleLastModified();
+}
diff --git 
a/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java
 
b/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java
new file mode 100644
index 0000000..4305cda
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java
@@ -0,0 +1,91 @@
+/*
+ * 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.sling.commons.osgi.bundleversion;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+import org.osgi.framework.Constants;
+import org.osgi.framework.Version;
+
+/** BundleVersionInfo based on a bundle jar file */
+public class FileBundleVersionInfo implements BundleVersionInfo<File> {
+
+    private final String symbolicName;
+    private final Version version;
+    private final boolean isSnapshot;
+    private final long lastModified;
+    private final File source;
+    
+    public FileBundleVersionInfo(File bundle) throws IOException {
+        source = bundle;
+        final Manifest m = new JarFile(bundle).getManifest();
+        if(m == null) {
+            symbolicName = null;
+            version = null;
+            isSnapshot = false;
+            lastModified = BND_LAST_MODIFIED_MISSING;
+        } else {
+            symbolicName = 
m.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
+            final String v = 
m.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
+            version = v == null ? null : new Version(v);
+            isSnapshot = v != null && v.contains(SNAPSHOT_MARKER);
+            final String last = 
m.getMainAttributes().getValue(BND_LAST_MODIFIED);
+            long lastMod = BND_LAST_MODIFIED_MISSING;
+            if(last != null) {
+                try {
+                    lastMod = Long.parseLong(last);
+                } catch(NumberFormatException ignore) {
+                }
+            }
+            lastModified = lastMod;
+        }
+    }
+    
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + " " + source.getAbsolutePath();
+    }
+    
+    public boolean isBundle() {
+        return symbolicName != null;
+    }
+    
+    public long getBundleLastModified() {
+        return lastModified;
+    }
+
+    public String getBundleSymbolicName() {
+        return symbolicName;
+    }
+
+    public File getSource() {
+        return source;
+    }
+
+    public Version getVersion() {
+        return version;
+    }
+
+    public boolean isSnapshot() {
+        return isSnapshot;
+    }
+}
diff --git 
a/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfoTest.java
 
b/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfoTest.java
new file mode 100644
index 0000000..627b2ac
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfoTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.sling.commons.osgi.bundleversion;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Version;
+
+/** Test the BundleBundleVersionInfo class - not extremely
+ *  valid is we're testing with mock Bundles, but should
+ *  at least catch regressions.
+ */
+public class BundleBundleVersionInfoTest {
+    protected Mockery context;
+    
+    @Before
+    public void setUp() {
+        context = new JUnit4Mockery();
+    }
+
+    private Bundle getMockBundle(final String symbolicName, final Version v, 
final long lastModified) {
+        final Dictionary<String, Object> h = new Hashtable<String, Object>();
+        h.put(Constants.BUNDLE_VERSION, v);
+        if(lastModified > 0) {
+            h.put(BundleVersionInfo.BND_LAST_MODIFIED, 
String.valueOf(lastModified));
+        }
+        
+        final Bundle b = context.mock(Bundle.class);
+        context.checking(new Expectations() {{
+            allowing(b).getHeaders();
+            will(returnValue(h));
+            allowing(b).getSymbolicName();
+            will(returnValue(symbolicName));
+            allowing(b).getLastModified();
+            will(returnValue(lastModified));
+        }});
+        return b;
+    }
+    
+    @Test
+    public void testVersionInfo() {
+        final String name = "some.bundle";
+        final Version version = new Version("1.0.4");
+        final long lastMod = 1234L;
+        final Bundle b = getMockBundle(name, version, lastMod); 
+        
+        BundleVersionInfo<?> vi = new BundleBundleVersionInfo(b);
+        assertEquals("Symbolic name matches", name, 
vi.getBundleSymbolicName());
+        assertEquals("Version matches", version, vi.getVersion());
+        assertTrue("isBundle", vi.isBundle());
+        assertFalse("Not a snapshot", vi.isSnapshot());
+        assertEquals("Last-Modified matches", lastMod, 
vi.getBundleLastModified());
+        assertTrue("Bundle is stored as source", vi.getSource() == b);
+    }
+    
+    @Test
+    public void testSnapshot() {
+        final String name = "some.bundle";
+        final Version version = new Version("1.0.4.SNAPSHOT");
+        final long lastMod = 0;
+        final Bundle b = getMockBundle(name, version, lastMod); 
+        
+        BundleVersionInfo<?> vi = new BundleBundleVersionInfo(b);
+        assertEquals("Symbolic name matches", name, 
vi.getBundleSymbolicName());
+        assertEquals("Version matches", version, vi.getVersion());
+        assertTrue("isBundle", vi.isBundle());
+        assertTrue("Bundle is a snapshot", vi.isSnapshot());
+        assertEquals("Last-Modified matches", 
BundleVersionInfo.BND_LAST_MODIFIED_MISSING, vi.getBundleLastModified());
+        assertTrue("Bundle is stored as source", vi.getSource() == b);
+   }
+}
\ No newline at end of file
diff --git 
a/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java
 
b/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java
new file mode 100644
index 0000000..f09c292
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.sling.commons.osgi.bundleversion;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.Test;
+
+public class BundleVersionComparatorTest {
+    
+    @Test
+    public void testSortBundles() {
+        final MockBundleVersionInfo [] sorted = {
+                new MockBundleVersionInfo("a.name", "1.1", 1),
+                new MockBundleVersionInfo("a.name", "1.0", 1),
+                new MockBundleVersionInfo("b", "1.2.0.SNAPSHOT", 2),
+                new MockBundleVersionInfo("b", "1.2.0.SNAPSHOT", 1),
+                new MockBundleVersionInfo("b", "1.1", 1),
+                new MockBundleVersionInfo("b", "1.0.1.SNAPSHOT", 2),
+                new MockBundleVersionInfo("b", "1.0.1.SNAPSHOT", 1),
+                new MockBundleVersionInfo("b", "1.0", 1),
+                new MockBundleVersionInfo("b", "0.9", 1),
+                new MockBundleVersionInfo("b", "0.8.1", 1),
+                new MockBundleVersionInfo("b", "0.8.0", 1)
+        };
+        
+        final List<BundleVersionInfo<?>> list = new 
ArrayList<BundleVersionInfo<?>>();
+        for(int i = sorted.length - 1 ; i >= 0; i--) {
+            list.add(sorted[i]);
+        }
+        
+        final String firstBeforeSort = list.get(0).toString();
+        Collections.sort(list, new BundleVersionComparator());
+        final String newFirstItem = list.get(0).toString();
+        assertFalse("First item (" + newFirstItem + ") must have changed 
during sort", firstBeforeSort.equals(newFirstItem));
+        
+        int i = 0;
+        for(BundleVersionInfo<?> vi : list) {
+            assertEquals("Item sorted as expected at index " + i, 
sorted[i].toString(), vi.toString());
+            i++;
+        }
+    }
+    
+    @Test
+    public void testEqual() {
+        final MockBundleVersionInfo a = new MockBundleVersionInfo("a", "1.0", 
2);
+        final MockBundleVersionInfo b = new MockBundleVersionInfo("a", "1.0", 
1);
+        final BundleVersionComparator c = new BundleVersionComparator();
+        assertEquals("Last-modified must not be relevant for non-snapshot 
bundles", 0, c.compare(a, b));
+    }
+    
+    public void testExceptionsOnNull() {
+        final MockBundleVersionInfo a = new MockBundleVersionInfo("a", "1.0", 
2);
+        final BundleVersionComparator c = new BundleVersionComparator();
+        
+        try {
+            c.compare(a, null);
+            fail("Expected an IllegalArgumentException");
+        } catch(IllegalArgumentException asExpected) {
+        }
+
+        try {
+            c.compare(null, a);
+            fail("Expected an IllegalArgumentException");
+        } catch(IllegalArgumentException asExpected) {
+        }
+
+        try {
+            c.compare(null, null);
+            fail("Expected an IllegalArgumentException");
+        } catch(IllegalArgumentException asExpected) {
+        }
+    }
+    
+    public void testExceptionOnNonBundle() {
+        final MockBundleVersionInfo a = new MockBundleVersionInfo("a", "1.0", 
2);
+        final MockBundleVersionInfo nonBundle = new MockBundleVersionInfo();
+        final BundleVersionComparator c = new BundleVersionComparator();
+        
+        try {
+            c.compare(a, nonBundle);
+            fail("Expected an IllegalArgumentException");
+        } catch(IllegalArgumentException asExpected) {
+        }
+        
+        try {
+            c.compare(nonBundle, a);
+            fail("Expected an IllegalArgumentException");
+        } catch(IllegalArgumentException asExpected) {
+        }
+    }
+}
diff --git 
a/src/test/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfoTest.java
 
b/src/test/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfoTest.java
new file mode 100644
index 0000000..9f8ea09
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfoTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.sling.commons.osgi.bundleversion;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.junit.Test;
+import org.osgi.framework.Version;
+
+public class FileBundleVersionInfoTest {
+    
+    private File getTestJar(String namePrefix) {
+        final File testJarsFolder = new 
File(System.getProperty("test.jars.folder"));
+        for(String jar : testJarsFolder.list()) {
+            if(jar.startsWith(namePrefix)) {
+                return new File(testJarsFolder, jar);
+            }
+        }
+        fail("No test jar found with prefix " + namePrefix + " in " + 
testJarsFolder.getAbsolutePath());
+        return null;
+    }
+    
+    @Test
+    public void testSlingApiJar() throws IOException {
+        final File testJar = getTestJar("org.apache.sling.api");
+        final BundleVersionInfo<?> vi = new FileBundleVersionInfo(testJar);
+        assertEquals("org.apache.sling.api", vi.getBundleSymbolicName());
+        assertEquals(vi.getVersion(), new Version("2.0.6"));
+        assertFalse(vi.isSnapshot());
+        assertEquals(1250080966786L, vi.getBundleLastModified());
+        assertTrue(vi.isBundle());
+        final Object src = vi.getSource();
+        assertTrue(src instanceof File);
+        assertEquals(testJar.getAbsolutePath(), ((File)src).getAbsolutePath());
+    }
+    
+    @Test
+    public void testSlf4jJar() throws IOException {
+        final File testJar = getTestJar("jcr");
+        final BundleVersionInfo<?> vi = new FileBundleVersionInfo(testJar);
+        assertFalse(vi.isBundle());
+        assertNull(vi.getBundleSymbolicName());
+        assertNull(vi.getVersion());
+        assertEquals(BundleVersionInfo.BND_LAST_MODIFIED_MISSING, 
vi.getBundleLastModified());
+        final Object src = vi.getSource();
+        assertTrue(src instanceof File);
+        assertEquals(testJar.getAbsolutePath(), ((File)src).getAbsolutePath());
+    }
+}
diff --git 
a/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java
 
b/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java
new file mode 100644
index 0000000..ee5f6f6
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java
@@ -0,0 +1,72 @@
+/*
+ * 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.sling.commons.osgi.bundleversion;
+
+import org.osgi.framework.Version;
+
+class MockBundleVersionInfo implements BundleVersionInfo<String> {
+
+    private final String source;
+    private final String symbolicName;
+    private final Version version;
+    private final long lastModified;
+    
+    MockBundleVersionInfo() {
+        source = null;
+        symbolicName = null;
+        version = null;
+        lastModified = BundleVersionInfo.BND_LAST_MODIFIED_MISSING;
+    }
+    
+    MockBundleVersionInfo(String symbolicName, String version, long 
lastModified) {
+        this.symbolicName = symbolicName;
+        this.version = new Version(version);
+        this.source = symbolicName + "." + version + "." + lastModified;
+        this.lastModified = lastModified;
+    }
+    
+    @Override
+    public String toString() {
+        return source;
+    }
+    
+    public long getBundleLastModified() {
+        return lastModified;
+    }
+
+    public String getBundleSymbolicName() {
+        return symbolicName;
+    }
+
+    public String getSource() {
+        return source;
+    }
+
+    public Version getVersion() {
+        return version;
+    }
+
+    public boolean isBundle() {
+        return symbolicName != null;
+    }
+
+    public boolean isSnapshot() {
+        return version.toString().contains(SNAPSHOT_MARKER);
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to