Author: davidb
Date: Thu May 29 07:59:22 2014
New Revision: 1598214

URL: http://svn.apache.org/r1598214
Log:
[FELIX-3309] Incorrect replacement logic for dashes in framework version.

This commit addresses the issue.
Unit test included.

Added:
    
felix/trunk/framework/src/test/java/org/apache/felix/framework/FrameworkVersionTest.java
Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java

Modified: 
felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java?rev=1598214&r1=1598213&r2=1598214&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java 
(original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java 
Thu May 29 07:59:22 2014
@@ -4537,11 +4537,7 @@ public class Felix extends BundleImpl im
             new StringBuffer(
                 props.getProperty(
                     FelixConstants.FELIX_VERSION_PROPERTY, "0.0.0"));
-        if (sb.toString().indexOf("-") >= 0)
-        {
-            sb.setCharAt(sb.toString().indexOf("-"), '.');
-        }
-        String toRet = sb.toString();
+        String toRet = cleanMavenVersion(sb);
         if (toRet.indexOf("${pom") >= 0)
         {
             return "0.0.0";
@@ -4552,6 +4548,40 @@ public class Felix extends BundleImpl im
         }
     }
 
+    /**
+     * The main purpose of this method is to turn a.b.c-SNAPSHOT into 
a.b.c.SNAPSHOT
+     * it can also deal with a.b-SNAPSHOT and turns it into a.b.0.SNAPSHOT and
+     * will leave the dash in a.b.c.something-else, as it's valid in that last 
example.
+     * In short this method attempts to map a Maven version to an OSGi version 
as well
+     * as possible.
+     * @param sb The version to be cleaned
+     * @return The cleaned version
+     */
+    private static String cleanMavenVersion(StringBuffer sb)
+    {
+        int dots = 0;
+        for (int i = 0; i < sb.length(); i++)
+        {
+            switch (sb.charAt(i))
+            {
+                case '.':
+                    dots++;
+                    break;
+                case '-':
+                    if (dots < 3)
+                    {
+                        sb.setCharAt(i, '.');
+                        for (int j = dots; j < 2; j++)
+                        {
+                            sb.insert(i, ".0");
+                        }
+                    }
+                    break;
+            }
+        }
+        return sb.toString();
+    }
+
     //
     // Private utility methods.
     //

Added: 
felix/trunk/framework/src/test/java/org/apache/felix/framework/FrameworkVersionTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/framework/src/test/java/org/apache/felix/framework/FrameworkVersionTest.java?rev=1598214&view=auto
==============================================================================
--- 
felix/trunk/framework/src/test/java/org/apache/felix/framework/FrameworkVersionTest.java
 (added)
+++ 
felix/trunk/framework/src/test/java/org/apache/felix/framework/FrameworkVersionTest.java
 Thu May 29 07:59:22 2014
@@ -0,0 +1,49 @@
+/*
+ * 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.framework;
+
+import java.lang.reflect.Method;
+
+import junit.framework.TestCase;
+
+import org.osgi.framework.Version;
+
+public class FrameworkVersionTest extends TestCase
+{
+    public void testFrameworkVersion() throws Exception
+    {
+        testFrameworkVersion("1.0.0", "1");
+        testFrameworkVersion("2.3.0", "2.3");
+        testFrameworkVersion("1.0.0", "1.0.0");
+        testFrameworkVersion("5.0.0.SNAPSHOT", "5-SNAPSHOT");
+        testFrameworkVersion("1.0.0.SNAPSHOT", "1.0-SNAPSHOT");
+        testFrameworkVersion("1.2.3.SNAPSHOT", "1.2.3-SNAPSHOT");
+        testFrameworkVersion("1.2.3.foo-123", "1.2.3.foo-123");
+        testFrameworkVersion("1.2.3.foo-123-hello", "1.2.3.foo-123-hello");
+    }
+
+    private void testFrameworkVersion(String out, String in) throws Exception
+    {
+        Method method = Felix.class.getDeclaredMethod("cleanMavenVersion", new 
Class [] {StringBuffer.class});
+        method.setAccessible(true);
+
+        StringBuffer sb = new StringBuffer(in);
+        assertEquals(new Version(out), new Version((String) 
method.invoke(null, sb)));
+    }
+}


Reply via email to