You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ro...@apache.org on 2022/07/20 16:07:48 UTC

[activemq-artemis] 03/25: make a start on restoring AssertionLoggerHandler to get tests going

This is an automated email from the ASF dual-hosted git repository.

robbie pushed a commit to branch new-logging
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git

commit d8870696ed287d58a51b7d18ef1adae8ee5e46bc
Author: Robbie Gemmell <ro...@apache.org>
AuthorDate: Wed Jul 6 17:32:47 2022 +0100

    make a start on restoring AssertionLoggerHandler to get tests going
---
 .../artemis/logs/AssertionLoggerHandler.java       |  68 -------
 artemis-journal/pom.xml                            |   6 +
 artemis-unit-test-support/pom.xml                  |   5 +
 .../artemis/logs/AssertionLoggerHandler.java       | 224 +++++++++++++++++++++
 .../integration/server/SimpleStartStopTest.java    |   5 +-
 5 files changed, 237 insertions(+), 71 deletions(-)

diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/logs/AssertionLoggerHandler.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/logs/AssertionLoggerHandler.java
deleted file mode 100644
index ca41f1bf37..0000000000
--- a/artemis-commons/src/main/java/org/apache/activemq/artemis/logs/AssertionLoggerHandler.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * 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.
- */
-package org.apache.activemq.artemis.logs;
-
-import java.util.logging.Level;
-
-
-public class AssertionLoggerHandler {
-
-   public void close() throws SecurityException {
-   }
-
-   public static boolean hasLevel(Level level) {
-      return false;
-   }
-
-   public static boolean findText(long mstimeout, String... text) {
-      return false;
-   }
-
-   /**
-    * Find a line that contains the parameters passed as an argument
-    *
-    * @param text
-    * @return
-    */
-   public static boolean findText(final String... text) {
-      return false;
-   }
-
-   public static int countText(final String... text) {
-      return 0;
-   }
-
-   public static boolean matchText(final String pattern) {
-      return false;
-   }
-
-   public static final void clear() {
-   }
-
-   public static final void startCapture() {
-   }
-
-   /**
-    *
-    * @param individualMessages enables counting individual messages.
-    */
-   public static final void startCapture(boolean individualMessages) {
-   }
-
-   public static final void stopCapture() {
-   }
-}
diff --git a/artemis-journal/pom.xml b/artemis-journal/pom.xml
index 9b3028fbca..4d520480dd 100644
--- a/artemis-journal/pom.xml
+++ b/artemis-journal/pom.xml
@@ -89,5 +89,11 @@
          <artifactId>junit</artifactId>
          <scope>test</scope>
       </dependency>
+      <dependency>
+         <groupId>org.apache.activemq</groupId>
+         <artifactId>artemis-unit-test-support</artifactId>
+         <version>${project.version}</version>
+         <scope>test</scope>
+      </dependency>
    </dependencies>
 </project>
diff --git a/artemis-unit-test-support/pom.xml b/artemis-unit-test-support/pom.xml
index 91264ec7c4..4ab3314a69 100644
--- a/artemis-unit-test-support/pom.xml
+++ b/artemis-unit-test-support/pom.xml
@@ -41,5 +41,10 @@
          <artifactId>slf4j-api</artifactId>
          <scope>provided</scope>
       </dependency>
+      <dependency>
+         <groupId>org.apache.logging.log4j</groupId>
+         <artifactId>log4j-slf4j-impl</artifactId>
+         <scope>provided</scope>
+      </dependency>
    </dependencies>
 </project>
