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

chengzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 78538e3f83a Refactor agent logging e2e test (#30984)
78538e3f83a is described below

commit 78538e3f83a74f35abdf69c509e2a4c60b05903c
Author: jiangML <[email protected]>
AuthorDate: Mon Apr 22 14:37:46 2024 +0800

    Refactor agent logging e2e test (#30984)
    
    * Refactor logging e2e test in agent
    
    * Optimize FilePluginE2EIT
---
 .../test/e2e/agent/file/FilePluginE2EIT.java       |  43 +++++----
 .../e2e/agent/file/cases/IntegrationTestCases.java |  36 ++++++++
 .../file/cases/IntegrationTestCasesLoader.java     | 102 +++++++++++++++++++++
 .../test/e2e/agent/file/cases/LogTestCase.java     |  43 +++++++++
 .../file/src/test/resources/cases/jdbc/logs.xml    |  21 +++++
 .../file/src/test/resources/cases/proxy/logs.xml   |  21 +++++
 6 files changed, 244 insertions(+), 22 deletions(-)

diff --git 
a/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/FilePluginE2EIT.java
 
b/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/FilePluginE2EIT.java
index 8ab025e02ba..7452b61a9f8 100644
--- 
a/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/FilePluginE2EIT.java
+++ 
b/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/FilePluginE2EIT.java
@@ -17,44 +17,43 @@
 
 package org.apache.shardingsphere.test.e2e.agent.file;
 
-import lombok.extern.slf4j.Slf4j;
 import 
org.apache.shardingsphere.test.e2e.agent.common.AgentTestActionExtension;
-import org.apache.shardingsphere.test.e2e.agent.common.enums.AdapterType;
 import org.apache.shardingsphere.test.e2e.agent.common.env.E2ETestEnvironment;
 import org.apache.shardingsphere.test.e2e.agent.file.asserts.ContentAssert;
-import org.junit.jupiter.api.Test;
+import 
org.apache.shardingsphere.test.e2e.agent.file.cases.IntegrationTestCasesLoader;
+import org.apache.shardingsphere.test.e2e.agent.file.cases.LogTestCase;
 import org.junit.jupiter.api.condition.EnabledIf;
 import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.ArgumentsProvider;
+import org.junit.jupiter.params.provider.ArgumentsSource;
 
-import java.util.Collection;
+import java.util.stream.Stream;
 
 import static org.junit.jupiter.api.Assertions.assertFalse;
 
 @ExtendWith(AgentTestActionExtension.class)
-@Slf4j
 class FilePluginE2EIT {
     
     @EnabledIf("isEnabled")
-    @Test
-    void assertWithAgent() {
-        Collection<String> actualLogLines = 
E2ETestEnvironment.getInstance().getActualLogs();
-        assertFalse(actualLogLines.isEmpty(), "Actual log is empty");
-        if 
(AdapterType.PROXY.getValue().equalsIgnoreCase(E2ETestEnvironment.getInstance().getAdapter()))
 {
-            assertProxyWithAgent(actualLogLines);
-        } else {
-            assertJdbcWithAgent(actualLogLines);
-        }
-    }
-    
-    private void assertProxyWithAgent(final Collection<String> actualLogLines) 
{
-        ContentAssert.assertIs(actualLogLines, "Build meta data contexts 
finished, cost\\s(?=[1-9]+\\d*)");
-    }
-    
-    private void assertJdbcWithAgent(final Collection<String> actualLogLines) {
-        ContentAssert.assertIs(actualLogLines, "Build meta data contexts 
finished, cost\\s(?=[1-9]+\\d*)");
+    @ParameterizedTest
+    @ArgumentsSource(TestCaseArgumentsProvider.class)
+    void assertWithAgent(final LogTestCase testCase) {
+        
assertFalse(E2ETestEnvironment.getInstance().getActualLogs().isEmpty(), "The 
actual log is empty");
+        
ContentAssert.assertIs(E2ETestEnvironment.getInstance().getActualLogs(), 
testCase.getLogRegex());
     }
     
     private static boolean isEnabled() {
         return E2ETestEnvironment.getInstance().containsTestParameter();
     }
+    
+    private static class TestCaseArgumentsProvider implements 
ArgumentsProvider {
+        
+        @Override
+        public Stream<? extends Arguments> provideArguments(final 
ExtensionContext extensionContext) {
+            return 
IntegrationTestCasesLoader.getInstance().loadIntegrationTestCases(E2ETestEnvironment.getInstance().getAdapter()).stream().map(Arguments::of);
+        }
+    }
 }
diff --git 
a/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/cases/IntegrationTestCases.java
 
b/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/cases/IntegrationTestCases.java
new file mode 100644
index 00000000000..8a34cda4456
--- /dev/null
+++ 
b/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/cases/IntegrationTestCases.java
@@ -0,0 +1,36 @@
+/*
+ * 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.shardingsphere.test.e2e.agent.file.cases;
+
+import lombok.Getter;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import java.util.Collection;
+import java.util.LinkedList;
+
+/**
+ * Integration test cases.
+ */
+@XmlRootElement(name = "integration-test-cases")
+@Getter
+public final class IntegrationTestCases {
+    
+    @XmlElement(name = "test-case")
+    private final Collection<LogTestCase> testCases = new LinkedList<>();
+}
diff --git 
a/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/cases/IntegrationTestCasesLoader.java
 
b/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/cases/IntegrationTestCasesLoader.java
new file mode 100644
index 00000000000..90cc1d42301
--- /dev/null
+++ 
b/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/cases/IntegrationTestCasesLoader.java
@@ -0,0 +1,102 @@
+/*
+ * 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.shardingsphere.test.e2e.agent.file.cases;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import lombok.SneakyThrows;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Objects;
+
+/**
+ * Integration test cases loader.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class IntegrationTestCasesLoader {
+    
+    private static final String FILE_EXTENSION = ".xml";
+    
+    private static final IntegrationTestCasesLoader INSTANCE = new 
IntegrationTestCasesLoader();
+    
+    private Collection<LogTestCase> integrationTestCases;
+    
+    /**
+     * Get singleton instance.
+     *
+     * @return singleton instance
+     */
+    public static IntegrationTestCasesLoader getInstance() {
+        return INSTANCE;
+    }
+    
+    /**
+     * Load integration test cases.
+     *
+     * @param adapter adapter proxy or jdbc
+     * @return integration test cases
+     */
+    @SneakyThrows({IOException.class, URISyntaxException.class, 
JAXBException.class})
+    public Collection<LogTestCase> loadIntegrationTestCases(final String 
adapter) {
+        if (null != integrationTestCases) {
+            return integrationTestCases;
+        }
+        integrationTestCases = new LinkedList<>();
+        URL url = 
Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(String.format("cases/%s",
 adapter)));
+        Collection<File> files = getFiles(url);
+        for (File each : files) {
+            
integrationTestCases.addAll(unmarshal(each.getPath()).getTestCases());
+        }
+        return integrationTestCases;
+    }
+    
+    private Collection<File> getFiles(final URL url) throws IOException, 
URISyntaxException {
+        Collection<File> result = new LinkedList<>();
+        Files.walkFileTree(Paths.get(url.toURI()), new 
SimpleFileVisitor<Path>() {
+            
+            @Override
+            public FileVisitResult visitFile(final Path file, final 
BasicFileAttributes basicFileAttributes) {
+                if (file.getFileName().toString().endsWith(FILE_EXTENSION)) {
+                    result.add(file.toFile());
+                }
+                return FileVisitResult.CONTINUE;
+            }
+        });
+        return result;
+    }
+    
+    private IntegrationTestCases unmarshal(final String integrateCasesFile) 
throws IOException, JAXBException {
+        try (FileReader reader = new FileReader(integrateCasesFile)) {
+            return (IntegrationTestCases) 
JAXBContext.newInstance(IntegrationTestCases.class).createUnmarshaller().unmarshal(reader);
+        }
+    }
+}
diff --git 
a/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/cases/LogTestCase.java
 
b/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/cases/LogTestCase.java
new file mode 100644
index 00000000000..b6d9bb0a6c7
--- /dev/null
+++ 
b/test/e2e/agent/plugins/logging/file/src/test/java/org/apache/shardingsphere/test/e2e/agent/file/cases/LogTestCase.java
@@ -0,0 +1,43 @@
+/*
+ * 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.shardingsphere.test.e2e.agent.file.cases;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.test.e2e.agent.common.env.E2ETestEnvironment;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+
+/**
+ * Log test case.
+ */
+@Getter
+@Setter
+@XmlAccessorType(XmlAccessType.FIELD)
+public final class LogTestCase {
+    
+    @XmlAttribute(name = "log-regex")
+    private String logRegex;
+    
+    @Override
+    public String toString() {
+        return String.format("%s -> `%s`", 
E2ETestEnvironment.getInstance().getAdapter(), logRegex);
+    }
+}
diff --git 
a/test/e2e/agent/plugins/logging/file/src/test/resources/cases/jdbc/logs.xml 
b/test/e2e/agent/plugins/logging/file/src/test/resources/cases/jdbc/logs.xml
new file mode 100644
index 00000000000..1185024ba01
--- /dev/null
+++ b/test/e2e/agent/plugins/logging/file/src/test/resources/cases/jdbc/logs.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<integration-test-cases>
+    <test-case log-regex="Build meta data contexts finished, 
cost\s(?=[1-9]+\d*)"/>
+</integration-test-cases>
diff --git 
a/test/e2e/agent/plugins/logging/file/src/test/resources/cases/proxy/logs.xml 
b/test/e2e/agent/plugins/logging/file/src/test/resources/cases/proxy/logs.xml
new file mode 100644
index 00000000000..1185024ba01
--- /dev/null
+++ 
b/test/e2e/agent/plugins/logging/file/src/test/resources/cases/proxy/logs.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<integration-test-cases>
+    <test-case log-regex="Build meta data contexts finished, 
cost\s(?=[1-9]+\d*)"/>
+</integration-test-cases>

Reply via email to