Skip to content

Commit 841c0cf

Browse files
committed
BIGTOP-4361: Add Chinese check
1 parent 9ff8193 commit 841c0cf

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

bigtop-manager-bom/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<langchain4j.version>0.35.0</langchain4j.version>
5454
<mybatis-spring-boot-starter.version>3.0.3</mybatis-spring-boot-starter.version>
5555
<pagehelper-spring-boot-starter.version>2.1.0</pagehelper-spring-boot-starter.version>
56+
<javaparser.version>3.26.3</javaparser.version>
5657
</properties>
5758

5859
<dependencyManagement>
@@ -278,6 +279,11 @@
278279
<artifactId>langchain4j-reactor</artifactId>
279280
<version>${langchain4j.version}</version>
280281
</dependency>
282+
<dependency>
283+
<groupId>com.github.javaparser</groupId>
284+
<artifactId>javaparser-core</artifactId>
285+
<version>${javaparser.version}</version>
286+
</dependency>
281287
</dependencies>
282288
</dependencyManagement>
283289
</project>

bigtop-manager-common/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,10 @@
9191
<artifactId>jakarta.annotation-api</artifactId>
9292
</dependency>
9393

94+
<dependency>
95+
<groupId>com.github.javaparser</groupId>
96+
<artifactId>javaparser-core</artifactId>
97+
<scope>test</scope>
98+
</dependency>
9499
</dependencies>
95100
</project>
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
}

bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/PropertiesUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public static void writeProperties(String fileName, Map<String, Object> configMa
8585
* @throws IOException
8686
*/
8787
public static void writeProperties(String fileName, List<Map<String, Object>> configList) {
88-
// 创建Properties
8988
Properties properties = new Properties();
9089

9190
for (Map<String, Object> map : configList) {

0 commit comments

Comments
 (0)