|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.api; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Assertions; |
| 21 | +import org.junit.jupiter.api.Disabled; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import com.github.javaparser.JavaParser; |
| 25 | +import com.github.javaparser.ParseResult; |
| 26 | +import com.github.javaparser.ast.CompilationUnit; |
| 27 | +import com.github.javaparser.ast.comments.Comment; |
| 28 | +import com.github.javaparser.ast.visitor.VoidVisitorAdapter; |
| 29 | +import lombok.extern.slf4j.Slf4j; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.nio.file.FileVisitOption; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Path; |
| 35 | +import java.nio.file.Paths; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.List; |
| 38 | +import java.util.regex.Pattern; |
| 39 | +import java.util.stream.Stream; |
| 40 | + |
| 41 | +import static org.apache.seatunnel.api.ImportShadeClassCheckTest.isWindows; |
| 42 | + |
| 43 | +@Slf4j |
| 44 | +public class ChineseCharacterCheckTest { |
| 45 | + |
| 46 | + private final JavaParser JAVA_PARSER = new JavaParser(); |
| 47 | + |
| 48 | + private static final Pattern CHINESE_PATTERN = Pattern.compile("[\\u4e00-\\u9fa5]"); |
| 49 | + |
| 50 | + /** Defines what content should be checked for Chinese characters */ |
| 51 | + public enum CheckScope { |
| 52 | + /** Check both comments and code */ |
| 53 | + ALL, |
| 54 | + /** Check only comments */ |
| 55 | + COMMENTS_ONLY, |
| 56 | + /** Check only code (string literals) */ |
| 57 | + CODE_ONLY |
| 58 | + } |
| 59 | + |
| 60 | + @Disabled("Currently only checking comments") |
| 61 | + @Test |
| 62 | + public void checkChineseCharactersInAll() { |
| 63 | + checkChineseCharacters(CheckScope.ALL); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void checkChineseCharactersInCommentsOnly() { |
| 68 | + checkChineseCharacters(CheckScope.COMMENTS_ONLY); |
| 69 | + } |
| 70 | + |
| 71 | + @Disabled("Currently only checking comments") |
| 72 | + @Test |
| 73 | + public void checkChineseCharactersInCodeOnly() { |
| 74 | + checkChineseCharacters(CheckScope.CODE_ONLY); |
| 75 | + } |
| 76 | + |
| 77 | + private void checkChineseCharacters(CheckScope scope) { |
| 78 | + // Define path fragments for source and test Java files |
| 79 | + String mainPathFragment = isWindows ? "src\\main\\java" : "src/main/java"; |
| 80 | + String testPathFragment2 = isWindows ? "src\\test\\java" : "src/test/java"; |
| 81 | + |
| 82 | + try (Stream<Path> paths = Files.walk(Paths.get(".."), FileVisitOption.FOLLOW_LINKS)) { |
| 83 | + List<String> filesWithChinese = new ArrayList<>(); |
| 84 | + |
| 85 | + // Filter Java files in the specified directories |
| 86 | + paths.filter( |
| 87 | + path -> { |
| 88 | + String pathString = path.toString(); |
| 89 | + return pathString.endsWith(".java") |
| 90 | + && (pathString.contains(mainPathFragment) |
| 91 | + || pathString.contains(testPathFragment2)); |
| 92 | + }) |
| 93 | + .forEach( |
| 94 | + path -> { |
| 95 | + try { |
| 96 | + // Parse the Java file |
| 97 | + ParseResult<CompilationUnit> parseResult = |
| 98 | + JAVA_PARSER.parse(Files.newInputStream(path)); |
| 99 | + |
| 100 | + parseResult |
| 101 | + .getResult() |
| 102 | + .ifPresent( |
| 103 | + cu -> { |
| 104 | + // Check for Chinese characters in comments |
| 105 | + // if needed |
| 106 | + if (scope != CheckScope.CODE_ONLY) { |
| 107 | + List<Comment> comments = |
| 108 | + cu.getAllContainedComments(); |
| 109 | + for (Comment comment : comments) { |
| 110 | + if (CHINESE_PATTERN |
| 111 | + .matcher( |
| 112 | + comment |
| 113 | + .getContent()) |
| 114 | + .find()) { |
| 115 | + filesWithChinese.add( |
| 116 | + String.format( |
| 117 | + "Found Chinese characters in comment at %s: %s", |
| 118 | + path |
| 119 | + .toAbsolutePath(), |
| 120 | + comment.getContent() |
| 121 | + .trim())); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Check for Chinese characters in code if |
| 127 | + // needed |
| 128 | + if (scope != CheckScope.COMMENTS_ONLY) { |
| 129 | + ChineseCharacterVisitor visitor = |
| 130 | + new ChineseCharacterVisitor( |
| 131 | + path, filesWithChinese); |
| 132 | + visitor.visit(cu, null); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + } catch (Exception e) { |
| 137 | + log.error("Error parsing file: {}", path, e); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + // Assert that no files contain Chinese characters |
| 142 | + Assertions.assertEquals( |
| 143 | + 0, |
| 144 | + filesWithChinese.size(), |
| 145 | + () -> |
| 146 | + String.format( |
| 147 | + "Found Chinese characters in following files (Scope: %s):\n%s", |
| 148 | + scope, String.join("\n", filesWithChinese))); |
| 149 | + |
| 150 | + } catch (IOException e) { |
| 151 | + throw new RuntimeException(e); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private static class ChineseCharacterVisitor extends VoidVisitorAdapter<Void> { |
| 156 | + private final Path filePath; |
| 157 | + private final List<String> filesWithChinese; |
| 158 | + |
| 159 | + public ChineseCharacterVisitor(Path filePath, List<String> filesWithChinese) { |
| 160 | + this.filePath = filePath; |
| 161 | + this.filesWithChinese = filesWithChinese; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void visit(CompilationUnit cu, Void arg) { |
| 166 | + // Check for Chinese characters in string literals |
| 167 | + cu.findAll(com.github.javaparser.ast.expr.StringLiteralExpr.class) |
| 168 | + .forEach( |
| 169 | + str -> { |
| 170 | + if (CHINESE_PATTERN.matcher(str.getValue()).find()) { |
| 171 | + filesWithChinese.add( |
| 172 | + String.format( |
| 173 | + "Found Chinese characters in string literal at %s: %s", |
| 174 | + filePath.toAbsolutePath(), str.getValue())); |
| 175 | + } |
| 176 | + }); |
| 177 | + super.visit(cu, arg); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments