Hisoka-X commented on code in PR #8136:
URL: https://github.com/apache/seatunnel/pull/8136#discussion_r1855740498


##########
seatunnel-dist/src/test/java/org/apache/seatunnel/ImportShadeClassCheck.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.seatunnel;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import com.github.javaparser.JavaParser;
+import com.github.javaparser.ParseResult;
+import com.github.javaparser.Range;
+import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.ImportDeclaration;
+import com.github.javaparser.ast.NodeList;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+@Slf4j
+public class ImportShadeClassCheck {
+
+    private static Map<String, NodeList<ImportDeclaration>> importsMap = new 
HashMap<>();
+    private final String SEATUNNEL_SHADE_PREFIX = 
"org.apache.seatunnel.shade.";
+    private final boolean isWindows = 
System.getProperty("os.name").toLowerCase().startsWith("win");
+
+    @BeforeAll
+    public static void beforeAll() {
+        Path directoryPath = Paths.get(System.getProperty("user.dir"));
+        try {
+            Files.walk(directoryPath.getParent())
+                    .filter(path -> path.toString().endsWith(".java"))
+                    .forEach(
+                            path -> {
+                                try {
+                                    JavaParser javaParser = new JavaParser();
+                                    ParseResult<CompilationUnit> parseResult =
+                                            javaParser.parse(
+                                                    
Files.newInputStream(path.toFile().toPath()));
+                                    Optional<CompilationUnit> result = 
parseResult.getResult();
+                                    importsMap.put(path.toString(), 
result.get().getImports());
+                                } catch (Exception e) {
+                                    e.printStackTrace();
+                                }
+                            });
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Test
+    public void guavaShadeCheck() {
+        Map<String, List<String>> errorMap =
+                checkShade(Collections.singletonList("com.google.common"));
+        Assertions.assertEquals(0, errorMap.size(), errorMsg("guava", 
errorMap));
+    }
+
+    @Test
+    public void jacksonShadeCheck() {
+        Map<String, List<String>> errorMap =
+                checkShade(
+                        Collections.singletonList("com.fasterxml.jackson"),
+                        Arrays.asList(
+                                
"org.apache.seatunnel.format.compatible.debezium.json",
+                                
"org.apache.seatunnel.format.compatible.kafka.connect.json",
+                                "org.apache.seatunnel.connectors.druid.sink",
+                                
"org.apache.seatunnel.connectors.seatunnel.typesense.client"));
+        Assertions.assertEquals(0, errorMap.size(), errorMsg("jackson", 
errorMap));
+    }
+
+    @Test
+    public void jettyShadeCheck() {
+        Map<String, List<String>> errorMap =
+                checkShade(Collections.singletonList("org.eclipse.jetty"));
+        Assertions.assertEquals(0, errorMap.size(), errorMsg("jetty", 
errorMap));
+    }
+
+    @Test
+    public void janinoShadeCheck() {
+        Map<String, List<String>> errorMap =
+                checkShade(Arrays.asList("org.codehaus.janino", 
"org.codehaus.commons"));
+        Assertions.assertEquals(0, errorMap.size(), errorMsg("janino", 
errorMap));
+    }
+
+    private Map<String, List<String>> checkShade(List<String> prefixList) {
+        return checkShade(prefixList, Collections.emptyList());
+    }
+
+    private Map<String, List<String>> checkShade(
+            List<String> prefixList, List<String> packageWhiteList) {
+        Map<String, List<String>> errorMap = new HashMap<>();
+        importsMap.forEach(
+                (clazzPath, imports) -> {
+                    boolean match =
+                            packageWhiteList.stream()
+                                    .map(
+                                            whitePackage ->
+                                                    whitePackage.replace(
+                                                            ".", isWindows ? 
"\\" : "/"))
+                                    .anyMatch(clazzPath::contains);
+                    if (!match) {
+                        List<String> collect =
+                                imports.stream()
+                                        .filter(
+                                                importDeclaration -> {
+                                                    String importClz =
+                                                            
importDeclaration.getName().asString();
+                                                    return prefixList.stream()
+                                                            
.anyMatch(importClz::startsWith);
+                                                })
+                                        .map(this::getImportClassLineNum)
+                                        .collect(Collectors.toList());
+                        if (!collect.isEmpty()) {
+                            errorMap.put(clazzPath, collect);
+                        }
+                    }
+                });
+        return errorMap;
+    }
+
+    private String errorMsg(String checkType, Map<String, List<String>> 
errorMap) {
+        StringBuilder msg = new StringBuilder();
+        msg.append(String.format("%s shade is not up to code, need add prefix 
[", checkType))

Review Comment:
   can we tell to devs the code file name?



##########
seatunnel-dist/src/test/java/org/apache/seatunnel/ImportShadeClassCheck.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.seatunnel;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import com.github.javaparser.JavaParser;
+import com.github.javaparser.ParseResult;
+import com.github.javaparser.Range;
+import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.ImportDeclaration;
+import com.github.javaparser.ast.NodeList;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+@Slf4j
+public class ImportShadeClassCheck {
+
+    private static Map<String, NodeList<ImportDeclaration>> importsMap = new 
HashMap<>();
+    private final String SEATUNNEL_SHADE_PREFIX = 
"org.apache.seatunnel.shade.";
+    private final boolean isWindows = 
System.getProperty("os.name").toLowerCase().startsWith("win");
+
+    @BeforeAll
+    public static void beforeAll() {
+        Path directoryPath = Paths.get(System.getProperty("user.dir"));

Review Comment:
   Does all seatunnel project contains this property?



-- 
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.

To unsubscribe, e-mail: commits-unsubscr...@seatunnel.apache.org

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

Reply via email to