diff --git a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStore.java b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStore.java index 39f104e7..38e2020b 100644 --- a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStore.java +++ b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStore.java @@ -1,4 +1,4 @@ -// Copyright (c) 2019, 2022, Oracle and/or its affiliates. +// Copyright (c) 2019, 2024, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package com.oracle.weblogic.imagetool.cachestore; @@ -13,7 +13,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.time.LocalDateTime; -import java.util.HashSet; import java.util.Map; import java.util.Objects; import java.util.Properties; @@ -31,13 +30,13 @@ public class FileCacheStore implements CacheStore { private static final LoggingFacade logger = LoggingFactory.getLogger(FileCacheStore.class); private final Properties properties = new Properties(); - private final String metadataPath; + private final File metadataFile; + private final String cacheDir; FileCacheStore() throws CacheStoreException { try { - String userCacheDir = initCacheDir(); - metadataPath = userCacheDir + File.separator + Constants.DEFAULT_META_FILE; - File metadataFile = new File(metadataPath); + cacheDir = initCacheDir(); + metadataFile = Paths.get(cacheDir, Constants.DEFAULT_META_FILE).toFile(); if (metadataFile.exists() && metadataFile.isFile()) { loadProperties(metadataFile); } else { @@ -45,15 +44,6 @@ public class FileCacheStore implements CacheStore { throw new IOException("Failed to create file cache metadata file " + metadataFile.getName()); } } - if (properties.getProperty(Constants.CACHE_DIR_KEY) == null) { - properties.put(Constants.CACHE_DIR_KEY, userCacheDir); - persistToDisk(); - } - File cacheDir = new File(properties.getProperty(Constants.CACHE_DIR_KEY)); - if (!cacheDir.exists() && !cacheDir.mkdirs()) { - // the cache directory did not exist, and the mkdirs failed to create it - throw new IOException("Failed to create cache directory: " + cacheDir.getName()); - } } catch (IOException e) { CacheStoreException error = new CacheStoreException("Failed to establish a cache store on the filesystem", e); @@ -64,7 +54,7 @@ public class FileCacheStore implements CacheStore { @Override public String getCacheDir() { - return properties.getProperty(Constants.CACHE_DIR_KEY); + return cacheDir; } @Override @@ -92,9 +82,6 @@ public void addToCache(String key, String value) throws CacheStoreException { @Override public String deleteFromCache(String key) throws CacheStoreException { Objects.requireNonNull(key, Utils.getMessage("IMG-0066")); - if (Constants.CACHE_DIR_KEY.equalsIgnoreCase(key)) { - return properties.getProperty(Constants.CACHE_DIR_KEY, null); - } String oldValue = (String) properties.remove(key.toLowerCase()); if (oldValue != null) { persistToDisk(); @@ -104,12 +91,7 @@ public String deleteFromCache(String key) throws CacheStoreException { @Override public void clearCache() throws CacheStoreException { - // remove all cache entries except the cache directory - for (Object key: new HashSet<>(properties.keySet())) { - if (!key.equals(Constants.CACHE_DIR_KEY)) { - properties.remove(key); - } - } + properties.clear(); persistToDisk(); } @@ -124,7 +106,7 @@ public Map getCacheItems() { private void persistToDisk() throws CacheStoreException { logger.entering(); synchronized (properties) { - try (FileOutputStream outputStream = new FileOutputStream(metadataPath)) { + try (FileOutputStream outputStream = new FileOutputStream(metadataFile)) { properties.store(outputStream, "changed on:" + LocalDateTime.now()); } catch (IOException e) { CacheStoreException error = new CacheStoreException("Could not persist cache file", e); @@ -143,8 +125,6 @@ private void loadProperties(File propsFile) { } else { Properties tmpProperties = new Properties(); tmpProperties.load(bufferedReader); - // Do not let cache.dir to be modified outside setCacheDir - tmpProperties.remove(Constants.CACHE_DIR_KEY); tmpProperties.forEach((key, value) -> properties.put(((String) key).toLowerCase(), value)); } } catch (IOException e) { @@ -154,19 +134,18 @@ private void loadProperties(File propsFile) { logger.exiting(); } + private static String defaultCacheDir() { + return System.getProperty("user.home") + File.separator + "cache"; + } + /** * Initialize the cache store directory. * * @return cache directory */ private static String initCacheDir() throws IOException { - String cacheDirStr = System.getenv(CACHEDIR); - if (cacheDirStr == null) { - cacheDirStr = System.getProperty(CACHEDIR); - } - if (cacheDirStr == null) { - cacheDirStr = System.getProperty("user.home") + File.separator + "cache"; - } + String cacheDirStr = Utils.getEnvironmentProperty(CACHEDIR, FileCacheStore::defaultCacheDir); + Path cacheDir = Paths.get(cacheDirStr); boolean pathExists = Files.exists(cacheDir, LinkOption.NOFOLLOW_LINKS); diff --git a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/DeleteEntry.java b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/DeleteEntry.java index 3093db41..6f2f1a17 100644 --- a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/DeleteEntry.java +++ b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/DeleteEntry.java @@ -1,4 +1,4 @@ -// Copyright (c) 2019, 2021, Oracle and/or its affiliates. +// Copyright (c) 2019, 2024, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package com.oracle.weblogic.imagetool.cli.cache; @@ -20,9 +20,7 @@ public class DeleteEntry extends CacheOperation { @Override public CommandResponse call() throws Exception { if (!Utils.isEmptyString(key)) { - if (Constants.CACHE_DIR_KEY.equalsIgnoreCase(key)) { - return CommandResponse.success("Cannot delete key: " + key); - } else if (Constants.DELETE_ALL_FOR_SURE.equalsIgnoreCase(key)) { + if (Constants.DELETE_ALL_FOR_SURE.equalsIgnoreCase(key)) { cache().clearCache(); return CommandResponse.success("IMG-0046"); } else { diff --git a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/ListCacheItems.java b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/ListCacheItems.java index ad27006d..d042a6ce 100644 --- a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/ListCacheItems.java +++ b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/ListCacheItems.java @@ -1,4 +1,4 @@ -// Copyright (c) 2019, 2021, Oracle and/or its affiliates. +// Copyright (c) 2019, 2024, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package com.oracle.weblogic.imagetool.cli.cache; @@ -8,8 +8,6 @@ import com.oracle.weblogic.imagetool.api.model.CommandResponse; import com.oracle.weblogic.imagetool.cachestore.CacheStoreException; -import com.oracle.weblogic.imagetool.util.Constants; -import com.oracle.weblogic.imagetool.util.Utils; import picocli.CommandLine.Command; import picocli.CommandLine.Option; @@ -28,11 +26,6 @@ public CommandResponse call() throws CacheStoreException { return CommandResponse.success("IMG-0047"); } else { System.out.println("Cache contents"); - String cacheDir = resultMap.getOrDefault(Constants.CACHE_DIR_KEY, null); - if (!Utils.isEmptyString(cacheDir)) { - System.out.println(Constants.CACHE_DIR_KEY + "=" + cacheDir); - resultMap.remove(Constants.CACHE_DIR_KEY); - } Pattern pattern = Pattern.compile(key == null ? ".*" : key); resultMap.entrySet().stream() diff --git a/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Constants.java b/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Constants.java index fe5c73d1..81e57719 100644 --- a/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Constants.java +++ b/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Constants.java @@ -1,4 +1,4 @@ -// Copyright (c) 2019, 2021, Oracle and/or its affiliates. +// Copyright (c) 2019, 2024, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package com.oracle.weblogic.imagetool.util; @@ -10,14 +10,13 @@ public final class Constants { public static final String ARU_UPDATES_HOST = - Utils.getEnvironmentProperty("WLSIMG_ARU_HOST", "updates.oracle.com"); + Utils.getEnvironmentProperty("WLSIMG_ARU_HOST", () -> "updates.oracle.com"); public static final String ARU_REST_URL = "https://" + ARU_UPDATES_HOST + "/Orion/Services"; public static final String REL_URL = ARU_REST_URL + "/metadata?table=aru_releases"; public static final String RECOMMENDED_PATCHES_URL = ARU_REST_URL + "/search?patch_type=all&life_cycle=Recommended&product=%s&release=%s"; public static final String ARU_LANG_URL = ARU_REST_URL + "/metadata?table=aru_languages"; public static final String CONFLICTCHECKER_URL = ARU_REST_URL + "/conflict_checks"; - public static final String CACHE_DIR_KEY = "cache.dir"; public static final String DEFAULT_WLS_VERSION = "12.2.1.3.0"; public static final String DEFAULT_JDK_VERSION = "8u202"; public static final String DEFAULT_META_FILE = ".metadata"; @@ -29,7 +28,7 @@ public final class Constants { public static final String BUSYBOX = "busybox"; public static final List BUSYBOX_OS_IDS = Collections.unmodifiableList(Arrays.asList("bb", "alpine")); public static final String ORACLE_LINUX = "ghcr.io/oracle/oraclelinux:8-slim"; - public static final String BUILDER_DEFAULT = Utils.getEnvironmentProperty("WLSIMG_BUILDER", "docker"); + public static final String BUILDER_DEFAULT = Utils.getEnvironmentProperty("WLSIMG_BUILDER", () -> "docker"); private Constants() { //restrict access diff --git a/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java b/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java index 2c2e88a3..5aef7cd3 100644 --- a/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java +++ b/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java @@ -31,6 +31,7 @@ import java.util.ResourceBundle; import java.util.Set; import java.util.function.Predicate; +import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -545,13 +546,13 @@ public static String getPasswordFromInputs(String passwordStr, Path passwordFile * @param defaultValue if no environment variable is defined, nor system property, return this value * @return the value defined in the env or system property */ - public static String getEnvironmentProperty(String name, String defaultValue) { + public static String getEnvironmentProperty(String name, Supplier defaultValue) { String result = System.getenv(name); if (isEmptyString(result)) { result = System.getProperty(name); } if (isEmptyString(result)) { - return defaultValue; + return defaultValue.get(); } return result; } @@ -562,7 +563,7 @@ public static String getEnvironmentProperty(String name, String defaultValue) { * @return working directory */ public static String getBuildWorkingDir() throws IOException { - String workingDir = getEnvironmentProperty("WLSIMG_BLDDIR", System.getProperty("user.home")); + String workingDir = getEnvironmentProperty("WLSIMG_BLDDIR", () -> System.getProperty("user.home")); Path path = Paths.get(workingDir); boolean pathExists = Files.exists(path, LinkOption.NOFOLLOW_LINKS); diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/aru/AruUtilTest.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/aru/AruUtilTest.java index 23c8289d..336d1cc5 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/aru/AruUtilTest.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/aru/AruUtilTest.java @@ -1,4 +1,4 @@ -// Copyright (c) 2020, 2022, Oracle and/or its affiliates. +// Copyright (c) 2020, 2024, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package com.oracle.weblogic.imagetool.aru; @@ -6,13 +6,11 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; import java.util.stream.Collectors; import com.oracle.weblogic.imagetool.ResourceUtils; import com.oracle.weblogic.imagetool.installer.FmwInstallerType; -import com.oracle.weblogic.imagetool.logging.LoggingFacade; -import com.oracle.weblogic.imagetool.logging.LoggingFactory; +import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; @@ -23,22 +21,17 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +@ReduceTestLogging(loggerClass = AruUtil.class) @Tag("unit") class AruUtilTest { - private static final LoggingFacade logger = LoggingFactory.getLogger(AruUtil.class); - private static Level oldLevel; - @BeforeAll static void setUp() throws NoSuchFieldException, IllegalAccessException { - oldLevel = logger.getLevel(); - logger.setLevel(Level.SEVERE); // insert test class into AruUtil to intercept REST calls to ARU MockAruUtil.insertMockAruInstance(new TestAruUtil()); } @AfterAll static void tearDown() throws NoSuchFieldException, IllegalAccessException { - logger.setLevel(oldLevel); MockAruUtil.removeMockAruInstance(); } diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/CachedFileTest.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/CachedFileTest.java index be2b5239..b75821ed 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/CachedFileTest.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/CachedFileTest.java @@ -9,14 +9,12 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.List; -import java.util.logging.Level; import javax.xml.xpath.XPathExpressionException; import com.oracle.weblogic.imagetool.api.model.CachedFile; import com.oracle.weblogic.imagetool.aru.AruException; import com.oracle.weblogic.imagetool.installer.InstallerType; -import com.oracle.weblogic.imagetool.logging.LoggingFacade; -import com.oracle.weblogic.imagetool.logging.LoggingFactory; +import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging; import com.oracle.weblogic.imagetool.util.BuildPlatform; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; @@ -31,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; @Tag("unit") +@ReduceTestLogging(loggerClass = CachedFile.class) class CachedFileTest { static Path cacheDir; @@ -139,19 +138,12 @@ void resolveFallbackToLocalArch() throws IOException { @Test void copyFile(@TempDir Path contextDir) throws Exception { - LoggingFacade logger = LoggingFactory.getLogger(CachedFile.class); - Level oldLevel = logger.getLevel(); - logger.setLevel(Level.OFF); - try { - CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, ver12213); - // copy the file from the cache store to the fake build context directory - Path result = wlsInstallerFile.copyFile(cacheStore, contextDir.toString()); - // check to see if the file was copied correctly by examining the contents of the resulting file - assertLinesMatch(fileContents, Files.readAllLines(result), - "copied file contents do not match source"); - } finally { - logger.setLevel(oldLevel); - } + CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, ver12213); + // copy the file from the cache store to the fake build context directory + Path result = wlsInstallerFile.copyFile(cacheStore, contextDir.toString()); + // check to see if the file was copied correctly by examining the contents of the resulting file + assertLinesMatch(fileContents, Files.readAllLines(result), + "copied file contents do not match source"); } @Test diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStoreTest.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStoreTest.java index 6d61f0b1..251a2a3a 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStoreTest.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStoreTest.java @@ -1,4 +1,4 @@ -// Copyright (c) 2019, 2022, Oracle and/or its affiliates. +// Copyright (c) 2019, 2024, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package com.oracle.weblogic.imagetool.cachestore; @@ -48,7 +48,7 @@ void checkValueInCache() { assertTrue(cache().containsKey(testKey)), "containsKey failed to find key or value that was just added"); - // check (another way) that the value that was just added + // check (another way) that the value was just added assertDoesNotThrow(() -> assertEquals(testVal, cache().getValueFromCache(testKey), "Found unexpected value in cache"), "Get from cache threw an exception"); @@ -73,5 +73,9 @@ void verifyCacheSize() { assertDoesNotThrow(() -> assertNotNull(cache().getCacheItems(), "Get cache items should never be null"), "getCacheItems threw an exception"); + + assertDoesNotThrow(() -> + assertEquals(0, cache().getCacheItems().size(), "Get cache items should never be null"), + "getCacheItems threw an exception"); } } diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/OPatchFileTest.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/OPatchFileTest.java index 57aa7dff..ba48b685 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/OPatchFileTest.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/OPatchFileTest.java @@ -1,4 +1,4 @@ -// Copyright (c) 2020, 2022, Oracle and/or its affiliates. +// Copyright (c) 2020, 2024, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package com.oracle.weblogic.imagetool.cachestore; @@ -11,6 +11,8 @@ import com.oracle.weblogic.imagetool.aru.AruException; import com.oracle.weblogic.imagetool.aru.MockAruUtil; +import com.oracle.weblogic.imagetool.cli.menu.CommonOptions; +import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; @@ -22,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +@ReduceTestLogging(loggerClass = CommonOptions.class) @Tag("unit") class OPatchFileTest { private static CacheStore cacheStore; diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntryTest.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntryTest.java index c924561c..107bbf27 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntryTest.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntryTest.java @@ -1,17 +1,12 @@ -// Copyright (c) 2020, 2021, Oracle and/or its affiliates. +// Copyright (c) 2020, 2024, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package com.oracle.weblogic.imagetool.cli.cache; import java.io.PrintWriter; import java.io.StringWriter; -import java.util.logging.Level; import com.oracle.weblogic.imagetool.api.model.CommandResponse; -import com.oracle.weblogic.imagetool.logging.LoggingFacade; -import com.oracle.weblogic.imagetool.logging.LoggingFactory; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import picocli.CommandLine; @@ -22,20 +17,6 @@ @Tag("unit") class AddPatchEntryTest { - private static final LoggingFacade commandLogger = LoggingFactory.getLogger(AddPatchEntry.class); - private static Level oldLevel; - - @BeforeAll - static void setUp() { - // disable logging for the tested tool to prevent filling up the screen with errors (that are expected) - oldLevel = commandLogger.getLevel(); - commandLogger.setLevel(Level.OFF); - } - - @AfterAll - static void tearDown() { - commandLogger.setLevel(oldLevel); - } private static CommandLine getCommand() { AddPatchEntry app = new AddPatchEntry(); diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonOptionsTest.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonOptionsTest.java index 9f6af46a..74ba5344 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonOptionsTest.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonOptionsTest.java @@ -1,4 +1,4 @@ -// Copyright (c) 2020, 2022, Oracle and/or its affiliates. +// Copyright (c) 2020, 2024, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package com.oracle.weblogic.imagetool.cli.menu; @@ -20,6 +20,7 @@ import com.oracle.weblogic.imagetool.api.model.CachedFile; import com.oracle.weblogic.imagetool.logging.LoggingFacade; import com.oracle.weblogic.imagetool.logging.LoggingFactory; +import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging; import com.oracle.weblogic.imagetool.util.DockerfileOptions; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -31,6 +32,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +@ReduceTestLogging(loggerClass = CommonOptions.class) @Tag("unit") class CommonOptionsTest { private static void setPrivateField(String fieldname, Object target, Object value) diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptions2Test.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptions2Test.java index 7f81c0a5..1e453d3c 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptions2Test.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptions2Test.java @@ -1,16 +1,11 @@ -// Copyright (c) 2022, Oracle and/or its affiliates. +// Copyright (c) 2022, 2024, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package com.oracle.weblogic.imagetool.cli.menu; -import java.util.logging.Level; - import com.oracle.weblogic.imagetool.aru.AruUtil; import com.oracle.weblogic.imagetool.aru.InvalidCredentialException; -import com.oracle.weblogic.imagetool.logging.LoggingFacade; -import com.oracle.weblogic.imagetool.logging.LoggingFactory; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; +import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import picocli.CommandLine; @@ -18,20 +13,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; @Tag("unit") +@ReduceTestLogging(loggerClass = AruUtil.class) class CommonPatchingOptions2Test { - private static final LoggingFacade logger = LoggingFactory.getLogger(AruUtil.class); - private static Level oldLevel; - - @BeforeAll - static void setUp() throws NoSuchFieldException, IllegalAccessException { - oldLevel = logger.getLevel(); - logger.setLevel(Level.SEVERE); - } - - @AfterAll - static void tearDown() throws NoSuchFieldException, IllegalAccessException { - logger.setLevel(oldLevel); - } @Test void noPassword() { diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptionsTest.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptionsTest.java index 14d0283e..a2825e68 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptionsTest.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptionsTest.java @@ -1,4 +1,4 @@ -// Copyright (c) 2022, Oracle and/or its affiliates. +// Copyright (c) 2022, 2024, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package com.oracle.weblogic.imagetool.cli.menu; @@ -7,7 +7,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.logging.Level; import javax.xml.xpath.XPathExpressionException; import com.oracle.weblogic.imagetool.aru.AruException; @@ -18,8 +17,7 @@ import com.oracle.weblogic.imagetool.aru.MockAruUtil; import com.oracle.weblogic.imagetool.aru.MultiplePatchVersionsException; import com.oracle.weblogic.imagetool.installer.FmwInstallerType; -import com.oracle.weblogic.imagetool.logging.LoggingFacade; -import com.oracle.weblogic.imagetool.logging.LoggingFactory; +import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging; import com.oracle.weblogic.imagetool.util.InvalidPatchIdFormatException; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -33,14 +31,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue; @Tag("unit") +@ReduceTestLogging(loggerClass = AruUtil.class) class CommonPatchingOptionsTest { - private static final LoggingFacade logger = LoggingFactory.getLogger(AruUtil.class); - private static Level oldLevel; @BeforeAll static void setUp() throws NoSuchFieldException, IllegalAccessException { - oldLevel = logger.getLevel(); - logger.setLevel(Level.SEVERE); TestAruUtil.insertMockAruInstance(new TestAruUtil()); } @@ -79,7 +74,6 @@ public List getLatestPsu(FmwInstallerType type, String version, String @AfterAll static void tearDown() throws NoSuchFieldException, IllegalAccessException { - logger.setLevel(oldLevel); TestAruUtil.removeMockAruInstance(); } diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/installer/MiddlewareInstallTest.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/installer/MiddlewareInstallTest.java index e357ebfe..3a1d2837 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/installer/MiddlewareInstallTest.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/installer/MiddlewareInstallTest.java @@ -8,14 +8,11 @@ import java.nio.file.Path; import java.util.Collections; import java.util.List; -import java.util.logging.Level; import com.oracle.weblogic.imagetool.ResourceUtils; import com.oracle.weblogic.imagetool.cachestore.CacheStore; import com.oracle.weblogic.imagetool.cachestore.CacheStoreTestImpl; -import com.oracle.weblogic.imagetool.logging.LoggingFacade; -import com.oracle.weblogic.imagetool.logging.LoggingFactory; -import org.junit.jupiter.api.AfterAll; +import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -26,28 +23,19 @@ import static org.junit.jupiter.api.Assertions.assertTrue; @Tag("unit") +@ReduceTestLogging(loggerClass = MiddlewareInstall.class) class MiddlewareInstallTest { static Path cacheDir; static CacheStore cacheStore; - private static final LoggingFacade commandLogger = LoggingFactory.getLogger(MiddlewareInstall.class); - private static Level oldLevel; @BeforeAll static void setup(@TempDir Path cacheDir) throws IOException { - oldLevel = commandLogger.getLevel(); - commandLogger.setLevel(Level.WARNING); - MiddlewareInstallTest.cacheDir = cacheDir; cacheStore = new CacheStoreTestImpl(cacheDir); cacheStore.addToCache("wls_12.2.1.4.0", ResourceUtils.resourcePath("/dummyInstallers/test-installer.zip").toString()); } - @AfterAll - static void tearDown() { - commandLogger.setLevel(oldLevel); - } - @Test void copyInstaller(@TempDir Path buildContextDir) throws IOException { // Test a simple WLS install type, and copy the files to the build context folder diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/test/annotations/LogLevel.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/test/annotations/LogLevel.java new file mode 100644 index 00000000..16ada84f --- /dev/null +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/test/annotations/LogLevel.java @@ -0,0 +1,26 @@ +// Copyright (c) 2024, Oracle and/or its affiliates. +// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. + +package com.oracle.weblogic.imagetool.test.annotations; + +import java.util.logging.Level; + +/** + * An Enum representation of the Logger levels to be used with the TestLoggerOverride Annotation. + */ +public enum LogLevel { + INFO(Level.INFO), + WARNING(Level.WARNING), + SEVERE(Level.SEVERE), + OFF(Level.OFF); + + private final Level value; + + LogLevel(Level value) { + this.value = value; + } + + public Level value() { + return value; + } +} diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/test/annotations/ReduceTestLogging.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/test/annotations/ReduceTestLogging.java new file mode 100644 index 00000000..9456ae1b --- /dev/null +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/test/annotations/ReduceTestLogging.java @@ -0,0 +1,26 @@ +// Copyright (c) 2024, Oracle and/or its affiliates. +// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. + +package com.oracle.weblogic.imagetool.test.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +/** + * Used in conjunction with ReduceTestLoggingExtension to disable or reduce extraneously log messages during + * unit test execution. The original log level will be saved before executing any JUnit tests in the annotated class, + * and restored after all tests in the class are completed. + * During the tests of the annotated class, the specified logger (loggerClass) will have its minimum level set to the + * value specified in level(). The default level is OFF. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@ExtendWith(ReduceTestLoggingExtension.class) +public @interface ReduceTestLogging { + Class loggerClass(); + LogLevel level() default LogLevel.OFF; +} diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/test/annotations/ReduceTestLoggingExtension.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/test/annotations/ReduceTestLoggingExtension.java new file mode 100644 index 00000000..cbb741d0 --- /dev/null +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/test/annotations/ReduceTestLoggingExtension.java @@ -0,0 +1,40 @@ +// Copyright (c) 2024, Oracle and/or its affiliates. +// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. + +package com.oracle.weblogic.imagetool.test.annotations; + +import java.util.Optional; +import java.util.logging.Level; + +import com.oracle.weblogic.imagetool.logging.LoggingFacade; +import com.oracle.weblogic.imagetool.logging.LoggingFactory; +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +/** + * Used in conjunction with ReduceTestLogging to disable or reduce extraneously log messages during + * unit test execution. + */ +public class ReduceTestLoggingExtension implements BeforeAllCallback, AfterAllCallback { + private static LoggingFacade logger; + private static Level oldLevel; + + @Override + public void beforeAll(ExtensionContext extensionContext) throws Exception { + Optional> testClass = extensionContext.getTestClass(); + if (testClass.isPresent()) { + ReduceTestLogging annotation = testClass.get().getAnnotation(ReduceTestLogging.class); + logger = LoggingFactory.getLogger(annotation.loggerClass()); + oldLevel = logger.getLevel(); + logger.setLevel(annotation.level().value()); + } else { + throw new IllegalArgumentException("TestLoggerExtension can not be used outside of TestLoggerOverride."); + } + } + + @Override + public void afterAll(ExtensionContext extensionContext) throws Exception { + logger.setLevel(oldLevel); + } +} diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/util/DockerfileBuilderTest.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/util/DockerfileBuilderTest.java index 64aae8c9..cb709b2f 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/util/DockerfileBuilderTest.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/util/DockerfileBuilderTest.java @@ -14,11 +14,13 @@ import com.oracle.weblogic.imagetool.cli.menu.PackageManagerType; import com.oracle.weblogic.imagetool.installer.FmwInstallerType; import com.oracle.weblogic.imagetool.installer.MiddlewareInstall; +import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; +@ReduceTestLogging(loggerClass = MiddlewareInstall.class) @Tag("unit") class DockerfileBuilderTest { diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/util/UtilsTest.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/util/UtilsTest.java index a869d28f..2675d022 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/util/UtilsTest.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/util/UtilsTest.java @@ -12,11 +12,9 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.logging.Level; import com.oracle.weblogic.imagetool.api.model.CachedFile; -import com.oracle.weblogic.imagetool.logging.LoggingFacade; -import com.oracle.weblogic.imagetool.logging.LoggingFactory; +import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -33,6 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; @Tag("unit") +@ReduceTestLogging(loggerClass = CachedFile.class) @ExtendWith(SystemStubsExtension.class) class UtilsTest { @@ -217,21 +216,14 @@ void passwordResolution() throws Exception { @Test void oracleHomeFromResponseFile() throws Exception { - LoggingFacade logger = LoggingFactory.getLogger(CachedFile.class); - Level oldLevel = logger.getLevel(); - logger.setLevel(Level.WARNING); - try { - Path responseFile = Paths.get("./src/test/resources/utilsTest/responseFile1.txt"); - List responseFiles = new ArrayList<>(); - responseFiles.add(responseFile); - DockerfileOptions options = new DockerfileOptions("tests"); - - Utils.setOracleHome(responseFiles, options); - assertEquals("/my/oraclehomeDir", options.oracle_home(), - "set Oracle Home from response file failed"); - } finally { - logger.setLevel(oldLevel); - } + Path responseFile = Paths.get("./src/test/resources/utilsTest/responseFile1.txt"); + List responseFiles = new ArrayList<>(); + responseFiles.add(responseFile); + DockerfileOptions options = new DockerfileOptions("tests"); + + Utils.setOracleHome(responseFiles, options); + assertEquals("/my/oraclehomeDir", options.oracle_home(), + "set Oracle Home from response file failed"); } @Test