This is an automated email from the ASF dual-hosted git repository. pauls pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/felix-dev.git
The following commit(s) were added to refs/heads/master by this push: new c0ee164686 FELIX-6533 Fixed processing maven version from Felix.properties (#152) c0ee164686 is described below commit c0ee164686c46e70a6d49467f0834236f10d6338 Author: David Matějček <dma...@seznam.cz> AuthorDate: Fri May 20 00:01:19 2022 +0200 FELIX-6533 Fixed processing maven version from Felix.properties (#152) - fixed by refactoring: - impl for processing system versions incl. classifier moved to Version - Version used for both OS and Felix version - to stay compatible with previous versions the original method was just marked as deprecated, can be removed later (public static) - NativeLibraryClauseTest was incorrect for mac os (spaces) - FrameworkVersionTest was changed to VersionTest --- .../apache/felix/framework/ExtensionManager.java | 2 +- .../java/org/apache/felix/framework/Felix.java | 55 +---------- .../apache/felix/framework/VersionConverter.java | 109 +++++++++++++++++++++ .../util/manifestparser/NativeLibraryClause.java | 92 +++-------------- .../felix/framework/FrameworkVersionTest.java | 49 --------- .../felix/framework/VersionConverterTest.java | 48 +++++++++ .../manifestparser/NativeLibraryClauseTest.java | 15 ++- 7 files changed, 185 insertions(+), 185 deletions(-) diff --git a/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java b/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java index 5f0ee347a8..afd7dacc0f 100644 --- a/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java +++ b/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java @@ -223,7 +223,7 @@ class ExtensionManager implements Content if( osVersion != null) { - attributes.put(NativeNamespace.CAPABILITY_OSVERSION_ATTRIBUTE, new Version(NativeLibraryClause.normalizeOSVersion(osVersion))); + attributes.put(NativeNamespace.CAPABILITY_OSVERSION_ATTRIBUTE, VersionConverter.toOsgiVersion(osVersion)); } if( userLang != null) diff --git a/framework/src/main/java/org/apache/felix/framework/Felix.java b/framework/src/main/java/org/apache/felix/framework/Felix.java index eec15c9d58..85cce9984e 100644 --- a/framework/src/main/java/org/apache/felix/framework/Felix.java +++ b/framework/src/main/java/org/apache/felix/framework/Felix.java @@ -4905,7 +4905,7 @@ public class Felix extends BundleImpl implements Framework if ( !m_configMutableMap.containsKey(FelixConstants.FRAMEWORK_OS_VERSION)) { m_configMutableMap.put(FelixConstants.FRAMEWORK_OS_VERSION, - NativeLibraryClause.normalizeOSVersion(System.getProperty("os.version"))); + VersionConverter.toOsgiVersion(System.getProperty("os.version")).toString()); } if (!m_configMutableMap.containsKey(FelixConstants.FRAMEWORK_LANGUAGE)) { @@ -4913,7 +4913,7 @@ public class Felix extends BundleImpl implements Framework System.getProperty("user.language")); } m_configMutableMap.put( - FelixConstants.FELIX_VERSION_PROPERTY, getFrameworkVersion()); + FelixConstants.FELIX_VERSION_PROPERTY, getFrameworkVersion().toString()); Properties defaultProperties = Util.loadDefaultProperties(m_logger); @@ -4959,7 +4959,7 @@ public class Felix extends BundleImpl implements Framework * Read the framework version from the property file. * @return the framework version as a string. **/ - private static String getFrameworkVersion() + private static Version getFrameworkVersion() { // The framework version property. Properties props = new Properties(); @@ -4987,56 +4987,9 @@ public class Felix extends BundleImpl implements Framework } } - // Maven uses a '-' to separate the version qualifier, - // while OSGi uses a '.', so we need to convert to a '.' - StringBuilder sb = - new StringBuilder( - props.getProperty( - FelixConstants.FELIX_VERSION_PROPERTY, "0.0.0")); - String toRet = cleanMavenVersion(sb); - if (toRet.indexOf("${pom") >= 0) - { - return "0.0.0"; - } - else - { - return toRet; - } + return VersionConverter.toOsgiVersion(props.getProperty(FelixConstants.FELIX_VERSION_PROPERTY, "0.0.0")); } - /** - * 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(StringBuilder 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. diff --git a/framework/src/main/java/org/apache/felix/framework/VersionConverter.java b/framework/src/main/java/org/apache/felix/framework/VersionConverter.java new file mode 100644 index 0000000000..974740abb6 --- /dev/null +++ b/framework/src/main/java/org/apache/felix/framework/VersionConverter.java @@ -0,0 +1,109 @@ +/* + * 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.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.osgi.framework.Version; + +/** + * Converts generic version identifiers to the {@link Version} instances. + * + * @author David Matejcek + */ +public class VersionConverter { + + private static final Pattern FUZZY_VERSION = Pattern.compile("(\\d+)(\\.(\\d+)(\\.(\\d+))?)?([^a-zA-Z0-9](.*))?", + Pattern.DOTALL); + + + /** + * Converts generic version id to the {@link Version} instance. Examples: + * + * <pre> + * 0.0 -> 0.0.0 + * 2.3.4-SNAPSHOT -> 2.3.4.SNAPSHOT + * 8.7.6-special-edition -> 8.7.6.special-edition + * </pre> + * + * @param value any usual version id parseable by the {@link Version} class constructor after + * adding missing implicit values. + * @return {@link Version} + * @throws IllegalArgumentException If the numerical components are negative + * or the qualifier string is invalid. + */ + public static Version toOsgiVersion(String value) throws IllegalArgumentException { + return new Version(cleanupVersion(value)); + } + + private static String cleanupVersion(String version) { + StringBuilder result = new StringBuilder(); + Matcher m = FUZZY_VERSION.matcher(version); + if (m.matches()) { + String major = m.group(1); + String minor = m.group(3); + String micro = m.group(5); + String qualifier = m.group(7); + + if (major != null) { + result.append(major); + if (minor != null) { + result.append("."); + result.append(minor); + if (micro != null) { + result.append("."); + result.append(micro); + if (qualifier != null && !qualifier.isEmpty()) { + result.append("."); + cleanupModifier(result, qualifier); + } + } else if (qualifier != null && !qualifier.isEmpty()) { + result.append(".0."); + cleanupModifier(result, qualifier); + } else { + result.append(".0"); + } + } else if (qualifier != null && !qualifier.isEmpty()) { + result.append(".0.0."); + cleanupModifier(result, qualifier); + } else { + result.append(".0.0"); + } + } + } else { + result.append("0.0.0."); + cleanupModifier(result, version); + } + return result.toString(); + } + + + private static void cleanupModifier(StringBuilder result, String modifier) { + for (int i = 0; i < modifier.length(); i++) { + char c = modifier.charAt(i); + if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == '-') { + result.append(c); + } else { + result.append('_'); + } + } + } +} diff --git a/framework/src/main/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClause.java b/framework/src/main/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClause.java index bd4edf04ac..21a8fdfd91 100644 --- a/framework/src/main/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClause.java +++ b/framework/src/main/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClause.java @@ -41,6 +41,8 @@ import org.osgi.framework.FrameworkUtil; import org.osgi.framework.Version; import org.osgi.framework.VersionRange; +import static org.apache.felix.framework.VersionConverter.toOsgiVersion; + public class NativeLibraryClause { private static final String OS_AIX = "aix"; @@ -317,7 +319,7 @@ public class NativeLibraryClause private boolean checkOSVersions(String osVersion, String[] osversions) throws BundleException { - Version currentOSVersion = Version.parseVersion(normalizeOSVersion(osVersion)); + Version currentOSVersion = toOsgiVersion(osVersion); for (int i = 0; (osversions != null) && (i < osversions.length); i++) { try @@ -760,8 +762,8 @@ public class NativeLibraryClause String s = value.substring(1, value.length() - 1); String vlo = s.substring(0, s.indexOf(',')).trim(); String vhi = s.substring(s.indexOf(',') + 1, s.length()).trim(); - return new VersionRange(value.charAt(0), new Version(cleanupVersion(vlo)), new Version( - cleanupVersion(vhi)), value.charAt(value.length() - 1)).toString(); + return new VersionRange(value.charAt(0), toOsgiVersion(vlo), toOsgiVersion(vhi), + value.charAt(value.length() - 1)).toString(); } catch (Exception ex) @@ -770,86 +772,16 @@ public class NativeLibraryClause } } - return normalizeOSVersion(value); - } - - public static String normalizeOSVersion(String value) - { - return new Version(cleanupVersion(value)).toString(); + return toOsgiVersion(value).toString(); } - private static final Pattern FUZZY_VERSION = Pattern.compile( "(\\d+)(\\.(\\d+)(\\.(\\d+))?)?([^a-zA-Z0-9](.*))?", - Pattern.DOTALL ); - private static String cleanupVersion( String version ) - { - StringBuilder result = new StringBuilder(); - Matcher m = FUZZY_VERSION.matcher( version ); - if ( m.matches() ) - { - String major = m.group( 1 ); - String minor = m.group( 3 ); - String micro = m.group( 5 ); - String qualifier = m.group( 7 ); - - if ( major != null ) - { - result.append( major ); - if ( minor != null ) - { - result.append( "." ); - result.append( minor ); - if ( micro != null ) - { - result.append( "." ); - result.append( micro ); - if ( qualifier != null && qualifier.length() > 0 ) - { - result.append( "." ); - cleanupModifier( result, qualifier ); - } - } - else if ( qualifier != null && qualifier.length() > 0) - { - result.append( ".0." ); - cleanupModifier( result, qualifier ); - } - else - { - result.append( ".0" ); - } - } - else if ( qualifier != null && qualifier.length() > 0 ) - { - result.append( ".0.0." ); - cleanupModifier( result, qualifier ); - } - else - { - result.append( ".0.0" ); - } - } - } - else - { - result.append( "0.0.0." ); - cleanupModifier( result, version ); - } - return result.toString(); - } - - - private static void cleanupModifier( StringBuilder result, String modifier ) + /** + * @deprecated use {@link VersionConverter#toOsgiVersion(String)} instead, this method will be removed. + */ + @Deprecated + public static String normalizeOSVersion(String value) { - for ( int i = 0; i < modifier.length(); i++ ) - { - char c = modifier.charAt( i ); - if ( ( c >= '0' && c <= '9' ) || ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || c == '_' - || c == '-' ) - result.append( c ); - else - result.append( '_' ); - } + return toOsgiVersion(value).toString(); } - } diff --git a/framework/src/test/java/org/apache/felix/framework/FrameworkVersionTest.java b/framework/src/test/java/org/apache/felix/framework/FrameworkVersionTest.java deleted file mode 100644 index d9962fb8f4..0000000000 --- a/framework/src/test/java/org/apache/felix/framework/FrameworkVersionTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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 [] {StringBuilder.class}); - method.setAccessible(true); - - StringBuilder sb = new StringBuilder(in); - assertEquals(new Version(out), new Version((String) method.invoke(null, sb))); - } -} diff --git a/framework/src/test/java/org/apache/felix/framework/VersionConverterTest.java b/framework/src/test/java/org/apache/felix/framework/VersionConverterTest.java new file mode 100644 index 0000000000..1c4d1f8513 --- /dev/null +++ b/framework/src/test/java/org/apache/felix/framework/VersionConverterTest.java @@ -0,0 +1,48 @@ +/* + * 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 static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.osgi.framework.Version; + +public class VersionConverterTest { + + @Test + public void testConversions() throws Exception { + assertValid("1.0.0", "1"); + assertValid("2.3.0", "2.3"); + assertValid("1.0.0", "1.0.0"); + assertValid("5.0.0.SNAPSHOT", "5-SNAPSHOT"); + assertValid("1.0.0.SNAPSHOT", "1.0-SNAPSHOT"); + assertValid("1.2.3.SNAPSHOT", "1.2.3-SNAPSHOT"); + assertValid("1.2.3.foo-123", "1.2.3.foo-123"); + assertValid("1.2.3.foo-123-hello", "1.2.3.foo-123-hello"); + assertValid("1.2.3.4_5_6", "1.2.3.4.5.6"); + assertValid("1.2.3.classifier-M1", "1.2.3-classifier-M1"); + assertValid("1.2.3.classifier-M1", "1.2.3.classifier-M1"); + assertValid("1.2.3.classifier_M1", "1.2.3.classifier.M1"); + } + + + private void assertValid(String expectedVersion, String input) throws Exception { + assertEquals(new Version(expectedVersion), VersionConverter.toOsgiVersion(input)); + } +} diff --git a/framework/src/test/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClauseTest.java b/framework/src/test/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClauseTest.java index 342e85de3b..2db57f52c1 100644 --- a/framework/src/test/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClauseTest.java +++ b/framework/src/test/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClauseTest.java @@ -18,9 +18,14 @@ */ package org.apache.felix.framework.util.manifestparser; -import junit.framework.TestCase; +import org.junit.Test; -public class NativeLibraryClauseTest extends TestCase { +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class NativeLibraryClauseTest { + + @Test public void testNormalizeOSName() { assertEquals("win32", NativeLibraryClause.normalizeOSName("win 32")); assertEquals("win32", NativeLibraryClause.normalizeOSName("Win*")); @@ -86,6 +91,7 @@ public class NativeLibraryClauseTest extends TestCase { assertEquals("win32", NativeLibraryClause.normalizeOSName("win32")); } + @Test public void testgetOsNameWithAliases() { assertTrue(NativeLibraryClause.getOsNameWithAliases("win 32").contains("win32")); assertTrue(NativeLibraryClause.getOsNameWithAliases("Win*").contains("win32")); @@ -108,7 +114,7 @@ public class NativeLibraryClauseTest extends TestCase { assertTrue(NativeLibraryClause.getOsNameWithAliases("digitalunix_blah").contains("digitalunix")); assertTrue(NativeLibraryClause.getOsNameWithAliases("HPUX-999").contains("hpux")); assertTrue(NativeLibraryClause.getOsNameWithAliases("Irixxxx").contains("irix")); - assertTrue(NativeLibraryClause.getOsNameWithAliases("mac OS X").contains("mac os x")); + assertTrue(NativeLibraryClause.getOsNameWithAliases("mac OS X").contains("macosx")); assertTrue(NativeLibraryClause.getOsNameWithAliases("Netware").contains("netware")); assertTrue(NativeLibraryClause.getOsNameWithAliases("OpenBSD-0000").contains("openbsd")); assertTrue(NativeLibraryClause.getOsNameWithAliases("netbsd ").contains("netbsd")); @@ -124,7 +130,7 @@ public class NativeLibraryClauseTest extends TestCase { assertTrue(NativeLibraryClause.getOsNameWithAliases("hpux").contains("hpux")); assertTrue(NativeLibraryClause.getOsNameWithAliases("irix").contains("irix")); assertTrue(NativeLibraryClause.getOsNameWithAliases("linux").contains("linux")); - assertTrue(NativeLibraryClause.getOsNameWithAliases("mac os").contains("mac os")); + assertTrue(NativeLibraryClause.getOsNameWithAliases("mac os").contains("macos")); assertTrue(NativeLibraryClause.getOsNameWithAliases("netbsd").contains("netbsd")); assertTrue(NativeLibraryClause.getOsNameWithAliases("netware").contains("netware")); assertTrue(NativeLibraryClause.getOsNameWithAliases("openbsd").contains("openbsd")); @@ -150,6 +156,7 @@ public class NativeLibraryClauseTest extends TestCase { assertTrue(NativeLibraryClause.getOsNameWithAliases("win32").contains("win32")); } + @Test public void testNormalizeOSVersion() { // valid assertEquals("1.0.0", NativeLibraryClause.normalizeOSVersion("1"));