Skip to content

Commit 350b792

Browse files
committed
Merge branch 'sonar-cleanup' into 'main'
Minor cleanup from Sonar suggestions See merge request weblogic-cloud/weblogic-image-tool!481
2 parents 0eacb6d + 2a3f33e commit 350b792

File tree

16 files changed

+54
-47
lines changed

16 files changed

+54
-47
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStore.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
public class FileCacheStore implements CacheStore {
2828

29-
public static final String CACHEDIR = "WLSIMG_CACHEDIR";
29+
public static final String CACHE_DIR_ENV = "WLSIMG_CACHEDIR";
3030
private static final LoggingFacade logger = LoggingFactory.getLogger(FileCacheStore.class);
3131

3232
private final Properties properties = new Properties();
@@ -144,7 +144,7 @@ private static String defaultCacheDir() {
144144
* @return cache directory
145145
*/
146146
private static String initCacheDir() throws IOException {
147-
String cacheDirStr = Utils.getEnvironmentProperty(CACHEDIR, FileCacheStore::defaultCacheDir);
147+
String cacheDirStr = Utils.getEnvironmentProperty(CACHE_DIR_ENV, FileCacheStore::defaultCacheDir);
148148

149149
Path cacheDir = Paths.get(cacheDirStr);
150150

imagetool/src/main/java/com/oracle/weblogic/imagetool/inspect/InventoryPatch.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
import java.util.ArrayList;
77
import java.util.List;
8-
9-
import com.oracle.weblogic.imagetool.util.Utils;
8+
import java.util.regex.Matcher;
9+
import java.util.regex.Pattern;
1010

1111
public class InventoryPatch {
1212
private String bug;
@@ -27,26 +27,23 @@ public String description() {
2727

2828
/**
2929
* Parse the provided string into patch objects, and return the list of patches.
30-
* The fields should be separated by a semi-colon with three fields per patch.
30+
* The fields should be separated by a semicolon with three fields per patch.
3131
* @param patchesString patch data from the inspected image
3232
* @return a list of patch objects
3333
*/
3434
public static List<InventoryPatch> parseInventoryPatches(String patchesString) {
3535
List<InventoryPatch> patches = new ArrayList<>();
36-
if (!Utils.isEmptyString(patchesString)) {
37-
String[] tokens = patchesString.split(";");
38-
for (int i = 0; i < tokens.length; i++) {
39-
InventoryPatch patch = new InventoryPatch();
40-
patch.bug = tokens[i];
41-
if (i++ < tokens.length) {
42-
patch.uid = tokens[i];
43-
}
44-
if (i++ < tokens.length) {
45-
patch.description = tokens[i].replace("\"", "");
46-
}
47-
patches.add(patch);
48-
}
36+
// Pattern defines a tuple of three elements: patch ID, patch UID, and patch description.
37+
Pattern tuplePattern = Pattern.compile("(\\d+);(\\d+);\\\"(.*?)\\\";");
38+
Matcher patchMatcher = tuplePattern.matcher(patchesString);
39+
while (patchMatcher.find()) {
40+
InventoryPatch patch = new InventoryPatch();
41+
patch.bug = patchMatcher.group(1);
42+
patch.uid = patchMatcher.group(2);
43+
patch.description = patchMatcher.group(3);
44+
patches.add(patch);
4945
}
46+
5047
return patches;
5148
}
5249
}

imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/CacheStoreTestImpl.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
public class CacheStoreTestImpl implements CacheStore {
1111

12-
private HashMap<String, String> cache = new HashMap<>();
13-
private Path cacheDir;
12+
private final HashMap<String, String> cache = new HashMap<>();
13+
private final Path cacheDir;
1414

1515
public CacheStoreTestImpl(Path cacheDir) {
1616
this.cacheDir = cacheDir;
@@ -46,6 +46,7 @@ public String deleteFromCache(String key) {
4646

4747
@Override
4848
public void clearCache() {
49+
cache.clear();
4950
}
5051

5152
@Override

imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/CachedFileTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class CachedFileTest {
3535
static Path cacheDir;
3636
static CacheStore cacheStore;
3737
static final List<String> fileContents = Arrays.asList("A", "B", "C");
38-
static final String ver12213 = "12.2.1.3.0";
38+
static final String VER_12213 = "12.2.1.3.0";
3939

4040
@BeforeAll
4141
static void setup(@TempDir Path tempDir, @TempDir Path cacheDir) throws IOException {
@@ -49,7 +49,7 @@ static void setup(@TempDir Path tempDir, @TempDir Path cacheDir) throws IOExcept
4949
CachedFileTest.cacheDir = cacheDir;
5050
cacheStore = new CacheStoreTestImpl(cacheDir);
5151
// build a fake cache with several installers
52-
cacheStore.addToCache("wls_" + ver12213, path12213.toString());
52+
cacheStore.addToCache("wls_" + VER_12213, path12213.toString());
5353
cacheStore.addToCache("wls_12.2.1.4.0_" + BuildPlatform.getPlatformName(), path12214.toString());
5454
cacheStore.addToCache("wls_14.1.1.0.0_amd64", path1411.toString());
5555

@@ -83,7 +83,7 @@ void userProvidedPatchVersionAsId() {
8383
}
8484

8585
@Test
86-
void resolveFileNotFound() throws Exception {
86+
void resolveFileNotFound() {
8787
// resolve should fail for a CachedFile that is not in the store
8888
CachedFile fakeFile = new CachedFile(InstallerType.WLS, "10.3.6.0.0");
8989
assertThrows(FileNotFoundException.class, () -> fakeFile.resolve(cacheStore));
@@ -92,15 +92,15 @@ void resolveFileNotFound() throws Exception {
9292
@Test
9393
void resolveFileFindsFile() throws IOException {
9494
// Resolve a CachedFile stored in the cache (created in test setup above)
95-
CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, ver12213);
96-
String expected = cacheStore.getValueFromCache("wls_" + ver12213);
95+
CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, VER_12213);
96+
String expected = cacheStore.getValueFromCache("wls_" + VER_12213);
9797
assertEquals(expected, wlsInstallerFile.resolve(cacheStore), "CachedFile did not resolve file");
9898
}
9999

100100
@Test
101101
void resolveNoArchFile() throws IOException {
102102
// Look for a cache entry where the user specified the architecture/platform amd64
103-
CachedFile wlsNoArch = new CachedFile(InstallerType.WLS, ver12213, "amd64");
103+
CachedFile wlsNoArch = new CachedFile(InstallerType.WLS, VER_12213, "amd64");
104104

105105
// verify the cache is setup as expected.
106106
// wls_12.2.1.3.0 is in the cache, but wls_12.2.1.3.0_amd64 is NOT in the cache
@@ -138,7 +138,7 @@ void resolveFallbackToLocalArch() throws IOException {
138138

139139
@Test
140140
void copyFile(@TempDir Path contextDir) throws Exception {
141-
CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, ver12213);
141+
CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, VER_12213);
142142
// copy the file from the cache store to the fake build context directory
143143
Path result = wlsInstallerFile.copyFile(cacheStore, contextDir.toString());
144144
// check to see if the file was copied correctly by examining the contents of the resulting file

imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStoreTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,33 @@
2424
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
2525
class FileCacheStoreTest {
2626

27-
private static final String testKey = "abc_xyz_123";
28-
private static final String testVal = "this_is_a_test";
27+
private static final String TEST_KEY = "abc_xyz_123";
28+
private static final String TEST_VAL = "this_is_a_test";
2929

3030
@BeforeAll
3131
static void init(@TempDir File tempDir) throws CacheStoreException {
32-
System.setProperty(FileCacheStore.CACHEDIR, tempDir.getAbsolutePath());
32+
System.setProperty(FileCacheStore.CACHE_DIR_ENV, tempDir.getAbsolutePath());
3333
cache().clearCache();
3434
}
3535

3636
@Test
3737
@Order(1)
3838
void addingValueToCache() {
3939
// add value to cache
40-
assertDoesNotThrow(() -> cache().addToCache(testKey, testVal), "Add to cache threw an exception");
40+
assertDoesNotThrow(() -> cache().addToCache(TEST_KEY, TEST_VAL), "Add to cache threw an exception");
4141
}
4242

4343
@Test
4444
@Order(2)
4545
void checkValueInCache() {
4646
// check to see if the key that was just added is there, and value matches expected value
4747
assertDoesNotThrow(() ->
48-
assertTrue(cache().containsKey(testKey)),
48+
assertTrue(cache().containsKey(TEST_KEY)),
4949
"containsKey failed to find key or value that was just added");
5050

5151
// check (another way) that the value was just added
5252
assertDoesNotThrow(() ->
53-
assertEquals(testVal, cache().getValueFromCache(testKey), "Found unexpected value in cache"),
53+
assertEquals(TEST_VAL, cache().getValueFromCache(TEST_KEY), "Found unexpected value in cache"),
5454
"Get from cache threw an exception");
5555
}
5656

@@ -63,7 +63,7 @@ void deleteValueFromCache() {
6363
"Delete from cache threw an exception");
6464

6565
assertDoesNotThrow(() ->
66-
assertEquals(testVal, cache().deleteFromCache(testKey), "Value from deleted key did not match"),
66+
assertEquals(TEST_VAL, cache().deleteFromCache(TEST_KEY), "Value from deleted key did not match"),
6767
"Delete from cache threw an exception");
6868
}
6969

imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/PatchFileTest.java

-2
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,6 @@ void multiplePatchVersionsNoVersionSupplied() throws Exception {
209209
String filePathFromCache = cacheStore.getValueFromCache(patchId + "_12.2.1.3.0");
210210
assertNotNull(filePathFromCache, "Could not find new patch in cache");
211211
assertEquals(filePath, filePathFromCache, "Patch in cache does not match");
212-
213-
//assertEquals("600000000073715", patchFile.getReleaseNumber(), "Patch did not find release number");
214212
}
215213

216214
@Test

imagetool/src/test/java/com/oracle/weblogic/imagetool/inspect/InspectTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919

2020
@Tag("unit")
2121
class InspectTest {
22+
@Test
23+
void testPatchStringParser() {
24+
assertEquals(0, InventoryPatch.parseInventoryPatches("").size());
25+
assertEquals(1, InventoryPatch.parseInventoryPatches("30319071;23384603;\"One-off\";").size());
26+
assertEquals(3, InventoryPatch.parseInventoryPatches(
27+
"30319071;23384603;\"One-off\";26355633;21447583;\"One-off\";26287183;21447582;\"One-off\";").size());
28+
// same as previous but with last semi-colon removed. last patch should be discarded but shouldn't fail.
29+
assertEquals(2, InventoryPatch.parseInventoryPatches(
30+
"30319071;23384603;\"One-off\";26355633;21447583;\"One-off\";26287183;21447582;\"One-off\"").size());
31+
}
32+
2233
@Test
2334
void testJsonOutput() throws IOException {
2435
testPropertiesToJson("src/test/resources/inspect/image1.properties",

imagetool/src/test/java/com/oracle/weblogic/imagetool/util/DockerfileBuilderTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ void validateMustacheAliases() throws IOException {
4444

4545
MustacheFactory mf = new DefaultMustacheFactory(new File("src/main/resources/docker-files"));
4646
Mustache mustache = mf.compile("Create_Image.mustache");
47-
//mustache.execute(new PrintWriter(System.out), dockerfileOptions).flush();
4847
mustache.execute(new StringWriter(), dockerfileOptions).flush();
4948
assertTrue(true);
5049
}

imagetool/src/test/java/com/oracle/weblogic/imagetool/util/UtilsTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class UtilsTest {
4242
private SystemProperties overrideProperties;
4343

4444
@Test
45-
void firstGreaterThanLast() throws Exception {
45+
void firstGreaterThanLast() {
4646
String thisVersion = "12.2.1.3.0";
4747
String thatVersion = "12.2.1.2.0";
4848
assertTrue(Utils.compareVersions(thisVersion, thatVersion) > 0,
@@ -85,7 +85,7 @@ void firstGreaterThanLast() throws Exception {
8585
}
8686

8787
@Test
88-
void secondGreaterThanFirst() throws Exception {
88+
void secondGreaterThanFirst() {
8989
String thisVersion = "12.2.1.3.0";
9090
String thatVersion = "12.2.1.4.0";
9191
assertTrue(Utils.compareVersions(thisVersion, thatVersion) < 0,
@@ -123,7 +123,7 @@ void secondGreaterThanFirst() throws Exception {
123123
}
124124

125125
@Test
126-
void versionsShouldBeEqual() throws Exception {
126+
void versionsShouldBeEqual() {
127127
String thisVersion = "12.2.1.3.0";
128128
String thatVersion = "12.2.1.3.0";
129129
assertEquals(0, Utils.compareVersions(thisVersion, thatVersion),

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
<plugin>
155155
<groupId>org.apache.maven.plugins</groupId>
156156
<artifactId>maven-failsafe-plugin</artifactId>
157-
<version>3.1.2</version>
157+
<version>3.3.1</version>
158158
<configuration>
159159
<groups>${test.groups}</groups>
160160
<excludedGroups>failing</excludedGroups>

tests/src/test/java/com/oracle/weblogic/imagetool/tests/extensions/TimingExtension.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,21 @@
55

66
import java.util.concurrent.TimeUnit;
77

8-
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
9-
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
108
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
119
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
1210
import org.junit.jupiter.api.extension.ExtensionContext;
1311

1412
public class TimingExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
15-
private static final LoggingFacade logger = LoggingFactory.getLogger(TimingExtension.class);
16-
1713
private static final String START_TIME = "start time";
1814
private static final String EM = LoggingExtension.EM;
1915

2016
@Override
21-
public void beforeTestExecution(ExtensionContext context) throws Exception {
17+
public void beforeTestExecution(ExtensionContext context) {
2218
getStore(context).put(START_TIME, System.currentTimeMillis());
2319
}
2420

2521
@Override
26-
public void afterTestExecution(ExtensionContext context) throws Exception {
22+
public void afterTestExecution(ExtensionContext context) {
2723
long startTime = getStore(context).remove(START_TIME, long.class);
2824
long duration = System.currentTimeMillis() - startTime;
2925
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration);

tests/src/test/java/com/oracle/weblogic/imagetool/tests/utils/CacheCommand.java

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public CacheCommand type(String value) {
9090
* Generate the command using the provided command line options.
9191
* @return the imagetool command as a string suitable for running in ProcessBuilder
9292
*/
93+
@Override
9394
public String build() {
9495
return super.build()
9596
+ field("listItems", listItems)

tests/src/test/java/com/oracle/weblogic/imagetool/tests/utils/CreateAuxCommand.java

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public CreateAuxCommand wdtModelOnly(boolean value) {
7171
* Generate the command using the provided command line options.
7272
* @return the imagetool command as a string suitable for running in ProcessBuilder
7373
*/
74+
@Override
7475
public String build() {
7576
return super.build()
7677
+ field("--fromImage", fromImage)

tests/src/test/java/com/oracle/weblogic/imagetool/tests/utils/CreateCommand.java

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public CreateCommand wdtModelOnly(boolean value) {
146146
* Generate the command using the provided command line options.
147147
* @return the imagetool command as a string suitable for running in ProcessBuilder
148148
*/
149+
@Override
149150
public String build() {
150151
return super.build()
151152
+ field("--version", version)

tests/src/test/java/com/oracle/weblogic/imagetool/tests/utils/RebaseCommand.java

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public RebaseCommand tag(String value) {
3939
* Generate the command using the provided command line options.
4040
* @return the imagetool command as a string suitable for running in ProcessBuilder
4141
*/
42+
@Override
4243
public String build() {
4344
return super.build()
4445
+ field("--targetImage", targetImage)

tests/src/test/java/com/oracle/weblogic/imagetool/tests/utils/UpdateCommand.java

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public UpdateCommand wdtModelOnly(boolean value) {
6868
* Generate the command using the provided command line options.
6969
* @return the imagetool command as a string suitable for running in ProcessBuilder
7070
*/
71+
@Override
7172
public String build() {
7273
return super.build()
7374
+ field("--fromImage", fromImage)

0 commit comments

Comments
 (0)