Skip to content

Commit 8467ce1

Browse files
Merge pull request #412 from oracle/aru-version-compare
Compare ARU product version numerically
2 parents bdf9d20 + c442ffe commit 8467ce1

File tree

4 files changed

+187
-4
lines changed

4 files changed

+187
-4
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+
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CommonOptions.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ public void cleanup() throws IOException, InterruptedException {
245245
return;
246246
}
247247
Utils.deleteFilesRecursively(buildDir());
248-
Utils.removeIntermediateDockerImages(buildEngine, buildId());
248+
if (!dryRun) {
249+
Utils.removeIntermediateDockerImages(buildEngine, buildId());
250+
}
249251
}
250252

251253
/**
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
private void compareDifferingVersions(String firstVersion, String laterVersion) {
24+
Version v1 = new Version(firstVersion);
25+
Version v2 = new Version(laterVersion);
26+
assertEquals(1, v2.compareTo(v1));
27+
assertEquals(-1, v1.compareTo(v2));
28+
}
29+
30+
@Test
31+
void differentVersionNumbers() {
32+
compareDifferingVersions("1.2.3", "1.2.4");
33+
}
34+
35+
@Test
36+
void differentVersionLengths() {
37+
compareDifferingVersions("1.2.3", "1.2.3.1");
38+
}
39+
40+
@Test
41+
void integerComparison() {
42+
compareDifferingVersions("13.9.4.2.9", "13.9.4.2.10");
43+
}
44+
45+
@Test
46+
void nullVersion() {
47+
compareDifferingVersions(null, "1.2.3");
48+
}
49+
50+
@Test
51+
void nonNumericVersion() {
52+
assertThrows(NumberFormatException.class, () -> new Version("1.A.4"));
53+
assertThrows(NumberFormatException.class, () -> new Version("1.2.3-SNAP"));
54+
}
55+
56+
@Test
57+
void allowsNull() {
58+
Version a = new Version(null);
59+
Version b = new Version("1.2.3");
60+
assertEquals(1, b.compareTo(a));
61+
assertEquals(-1, a.compareTo(b));
62+
}
63+
64+
@Test
65+
void equalObjects() {
66+
Version a = new Version("1.2.3");
67+
Version b = new Version("1.2.3");
68+
assertEquals(a, b);
69+
assertEquals(b, a);
70+
assertEquals(a, a);
71+
assertNotEquals(a, null);
72+
}
73+
74+
@Test
75+
void hashcodeTest() {
76+
Version a = new Version("1.2.3");
77+
Version b = new Version("1.2.3");
78+
Version c = new Version("1.2.4");
79+
assertEquals(a.hashCode(), b.hashCode());
80+
assertNotEquals(a.hashCode(), c.hashCode());
81+
}
82+
}

0 commit comments

Comments
 (0)