⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 instructiontest.java

📁 java 调用windows的api
💻 JAVA
字号:
/*
 * InstructionTest.java -
 *
 * This file is part of the Jawin Project: http://jawinproject.sourceforge.net/
 * 
 * Please consult the LICENSE file in the project root directory,
 * or at the project site before using this software.
 */

/* $Id: InstructionTest.java,v 1.2 2004/06/21 14:04:10 arosii_moa Exp $ */

package org.jawin;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

import junit.framework.TestCase;

import org.jawin.io.LittleEndianInputStream;
import org.jawin.io.LittleEndianOutputStream;
import org.jawin.io.NakedByteStream;
import org.jawin.marshal.StructConverter;

/**
 * Testing the Jawin marshalling instruction strings, using the dll exports
 * from the instructionTests.cpp in the cpp CallCOMUnitTest project.
 * <br><br>
 * Uses function from the DLL named 'CallCOMUnitTestD.DLL' in the directory
 * specified in the 'test.dll.dir' property.
 *
 * @version     $Revision: 1.2 $
 * @author      Stuart Halloway, http://www.relevancellc.com/halloway/weblog/
 */
public class InstructionTest extends TestCase {

	public void testSendMeATen() throws Exception {
		InstructionTest.SendMeATen(10);
		InstructionTest.SendMeATenSharedStubs(10);
	}

	public void testFillInWithEleven() throws Exception {
		int eleven = InstructionTest.FillInWithEleven();
		assertTrue(eleven == 11);
	}

	public void testSkipDefaultedArg() throws Exception {
		int twelve = InstructionTest.SkipDefaultedArg(12);
		assertTrue(twelve == 12);
	}

	public void testGetStringLength() throws Exception {
		int five = InstructionTest.GetStringLength("hello");
		assertTrue(five == 5);
		
		// shared stub test
		int eleven = InstructionTest.GetStringLengthSharedStubs("hello again");
		assertTrue(eleven == 11);
	}

	public void testGetNthByte() throws Exception {
		byte[] bytes = { 2, 3, 5, 7, 11 };
		int three = InstructionTest.GetNthByte(bytes, 1);
		assertTrue(three == 3);
	}

	public void testFillOneTwoThree() throws Exception {
		InstructionTest.FillOneTwoThree();
	}

	public void testOneFishTwoFish() throws Exception {
		Object[] oftf = InstructionTest.OneFishTwoFish();
		assertTrue(new Integer(1).equals(oftf[0]));
		assertTrue("fish".equals(oftf[1]));
		assertTrue(new Integer(2).equals(oftf[2]));
		assertTrue("fish".equals(oftf[3]));
	}

	/**
	 * test for a mix of [out] and [retval] (and [in]) variables.
	 */
	public void testGetOutAndRetVal() throws Exception {
		FuncPtr fp = new FuncPtr(testDll, "GetOutAndRetVal");

		NakedByteStream nbs = new NakedByteStream();
		LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
		leos.writeInt(7);
		leos.writeInt((short)9);

		// shorts are also allocated 4 bytes on the stack since calling 
		// convention __stdcall is using 4 bytes chunks on the stack
		// so all smaller types will use this.
		byte[] result = fp.invoke("IAIA:I:L4n4L4n4", 16, nbs, null, ReturnFlags.CHECK_NONE);
		LittleEndianInputStream leis = new LittleEndianInputStream(new ByteArrayInputStream(result));
		
		// first read the return val
		int retVal = leis.readInt();
		
		// and then the two [out] parameters
		int intOut = leis.readInt();
		short shortOut = (short)leis.readInt();
		
		assertEquals(7 + 9, retVal);
		assertEquals(7 * 2, intOut);
		assertEquals(9 * 2, shortOut);
	}

	public void testDontSendATen() {
		try {
			InstructionTest.SendMeATen(9);
			fail("Should have rejected parameter 9");
		} catch (Exception ok) {
		}

		try {
			InstructionTest.SendMeATenSharedStubs(9);
			fail("Should have rejected parameter 9");
		} catch (Exception ok) {
		}
	}

	private static String testDllDir = System.getProperty("test.dll.dir");
	private static String testDll = testDllDir + File.separator + "CallCOMUnitTestD.DLL";

	/**
	 * Unit test for instruction code 'I'
	 */
	private static void SendMeATen(int n) throws IOException, COMException {
		FuncPtr fp = new FuncPtr(testDll, "SendMeATen");
		NakedByteStream nbs = new NakedByteStream();
		LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
		leos.writeInt(n);
		fp.invoke("I::", 4, nbs, null, ReturnFlags.CHECK_FALSE);
	}

	private static void SendMeATenSharedStubs(int n) throws COMException {
		FuncPtr fp = new FuncPtr(testDll, "SendMeATen");
		fp.invoke_I(n, ReturnFlags.CHECK_FALSE);
	}

	/**
	 * Unit test for instruction code 'A'
	 */
	private static int FillInWithEleven() throws IOException, COMException {
		FuncPtr fp = new FuncPtr(testDll, "FillInWithEleven");
		byte[] result = fp.invoke("A::n4", 4, null, null, ReturnFlags.CHECK_NONE);
		return StructConverter.bytesIntoInt(result, 0);
	}

	/**
	 * Unit test for instruction code 'k'
	 */
	private static int SkipDefaultedArg(int n) throws IOException, COMException {
		FuncPtr fp = new FuncPtr(testDll, "SkipDefaultedArg");
		NakedByteStream nbs = new NakedByteStream();
		LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
		leos.writeInt(n);
		byte[] result = fp.invoke("kI:I:", 8, nbs, null, ReturnFlags.CHECK_NONE);
		return StructConverter.bytesIntoInt(result, 0);
	}

	/**
	 * Unit test for instruction code 'G'
	 */
	private static int GetStringLength(String s) throws IOException, COMException {
		FuncPtr fp = new FuncPtr(testDll, "GetStringLength");
		NakedByteStream nbs = new NakedByteStream();
		LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
		leos.writeStringUnicode(s);
		byte[] result = fp.invoke("G:I:", 4, nbs, null, ReturnFlags.CHECK_NONE);
		return StructConverter.bytesIntoInt(result, 0);
	}
	
	private static int GetStringLengthSharedStubs(String s) throws COMException {
		FuncPtr fp = new FuncPtr(testDll, "GetStringLength");
		return fp.invoke_I(s, ReturnFlags.CHECK_NONE);
	}

	/**
	 * Unit test for instruction code 'P'
	 */
	private static int GetNthByte(byte[] b, int n) throws IOException, COMException {
		FuncPtr fp = new FuncPtr(testDll, "GetNthByte");
		NakedByteStream nbs = new NakedByteStream();
		LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
		for (int loop = 0; loop < b.length; loop++) {
			leos.writeByte(b[loop]);
		}
		leos.writeInt(n);
		String marshal = "P" + b.length + "I:I:";
		byte[] result = fp.invoke(marshal, 8, nbs, null, ReturnFlags.CHECK_NONE);
		return StructConverter.bytesIntoInt(result, 0);
	}

	/**
	 * Unit test for instruction code 'L'
	 */
	private static void FillOneTwoThree() throws IOException, COMException {
		FuncPtr fp = new FuncPtr(testDll, "FillOneTwoThree");
		//extract second arg, then back up and extract first
		byte[] result = fp.invoke("AAA::L4n4L-8n4", 12, null, null, ReturnFlags.CHECK_NONE);
		//verify extractions
		int two = StructConverter.bytesIntoInt(result, 0);
		int one = StructConverter.bytesIntoInt(result, 4);
		assertTrue(two == 2);
		assertTrue(one == 1);
	}

	/**
	 * Unit test for instruction code 'r'
	 */
	private static Object[] OneFishTwoFish() throws IOException, COMException {
		FuncPtr fp = new FuncPtr(testDll, "OneFishTwoFish");
		//extract second arg, then back up and extract first
		int size = Variant.SIZEOF * 4;
		String marshal = "M" + size + "::O|r4V!|";
		byte[] result = fp.invoke(marshal, 4, null, null, ReturnFlags.CHECK_NONE);
		LittleEndianInputStream leis = new LittleEndianInputStream(new ByteArrayInputStream(result));
		Object[] ret = new Object[4];
		for (int n = 0; n < 4; n++) {
			ret[n] = Variant.readObject(leis);
		}
		return ret;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -