|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.bigtop.manager.common.utils; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Assertions; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import com.github.javaparser.JavaParser; |
| 27 | +import com.github.javaparser.ParseResult; |
| 28 | +import com.github.javaparser.ast.CompilationUnit; |
| 29 | +import com.github.javaparser.ast.expr.StringLiteralExpr; |
| 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.Collections; |
| 38 | +import java.util.HashSet; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Set; |
| 41 | +import java.util.regex.Pattern; |
| 42 | +import java.util.stream.Stream; |
| 43 | + |
| 44 | +/** |
| 45 | + * Test case for checking Chinese characters in Java files |
| 46 | + */ |
| 47 | +public class ChineseCharacterCheckTest { |
| 48 | + |
| 49 | + private static final Logger log = LoggerFactory.getLogger(ChineseCharacterCheckTest.class); |
| 50 | + |
| 51 | + private static final Pattern CHINESE_CHAR_PATTERN = Pattern.compile("[\u4e00-\u9fa5]"); |
| 52 | + private static final Set<String> EXCLUDED_FILES = new HashSet<>(Collections.singletonList("Metrics")); |
| 53 | + private static final String MAIN_SOURCE_DIR = "src/main/java"; |
| 54 | + private static final String TEST_SOURCE_DIR = "src/test/java"; |
| 55 | + |
| 56 | + private final JavaParser javaParser = new JavaParser(); |
| 57 | + private final String sourceDir; |
| 58 | + private final String testDir; |
| 59 | + |
| 60 | + public ChineseCharacterCheckTest() { |
| 61 | + boolean isWindowsOs = System.getProperty("os.name").toLowerCase().startsWith("win"); |
| 62 | + String separator = isWindowsOs ? "\\" : "/"; |
| 63 | + this.sourceDir = MAIN_SOURCE_DIR.replace("/", separator); |
| 64 | + this.testDir = TEST_SOURCE_DIR.replace("/", separator); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void shouldNotContainChineseInComments() { |
| 69 | + List<String> violations = scanForChineseCharacters(ScanTarget.COMMENTS); |
| 70 | + assertNoChineseCharacters(violations); |
| 71 | + } |
| 72 | + |
| 73 | + private List<String> scanForChineseCharacters(ScanTarget target) { |
| 74 | + List<String> violations = new ArrayList<>(); |
| 75 | + try (Stream<Path> paths = Files.walk(Paths.get(".."), FileVisitOption.FOLLOW_LINKS)) { |
| 76 | + paths.filter(this::isValidJavaFile).forEach(path -> processFile(path, target, violations)); |
| 77 | + } catch (IOException e) { |
| 78 | + throw new RuntimeException("Failed to scan Java files", e); |
| 79 | + } |
| 80 | + return violations; |
| 81 | + } |
| 82 | + |
| 83 | + private boolean isValidJavaFile(Path path) { |
| 84 | + String pathStr = path.toString(); |
| 85 | + return pathStr.endsWith(".java") |
| 86 | + && (pathStr.contains(sourceDir) || pathStr.contains(testDir)) |
| 87 | + && EXCLUDED_FILES.stream().noneMatch(pathStr::contains); |
| 88 | + } |
| 89 | + |
| 90 | + private void processFile(Path path, ScanTarget target, List<String> violations) { |
| 91 | + try { |
| 92 | + ParseResult<CompilationUnit> parseResult = javaParser.parse(Files.newInputStream(path)); |
| 93 | + parseResult.getResult().ifPresent(cu -> { |
| 94 | + if (target.includeComments()) { |
| 95 | + checkComments(cu, path, violations); |
| 96 | + } |
| 97 | + if (target.includeCode()) { |
| 98 | + checkCode(cu, path, violations); |
| 99 | + } |
| 100 | + }); |
| 101 | + } catch (Exception e) { |
| 102 | + log.error("Error processing file: {}", path, e); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private void checkComments(CompilationUnit cu, Path path, List<String> violations) { |
| 107 | + cu.getAllContainedComments().stream() |
| 108 | + .filter(comment -> |
| 109 | + CHINESE_CHAR_PATTERN.matcher(comment.getContent()).find()) |
| 110 | + .forEach(comment -> violations.add( |
| 111 | + formatViolation(path, "comment", comment.getContent().trim()))); |
| 112 | + } |
| 113 | + |
| 114 | + private void checkCode(CompilationUnit cu, Path path, List<String> violations) { |
| 115 | + cu.findAll(StringLiteralExpr.class).stream() |
| 116 | + .filter(str -> CHINESE_CHAR_PATTERN.matcher(str.getValue()).find()) |
| 117 | + .forEach(str -> violations.add(formatViolation(path, "code", str.getValue()))); |
| 118 | + } |
| 119 | + |
| 120 | + private String formatViolation(Path path, String location, String content) { |
| 121 | + return String.format("Chinese characters found in %s at %s: %s", location, path.toAbsolutePath(), content); |
| 122 | + } |
| 123 | + |
| 124 | + private void assertNoChineseCharacters(List<String> violations) { |
| 125 | + Assertions.assertEquals( |
| 126 | + 0, |
| 127 | + violations.size(), |
| 128 | + () -> String.format( |
| 129 | + "Found Chinese characters in files:%n%s", String.join(System.lineSeparator(), violations))); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Defines what content should be checked for Chinese characters |
| 134 | + */ |
| 135 | + private enum ScanTarget { |
| 136 | + /** |
| 137 | + * Check only comments |
| 138 | + */ |
| 139 | + COMMENTS(true, false), |
| 140 | + /** |
| 141 | + * Check only code (string literals) |
| 142 | + */ |
| 143 | + CODE(false, true), |
| 144 | + /** |
| 145 | + * Check both comments and code |
| 146 | + */ |
| 147 | + ALL(true, true); |
| 148 | + |
| 149 | + private final boolean checkComments; |
| 150 | + private final boolean checkCode; |
| 151 | + |
| 152 | + ScanTarget(boolean checkComments, boolean checkCode) { |
| 153 | + this.checkComments = checkComments; |
| 154 | + this.checkCode = checkCode; |
| 155 | + } |
| 156 | + |
| 157 | + public boolean includeComments() { |
| 158 | + return checkComments; |
| 159 | + } |
| 160 | + |
| 161 | + public boolean includeCode() { |
| 162 | + return checkCode; |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments