1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import java.util.function.*;
18 
19 public class Main {
20   public static final boolean IS_ART = System.getProperty("java.vm.name").equals("Dalvik");
21 
22   public static final class Names {
23     public final String native_name;
24     public final String java_name;
25 
Names(String ntv, String java)26     public Names(String ntv, String java) {
27       this.native_name = ntv;
28       this.java_name = java;
29     }
30 
equals(Object o)31     public boolean equals(Object o) {
32       if (o instanceof Names) {
33         Names on = (Names) o;
34         return on.native_name.equals(native_name) && on.java_name.equals(java_name);
35       } else {
36         return false;
37       }
38     }
39 
toString()40     public String toString() {
41       return "Names{native: \"" + native_name + "\", java: \"" + java_name + "\"}";
42     }
43   }
44 
checkDefaultNames(Names res)45   public static void checkDefaultNames(Names res) {
46     if (IS_ART) {
47       if (!res.native_name.matches("Thread-[0-9]+")) {
48         throw new Error("Bad thread name! " + res);
49       }
50     } else {
51       if (!res.native_name.equals("native-thread")) {
52         throw new Error("Bad thread name! " + res);
53       }
54     }
55     if (!res.java_name.matches("Thread-[0-9]+")) {
56       throw new Error("Bad thread name! " + res);
57     }
58   }
59 
checkNames(Names res, Names art_exp, Names ri_exp)60   public static void checkNames(Names res, Names art_exp, Names ri_exp) {
61     if (IS_ART) {
62       if (!res.equals(art_exp)) {
63         throw new Error("Not equal " + res + " != " + art_exp);
64       }
65     } else {
66       if (!res.equals(ri_exp)) {
67         throw new Error("Not equal " + res + " != " + ri_exp);
68       }
69     }
70   }
71 
main(String[] args)72   public static void main(String[] args) throws Exception {
73     System.loadLibrary(args[0]);
74     Names[] name = new Names[1];
75     BiConsumer<String, Thread> thdResult =
76         (String native_name, Thread jthread) -> {
77           name[0] = new Names(native_name, jthread.getName());
78         };
79 
80     runThreadTest(thdResult);
81     checkDefaultNames(name[0]);
82 
83     runThreadTestWithName(thdResult);
84     checkNames(
85         name[0],
86         new Names("java-native-thr", "java-native-thread"),
87         new Names("native-thread", "java-native-thread"));
88 
89     runThreadTestSetJava(thdResult);
90     checkNames(
91         name[0],
92         new Names("native-thread-s", "native-thread-set-java"),
93         new Names("native-thread", "native-thread-set-java"));
94   }
95 
runThreadTest(BiConsumer<String, Thread> results)96   public static native void runThreadTest(BiConsumer<String, Thread> results);
97 
runThreadTestWithName(BiConsumer<String, Thread> results)98   public static native void runThreadTestWithName(BiConsumer<String, Thread> results);
99 
runThreadTestSetJava(BiConsumer<String, Thread> results)100   public static native void runThreadTestSetJava(BiConsumer<String, Thread> results);
101 }
102