diff --git a/artemis-unit-test-support/src/main/java/org/apache/activemq/artemis/logs/AssertionLoggerHandler.java b/artemis-unit-test-support/src/main/java/org/apache/activemq/artemis/logs/AssertionLoggerHandler.java
new file mode 100644
index 0000000000..4bb25932f2
--- /dev/null
+++ b/artemis-unit-test-support/src/main/java/org/apache/activemq/artemis/logs/AssertionLoggerHandler.java
@@ -0,0 +1,224 @@
+/*
+ * 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.
+ */
+package org.apache.activemq.artemis.logs;
+
+import java.io.Serializable;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.regex.Pattern;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+
+/**
+ * This class contains a tool where programs could intercept for LogMessage given an interval of time between {@link #startCapture()}
+ * and {@link #stopCapture()}
+ *
+ * Be careful with this use as this is intended for testing only (such as testcases)
+ */
+public class AssertionLoggerHandler extends AbstractAppender {
+
+   public enum LogLevel {
+      ERROR(Level.ERROR),
+      WARN(Level.WARN),
+      INFO(Level.INFO),
+      DEBUG(Level.DEBUG),
+      TRACE(Level.TRACE);
+
+      Level implLevel;
+
+      private LogLevel(Level implLevel) {
+         this.implLevel = implLevel;
+      }
+
+      private Level toImplLevel() {
+         return implLevel;
+      }
+   }
+
+   private static final Map<String, LogEvent> messages = new ConcurrentHashMap<>();
+   private static List<String> traceMessages;
+   private static volatile boolean capture = false;
+
+   protected AssertionLoggerHandler(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions, Property[] properties) {
+      super(name, filter, layout, ignoreExceptions, properties);
+   }
+
+   @Override
+   public void append(LogEvent event) {
+      if (capture) {
+         //TODO: see getFormattedMessage() around handling StringBuilderFormattable interface as well, check it out
+         String formattedMessage = event.getMessage().getFormattedMessage();
+         messages.put(formattedMessage, event);
+         if (traceMessages != null) {
+            traceMessages.add(formattedMessage);
+         }
+      }
+   }
+
+   @Override
+   public void stop() {
+      super.stop();
+      // TODO Do we need to do anything here? Set capture false and clear?
+   }
+
+   /**
+    * is there any record matching Level?
+    *
+    * @param level
+    * @return
+    */
+   public static boolean hasLevel(LogLevel level) {
+      Level implLevel = level.toImplLevel();
+
+      for (LogEvent event : messages.values()) {
+         if (implLevel.equals(event.getLevel())) {
+            return true;
+         }
+      }
+
+      return false;
+   }
+
+   public static boolean findText(long mstimeout, String... text) {
+
+      long timeMax = System.currentTimeMillis() + mstimeout;
+      do {
+         if (findText(text)) {
+            return true;
+         }
+      }
+      while (timeMax > System.currentTimeMillis());
+
+      return false;
+
+   }
+
+   /**
+    * Find a line that contains the parameters passed as an argument
+    *
+    * @param text
+    * @return
+    */
+   public static boolean findText(final String... text) {
+      for (Map.Entry<String, LogEvent> entry : messages.entrySet()) {
+         String key = entry.getKey();
+         boolean found = true;
+
+         for (String txtCheck : text) {
+            found = key.contains(txtCheck);
+            if (!found) {
+               // If the main log message doesn't contain what we're looking for let's look in the message from the exception (if there is one).
+               Throwable throwable = entry.getValue().getThrown();
+               if (throwable != null && throwable.getMessage() != null) {
+                  found = throwable.getMessage().contains(txtCheck);
+                  if (!found) {
+                     break;
+                  }
+               } else {
+                  break;
+               }
+            }
+         }
+
+         if (found) {
+            return true;
+         }
+      }
+
+      return false;
+   }
+
+   public static int countText(final String... text) {
+      int found = 0;
+      if (traceMessages != null) {
+         for (String str : traceMessages) {
+            for (String txtCheck : text) {
+               if (str.contains(txtCheck)) {
+                  found++;
+               }
+            }
+         }
+      } else {
+         for (Map.Entry<String, LogEvent> entry : messages.entrySet()) {
+            String key = entry.getKey();
+
+            for (String txtCheck : text) {
+               if (key.contains(txtCheck)) {
+                  found++;
+               }
+            }
+         }
+      }
+
+      return found;
+   }
+
+   public static boolean matchText(final String pattern) {
+      Pattern r = Pattern.compile(pattern);
+
+      for (Map.Entry<String, LogEvent> entry : messages.entrySet()) {
+         if (r.matcher(entry.getKey()).matches()) {
+            return true;
+         } else {
+            Throwable throwable = entry.getValue().getThrown();
+            if (throwable != null && throwable.getMessage() != null) {
+               if (r.matcher(throwable.getMessage()).matches()) {
+                  return true;
+               }
+            }
+         }
+      }
+
+      return false;
+   }
+
+   public static final void clear() {
+      messages.clear();
+      if (traceMessages != null) {
+         traceMessages.clear();
+      }
+   }
+
+   public static final void startCapture() {
+      startCapture(false);
+   }
+
+   /**
+    *
+    * @param individualMessages enables counting individual messages.
+    */
+   public static final void startCapture(boolean individualMessages) {
+      clear();
+      if (individualMessages) {
+         traceMessages = new LinkedList<>();
+      }
+      capture = true;
+   }
+
+   public static final void stopCapture() {
+      capture = false;
+      clear();
+      traceMessages = null;
+   }
+}
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/server/SimpleStartStopTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/server/SimpleStartStopTest.java
index 9b375ebe5d..f4b0a4a80f 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/server/SimpleStartStopTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/server/SimpleStartStopTest.java
@@ -23,11 +23,10 @@ import java.util.concurrent.atomic.AtomicInteger;
 import org.apache.activemq.artemis.core.persistence.impl.journal.JournalRecordIds;
 import org.apache.activemq.artemis.core.server.ActiveMQServer;
 import org.apache.activemq.artemis.logs.AssertionLoggerHandler;
+import org.apache.activemq.artemis.logs.AssertionLoggerHandler.LogLevel;
 import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
-import org.junit.Ignore;
 import org.junit.Test;
 
-@Ignore("Needs updated to account for logging impl changes") //TODO: reinstate
 public class SimpleStartStopTest extends ActiveMQTestBase {
 
    /**
@@ -52,7 +51,7 @@ public class SimpleStartStopTest extends ActiveMQTestBase {
          }
 
          // There shouldn't be any error from starting / stopping the server
-         //TODO: assertFalse("There shouldn't be any error for just starting / stopping the server", AssertionLoggerHandler.hasLevel(Level.ERROR));
+         assertFalse("There shouldn't be any error for just starting / stopping the server", AssertionLoggerHandler.hasLevel(LogLevel.ERROR));
          assertFalse(AssertionLoggerHandler.findText("AMQ224008"));
 
          HashMap<Integer, AtomicInteger> records = this.internalCountJournalLivingRecords(server.getConfiguration(), false);