WangHan
2024-09-12 d5855a4926926698b740bc6c7ba489de47adb68b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package tech.powerjob.official.processors.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import tech.powerjob.worker.core.processor.TaskContext;
import org.junit.jupiter.api.Test;
import tech.powerjob.official.processors.TestUtils;
 
import java.util.regex.Pattern;
 
import static org.junit.jupiter.api.Assertions.*;
 
/**
 * test FileCleanupProcessor
 *
 * @author tjq
 * @since 2021/2/1
 */
class FileCleanupProcessorTest {
 
    @Test
    void testPatternCompile() throws Exception {
        String fileName = "abc.log";
        System.out.println(fileName.matches("[\\s\\S]*log"));
        System.out.println(Pattern.matches("[a-z.0-9]*log", fileName));
    }
 
    @Test
    void testScriptCompile() throws Exception {
        Pattern compile = Pattern.compile("(shell|python)_[0-9]*\\.(sh|py)");
        String fileNameA = "shell_158671537124147264.sh";
        String fileNameB = "python_158671537124147264.py";
        assertTrue(compile.matcher(fileNameA).matches());
        assertTrue(compile.matcher(fileNameB).matches());
    }
 
    @Test
    void testProcess() throws Exception {
        JSONObject params = new JSONObject();
        params.put("dirPath", "/Users/tjq/logs");
        params.put("filePattern", "[\\s\\S]*log");
        params.put("retentionTime", 0);
        JSONArray array = new JSONArray();
        array.add(params);
 
        String paramsStr = array.toJSONString();
        System.out.println(paramsStr);
 
        TaskContext taskContext = TestUtils.genTaskContext(paramsStr);
        System.out.println(new FileCleanupProcessor().process(taskContext));
    }
 
    @Test
    void testCleanWorkerScript() throws Exception {
        JSONObject params = new JSONObject();
        params.put("dirPath", "/");
        params.put("filePattern", "(shell|python)_[0-9]*\\.(sh|py)");
        params.put("retentionTime", 24);
        JSONArray array = new JSONArray();
        array.add(params);
 
        TaskContext taskContext = TestUtils.genTaskContext(array.toJSONString());
        System.out.println(new FileCleanupProcessor().process(taskContext));
    }
}