mirror of
https://github.com/opennetworkinglab/onos.git
synced 2025-10-18 02:41:49 +02:00
Fix for OS-12, NumberFormatException on badly formatted version string
Change-Id: I7d667b12bcc00e8a8135c367cb9463f266130d31
This commit is contained in:
parent
eb42a73346
commit
1a783729a1
@ -50,20 +50,30 @@ public final class VersionUtil {
|
|||||||
* @return an int number
|
* @return an int number
|
||||||
*/
|
*/
|
||||||
public static int versionCompare(String fromVersion, String toVersion) {
|
public static int versionCompare(String fromVersion, String toVersion) {
|
||||||
|
if (fromVersion == null || toVersion == null) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
String[] fromArr = fromVersion.split("\\.");
|
String[] fromArr = fromVersion.split("\\.");
|
||||||
String[] toArr = toVersion.split("\\.");
|
String[] toArr = toVersion.split("\\.");
|
||||||
int fromFirst = Integer.parseInt(fromArr[0]);
|
if (fromArr.length != 3 || toArr.length != 3) {
|
||||||
int fromMiddle = Integer.parseInt(fromArr[1]);
|
return -1;
|
||||||
int fromEnd = Integer.parseInt(fromArr[2]);
|
}
|
||||||
int toFirst = Integer.parseInt(toArr[0]);
|
try {
|
||||||
int toMiddle = Integer.parseInt(toArr[1]);
|
int fromFirst = Integer.parseInt(fromArr[0]);
|
||||||
int toEnd = Integer.parseInt(toArr[2]);
|
int fromMiddle = Integer.parseInt(fromArr[1]);
|
||||||
if (fromFirst - toFirst != 0) {
|
int fromEnd = Integer.parseInt(fromArr[2]);
|
||||||
return fromFirst - toFirst;
|
int toFirst = Integer.parseInt(toArr[0]);
|
||||||
} else if (fromMiddle - toMiddle != 0) {
|
int toMiddle = Integer.parseInt(toArr[1]);
|
||||||
return fromMiddle - toMiddle;
|
int toEnd = Integer.parseInt(toArr[2]);
|
||||||
} else {
|
if (fromFirst - toFirst != 0) {
|
||||||
return fromEnd - toEnd;
|
return fromFirst - toFirst;
|
||||||
|
} else if (fromMiddle - toMiddle != 0) {
|
||||||
|
return fromMiddle - toMiddle;
|
||||||
|
} else {
|
||||||
|
return fromEnd - toEnd;
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException nfe) {
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user