You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2014/11/20 18:01:05 UTC

[2/4] incubator-brooklyn git commit: Fix + test Osgis.parseOsgiIdentifier

Fix + test Osgis.parseOsgiIdentifier


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/6933be6e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/6933be6e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/6933be6e

Branch: refs/heads/master
Commit: 6933be6ef9ef408fde948052e9799a74c0096a71
Parents: 068de70
Author: Aled Sage <al...@gmail.com>
Authored: Tue Nov 18 23:18:50 2014 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Thu Nov 20 15:26:02 2014 +0000

----------------------------------------------------------------------
 .../src/main/java/brooklyn/util/osgi/Osgis.java | 33 +++++++++++-----
 .../test/java/brooklyn/util/osgi/OsgisTest.java | 41 ++++++++++++++++++++
 2 files changed, 64 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/6933be6e/core/src/main/java/brooklyn/util/osgi/Osgis.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/osgi/Osgis.java b/core/src/main/java/brooklyn/util/osgi/Osgis.java
index c6c9d9a..4fe25f9 100644
--- a/core/src/main/java/brooklyn/util/osgi/Osgis.java
+++ b/core/src/main/java/brooklyn/util/osgi/Osgis.java
@@ -74,6 +74,7 @@ import brooklyn.util.time.Time;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Joiner;
+import com.google.common.base.Objects;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
 import com.google.common.base.Stopwatch;
@@ -93,8 +94,8 @@ public class Osgis {
     private static final Set<String> SYSTEM_BUNDLES = MutableSet.of();
 
     public static class VersionedName {
-        private String symbolicName;
-        private Version version;
+        private final String symbolicName;
+        private final Version version;
         public VersionedName(Bundle b) {
             this.symbolicName = b.getSymbolicName();
             this.version = b.getVersion();
@@ -112,12 +113,22 @@ public class Osgis {
         public boolean equals(String sn, Version v) {
             return symbolicName.equals(sn) && (version == null && v == null || version != null && version.equals(v));
         }
-        protected String getSymbolicName() {
+        public String getSymbolicName() {
             return symbolicName;
         }
-        protected Version getVersion() {
+        public Version getVersion() {
             return version;
         }
+        @Override
+        public int hashCode() {
+            return Objects.hashCode(symbolicName, version);
+        }
+        @Override
+        public boolean equals(Object other) {
+            if (!(other instanceof VersionedName)) return false;
+            VersionedName o = (VersionedName) other;
+            return Objects.equal(symbolicName, o.symbolicName) && Objects.equal(version, o.version);
+        }
     }
     
     public static class BundleFinder {
@@ -610,8 +621,8 @@ public class Osgis {
     }
 
     /** Takes a string which might be of the form "symbolic-name" or "symbolic-name:version" (or something else entirely)
-     * and returns an array of 1 or 2 string items being the symbolic name or symbolic name and version if possible
-     * (or returning {@link Maybe#absent()} if not, with a suitable error message). */
+     * and returns a VersionedName. The versionedName.getVersion() will be null if if there was no version in the input
+     * (or returning {@link Maybe#absent()} if not valid, with a suitable error message). */
     public static Maybe<VersionedName> parseOsgiIdentifier(String symbolicNameOptionalWithVersion) {
         if (Strings.isBlank(symbolicNameOptionalWithVersion))
             return Maybe.absent("OSGi identifier is blank");
@@ -621,10 +632,12 @@ public class Osgis {
             return Maybe.absent("OSGi identifier has too many parts; max one ':' symbol");
         
         Version v = null;
-        try {
-            v = Version.parseVersion(parts[1]);
-        } catch (IllegalArgumentException e) {
-            return Maybe.absent("OSGi identifier has invalid version string");
+        if (parts.length == 2) {
+            try {
+                v = Version.parseVersion(parts[1]);
+            } catch (IllegalArgumentException e) {
+                return Maybe.absent("OSGi identifier has invalid version string ("+e.getMessage()+")");
+            }
         }
         
         return Maybe.of(new VersionedName(parts[0], v));

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/6933be6e/core/src/test/java/brooklyn/util/osgi/OsgisTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/util/osgi/OsgisTest.java b/core/src/test/java/brooklyn/util/osgi/OsgisTest.java
new file mode 100644
index 0000000..49f8017
--- /dev/null
+++ b/core/src/test/java/brooklyn/util/osgi/OsgisTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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 brooklyn.util.osgi;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+
+import org.osgi.framework.Version;
+import org.testng.annotations.Test;
+
+import brooklyn.util.osgi.Osgis.VersionedName;
+
+public class OsgisTest {
+
+    @Test
+    public void testParseOsgiIdentifier() throws Exception {
+        assertEquals(Osgis.parseOsgiIdentifier("a.b").get(), new VersionedName("a.b", null));
+        assertEquals(Osgis.parseOsgiIdentifier("a.b:0.1.2").get(), new VersionedName("a.b", Version.parseVersion("0.1.2")));
+        assertEquals(Osgis.parseOsgiIdentifier("a.b:0.0.0.SNAPSHOT").get(), new VersionedName("a.b", Version.parseVersion("0.0.0.SNAPSHOT")));
+        assertFalse(Osgis.parseOsgiIdentifier("a.b:0.notanumber.2").isPresent()); // invalid version
+        assertFalse(Osgis.parseOsgiIdentifier("a.b:0.1.2:3.4.5").isPresent());    // too many colons
+        assertFalse(Osgis.parseOsgiIdentifier("a.b:0.0.0_SNAPSHOT").isPresent()); // invalid version
+        assertFalse(Osgis.parseOsgiIdentifier("").isPresent());
+    }
+}