1 /*
2  * Copyright (c) 2002, 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.jdi.EventSet.toString;
24 
25 import nsk.share.*;
26 import nsk.share.jpda.*;
27 import nsk.share.jdi.*;
28 
29 import com.sun.jdi.*;
30 import com.sun.jdi.request.*;
31 import com.sun.jdi.event.*;
32 import com.sun.jdi.connect.*;
33 import java.io.*;
34 import java.util.*;
35 
36 /**
37  * The debugger application of the test.
38  */
39 public class tostring001 {
40 
41     //------------------------------------------------------- immutable common fields
42 
43     final static String SIGNAL_READY = "ready";
44     final static String SIGNAL_GO    = "go";
45     final static String SIGNAL_QUIT  = "quit";
46 
47     private static int waitTime;
48     private static int exitStatus;
49     private static ArgumentHandler     argHandler;
50     private static Log                 log;
51     private static Debugee             debuggee;
52     private static ReferenceType       debuggeeClass;
53 
54     //------------------------------------------------------- mutable common fields
55 
56     private final static String prefix = "nsk.jdi.EventSet.toString.";
57     private final static String className = "tostring001";
58     private final static String debuggerName = prefix + className;
59     private final static String debuggeeName = debuggerName + "a";
60 
61     //------------------------------------------------------- test specific fields
62 
63     //------------------------------------------------------- immutable common methods
64 
main(String argv[])65     public static void main(String argv[]) {
66         System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));
67     }
68 
display(String msg)69     private static void display(String msg) {
70         log.display("debugger > " + msg);
71     }
72 
complain(String msg)73     private static void complain(String msg) {
74         log.complain("debugger FAILURE > " + msg);
75     }
76 
run(String argv[], PrintStream out)77     public static int run(String argv[], PrintStream out) {
78 
79         exitStatus = Consts.TEST_PASSED;
80 
81         argHandler = new ArgumentHandler(argv);
82         log = new Log(out, argHandler);
83         waitTime = argHandler.getWaitTime() * 60000;
84 
85         debuggee = Debugee.prepareDebugee(argHandler, log, debuggeeName);
86 
87         debuggeeClass = debuggee.classByName(debuggeeName);
88         if ( debuggeeClass == null ) {
89             complain("Class '" + debuggeeName + "' not found.");
90             exitStatus = Consts.TEST_FAILED;
91         }
92 
93         execTest();
94 
95         debuggee.quit();
96 
97         return exitStatus;
98     }
99 
100     //------------------------------------------------------ mutable common method
101 
execTest()102     private static void execTest() {
103 
104         BreakpointRequest brkp = debuggee.setBreakpoint(debuggeeClass,
105                                                     tostring001a.brkpMethodName,
106                                                     tostring001a.brkpLineNumber);
107         debuggee.resume();
108 
109         debuggee.sendSignal(SIGNAL_GO);
110         EventSet eventSet = null;
111 
112 /*
113         // waiting for VMStartEvent event
114         try {
115             eventSet = waitEventSet(null, waitTime);
116         } catch (InterruptedException e) {
117             throw new Failure("unexpected InterruptedException while waiting for VMStartEvent event");
118         }
119 
120         display("Checking toString() method for eventSet of VMStartEvent event");
121         checkToString (eventSet);
122 */
123 
124         // waiting the breakpoint event
125         try {
126             eventSet = waitEventSet(brkp, waitTime);
127         } catch (InterruptedException e) {
128             throw new Failure("unexpected InterruptedException while waiting for Breakpoint event");
129         }
130         if (!(eventSet.eventIterator().nextEvent() instanceof BreakpointEvent)) {
131             debuggee.resume();
132             throw new Failure("BreakpointEvent didn't arrive");
133         }
134 
135         display("Checking toString() method for eventSet of Breakpoint event");
136         checkToString (eventSet);
137 
138         display("Checking completed!");
139         debuggee.resume();
140     }
141 
142     //--------------------------------------------------------- test specific methods
143 
waitEventSet(EventRequest request, long timeout)144     private static EventSet waitEventSet(EventRequest request, long timeout)
145                             throws InterruptedException {
146         Event event;
147         long totalTime = timeout;
148         long tmp, begin = System.currentTimeMillis(),
149              delta = 0;
150         boolean exit = false;
151         EventIterator eventIterator = null;
152         EventSet eventSet = null;
153 
154         while (totalTime > 0 && !exit) {
155             if (eventIterator == null || !eventIterator.hasNext()) {
156                 eventSet = debuggee.VM().eventQueue().remove(totalTime);
157                 if (eventSet != null) {
158                     eventIterator = eventSet.eventIterator();
159                 } else {
160                     eventIterator = null;
161                 }
162             }
163             if (eventIterator != null) {
164                 while (eventIterator.hasNext()) {
165                     event = eventIterator.nextEvent();
166                     display(" event ===>>> " + event);
167                     if (event.request() != null && event.request().equals(request)) {
168                         return eventSet;
169                     } else if (request == null && event instanceof VMStartEvent) {
170                         return eventSet;
171                     } else if (event instanceof VMDisconnectEvent) {
172                         exit = true;
173                         break;
174                     } // if
175                 } // while
176             } // if
177             tmp = System.currentTimeMillis();
178             delta = tmp - begin;
179             totalTime -= delta;
180                 begin = tmp;
181         }
182         return null;
183     }
184 
checkToString(EventSet eventSet)185     private static void checkToString (EventSet eventSet) {
186         String str = eventSet.toString();
187         if (str == null) {
188             complain("toString() returns null for event set");
189             exitStatus = Consts.TEST_FAILED;
190         } else if (str.length() == 0) {
191             complain("toString() returns empty string for event set");
192             exitStatus = Consts.TEST_FAILED;
193         } else {
194             display("toString() returns for event set : " + str);
195         }
196     }
197 }
198 //--------------------------------------------------------- test specific classes
199