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 
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  * @run driver jdk.test.lib.FileInstaller . .
51  * @build nsk.jdb.watch.watch002.watch002
52  *        nsk.jdb.watch.watch002.watch002a
53  * @run main/othervm PropertyResolvingWrapper nsk.jdb.watch.watch002.watch002
54  *      -arch=${os.family}-${os.simpleArch}
55  *      -waittime=5
56  *      -debugee.vmkind=java
57  *      -transport.address=dynamic
58  *      -jdb=${test.jdk}/bin/jdb
59  *      -java.options="${test.vm.opts} ${test.java.opts}"
60  *      -workdir=.
61  *      -debugee.vmkeys="${test.vm.opts} ${test.java.opts}"
62  */
63 
64 package nsk.jdb.watch.watch002;
65 
66 import nsk.share.*;
67 import nsk.share.jdb.*;
68 
69 import java.io.*;
70 import java.util.*;
71 
72 public class watch002 extends JdbTest {
73 
main(String argv[])74     public static void main (String argv[]) {
75         System.exit(run(argv, System.out) + JCK_STATUS_BASE);
76     }
77 
run(String argv[], PrintStream out)78     public static int run(String argv[], PrintStream out) {
79         debuggeeClass =  DEBUGGEE_CLASS;
80         firstBreak = FIRST_BREAK;
81         lastBreak = LAST_BREAK;
82         compoundPromptIdent = COMPOUND_PROMPT_IDENT;
83         return new watch002().runTest(argv, out);
84     }
85 
86     static final String PACKAGE_NAME       = "nsk.jdb.watch.watch002";
87     static final String TEST_CLASS         = PACKAGE_NAME + ".watch002";
88     static final String DEBUGGEE_CLASS     = TEST_CLASS + "a";
89     static final String DEBUGGEE_CLASS2    = DEBUGGEE_CLASS + "$CheckedFields";
90     static final String FIRST_BREAK        = DEBUGGEE_CLASS + ".main";
91     static final String LAST_BREAK         = DEBUGGEE_CLASS + ".breakHere";
92     static final String COMPOUND_PROMPT_IDENT = "main";
93 
94     static String[] checkedFields  = { "FS0", "FS1" };
95     static String[] checkedFields2 = { "FP1", "FU1", "FR1", "FT1", "FV1" };
96 
runCases()97     protected void runCases() {
98         String[] reply;
99         Paragrep grep;
100         int count;
101         Vector v;
102         String found;
103 
104         jdb.setBreakpointInMethod(LAST_BREAK);
105 
106         reply = jdb.receiveReplyFor(JdbCommand.fields + DEBUGGEE_CLASS);
107 
108         reply = jdb.receiveReplyFor(JdbCommand.fields + DEBUGGEE_CLASS2);
109 
110         watchFields (DEBUGGEE_CLASS, checkedFields);
111         watchFields (DEBUGGEE_CLASS2, checkedFields2);
112 
113         jdb.contToExit((checkedFields.length *2)  + (checkedFields2.length *2) + 2);
114 
115         reply = jdb.getTotalReply();
116         if (!checkFields (DEBUGGEE_CLASS, reply, checkedFields)) {
117             success = false;
118         }
119         if (!checkFields (DEBUGGEE_CLASS2, reply, checkedFields2)) {
120             success = false;
121         }
122     }
123 
watchFields(String className, String[] checkedFields)124     private void watchFields (String className, String[] checkedFields) {
125         String[] reply;
126 
127         for (int i = 0; i < checkedFields.length; i++) {
128             reply = jdb.receiveReplyFor(JdbCommand.watch + " all " + className + "." + checkedFields[i]);
129         }
130 
131     }
132 
checkFields(String className, String[] reply, String[] checkedFields)133     private boolean checkFields (String className, String[] reply, String[] checkedFields) {
134         Paragrep grep;
135         String found;
136         boolean result = true;
137         int count;
138         Vector v = new Vector();
139 
140         grep = new Paragrep(reply);
141         v.add("access encountered");
142         for (int i = 0; i < checkedFields.length; i++) {
143             v.removeAllElements();
144             v.add("access encountered");
145             v.add(className + "." + checkedFields[i]);
146 
147             found = grep.findFirst(v);
148             if (found.length() == 0) {
149                 log.complain("Failed to report access to field " + className + "." + checkedFields[i]);
150                 result = false;
151             }
152 
153             v.removeAllElements();
154             v.add("will be");
155             v.add(className + "." + checkedFields[i]);
156 
157             found = grep.findFirst(v);
158             if (found.length() == 0) {
159                 log.complain("Failed to report modification of field " + className + "." + checkedFields[i]);
160                 result = false;
161             }
162         }
163         return result;
164     }
165 }
166