1 /*
2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package nsk.share.jpda;
24 
25 import java.net.*;
26 import nsk.share.*;
27 
28 /*
29  * This thread class executes in loop native methods with different return types
30  */
31 public class NativeMethodsTestThread extends Thread {
32 
33     static {
34         System.loadLibrary("NativeMethodsTestThread");
35     }
36 
VoidMethod(String message)37     native void VoidMethod(String message);
38 
BooleanMethod(String message)39     native boolean BooleanMethod(String message);
40 
ByteMethod(String message)41     native byte ByteMethod(String message);
42 
ShortMethod(String message)43     native short ShortMethod(String message);
44 
CharMethod(String message)45     native char CharMethod(String message);
46 
IntMethod(String message)47     native int IntMethod(String message);
48 
LongMethod(String message)49     native long LongMethod(String message);
50 
FloatMethod(String message)51     native float FloatMethod(String message);
52 
DoubleMethod(String message)53     native double DoubleMethod(String message);
54 
ObjectArrayMethod(String message)55     native Object[] ObjectArrayMethod(String message);
56 
StringMethod(String message)57     native String StringMethod(String message);
58 
ThreadMethod(String message)59     native Thread ThreadMethod(String message);
60 
ThreadGroupMethod(String message)61     native ThreadGroup ThreadGroupMethod(String message);
62 
ClassObjectMethod(String message)63     native Class ClassObjectMethod(String message);
64 
ClassLoaderMethod(String message)65     native ClassLoader ClassLoaderMethod(String message);
66 
ObjectMethod(String message)67     native Object ObjectMethod(String message);
68 
BooleanWrapperMethod(String message)69     native Boolean BooleanWrapperMethod(String message);
70 
ByteWrapperMethod(String message)71     native Byte ByteWrapperMethod(String message);
72 
ShortWrapperMethod(String message)73     native Short ShortWrapperMethod(String message);
74 
CharWrapperMethod(String message)75     native Character CharWrapperMethod(String message);
76 
IntWrapperMethod(String message)77     native Integer IntWrapperMethod(String message);
78 
LongWrapperMethod(String message)79     native Long LongWrapperMethod(String message);
80 
FloatWrapperMethod(String message)81     native Float FloatWrapperMethod(String message);
82 
DoubleWrapperMethod(String message)83     native Double DoubleWrapperMethod(String message);
84 
log(String message)85     private void log(String message) {
86         log.display(message);
87     }
88 
89     public static boolean expectedBooleanValue = Boolean.TRUE;
90 
91     public static byte expectedByteValue = Byte.MAX_VALUE;
92 
93     public static char expectedCharValue = Character.MAX_VALUE;
94 
95     public static short expectedShortValue = Short.MAX_VALUE;
96 
97     public static int expectedIntValue = Integer.MAX_VALUE;
98 
99     public static long expectedLongValue = Long.MAX_VALUE;
100 
101     public static float expectedFloatValue = Float.MAX_VALUE;
102 
103     public static double expectedDoubleValue = Double.MAX_VALUE;
104 
105     public static Object[] expectedObjectArrayValue = new Object[1000];
106 
107     public static Thread expectedThreadValue = new Thread();
108 
109     public static ThreadGroup expectedThreadGroupValue = new ThreadGroup("Expected thread group");
110 
111     public static Class expectedClassObjectValue = NativeMethodsTestThread.class;
112 
113     public static ClassLoader expectedClassLoaderValue = new URLClassLoader(new URL[] {});
114 
115     public static String expectedStringValue = "EXPECTED STRING";
116 
117     public static Object expectedObjectValue = new Object();
118 
119     public static Boolean expectedBooleanWrapperValue = new Boolean(Boolean.TRUE);
120 
121     public static Byte expectedByteWrapperValue = new Byte(Byte.MAX_VALUE);
122 
123     public static Character expectedCharWrapperValue = new Character(Character.MAX_VALUE);
124 
125     public static Short expectedShortWrapperValue = new Short(Short.MAX_VALUE);
126 
127     public static Integer expectedIntWrapperValue = new Integer(Integer.MAX_VALUE);
128 
129     public static Long expectedLongWrapperValue = new Long(Long.MAX_VALUE);
130 
131     public static Float expectedFloatWrapperValue = new Float(Float.MAX_VALUE);
132 
133     public static Double expectedDoubleWrapperValue = new Double(Double.MAX_VALUE);
134 
135     // names of tested types, this names can be used to derive names of tested methods(typeName + 'Method'),
136     public static String testedTypesNames[] = {"Void", "Boolean", "Byte", "Short", "Char", "Int", "Long", "Float", "Double", "ObjectArray",
137             "String", "Thread", "ThreadGroup", "ClassObject", "ClassLoader", "Object", "BooleanWrapper", "ByteWrapper", "ShortWrapper",
138             "CharWrapper", "IntWrapper", "LongWrapper", "FloatWrapper", "DoubleWrapper" };
139 
140     private Log log;
141 
142     // is forceEarlyReturn would called for this thread
143     private boolean isTestThread;
144 
145     // how many times call all test methods
146     private int iterationsNumber = 1;
147 
148     // test thread wait on 'startExecutionWicket' in beginning of run()
149     private Wicket startExecutionWicket = new Wicket();
150 
151     private boolean success = true;
152 
NativeMethodsTestThread(Log log, boolean isTestThread, int iterationNumber)153     public NativeMethodsTestThread(Log log, boolean isTestThread, int iterationNumber) {
154         this.log = log;
155         this.isTestThread = isTestThread;
156 
157         this.iterationsNumber = iterationNumber;
158     }
159 
160     private volatile boolean stopExecution;
161 
stopExecution()162     public void stopExecution() {
163         stopExecution = true;
164     }
165 
startExecuion()166     public void startExecuion() {
167         startExecutionWicket.unlockAll();
168     }
169 
run()170     public void run() {
171         // first, debuggee VM starts and suspends test threads to let debugger initialize breakpoints
172         startExecutionWicket.waitFor();
173 
174         int iterationCount = 0;
175 
176         // test thread executes test methods 'iterationNumber' times
177         // non-test thread execute until not interrupted
178         while ((iterationCount++ < iterationsNumber) || (!isTestThread && !stopExecution)) {
179             // execute test methods in order given in 'testMethodsNames' array
180             for (int i = 0; i < testedTypesNames.length; i++) {
181                 executeMethod(testedTypesNames[i] + "Method");
182             }
183         }
184 
185         log("Test thread exit");
186     }
187 
188     // execute test method and check that correct value is returned
executeMethod(String methodName)189     private void executeMethod(String methodName) {
190         String message = Thread.currentThread() + " in " + methodName;
191         if (methodName.equals("VoidMethod")) {
192             VoidMethod(message);
193         }
194         if (methodName.equals("BooleanMethod")) {
195             boolean result = BooleanMethod(message);
196 
197             log("Result: " + result);
198         }
199         if (methodName.equals("ByteMethod")) {
200             byte result = ByteMethod(message);
201 
202             log("Result: " + result);
203         }
204         if (methodName.equals("CharMethod")) {
205             char result = CharMethod(message);
206 
207             log("Result: " + result);
208         }
209         if (methodName.equals("ShortMethod")) {
210             short result = ShortMethod(message);
211 
212             log("Result: " + result);
213         }
214         if (methodName.equals("IntMethod")) {
215             int result = IntMethod(message);
216 
217             log("Result: " + result);
218         }
219         if (methodName.equals("LongMethod")) {
220             long result = LongMethod(message);
221 
222             log("Result: " + result);
223         }
224         if (methodName.equals("FloatMethod")) {
225             float result = FloatMethod(message);
226 
227             log("Result: " + result);
228         }
229         if (methodName.equals("DoubleMethod")) {
230             double result = DoubleMethod(message);
231 
232             log("Result: " + result);
233         }
234         if (methodName.equals("StringMethod")) {
235             String result = StringMethod(message);
236 
237             log("Result: " + result);
238         }
239         if (methodName.equals("ObjectMethod")) {
240             Object result = ObjectMethod(message);
241 
242             log("Result: " + result);
243         }
244         if (methodName.equals("ObjectArrayMethod")) {
245             Object[] result = ObjectArrayMethod(message);
246 
247             log("Result: " + result);
248         }
249         if (methodName.equals("ThreadMethod")) {
250             Thread result = ThreadMethod(message);
251 
252             log("Result: " + result);
253         }
254         if (methodName.equals("ThreadGroupMethod")) {
255             ThreadGroup result = ThreadGroupMethod(message);
256 
257             log("Result: " + result);
258         }
259         if (methodName.equals("ClassObjectMethod")) {
260             Class result = ClassObjectMethod(message);
261 
262             log("Result: " + result);
263         }
264         if (methodName.equals("ClassLoaderMethod")) {
265             ClassLoader result = ClassLoaderMethod(message);
266 
267             log("Result: " + result);
268         }
269         if (methodName.equals("BooleanWrapperMethod")) {
270             Boolean result = BooleanWrapperMethod(message);
271 
272             log("Result: " + result);
273         }
274         if (methodName.equals("ByteWrapperMethod")) {
275             Byte result = ByteWrapperMethod(message);
276 
277             log("Result: " + result);
278         }
279         if (methodName.equals("ShortWrapperMethod")) {
280             Short result = ShortWrapperMethod(message);
281 
282             log("Result: " + result);
283         }
284         if (methodName.equals("CharWrapperMethod")) {
285             Character result = CharWrapperMethod(message);
286 
287             log("Result: " + result);
288         }
289         if (methodName.equals("IntWrapperMethod")) {
290             Integer result = IntWrapperMethod(message);
291 
292             log("Result: " + result);
293         }
294         if (methodName.equals("LongWrapperMethod")) {
295             Long result = LongWrapperMethod(message);
296 
297             log("Result: " + result);
298         }
299         if (methodName.equals("FloatWrapperMethod")) {
300             Float result = FloatWrapperMethod(message);
301 
302             log("Result: " + result);
303         }
304         if (methodName.equals("DoubleWrapperMethod")) {
305             Double result = DoubleWrapperMethod(message);
306 
307             log("Result: " + result);
308         }
309     }
310 
getSuccess()311     public boolean getSuccess() {
312         return success;
313     }
314 }
315