刘光辉
昨天 bb638871a7fb692d80f1b7a758f991dc0879002c
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
package jnpf.audit.sdk.aspect;
 
import java.nio.charset.StandardCharsets;
import java.util.Base64;
 
/** Header decoding and title sanitizing checks; runnable without a test framework. */
public final class AuditRequestTitleResolverChecks {
 
    private AuditRequestTitleResolverChecks() {
    }
 
    public static void main(String[] args) {
        String encoded = Base64.getEncoder().encodeToString(("["
                + "{\"field\":\"code\",\"displayValue\":\"LIMS-001\"},"
                + "{\"field\":\"product\",\"displayValue\":\"阿莫西林\\n胶囊\"}"
                + "]").getBytes(StandardCharsets.UTF_8));
        check("LIMS-001/阿莫西林 胶囊".equals(AuditRequestTitleResolver.resolveEncoded(encoded)));
        String full = repeat('a', 400);
        String capped = encode("[{\"displayValue\":\"" + full + "\"},{\"displayValue\":\"tail\"}]");
        check(full.equals(AuditRequestTitleResolver.resolveEncoded(capped)));
        check(AuditRequestTitleResolver.resolveEncoded("not-base64") == null);
        check(AuditRequestTitleResolver.resolveEncoded(null) == null);
        System.out.println("AuditRequestTitleResolverChecks: all checks passed");
    }
 
    private static void check(boolean condition) {
        if (!condition) {
            throw new AssertionError();
        }
    }
 
    private static String encode(String value) {
        return Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.UTF_8));
    }
 
    private static String repeat(char value, int count) {
        StringBuilder out = new StringBuilder(count);
        for (int i = 0; i < count; i++) {
            out.append(value);
        }
        return out.toString();
    }
}