1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 /**
20  * @author Anatoly F. Bondarenko
21  */
22 
23 /**
24  * Created on 04.03.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.ObjectReference;
27 
28 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
29 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
31 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32 import org.apache.harmony.jpda.tests.framework.jdwp.Value;
33 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
34 import org.apache.harmony.jpda.tests.jdwp.share.JDWPTestConstants;
35 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
36 
37 
38 /**
39  * JDWP Unit test for ObjectReference.DisableCollection command.
40  */
41 public class DisableCollectionTest extends JDWPSyncTestCase {
42 
43     static final String thisCommandName = "ObjectReference::DisableCollection command";
44 
45     static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/DisableCollectionDebuggee;";
46 
getDebuggeeClassName()47     protected String getDebuggeeClassName() {
48         return "org.apache.harmony.jpda.tests.jdwp.ObjectReference.DisableCollectionDebuggee";
49     }
50 
51     /**
52      * This testcase exercises ObjectReference.DisableCollection command.
53      * <BR>The test starts DisableCollectionDebuggee class, gets
54      * objectID as value of static field of this class which (field) represents
55      * checked object. Then for this objectID test executes
56      * ObjectReference.DisableCollection command for checked object. After that
57      * Debuggee tries to unload checked object and checks if checked object is
58      * unloaded. <BR>If so the test fails, otherwise it passes.
59      */
testDisableCollection001()60     public void testDisableCollection001() {
61         String thisTestName = "testDisableCollection001";
62         logWriter.println("==> " + thisTestName + " for " + thisCommandName
63                 + ": START...");
64         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
65         finalSyncMessage = "TO_FINISH";
66 
67         long refTypeID = getClassIDBySignature(debuggeeSignature);
68 
69         logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
70         logWriter.println("=> referenceTypeID for Debuggee class = "
71                 + refTypeID);
72 
73         long checkedFieldID = checkField(refTypeID, "checkedObject");
74 
75         logWriter
76                 .println("=> Send ReferenceType::GetValues command for received fieldID and get ObjectID to check...");
77 
78         CommandPacket getValuesCommand = new CommandPacket(
79                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
80                 JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
81         getValuesCommand.setNextValueAsReferenceTypeID(refTypeID);
82         getValuesCommand.setNextValueAsInt(1);
83         getValuesCommand.setNextValueAsFieldID(checkedFieldID);
84 
85         ReplyPacket getValuesReply = debuggeeWrapper.vmMirror
86                 .performCommand(getValuesCommand);
87         getValuesCommand = null;
88         checkReplyPacket(getValuesReply, "ReferenceType::GetValues command");
89 
90         int returnedValuesNumber = getValuesReply.getNextValueAsInt();
91         logWriter
92                 .println("=> Returned values number = " + returnedValuesNumber);
93         assertEquals(
94                 "Invalid number of values returned from ReferenceType::GetValues command,",
95                 1, returnedValuesNumber);
96 
97         Value checkedObjectFieldValue = getValuesReply.getNextValueAsValue();
98         byte checkedObjectFieldTag = checkedObjectFieldValue.getTag();
99         logWriter.println("=> Returned field value tag for checked object= "
100                 + checkedObjectFieldTag + "("
101                 + JDWPConstants.Tag.getName(checkedObjectFieldTag) + ")");
102         assertEquals("Invalid value tag for checked object,",
103                 JDWPConstants.Tag.OBJECT_TAG, checkedObjectFieldTag,
104                 JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
105                 JDWPConstants.Tag.getName((byte) (checkedObjectFieldTag)));
106 
107         long checkedObjectID = checkedObjectFieldValue.getLongValue();
108         logWriter.println("=> Returned checked ObjectID = " + checkedObjectID);
109 
110         logWriter.println("\n=> CHECK: send " + thisCommandName
111                 + " for checked ObjectID...");
112 
113         CommandPacket checkedCommand = new CommandPacket(
114                 JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
115                 JDWPCommands.ObjectReferenceCommandSet.DisableCollectionCommand);
116         checkedCommand.setNextValueAsObjectID(checkedObjectID);
117 
118         ReplyPacket checkedReply = debuggeeWrapper.vmMirror
119                 .performCommand(checkedCommand);
120         checkedCommand = null;
121         checkReplyPacket(checkedReply, thisCommandName);
122 
123         logWriter.println("=> CHECK: Reply is received without any error");
124         logWriter
125                 .println("=> Send to Debuggee signal to continue and try to unload checked ObjectID...");
126         finalSyncMessage = null;
127         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
128         String messageFromDebuggee = synchronizer
129                 .receiveMessageWithoutException("DisableCollectionDebuggee(#2)");
130         logWriter.println("==> debuggeeMsg = |" + messageFromDebuggee + "|");
131         if (messageFromDebuggee.equals("Checked Object is UNLOADed!")) {
132             logWriter.println("\n## FAILURE: Checked Object is UNLOADed after "
133                     + thisCommandName);
134             fail("Checked Object is UNLOADed after " + thisCommandName);
135         } else {
136             logWriter
137                     .println("\n=> PASSED: Checked Object is NOT UNLOADed after "
138                             + thisCommandName);
139         }
140 
141         logWriter.println("=> Send to Debuggee signal to funish ...");
142         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
143         logWriter.println("==> " + thisTestName + " for " + thisCommandName
144                 + ": FINISH");
145 
146         assertAllDataRead(checkedReply);
147     }
148 
149     /**
150      * This testcase exercises ObjectReference.DisableCollection command.
151      * <BR>The test starts DisableCollectionDebuggee class. Then attempts to
152      * disable collection for an invalid objectID and checks INVALID_OBJECT is
153      * returned.
154      */
testDisableCollection_invalid()155     public void testDisableCollection_invalid() {
156         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
157 
158         disableCollection(JDWPTestConstants.INVALID_OBJECT_ID,
159             JDWPConstants.Error.INVALID_OBJECT);
160 
161         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
162     }
163 
164     /**
165      * This testcase exercises ObjectReference.DisableCollection command.
166      * <BR>The test starts DisableCollectionDebuggee class. Then attempts to
167      * disable collection for "null" object (id=0) and checks INVALID_OBJECT is
168      * returned.
169      */
testDisableCollection_null()170     public void testDisableCollection_null() {
171         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
172 
173         disableCollection(JDWPTestConstants.NULL_OBJECT_ID,
174             JDWPConstants.Error.INVALID_OBJECT);
175 
176         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
177     }
178 
disableCollection(long objectID, int expectedErrorCode)179     private void disableCollection(long objectID, int expectedErrorCode) {
180       CommandPacket command = new CommandPacket(
181           JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
182           JDWPCommands.ObjectReferenceCommandSet.DisableCollectionCommand);
183       command.setNextValueAsObjectID(objectID);
184 
185       ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(command);
186       checkReplyPacket(reply, thisCommandName, expectedErrorCode);
187     }
188 }
189