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
19package org.apache.harmony.jpda.tests.jdwp.ReferenceType;
20
21import java.io.DataInputStream;
22import java.io.FileInputStream;
23
24import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
25import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
26import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
27import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
28import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
29
30public class ClassFileVersionTest extends JDWPSyncTestCase {
31
32	static final int testStatusPassed = 0;
33
34	static final int testStatusFailed = -1;
35
36	static final String thisCommandName = "ReferenceType.ClassFileVersion command";
37
38	static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/ClassFileVersionDebuggee;";
39
40	static final String debuggeeClass = "org/apache/harmony/jpda/tests/jdwp/ReferenceType/ClassFileVersionDebuggee.class";
41
42	@Override
43	protected String getDebuggeeClassName() {
44		return "org.apache.harmony.jpda.tests.jdwp.ReferenceType.ClassFileVersionDebuggee";
45	}
46
47	/**
48	 * This testcase exercises ReferenceType.ClassFileVersion command. <BR>
49	 * The test starts ClassFileVersionDebuggee class, requests referenceTypeId
50	 * for this class by VirtualMachine.ClassesBySignature command, then
51	 * performs ReferenceType.ClassFileVersion command and checks that returned
52	 * majorVersion and minorVersion are equal to expected values.
53	 */
54	public void testClassFileVersion001() {
55		String thisTestName = "testClassFileVersion001";
56		logWriter.println("==> " + thisTestName + " for " + thisCommandName
57				+ ": START...");
58		synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
59
60		long refTypeID = getClassIDBySignature(debuggeeSignature);
61
62		logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
63		logWriter.println("=> referenceTypeID for Debuggee class = "
64				+ refTypeID);
65		logWriter.println("=> CHECK: send " + thisCommandName
66				+ " and check reply...");
67
68		CommandPacket classFileVersionCommand = new CommandPacket(
69				JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
70				JDWPCommands.ReferenceTypeCommandSet.ClassFileVersionCommand);
71		classFileVersionCommand.setNextValueAsReferenceTypeID(refTypeID);
72
73		ReplyPacket classFileVersionReply = debuggeeWrapper.vmMirror
74				.performCommand(classFileVersionCommand);
75		classFileVersionCommand = null;
76		checkReplyPacket(classFileVersionReply, thisCommandName);
77
78		int majorVersion = classFileVersionReply.getNextValueAsInt();
79		int minorVersion = classFileVersionReply.getNextValueAsInt();
80
81		int expectedMinorVersion = -1;
82		int expectedMajorVersion = -1;
83		try {
84			DataInputStream in = new DataInputStream(new FileInputStream(
85					debuggeeClass));
86			int magic = in.readInt();
87			if (magic != 0xcafebabe) {
88				printErrorAndFail(debuggeeClass + " is not a valid class!");
89			}
90			expectedMinorVersion = in.readUnsignedShort();
91			expectedMajorVersion = in.readUnsignedShort();
92			in.close();
93		} catch (Exception e) {
94			printErrorAndFail(thisCommandName + "has error in reading target class file!");
95		}
96
97		assertEquals(thisCommandName + "returned invalid majorVersion,", expectedMajorVersion, majorVersion, null, null);
98		assertEquals(thisCommandName + "returned invalid minorVersion,", expectedMinorVersion, minorVersion, null, null);
99
100		logWriter.println("=> CHECK: PASSED: expected majorVersion and minorVersion are returned:");
101	    logWriter.println("=> majorVersion = " + majorVersion);
102	    logWriter.println("=> minorVersion = " + minorVersion);
103
104	    synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
105	    logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
106
107	    assertAllDataRead(classFileVersionReply);
108	}
109}
110