Re: svn commit: r896880 - in /sling/trunk/bundles/commons/osgi: ./ src/main/java/org/apache/sling/commons/osgi/bundleversion/ src/test/java/org/apache/sling/commons/osgi/bundleversion/

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r896880 - in /sling/trunk/bundles/commons/osgi: ./ src/main/java/org/apache/sling/commons/osgi/bundleversion/ src/test/java/org/apache/sling/commons/osgi/bundleversion/

Felix Meschberger-2
Hi,

Cool.

How about making BundleVersionInfo abstract and Comparable ? Thus the
comparator would not be needed and use would probably be simpler and easier.

Regards
Felix

On 07.01.2010 14:52, [hidden email] wrote:

> Author: bdelacretaz
> Date: Thu Jan  7 13:51:44 2010
> New Revision: 896880
>
> URL: http://svn.apache.org/viewvc?rev=896880&view=rev
> Log:
> SLING-1278 - Utilities for bundle version extraction and comparison
>
> Added:
>     sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/
>     sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java   (with props)
>     sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java   (with props)
>     sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java   (with props)
>     sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java   (with props)
>     sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/
>     sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfoTest.java   (with props)
>     sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java   (with props)
>     sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfoTest.java   (with props)
>     sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java   (with props)
> Modified:
>     sling/trunk/bundles/commons/osgi/pom.xml
>
> Modified: sling/trunk/bundles/commons/osgi/pom.xml
> URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/pom.xml?rev=896880&r1=896879&r2=896880&view=diff
> ==============================================================================
> --- sling/trunk/bundles/commons/osgi/pom.xml (original)
> +++ sling/trunk/bundles/commons/osgi/pom.xml Thu Jan  7 13:51:44 2010
> @@ -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>
>
> Added: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java
> URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java?rev=896880&view=auto
> ==============================================================================
> --- sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java (added)
> +++ sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java Thu Jan  7 13:51:44 2010
> @@ -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);
> +    }
> +}
>
> Propchange: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java
> ------------------------------------------------------------------------------
>     svn:keywords = Author Date Id Revision Rev URL
>
> Added: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java
> URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java?rev=896880&view=auto
> ==============================================================================
> --- sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java (added)
> +++ sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java Thu Jan  7 13:51:44 2010
> @@ -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;
> +    }
> +}
>
> Propchange: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java
> ------------------------------------------------------------------------------
>     svn:keywords = Author Date Id Revision Rev URL
>
> Added: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java
> URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java?rev=896880&view=auto
> ==============================================================================
> --- sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java (added)
> +++ sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java Thu Jan  7 13:51:44 2010
> @@ -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();
> +}
>
> Propchange: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java
> ------------------------------------------------------------------------------
>     svn:keywords = Author Date Id Revision Rev URL
>
> Added: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java
> URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java?rev=896880&view=auto
> ==============================================================================
> --- sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java (added)
> +++ sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java Thu Jan  7 13:51:44 2010
> @@ -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;
> +    }
> +}
>
> Propchange: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java
> ------------------------------------------------------------------------------
>     svn:keywords = Author Date Id Revision Rev URL
>
> Added: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfoTest.java
> URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfoTest.java?rev=896880&view=auto
> ==============================================================================
> --- sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfoTest.java (added)
> +++ sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfoTest.java Thu Jan  7 13:51:44 2010
> @@ -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
>
> Propchange: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfoTest.java
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfoTest.java
> ------------------------------------------------------------------------------
>     svn:keywords = Author Date Id Revision Rev URL
>
> Added: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java
> URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java?rev=896880&view=auto
> ==============================================================================
> --- sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java (added)
> +++ sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java Thu Jan  7 13:51:44 2010
> @@ -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) {
> +        }
> +    }
> +}
>
> Propchange: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java
> ------------------------------------------------------------------------------
>     svn:keywords = Author Date Id Revision Rev URL
>
> Added: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfoTest.java
> URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfoTest.java?rev=896880&view=auto
> ==============================================================================
> --- sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfoTest.java (added)
> +++ sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfoTest.java Thu Jan  7 13:51:44 2010
> @@ -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());
> +    }
> +}
>
> Propchange: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfoTest.java
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfoTest.java
> ------------------------------------------------------------------------------
>     svn:keywords = Author Date Id Revision Rev URL
>
> Added: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java
> URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java?rev=896880&view=auto
> ==============================================================================
> --- sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java (added)
> +++ sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java Thu Jan  7 13:51:44 2010
> @@ -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);
> +    }
> +}
>
> Propchange: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java
> ------------------------------------------------------------------------------
>     svn:keywords = Author Date Id Revision Rev URL
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r896880 - in /sling/trunk/bundles/commons/osgi: ./ src/main/java/org/apache/sling/commons/osgi/bundleversion/ src/test/java/org/apache/sling/commons/osgi/bundleversion/

Bertrand Delacretaz
On Thu, Jan 7, 2010 at 3:10 PM, Felix Meschberger <[hidden email]> wrote:
> ...How about making BundleVersionInfo abstract and Comparable ? Thus the
> comparator would not be needed and use would probably be simpler and easier....

Good idea, I'll do that!
-Bertrand