001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Viacheslav G. Rybalov
021: * @version $Revision: 1.4 $
022: */
023:
024: /**
025: * Created on 05.03.2005
026: */package org.apache.harmony.jpda.tests.jdwp.ClassObjectReference;
027:
028: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
029: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
030: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
031: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
032: import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
033: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
034:
035: /**
036: * JDWP unit test for ClassObjectReference.ReflectedType command.
037: */
038:
039: public class ReflectedTypeTest extends JDWPSyncTestCase {
040:
041: /**
042: * Returns full name of debuggee class which is used by this test.
043: * @return full name of debuggee class.
044: */
045: protected String getDebuggeeClassName() {
046: return "org.apache.harmony.jpda.tests.jdwp.share.debuggee.HelloWorld";
047: }
048:
049: /**
050: * This testcase exercises ClassObjectReference.ReflectedType command.
051: * <BR>Starts <A HREF="../share/debuggee/HelloWorld.html">HelloWorld</A> debuggee.
052: * <BR>Then checks the following four classes:
053: * <BR> - java/lang/Object;
054: * <BR> - java/lang/String;
055: * <BR> - java/lang/Runnable;
056: * <BR> - HelloWorld.
057: * <BR>
058: * <BR>The following statements are checked:
059: * <BR> It is expected:
060: * <BR> - refTypeTag takes one of the TypeTag constants: CLASS, INTERFACE;
061: * <BR> - refTypeTag equals to refTypeTag returned by command
062: * VirtualMachine.ClassesBySignature;
063: * <BR> - typeID equals to typeID returned by the JDWP command
064: * VirtualMachine.ClassesBySignature;
065: * <BR> - All data were read;
066: */
067: public void testReflectedType001() {
068: logWriter.println("==> testReflectedType001 START...");
069: synchronizer
070: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
071:
072: String[] testedClasses = {
073: "java/lang/Object",
074: "java/lang/String",
075: "java/lang/Runnable",
076: "org/apache/harmony/jpda/tests/jdwp/share/debuggee/HelloWorld", };
077:
078: byte expectedRefTypeTags[] = { JDWPConstants.TypeTag.CLASS,
079: JDWPConstants.TypeTag.CLASS,
080: JDWPConstants.TypeTag.INTERFACE,
081: JDWPConstants.TypeTag.CLASS, };
082:
083: for (int i = 0; i < testedClasses.length; i++) {
084: logWriter.println("\n==> Checked class: "
085: + testedClasses[i]);
086:
087: // Get referenceTypeID
088: logWriter
089: .println("==> Send VirtualMachine::ClassesBySignature command for checked class...");
090: CommandPacket packet = new CommandPacket(
091: JDWPCommands.VirtualMachineCommandSet.CommandSetID,
092: JDWPCommands.VirtualMachineCommandSet.ClassesBySignatureCommand);
093: packet.setNextValueAsString("L" + testedClasses[i] + ";");
094: ReplyPacket reply = debuggeeWrapper.vmMirror
095: .performCommand(packet);
096: checkReplyPacket(reply,
097: "VirtualMachine::ClassesBySignature command");
098:
099: int classes = reply.getNextValueAsInt();
100: logWriter.println("==> Number of returned classes = "
101: + classes);
102: //this class may be loaded only once
103: byte refInitTypeTag = 0;
104: long typeInitID = 0;
105: int status = 0;
106:
107: for (int j = 0; j < classes; j++) {
108: refInitTypeTag = reply.getNextValueAsByte();
109: typeInitID = reply.getNextValueAsReferenceTypeID();
110: status = reply.getNextValueAsInt();
111: logWriter.println("==> refTypeId[" + j + "] = "
112: + typeInitID);
113: logWriter.println("==> refTypeTag[" + j + "] = "
114: + refInitTypeTag + "("
115: + JDWPConstants.TypeTag.getName(refInitTypeTag)
116: + ")");
117: logWriter.println("==> classStatus[" + j + "] = "
118: + status + "("
119: + JDWPConstants.ClassStatus.getName(status)
120: + ")");
121:
122: String classSignature = debuggeeWrapper.vmMirror
123: .getClassSignature(typeInitID);
124: logWriter.println("==> classSignature[" + j + "] = "
125: + classSignature);
126:
127: packet = new CommandPacket(
128: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
129: JDWPCommands.ReferenceTypeCommandSet.ClassLoaderCommand);
130: packet.setNextValueAsReferenceTypeID(typeInitID);
131: ReplyPacket reply2 = debuggeeWrapper.vmMirror
132: .performCommand(packet);
133: checkReplyPacket(reply,
134: "ReferenceType::ClassLoader command");
135:
136: long classLoaderID = reply2.getNextValueAsObjectID();
137: logWriter.println("==> classLoaderID[" + j + "] = "
138: + classLoaderID);
139:
140: if (classLoaderID != 0) {
141: String classLoaderSignature = getObjectSignature(classLoaderID);
142: logWriter.println("==> classLoaderSignature[" + j
143: + "] = " + classLoaderSignature);
144: } else {
145: logWriter
146: .println("==> classLoader is system class loader");
147: }
148: }
149:
150: assertEquals(
151: "VirtualMachine::ClassesBySignature returned invalid number of classes,",
152: 1, classes);
153: assertEquals(
154: "VirtualMachine::ClassesBySignature returned invalid TypeTag,",
155: expectedRefTypeTags[i], refInitTypeTag,
156: JDWPConstants.TypeTag
157: .getName(expectedRefTypeTags[i]),
158: JDWPConstants.TypeTag.getName(refInitTypeTag));
159:
160: // Get ClassObject
161: logWriter
162: .println("==> Send ReferenceType::ClassObject command for checked class...");
163: packet = new CommandPacket(
164: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
165: JDWPCommands.ReferenceTypeCommandSet.ClassObjectCommand);
166: packet.setNextValueAsReferenceTypeID(typeInitID);
167: reply = debuggeeWrapper.vmMirror.performCommand(packet);
168: checkReplyPacket(reply,
169: "ReferenceType::ClassObject command");
170:
171: long classObject = reply.getNextValueAsClassObjectID();
172: assertAllDataRead(reply);
173: logWriter.println("==> classObjectID=" + classObject);
174:
175: // Get ReflectedType
176: logWriter
177: .println("==> Send ClassObjectReference::ReflectedType command for classObjectID...");
178: packet = new CommandPacket(
179: JDWPCommands.ClassObjectReferenceCommandSet.CommandSetID,
180: JDWPCommands.ClassObjectReferenceCommandSet.ReflectedTypeCommand);
181: packet.setNextValueAsObjectID(classObject);
182: reply = debuggeeWrapper.vmMirror.performCommand(packet);
183: checkReplyPacket(reply,
184: "ClassObjectReference::ReflectedType command");
185:
186: byte refTypeTag = reply.getNextValueAsByte();
187: long typeID = reply.getNextValueAsReferenceTypeID();
188: logWriter.println("==> reflectedTypeId = " + typeID);
189: logWriter.println("==> reflectedTypeTag = " + refTypeTag
190: + "(" + JDWPConstants.TypeTag.getName(refTypeTag)
191: + ")");
192:
193: assertEquals(
194: "ClassObjectReference::ReflectedType returned invalid reflected TypeTag,",
195: expectedRefTypeTags[i], refTypeTag,
196: JDWPConstants.TypeTag
197: .getName(expectedRefTypeTags[i]),
198: JDWPConstants.TypeTag.getName(refTypeTag));
199: assertEquals(
200: "ClassObjectReference::ReflectedType returned invalid reflected typeID,",
201: typeInitID, typeID);
202: assertAllDataRead(reply);
203: }
204:
205: synchronizer
206: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
207: }
208:
209: /**
210: * Starts this test by junit.textui.TestRunner.run() method.
211: */
212: public static void main(String[] args) {
213: junit.textui.TestRunner.run(ReflectedTypeTest.class);
214: }
215: }
|