1 /*
2  * Copyright (c) 2002, 2020, 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 
25 /*
26  * @test
27  *
28  * @summary converted from VM Testbase nsk/jdb/watch/watch002.
29  * VM Testbase keywords: [jpda, jdb]
30  * VM Testbase readme:
31  * DECSRIPTION
32  * A positive test case for the 'watch all <class id>.<field name>' command.
33  * There are two test cases:
34  *  - access and modification watch set for the fields defined in class,
35  *  - access and modification watch set for the fields defined in inner class.
36  * The debugged application invokes the methods in which all checked fields
37  * participate in assigned expressions as well as are assigned.
38  * The test passes jdb correctly reports on access and modification events for
39  * all checked fields. Correct report message in jdb stdout should contain full
40  * name of the field and "access encountered" or "will be" words.
41  * The test consists of two program:
42  *   watch002.java - launches jdb and debuggee, writes commands to jdb, reads the jdb output,
43  *   watch002a.java - the debugged application.
44  * COMMENTS
45  * Test fixed according to test bug:
46  *     5045859 TEST_BUG: some JDB tests do not recognize JDB prompt
47  *
48  * @library /vmTestbase
49  *          /test/lib
50  * @build nsk.jdb.watch.watch002.watch002
51  *        nsk.jdb.watch.watch002.watch002a
52  * @run main/othervm PropertyResolvingWrapper nsk.jdb.watch.watch002.watch002
53  *      -arch=${os.family}-${os.simpleArch}
54  *      -waittime=5
55  *      -debugee.vmkind=java
56  *      -transport.address=dynamic
57  *      -jdb=${test.jdk}/bin/jdb
58  *      -java.options="${test.vm.opts} ${test.java.opts}"
59  *      -workdir=.
60  *      -debugee.vmkeys="${test.vm.opts} ${test.java.opts}"
61  */
62 
63 package nsk.jdb.watch.watch002;
64 
65 import nsk.share.*;
66 import nsk.share.jdb.*;
67 
68 import java.io.*;
69 import java.util.*;
70 
71 public class watch002 extends JdbTest {
72 
main(String argv[])73     public static void main (String argv[]) {
74         System.exit(run(argv, System.out) + JCK_STATUS_BASE);
75     }
76 
run(String argv[], PrintStream out)77     public static int run(String argv[], PrintStream out) {
78         debuggeeClass =  DEBUGGEE_CLASS;
79         firstBreak = FIRST_BREAK;
80         lastBreak = LAST_BREAK;
81         compoundPromptIdent = COMPOUND_PROMPT_IDENT;
82         return new watch002().runTest(argv, out);
83     }
84 
85     static final String PACKAGE_NAME       = "nsk.jdb.watch.watch002";
86     static final String TEST_CLASS         = PACKAGE_NAME + ".watch002";
87     static final String DEBUGGEE_CLASS     = TEST_CLASS + "a";
88     static final String DEBUGGEE_CLASS2    = DEBUGGEE_CLASS + "$CheckedFields";
89     static final String FIRST_BREAK        = DEBUGGEE_CLASS + ".main";
90     static final String LAST_BREAK         = DEBUGGEE_CLASS + ".breakHere";
91     static final String COMPOUND_PROMPT_IDENT = "main";
92 
93     static String[] checkedFields  = { "FS0", "FS1" };
94     static String[] checkedFields2 = { "FP1", "FU1", "FR1", "FT1", "FV1" };
95 
runCases()96     protected void runCases() {
97         String[] reply;
98         Paragrep grep;
99         int count;
100         Vector v;
101         String found;
102 
103         jdb.setBreakpointInMethod(LAST_BREAK);
104 
105         reply = jdb.receiveReplyFor(JdbCommand.fields + DEBUGGEE_CLASS);
106 
107         reply = jdb.receiveReplyFor(JdbCommand.fields + DEBUGGEE_CLASS2);
108 
109         watchFields (DEBUGGEE_CLASS, checkedFields);
110         watchFields (DEBUGGEE_CLASS2, checkedFields2);
111 
112         jdb.contToExit((checkedFields.length *2)  + (checkedFields2.length *2) + 2);
113 
114         reply = jdb.getTotalReply();
115         if (!checkFields (DEBUGGEE_CLASS, reply, checkedFields)) {
116             success = false;
117         }
118         if (!checkFields (DEBUGGEE_CLASS2, reply, checkedFields2)) {
119             success = false;
120         }
121     }
122 
watchFields(String className, String[] checkedFields)123     private void watchFields (String className, String[] checkedFields) {
124         String[] reply;
125 
126         for (int i = 0; i < checkedFields.length; i++) {
127             reply = jdb.receiveReplyFor(JdbCommand.watch + " all " + className + "." + checkedFields[i]);
128         }
129 
130     }
131 
checkFields(String className, String[] reply, String[] checkedFields)132     private boolean checkFields (String className, String[] reply, String[] checkedFields) {
133         Paragrep grep;
134         String found;
135         boolean result = true;
136         int count;
137         Vector v = new Vector();
138 
139         grep = new Paragrep(reply);
140         v.add("access encountered");
141         for (int i = 0; i < checkedFields.length; i++) {
142             v.removeAllElements();
143             v.add("access encountered");
144             v.add(className + "." + checkedFields[i]);
145 
146             found = grep.findFirst(v);
147             if (found.length() == 0) {
148                 log.complain("Failed to report access to field " + className + "." + checkedFields[i]);
149                 result = false;
150             }
151 
152             v.removeAllElements();
153             v.add("will be");
154             v.add(className + "." + checkedFields[i]);
155 
156             found = grep.findFirst(v);
157             if (found.length() == 0) {
158                 log.complain("Failed to report modification of field " + className + "." + checkedFields[i]);
159                 result = false;
160             }
161         }
162         return result;
163     }
164 }
165