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 Vitaly A. Provodin
21  */
22 
23 /**
24  * Created on 03.02.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.VirtualMachine;
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.jdwp.share.JDWPSyncTestCase;
33 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
34 
35 
36 /**
37  * JDWP Unit test for VirtualMachine.AllClasses command.
38  */
39 public class AllClassesTest extends JDWPSyncTestCase {
40 
getDebuggeeClassName()41     protected String getDebuggeeClassName() {
42         return "org.apache.harmony.jpda.tests.jdwp.share.debuggee.HelloWorld";
43     }
44 
45     /**
46      * This testcase exercises VirtualMachine.AllClasses command.
47      * <BR>At first the test starts HelloWorld debuggee.
48      * <BR> Then the test performs VirtualMachine.AllClasses command and checks that:
49      * <BR>&nbsp;&nbsp; - number of reference types returned by command has
50      *                    non-zero value;
51      * <BR>&nbsp;&nbsp; - there are no classes with the 'ARRAY' or
52      *                    'PRIMITIVE' bits in the status flag;
53      */
testAllClasses002()54     public void testAllClasses002() {
55         logWriter.println("==> testAllClasses002: START...");
56 
57         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
58 
59         logWriter.println("==> Send VirtualMachine::AllClasses command...");
60         CommandPacket packet = new CommandPacket(
61                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
62                 JDWPCommands.VirtualMachineCommandSet.AllClassesCommand);
63         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
64         checkReplyPacket(reply, "VirtualMachine::AllClasses command");
65 
66         long typeID;
67         String signature;
68         int status;
69 
70         int classes = reply.getNextValueAsInt();
71         assertTrue(classes > 0);
72 
73         int count = 0;
74         for (int i = 0; i < classes; i++) {
75 
76             reply.getNextValueAsByte();
77             typeID = reply.getNextValueAsReferenceTypeID();
78             signature = reply.getNextValueAsString();
79             status = reply.getNextValueAsInt();
80 
81             if ( (status & JDWPConstants.ClassStatus.ARRAY) != 0 ){
82                 logWriter.println("## FAILURE: Unexpected status is returned:");
83                 logWriter.println("##          ReferenceTypeId = " + typeID);
84                 logWriter.println("##          Class signature: " + signature);
85                 logWriter.println("##          Class status = 0x" + Integer.toHexString(status)
86                     + "(" + JDWPConstants.ClassStatus.getName(status)+ ")");
87                 logWriter.println("##          Status \"0x"
88                         + Integer.toHexString(JDWPConstants.ClassStatus.ARRAY)
89                         + "("
90                         + JDWPConstants.ClassStatus.getName(JDWPConstants.ClassStatus.ARRAY)
91                         + ")\" must NOT be returned!");
92                 count++;
93             }
94             if ( (status & JDWPConstants.ClassStatus.PRIMITIVE) != 0 ){
95                 logWriter.println("## FAILURE: Unexpected status is returned:");
96                 logWriter.println("##          ReferenceTypeId = " + typeID);
97                 logWriter.println("##          Class signature: " + signature);
98                 logWriter.println("##          Class status = 0x" + Integer.toHexString(status)
99                     + "(" + JDWPConstants.ClassStatus.getName(status)+ ")");
100                 logWriter.println("##          Status \"0x"
101                         + Integer.toHexString(JDWPConstants.ClassStatus.PRIMITIVE)
102                         + "("
103                         + JDWPConstants.ClassStatus.getName(JDWPConstants.ClassStatus.PRIMITIVE)
104                         + ")\" must NOT be returned!");
105                 count++;
106             }
107         }
108         assertEquals("count must be 0", 0, count);
109         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
110         logWriter.println("==> testAllClasses002: OK.");
111     }
112 
113     /**
114      * This testcase exercises VirtualMachine.AllClasses command.
115      * <BR>At first the test starts HelloWorld debuggee.
116      * <BR> Then the test performs VirtualMachine.AllClasses command and checks that:
117      * <BR>&nbsp;&nbsp; - number of reference types returned by command has
118      * non-zero value;
119      * <BR>&nbsp;&nbsp; - refTypeTag takes one of the TypeTag constants:
120      *                    'CLASS', 'INTERFACE', 'ARRAY';
121      * <BR>&nbsp;&nbsp; - length of the signature string is not zero and starts with 'L' or
122      *                    '[' symbols;
123      * <BR>&nbsp;&nbsp; - signature of at least one class contains the "HelloWorld" string;
124      * <BR>&nbsp;&nbsp; - All data were read from reply packet;
125      */
testAllClasses001()126     public void testAllClasses001() {
127         logWriter.println("==> testAllClasses001: START...");
128 
129         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
130 
131         logWriter.println("==> Send VirtualMachine::AllClasses command...");
132         CommandPacket packet = new CommandPacket(
133                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
134                 JDWPCommands.VirtualMachineCommandSet.AllClassesCommand);
135         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
136         checkReplyPacket(reply, "VirtualMachine::AllClasses command");
137 
138         byte refTypeTag;
139         String refTypeTagName;
140         long typeID;
141         String signature;
142         int status;
143         String msgLine;
144         boolean flagForHelloWorld = false;
145 
146         int classes = reply.getNextValueAsInt();
147         logWriter.println("==> Number of reference types = " + classes);
148         assertTrue(classes > 0);
149 
150         int printBound_1 = classes;
151         int printBound_2 = 0;
152         if ( classes > 50 ) {
153             printBound_1 = 5;
154             printBound_2 = classes - 5;
155         }
156         for (int i = 0; i < classes; i++) {
157 
158             boolean infoIsPrinted = false;
159             refTypeTag = reply.getNextValueAsByte();
160             try {
161                 refTypeTagName = JDWPConstants.TypeTag.getName(refTypeTag);
162             } catch ( Throwable thrown ) {
163                 refTypeTagName = "UnknownTagName";
164             }
165             msgLine = "\n" + i + ". " + refTypeTagName;
166             typeID = reply.getNextValueAsReferenceTypeID();
167             signature = reply.getNextValueAsString();
168             msgLine = msgLine + ": " + signature;
169             status = reply.getNextValueAsInt();
170             msgLine = msgLine + " " + JDWPConstants.ClassStatus.getName(status);
171             if ( (i < printBound_1) || (i >= printBound_2) ) {
172                 logWriter.println(msgLine);
173                 logWriter.println("\treferenceTypeID = " + typeID);
174                 logWriter.println("\trefTypeTag = " + refTypeTagName
175                         + "(" + refTypeTag + ")");
176                 logWriter.println("\tsignature = " + signature);
177                 if ( i == (printBound_1-1) ) {
178                     logWriter.println("...\n...\n...");
179                 }
180                 infoIsPrinted = true;
181             }
182 
183             try {
184                 assertTrue(refTypeTag == JDWPConstants.TypeTag.ARRAY
185                         || refTypeTag == JDWPConstants.TypeTag.CLASS
186                         || refTypeTag == JDWPConstants.TypeTag.INTERFACE);
187 
188                 assertTrue(signature.length() > 0);
189                 assertTrue(signature.toCharArray()[0] == 'L'
190                         || signature.toCharArray()[0] == '[');
191             } catch ( Throwable thrown) {
192                 // some assert is caught
193                 if ( !infoIsPrinted ) {
194                     logWriter.println(msgLine);
195                     logWriter.println("\treferenceTypeID = " + typeID);
196                     logWriter.println("\trefTypeTag = " + refTypeTagName
197                             + "(" + refTypeTag + ")");
198                     logWriter.println("\tsignature = " + signature );
199                 }
200                 logWriter.println("## FAILURE is found out for this referenceType!\n");
201                 assertTrue(refTypeTag == JDWPConstants.TypeTag.ARRAY
202                         || refTypeTag == JDWPConstants.TypeTag.CLASS
203                         || refTypeTag == JDWPConstants.TypeTag.INTERFACE);
204 
205                 assertTrue(signature.length() > 0);
206                 assertTrue(signature.toCharArray()[0] == 'L'
207                         || signature.toCharArray()[0] == '[');
208             }
209 
210             if (signature.indexOf("HelloWorld") != -1)
211                 flagForHelloWorld = true;
212         }
213         assertAllDataRead(reply);
214         assertTrue("HelloWorld has not been found in signatures of returned reference types",
215                 flagForHelloWorld);
216 
217         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
218         logWriter.println("==> testAllClasses001: OK.");
219     }
220 }
221