You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2021/01/26 11:42:50 UTC

[GitHub] [maven] slawekjaranowski commented on a change in pull request #438: [MNG-7083] - introduce lazy Log message evaluation

slawekjaranowski commented on a change in pull request #438:
URL: https://github.com/apache/maven/pull/438#discussion_r564440925



##########
File path: maven-plugin-api/src/main/java/org/apache/maven/plugin/logging/Log.java
##########
@@ -63,6 +65,20 @@
      */
     void debug( Throwable error );
 
+    /**
+     * Send a message to the user in the <b>debug</b> error level by computing the message
+     * only when needed. The supplier will be called only if @see #isDebugEnabled() is <b>true</b>.
+     * 
+     * @param messageSupplier a non null Supplier of the message to use
+     */
+    default void debug( Supplier<String> messageSupplier )

Review comment:
       why do you not add  methods, like
   
       void debug( Supplier<String> messageSupplier, Throwable error  )

##########
File path: maven-plugin-api/src/test/java/org/apache/maven/plugin/logging/SystemStreamLogTest.java
##########
@@ -0,0 +1,94 @@
+package org.apache.maven.plugin.logging;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+
+import java.io.PrintStream;
+import java.util.function.Supplier;
+
+/**
+ * Tests for {@link SystemStreamLog} 
+ */
+public class SystemStreamLogTest 
+{
+  private static final String EXPECTED_MESSAGE = "expected message";
+  private static PrintStream outStream;
+  private static PrintStream errStream;
+  private static PrintStream mockOut;
+  private static PrintStream mockErr;
+
+  /*
+   * As SystemStreamLog info/warn/error log levels are active, this test checks that
+   * a message supplier is really called/executed when logging at those levels 
+   */
+  @Test
+  public void testLazyMessageIsEvaluatedForActiveLogLevels()
+  {
+    Supplier messageSupplier = Mockito.mock(Supplier.class);
+    Mockito.when(messageSupplier.get()).thenReturn(EXPECTED_MESSAGE);
+
+    Log logger = new SystemStreamLog();
+    logger.info(messageSupplier);
+    logger.warn(messageSupplier);
+    logger.error(messageSupplier);

Review comment:
       missing? `logger.debug`

##########
File path: maven-plugin-api/src/test/java/org/apache/maven/plugin/logging/SystemStreamLogTest.java
##########
@@ -0,0 +1,94 @@
+package org.apache.maven.plugin.logging;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+
+import java.io.PrintStream;
+import java.util.function.Supplier;
+
+/**
+ * Tests for {@link SystemStreamLog} 
+ */
+public class SystemStreamLogTest 
+{
+  private static final String EXPECTED_MESSAGE = "expected message";
+  private static PrintStream outStream;
+  private static PrintStream errStream;
+  private static PrintStream mockOut;
+  private static PrintStream mockErr;
+
+  /*
+   * As SystemStreamLog info/warn/error log levels are active, this test checks that
+   * a message supplier is really called/executed when logging at those levels 
+   */
+  @Test
+  public void testLazyMessageIsEvaluatedForActiveLogLevels()
+  {
+    Supplier messageSupplier = Mockito.mock(Supplier.class);
+    Mockito.when(messageSupplier.get()).thenReturn(EXPECTED_MESSAGE);
+
+    Log logger = new SystemStreamLog();
+    logger.info(messageSupplier);
+    logger.warn(messageSupplier);
+    logger.error(messageSupplier);
+    
+    Mockito.verify(messageSupplier, Mockito.times(3)).get();
+  }
+
+  /*
+   * As SystemStreamLog debug log level is inactive, this test checks that
+   * a message supplier is not called/executed when logging at debug level
+   */
+  @Test
+  public void testDebugLazyMessageIsNotEvaluated()
+  {
+    Supplier messageSupplier = Mockito.mock(Supplier.class);
+    Mockito.when(messageSupplier.get()).thenReturn(EXPECTED_MESSAGE);
+
+    Log logger = new SystemStreamLog();
+    logger.debug(messageSupplier);
+
+    Mockito.verify(messageSupplier, Mockito.never()).get();
+  }
+
+  @BeforeAll
+  public static void initialize()
+  {
+    outStream = System.out;
+    errStream = System.err;
+
+    mockOut = Mockito.mock(PrintStream.class);
+    System.setOut(mockOut);
+    mockErr = Mockito.mock(PrintStream.class);

Review comment:
       `mockOut`, `mockErr` - are not used in test - why do you remember reference in fields

##########
File path: maven-plugin-api/src/test/java/org/apache/maven/plugin/logging/SystemStreamLogTest.java
##########
@@ -0,0 +1,94 @@
+package org.apache.maven.plugin.logging;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+
+import java.io.PrintStream;
+import java.util.function.Supplier;
+
+/**
+ * Tests for {@link SystemStreamLog} 
+ */
+public class SystemStreamLogTest 
+{
+  private static final String EXPECTED_MESSAGE = "expected message";
+  private static PrintStream outStream;
+  private static PrintStream errStream;
+  private static PrintStream mockOut;
+  private static PrintStream mockErr;
+
+  /*
+   * As SystemStreamLog info/warn/error log levels are active, this test checks that
+   * a message supplier is really called/executed when logging at those levels 
+   */
+  @Test
+  public void testLazyMessageIsEvaluatedForActiveLogLevels()
+  {
+    Supplier messageSupplier = Mockito.mock(Supplier.class);
+    Mockito.when(messageSupplier.get()).thenReturn(EXPECTED_MESSAGE);
+
+    Log logger = new SystemStreamLog();
+    logger.info(messageSupplier);
+    logger.warn(messageSupplier);
+    logger.error(messageSupplier);
+    
+    Mockito.verify(messageSupplier, Mockito.times(3)).get();
+  }
+
+  /*
+   * As SystemStreamLog debug log level is inactive, this test checks that
+   * a message supplier is not called/executed when logging at debug level
+   */
+  @Test
+  public void testDebugLazyMessageIsNotEvaluated()
+  {
+    Supplier messageSupplier = Mockito.mock(Supplier.class);
+    Mockito.when(messageSupplier.get()).thenReturn(EXPECTED_MESSAGE);
+
+    Log logger = new SystemStreamLog();
+    logger.debug(messageSupplier);

Review comment:
       why only `logger.debug`
   
   It is inconsistent with tests in `DefaultLogTest`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org