diff --git a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/utils/VersionUtil.java b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/utils/VersionUtil.java index e19526c96a..bc69de65eb 100644 --- a/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/utils/VersionUtil.java +++ b/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/utils/VersionUtil.java @@ -50,20 +50,30 @@ public final class VersionUtil { * @return an int number */ public static int versionCompare(String fromVersion, String toVersion) { + if (fromVersion == null || toVersion == null) { + return -1; + } String[] fromArr = fromVersion.split("\\."); String[] toArr = toVersion.split("\\."); - int fromFirst = Integer.parseInt(fromArr[0]); - int fromMiddle = Integer.parseInt(fromArr[1]); - int fromEnd = Integer.parseInt(fromArr[2]); - int toFirst = Integer.parseInt(toArr[0]); - int toMiddle = Integer.parseInt(toArr[1]); - int toEnd = Integer.parseInt(toArr[2]); - if (fromFirst - toFirst != 0) { - return fromFirst - toFirst; - } else if (fromMiddle - toMiddle != 0) { - return fromMiddle - toMiddle; - } else { - return fromEnd - toEnd; + if (fromArr.length != 3 || toArr.length != 3) { + return -1; + } + try { + int fromFirst = Integer.parseInt(fromArr[0]); + int fromMiddle = Integer.parseInt(fromArr[1]); + int fromEnd = Integer.parseInt(fromArr[2]); + int toFirst = Integer.parseInt(toArr[0]); + int toMiddle = Integer.parseInt(toArr[1]); + int toEnd = Integer.parseInt(toArr[2]); + if (fromFirst - toFirst != 0) { + return fromFirst - toFirst; + } else if (fromMiddle - toMiddle != 0) { + return fromMiddle - toMiddle; + } else { + return fromEnd - toEnd; + } + } catch (NumberFormatException nfe) { + return -1; } } } diff --git a/protocols/ovsdb/rfc/src/test/java/org/onosproject/ovsdb/rfc/utils/VersionUtilTest.java b/protocols/ovsdb/rfc/src/test/java/org/onosproject/ovsdb/rfc/utils/VersionUtilTest.java new file mode 100644 index 0000000000..d18aca1b5f --- /dev/null +++ b/protocols/ovsdb/rfc/src/test/java/org/onosproject/ovsdb/rfc/utils/VersionUtilTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2015-present Open Networking Foundation + * + * Licensed 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.onosproject.ovsdb.rfc.utils; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.lessThan; + +/** + * Version utility class tests. + */ +public class VersionUtilTest { + + @Test + public void testVersionCompare() { + assertThat(VersionUtil.versionCompare("1.2.3", null), lessThan(0)); + assertThat(VersionUtil.versionCompare(null, "1.2.3"), lessThan(0)); + + assertThat(VersionUtil.versionCompare("1.2.x", "1.2.3"), lessThan(0)); + assertThat(VersionUtil.versionCompare("1.2.3", "1.2.y"), lessThan(0)); + + assertThat(VersionUtil.versionCompare("1", "1.2.3"), lessThan(0)); + assertThat(VersionUtil.versionCompare("1.2", "1.2.3"), lessThan(0)); + assertThat(VersionUtil.versionCompare("1.2.3.4", "1.2.3"), lessThan(0)); + + assertThat(VersionUtil.versionCompare("1.2.3", "1.2.3"), is(0)); + assertThat(VersionUtil.versionCompare("2.2.3", "1.2.3"), greaterThan(0)); + assertThat(VersionUtil.versionCompare("1.2.3", "2.2.3"), lessThan(0)); + } +} \ No newline at end of file