From 99fb6782f2a37be569309cc37591ce7b9c86092b Mon Sep 17 00:00:00 2001 From: minherz Date: Tue, 18 Jan 2022 21:00:28 +0200 Subject: [PATCH] fix: use initialized logging option in constructor (#843) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add unit test to validate LoggingHandler construction with default args. Fixes #842 * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- .../google/cloud/logging/LoggingHandler.java | 2 +- .../cloud/logging/LoggingHandlerTest.java | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java index 983127d5a..92b9f8794 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java @@ -241,7 +241,7 @@ public LoggingHandler( setLevel(level); baseLevel = level.equals(Level.ALL) ? Level.FINEST : level; flushLevel = config.getFlushLevel(); - Boolean f1 = options.getAutoPopulateMetadata(); + Boolean f1 = loggingOptions.getAutoPopulateMetadata(); Boolean f2 = config.getAutoPopulateMetadata(); autoPopulateMetadata = isTrueOrNull(f1) && isTrueOrNull(f2); redirectToStdout = firstNonNull(config.getRedirectToStdout(), Boolean.FALSE); diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java index 921d58307..dbe3f7f5f 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java @@ -22,6 +22,7 @@ import static org.easymock.EasyMock.reset; import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.google.api.client.util.Strings; @@ -52,6 +53,7 @@ public class LoggingHandlerTest { private static final String LOG_NAME = "java.log"; private static final String MESSAGE = "message"; private static final String PROJECT = "project"; + private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT"; private static final MonitoredResource DEFAULT_RESOURCE = MonitoredResource.of("global", ImmutableMap.of("project_id", PROJECT)); @@ -205,9 +207,9 @@ public void setUp() { expect(options.getService()).andStubReturn(logging); expect(options.getAutoPopulateMetadata()).andStubReturn(Boolean.FALSE); logging.setFlushSeverity(EasyMock.anyObject(Severity.class)); - expectLastCall().once(); + expectLastCall().anyTimes(); logging.setWriteSynchronicity(EasyMock.anyObject(Synchronicity.class)); - expectLastCall().once(); + expectLastCall().anyTimes(); } @After @@ -221,6 +223,19 @@ private static LogRecord newLogRecord(Level level, String message) { return record; } + @Test + public void testDefaultHandlerCreation() { + String oldProject = System.getProperty(PROJECT_ENV_NAME); + System.setProperty(PROJECT_ENV_NAME, PROJECT); + replay(options, logging); + assertNotNull(new LoggingHandler()); + if (oldProject != null) { + System.setProperty(PROJECT_ENV_NAME, oldProject); + } else { + System.clearProperty(PROJECT_ENV_NAME); + } + } + @Test public void testPublishLevels() { logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS); @@ -483,6 +498,7 @@ public void testFlushLevel() { @Test public void testSyncWrite() { + reset(logging); LogEntry entry = LogEntry.newBuilder(Payload.StringPayload.of(MESSAGE)) .setSeverity(Severity.DEBUG) @@ -491,10 +507,10 @@ public void testSyncWrite() { .setTimestamp(123456789L) .build(); + logging.setWriteSynchronicity(Synchronicity.ASYNC); logging.setWriteSynchronicity(Synchronicity.SYNC); - expectLastCall().once(); logging.write(ImmutableList.of(entry), DEFAULT_OPTIONS); - expectLastCall().once(); + logging.setFlushSeverity(Severity.ERROR); replay(options, logging); LoggingHandler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE);