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 package nsk.jdi.EventRequest._bounds_;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 import com.sun.jdi.*;
31 import com.sun.jdi.request.*;
32 import com.sun.jdi.event.*;
33 
34 import java.util.*;
35 import java.io.*;
36 
37 /**
38  *
39  * The test checks up the methods of <code>com.sun.jdi.EventRequest</code>: <br>
40  *     1. <code>putProperty(Object, Object)</code>                          <br>
41  *     2. <code>getProperty(Object)</code>                                  <br>
42  *     3. <code>setSuspendPolicy(int)</code>                                <br>
43  * to correctly work for boundary values of argument.                       <br>
44  *
45  * The test performs the next testcases:                                    <br>
46  *     1. <code>putProperty(null, String)</code> - in this case it is
47  *        expected property <code>null</code> with value of
48  *        <code>String</code> will be created.                              <br>
49  *        No exception must be thrown.
50  *     2. <code>getProperty(null)</code> - value of <code>String</code> from
51  *        the previous case is expected as return value. No exception must
52  *        be thrown.                                                        <br>
53  *     3. <code>setSuspendPolicy(int)</code> is checked for the next
54  *        parameters: <code>Integer.MIN_VALUE</code>, <code>-1</code>,
55  *        <code>Integer.MAX_VALUE</code>. IllegalArgumentException is expected. <br>
56  */
57 
58 public class eventrequest001 {
59 
60     private final static String prefix = "nsk.jdi.EventRequest._bounds_.";
61     private final static String debuggerName = prefix + "eventrequest001";
62     private final static String debugeeName = debuggerName + "a";
63 
64     public final static String SGNL_READY = "ready";
65     public final static String SGNL_QUIT = "quit";
66 
67     public static int exitStatus;
68     public static Log log;
69     public static Debugee debugee;
70 
71     private static String propertyValue = "something";
72     private static int policies[] = {
73                                         Integer.MIN_VALUE,
74                                         -1,
75                                         Integer.MAX_VALUE
76     };
77 
78 //----------------------------------------------------
79 
display(String msg)80     public static void display(String msg) {
81         log.display(msg);
82     }
83 
complain(String msg)84     public static void complain(String msg) {
85         log.complain("debugger FAILURE> " + msg + "\n");
86     }
87 
main(String argv[])88     public static void main(String argv[]) {
89         System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));
90     }
91 
run(String argv[], PrintStream out)92     public static int run(String argv[], PrintStream out) {
93 
94         exitStatus = Consts.TEST_PASSED;
95 
96         eventrequest001 thisTest = new eventrequest001();
97 
98         ArgumentHandler argHandler = new ArgumentHandler(argv);
99         log = new Log(out, argHandler);
100 
101         debugee = Debugee.prepareDebugee(argHandler, log, debugeeName);
102 
103         thisTest.execTest();
104         display("Test finished. exitStatus = " + exitStatus);
105 
106         return exitStatus;
107     }
108 
execTest()109     private void execTest() {
110 
111         debugee.VM().suspend();
112         ReferenceType testedClass = debugee.classByName(debugeeName);
113 
114         BreakpointRequest brkp = debugee.setBreakpoint(testedClass,
115                                                     eventrequest001a.brkpMethodName,
116                                                     eventrequest001a.brkpLineNumber);
117 
118         display("\nTEST BEGINS");
119         display("===========");
120 
121         try {
122             display("setting property <null>: value <" + propertyValue + ">");
123             brkp.putProperty(null, propertyValue);
124         } catch (Exception e) {
125             complain("Unexpected: " + e);
126             exitStatus = Consts.TEST_FAILED;
127         }
128         display("");
129 
130         Object obj = null;
131         try {
132             display("reading property <null>:");
133             obj = brkp.getProperty(null);
134             display("\t\t value : <" + obj + ">");
135         } catch (Exception e) {
136             complain("Unexpected: " + e);
137             exitStatus = Consts.TEST_FAILED;
138         }
139         if (!obj.equals(propertyValue)) {
140             complain("Unexpected property value: " + obj);
141             exitStatus = Consts.TEST_FAILED;
142         }
143 
144         display("");
145         for (int i = 0; i < policies.length; i++) {
146             display("invoking brkp.setSuspendPolicy(" + policies[i] + ")");
147             brkp.disable();
148             try {
149                 brkp.setSuspendPolicy(policies[i]);
150 
151                 complain("No exception was thrown for argument: " + policies[i]);
152                 exitStatus = Consts.TEST_FAILED;
153             } catch (IllegalArgumentException e1) {
154                 display("Expected IllegalArgumentException was thrown for argument: " + policies[i]);
155             } catch (Exception e) {
156                 complain("Unexpected exception was thrown for argument: " + policies[i] + " :\n\t" + e );
157                 exitStatus = Consts.TEST_FAILED;
158             }
159             brkp.enable();
160             display("");
161         }
162 
163         // breakpoint should be disabled before test finishing
164         brkp.disable();
165 
166         display("=============");
167         display("TEST FINISHES\n");
168 
169         debugee.resume();
170         debugee.quit();
171     }
172 }
173