Fix for OS-12, NumberFormatException on badly formatted version string

Change-Id: I7d667b12bcc00e8a8135c367cb9463f266130d31
This commit is contained in:
Ray Milkey 2018-06-27 13:21:43 -07:00
parent eb42a73346
commit 1a783729a1
2 changed files with 68 additions and 12 deletions

View File

@ -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;
}
}
}

View File

@ -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));
}
}