Skip to content

Commit 7c211f3

Browse files
committed
replace text compare of ARU product version with numeric comparison
1 parent bdf9d20 commit 7c211f3

File tree

3 files changed

+181
-3
lines changed

3 files changed

+181
-3
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/aru/AruPatch.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class AruPatch implements Comparable<AruPatch> {
2525
private static final LoggingFacade logger = LoggingFactory.getLogger(AruPatch.class);
2626

2727
private String patchId;
28-
private String version;
28+
private Version version;
2929
private String description;
3030
private String product;
3131
private String release;
@@ -45,12 +45,20 @@ public AruPatch patchId(String value) {
4545
return this;
4646
}
4747

48+
/**
49+
* The ARU version number of the FMW product associated with this patch.
50+
* @return The string value of the version found in ARU.
51+
*/
4852
public String version() {
49-
return version;
53+
if (version != null) {
54+
return version.toString();
55+
} else {
56+
return null;
57+
}
5058
}
5159

5260
public AruPatch version(String value) {
53-
version = value;
61+
version = new Version(value);
5462
return this;
5563
}
5664

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.aru;
5+
6+
import java.util.Arrays;
7+
8+
public class Version implements Comparable<Version> {
9+
private final int[] sequence;
10+
private final String stringValue;
11+
12+
/**
13+
* Representation of the ARU version number used for Oracle products.
14+
* Version must be one or more integers separated by a period, ".".
15+
* @param value String to be parsed as the ARU version.
16+
*/
17+
public Version(String value) {
18+
stringValue = value;
19+
20+
if (value != null && !value.isEmpty()) {
21+
// split version into a sequence tokens using the period separator
22+
sequence = Arrays.stream(value.split("\\."))
23+
.mapToInt(Integer::parseInt)
24+
.toArray();
25+
} else {
26+
sequence = new int[1];
27+
}
28+
}
29+
30+
/**
31+
* Return the sequence of version tokens padded to the minimum length with 0's.
32+
* The sequence will NOT be truncated.
33+
* @param minLength minimum number of version tokens in the array.
34+
* @return sequence of version tokens
35+
*/
36+
public int[] getSequence(int minLength) {
37+
if (sequence.length < minLength) {
38+
return Arrays.copyOf(sequence, minLength);
39+
}
40+
return sequence;
41+
}
42+
43+
/**
44+
* Compare this version number against the provided version, returning -1, 0, or 1 if
45+
* this version is less than, equal to, or greater than the provided version, respectively.
46+
* @param provided the object to be compared.
47+
* @return -1, 0, or 1 if this version is less than, equal to, or greater than the provided version
48+
*/
49+
@Override
50+
public int compareTo(Version provided) {
51+
int match = 0;
52+
int sequenceLength = Math.max(sequence.length, provided.sequence.length);
53+
int[] mySequence = getSequence(sequenceLength);
54+
int[] providedSequence = provided.getSequence(sequenceLength);
55+
56+
for (int i = 0; i < sequenceLength; i++) {
57+
if (mySequence[i] > providedSequence[i]) {
58+
match = 1;
59+
} else if (mySequence[i] < providedSequence[i]) {
60+
match = -1;
61+
}
62+
if (match != 0) {
63+
break;
64+
}
65+
}
66+
67+
return match;
68+
}
69+
70+
@Override
71+
public boolean equals(Object o) {
72+
if (this == o) {
73+
return true;
74+
}
75+
if (o == null || getClass() != o.getClass()) {
76+
return false;
77+
}
78+
Version version = (Version) o;
79+
return this.compareTo(version) == 0;
80+
}
81+
82+
@Override
83+
public int hashCode() {
84+
return Arrays.hashCode(sequence);
85+
}
86+
87+
@Override
88+
public String toString() {
89+
return stringValue;
90+
}
91+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.aru;
5+
6+
import org.junit.jupiter.api.Tag;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
@Tag("unit")
14+
class VersionTest {
15+
@Test
16+
void sameVersionNumber() {
17+
Version a = new Version("1.2.3");
18+
Version b = new Version("1.2.3");
19+
assertEquals(0, b.compareTo(a));
20+
assertEquals(0, a.compareTo(b));
21+
}
22+
23+
@Test
24+
void differentVersionNumbers() {
25+
Version a = new Version("1.2.3");
26+
Version b = new Version("1.2.4");
27+
assertEquals(1, b.compareTo(a));
28+
assertEquals(-1, a.compareTo(b));
29+
}
30+
31+
@Test
32+
void differentVersionLengths() {
33+
Version a = new Version("1.2.3");
34+
Version b = new Version("1.2.3.1");
35+
assertEquals(1, b.compareTo(a));
36+
assertEquals(-1, a.compareTo(b));
37+
}
38+
39+
@Test
40+
void integerComparison() {
41+
Version a = new Version("13.9.4.2.9");
42+
Version b = new Version("13.9.4.2.10");
43+
assertEquals(1, b.compareTo(a));
44+
assertEquals(-1, a.compareTo(b));
45+
}
46+
47+
@Test
48+
void nonNumericVersion() {
49+
assertThrows(NumberFormatException.class, () -> new Version("1.A.4"));
50+
assertThrows(NumberFormatException.class, () -> new Version("1.2.3-SNAP"));
51+
}
52+
53+
@Test
54+
void allowsNull() {
55+
Version a = new Version(null);
56+
Version b = new Version("1.2.3");
57+
assertEquals(1, b.compareTo(a));
58+
assertEquals(-1, a.compareTo(b));
59+
}
60+
61+
@Test
62+
void equalObjects() {
63+
Version a = new Version("1.2.3");
64+
Version b = new Version("1.2.3");
65+
Version c = a;
66+
assertEquals(a, b);
67+
assertEquals(b, a);
68+
assertEquals(a, c);
69+
}
70+
71+
@Test
72+
void hashcodeTest() {
73+
Version a = new Version("1.2.3");
74+
Version b = new Version("1.2.3");
75+
Version c = new Version("1.2.4");
76+
assertEquals(a.hashCode(), b.hashCode());
77+
assertNotEquals(a.hashCode(), c.hashCode());
78+
}
79+
}

0 commit comments

Comments
 (0)