1 /*
2  * Copyright (c) 2000, 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 
24 package nsk.jdi.LongValue.value;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 import com.sun.jdi.*;
31 import java.util.*;
32 import java.io.*;
33 
34 /**
35  * The test for the implementation of an object of the type     <BR>
36  * LongValue.                                                   <BR>
37  *                                                              <BR>
38  * The test checks up that results of the method                <BR>
39  * <code>com.sun.jdi.LongValue.value()</code>                   <BR>
40  * complies with its spec.                                      <BR>
41  * <BR>
42  * The cases for testing are as follows :               <BR>
43  *                                                      <BR>
44  * when a gebuggee executes the following :             <BR>
45  *      public static long smallest = Long.MIN_VALUE;   <BR>
46  *      public static long zero     = 0;                <BR>
47  *      public static long largest  = Long.MAX_VALUE;   <BR>
48  *                                                      <BR>
49  * which a debugger mirros as :                         <BR>
50  *                                                      <BR>
51  *      LongValue lvsmallest;                           <BR>
52  *      LongValue lvzero;                               <BR>
53  *      LongValue lvlargest;                            <BR>
54  *                                                      <BR>
55  * the following is true:                               <BR>
56  *                                                      <BR>
57  *      lvsmallest == Long.MIN_VALUE                    <BR>
58  *      lvzero     == 0                                 <BR>
59  *      lvlargest  == Long.MAX_VALUE                    <BR>
60  * <BR>
61  */
62 
63 public class value001 {
64 
65     //----------------------------------------------------- templete section
66     static final int PASSED = 0;
67     static final int FAILED = 2;
68     static final int PASS_BASE = 95;
69 
70     //----------------------------------------------------- templete parameters
71     static final String
72     sHeader1 = "\n==> nsk/jdi/LongValue/value/value001",
73     sHeader2 = "--> value001: ",
74     sHeader3 = "##> value001: ";
75 
76     //----------------------------------------------------- main method
77 
main(String argv[])78     public static void main (String argv[]) {
79         int result = run(argv, System.out);
80         System.exit(result + PASS_BASE);
81     }
82 
run(String argv[], PrintStream out)83     public static int run (String argv[], PrintStream out) {
84         return new value001().runThis(argv, out);
85     }
86 
87      //--------------------------------------------------   log procedures
88 
89     private static boolean verbMode = false;
90 
91     private static Log  logHandler;
92 
log1(String message)93     private static void log1(String message) {
94         logHandler.display(sHeader1 + message);
95     }
log2(String message)96     private static void log2(String message) {
97         logHandler.display(sHeader2 + message);
98     }
log3(String message)99     private static void log3(String message) {
100         logHandler.complain(sHeader3 + message);
101     }
102 
103     //  ************************************************    test parameters
104 
105     private String debuggeeName =
106         "nsk.jdi.LongValue.value.value001a";
107 
108     //====================================================== test program
109 
110     static ArgumentHandler      argsHandler;
111     static int                  testExitCode = PASSED;
112 
113     //------------------------------------------------------ common section
114 
runThis(String argv[], PrintStream out)115     private int runThis (String argv[], PrintStream out) {
116 
117         Debugee debuggee;
118 
119         argsHandler     = new ArgumentHandler(argv);
120         logHandler      = new Log(out, argsHandler);
121         Binder binder   = new Binder(argsHandler, logHandler);
122 
123         if (argsHandler.verbose()) {
124             debuggee = binder.bindToDebugee(debuggeeName + " -vbs");  // *** tp
125         } else {
126             debuggee = binder.bindToDebugee(debuggeeName);            // *** tp
127         }
128 
129         IOPipe pipe     = new IOPipe(debuggee);
130 
131         debuggee.redirectStderr(out);
132         log2("value001a debuggee launched");
133         debuggee.resume();
134 
135         String line = pipe.readln();
136         if ((line == null) || !line.equals("ready")) {
137             log3("signal received is not 'ready' but: " + line);
138             return FAILED;
139         } else {
140             log2("'ready' recieved");
141         }
142 
143         VirtualMachine vm = debuggee.VM();
144 
145     //------------------------------------------------------  testing section
146         log1("      TESTING BEGINS");
147 
148         for (int i = 0; ; i++) {
149             pipe.println("newcheck");
150             line = pipe.readln();
151 
152             if (line.equals("checkend")) {
153                 log2("     : returned string is 'checkend'");
154                 break ;
155             } else if (!line.equals("checkready")) {
156                 log3("ERROR: returned string is not 'checkready'");
157                 testExitCode = FAILED;
158                 break ;
159             }
160 
161             log1("new check: #" + i);
162 
163             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part
164 
165             List listOfDebuggeeExecClasses = vm.classesByName(debuggeeName);
166             if (listOfDebuggeeExecClasses.size() != 1) {
167                 testExitCode = FAILED;
168                 log3("ERROR: listOfDebuggeeExecClasses.size() != 1");
169                 break ;
170             }
171             ReferenceType execClass =
172                         (ReferenceType) listOfDebuggeeExecClasses.get(0);
173 
174             Field flsmallest = execClass.fieldByName("smallest");
175             Field flzero     = execClass.fieldByName("zero");
176             Field fllargest  = execClass.fieldByName("largest");
177 
178             LongValue lvsmallest = (LongValue) execClass.getValue(flsmallest);
179             LongValue lvzero     = (LongValue) execClass.getValue(flzero);
180             LongValue lvlargest  = (LongValue) execClass.getValue(fllargest);
181 
182             int i2;
183 
184             for (i2 = 0; ; i2++) {
185 
186                 int expresult = 0;
187 
188                 log2("new check: #" + i2);
189 
190                 switch (i2) {
191 
192                 case 0: if (lvsmallest.value() != Long.MIN_VALUE)
193                             expresult = 1;
194                         break;
195 
196                 case 1: if (lvzero.value() != 0)
197                             expresult = 1;
198                         break;
199 
200                 case 2: if (lvlargest.value() != Long.MAX_VALUE)
201                             expresult = 1;
202                         break;
203 
204 
205                 default: expresult = 2;
206                          break ;
207                 }
208 
209                 if (expresult == 2) {
210                     log2("      test cases finished");
211                     break ;
212                 } else if (expresult == 1) {
213                     log3("ERROR: expresult != true;  check # = " + i);
214                     testExitCode = FAILED;
215                 }
216             }
217             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
218         }
219         log1("      TESTING ENDS");
220 
221     //--------------------------------------------------   test summary section
222     //-------------------------------------------------    standard end section
223 
224         pipe.println("quit");
225         log2("waiting for the debuggee to finish ...");
226         debuggee.waitFor();
227 
228         int status = debuggee.getStatus();
229         if (status != PASSED + PASS_BASE) {
230             log3("debuggee returned UNEXPECTED exit status: " +
231                    status + " != PASS_BASE");
232             testExitCode = FAILED;
233         } else {
234             log2("debuggee returned expected exit status: " +
235                    status + " == PASS_BASE");
236         }
237 
238         if (testExitCode != PASSED) {
239             logHandler.complain("TEST FAILED");
240         }
241         return testExitCode;
242     }
243 }
244