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

📄 vtabletestbase.java

📁 java 调用windows的api
💻 JAVA
字号:
/*
 * VtableTestBase.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: VtableTestBase.java,v 1.3 2004/08/01 21:22:51 arosii_moa Exp $ */

package org.jawin;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Date;

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

/**
 * base testcase class for Vtable based tests. Compare this with the Dispatch based
 * tests in {@link org.jawin.DispatchTestBase}.
 * <br><br>
 * TODO - we still need some tests for some of the more complex methods.
 *
 * @version     $Revision: 1.3 $
 * @author      Morten Andersen, arosii_moa (at) users.sourceforge.net
 */
public abstract class VtableTestBase extends COMTestBase {

	/*
	 * the vtable indexes starts in 7 because the objects extends IDispatch,
	 * which uses index 0-2 for IUnknown methods and index 3-6 for
	 * the IDispatch methods.
	 */
	private static final int VT_PROP_SHORT_GET = 7;
	private static final int VT_PROP_SHORT_PUT = 8;

	private static final int VT_PROP_BOOLEAN_GET = 9;
	private static final int VT_PROP_BOOLEAN_PUT = 10;

	private static final int VT_PROP_BSTR_GET = 11;
	private static final int VT_PROP_BSTR_PUT = 12;

	private static final int VT_PROP_DATE_GET = 13;
	private static final int VT_PROP_DATE_PUT = 14;

	private static final int VT_PROP_DOUBLE_GET = 15;
	private static final int VT_PROP_DOUBLE_PUT = 16;

	private static final int VT_PROP_FLOAT_GET = 17;
	private static final int VT_PROP_FLOAT_PUT = 18;

	private static final int VT_PROP_SHORT_INDEX_GET = 24;
	private static final int VT_PROP_SHORT_INDEX_PUT = 25;
	
	private static final int VT_TEST_NULLPOINTER_EXCEPTION = 37;
	private static final int VT_TEST_DIVBYZERO_EXCEPTION = 38;
	private static final int VT_TEST_STD_EXCEPTION = 39;

	protected IDualTestItf idtc;

	/**
	 * should be implemented by subclass to return the COMPtr used in the test.
	 */
	protected abstract IDualTestItf initCOMPtr() throws COMException;

	/**
	 * should be implemented by subclass to release the COMPtr used in the test.
	 */
	protected abstract void closeCOMPtr(IDualTestItf itf) throws COMException;

	/**
	 * should be implemented by subclass and return the thread model for the test.
	 */
	protected abstract COINIT getThreadModel();
	
	protected void setUp() throws Exception {
		super.setUp(getThreadModel());
		idtc = initCOMPtr();
	}

	protected void tearDown() throws Exception {
		closeCOMPtr(idtc);
		super.tearDown();
	}

	public void testPropShortVtable() throws Exception {
		short testValue = 117;

		// set the value
		try {
			NakedByteStream nbs = new NakedByteStream();
			LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
			//arg stream
			leos.writeInt(testValue);
			idtc.comInvoke(VT_PROP_SHORT_PUT, "I:H:", 4, nbs, null);
		} catch (IOException ioe) {
			throw new COMException(ioe);
		}

		// retrieve the value
		byte[] result = idtc.comInvoke(VT_PROP_SHORT_GET, "A:H:n4", 4, null, null);
		assertEquals(testValue, StructConverter.bytesIntoShort(result, 0));
	}

	private void booleanTester(boolean testValue) throws Exception {
		// set the value
		try {
			NakedByteStream nbs = new NakedByteStream();
			LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
			//arg stream
			leos.writeBoolean(testValue);
			// FIXME return to 'Z' and stacksize 4?
			idtc.comInvoke(VT_PROP_BOOLEAN_PUT, "Z:H:l", 4, nbs, null);
		} catch (IOException ioe) {
			throw new COMException(ioe);
		}

		// retrieve the value
		byte[] result = idtc.comInvoke(VT_PROP_BOOLEAN_GET, "A:H:n4", 4, null, null);
		assertEquals(testValue, StructConverter.bytesIntoBoolean(result, 0));
	}

	public void testPropBoolVtable() throws Exception {
		booleanTester(true);
		booleanTester(false);
	}

	public void testPropBSTRVtable() throws Exception {
		String testValue = "random string";

		// set the value
		try {
			NakedByteStream nbs = new NakedByteStream();
			LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
			//arg stream
			leos.writeStringUnicode(testValue);
			idtc.comInvoke(VT_PROP_BSTR_PUT, "G:H:l", 4, nbs, null);

			// retrieve the value
			byte[] result = idtc.comInvoke(VT_PROP_BSTR_GET, "A:H:O|g|", 4, null, null);
			LittleEndianInputStream leis = new LittleEndianInputStream(new ByteArrayInputStream(result));
			assertEquals(testValue, leis.readStringUnicode());
		} catch (IOException ioe) {
			throw new COMException(ioe);
		}
	}

	public void testPropDateVtable() throws Exception {
		Date testValue = new Date();

		// set the value
		try {
			NakedByteStream nbs = new NakedByteStream();
			LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
			//arg stream
			leos.writeDate(testValue);
			idtc.comInvoke(VT_PROP_DATE_PUT, "D:H:l", 8, nbs, null);

			// retrieve the value
			byte[] result = idtc.comInvoke(VT_PROP_DATE_GET, "M8:H:n8", 4, null, null);
			LittleEndianInputStream leis = new LittleEndianInputStream(new ByteArrayInputStream(result));
			assertEquals(testValue, leis.readDate());
		} catch (IOException ioe) {
			throw new COMException(ioe);
		}
	}

	public void testPropFloatVtable() throws Exception {
		float testValue = 11.3f;
		
		// set the value
		try {
			NakedByteStream nbs = new NakedByteStream();
			LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
			//arg stream
			leos.writeFloat(testValue);
			idtc.comInvoke(VT_PROP_FLOAT_PUT, "I:H:l", 4, nbs, null);
		} catch (IOException ioe) {
			throw new COMException(ioe);
		}
		
		// retrieve the value
		byte[] result = idtc.comInvoke(VT_PROP_FLOAT_GET, "A:H:n4", 4, null, null);
		assertEquals(testValue, StructConverter.bytesIntoFloat(result, 0), 0.0f);
	}

	public void testPropDoubleVtable() throws Exception {
		double testValue = 12.3;
		
		// set the value
		try {
			NakedByteStream nbs = new NakedByteStream();
			LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
			//arg stream
			leos.writeDouble(testValue);
			idtc.comInvoke(VT_PROP_DOUBLE_PUT, "D:H:l", 8, nbs, null);
		} catch (IOException ioe) {
			throw new COMException(ioe);
		}
		
		// retrieve the value
		byte[] result = idtc.comInvoke(VT_PROP_DOUBLE_GET, "M8:H:n8", 4, null, null);
		assertEquals(testValue, StructConverter.bytesIntoDouble(result, 0), 0.0);
	}

	public void testPropShortIndexVtable() throws Exception {
		short testValue = 118;
		short testIndex = 3;

		// set the value
		try {
			NakedByteStream nbs = new NakedByteStream();
			LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
			//arg stream
			leos.writeInt(testIndex);
			leos.writeInt(testValue);
			idtc.comInvoke(VT_PROP_SHORT_INDEX_PUT, "II:H:", 8, nbs, null);

			// retrieve the value
			nbs = new NakedByteStream();
			leos = new LittleEndianOutputStream(nbs);
			//arg stream
			leos.writeInt(testIndex);
			byte[] result = idtc.comInvoke(VT_PROP_SHORT_INDEX_GET, "IA:H:ln4", 8, nbs, null);
			assertEquals(testValue, StructConverter.bytesIntoShort(result, 0));
		} catch (IOException ioe) {
			throw new COMException(ioe);
		}
	}
	
	public void testNullPointerException() throws Exception {
		try {
			idtc.comInvoke(VT_TEST_NULLPOINTER_EXCEPTION, "", 0, null, null);
			fail("COM Error/Exception excepted");
		} catch (COMError e) { /* expected */
		} catch (COMException e) { /* expected */ }
	}

	public void testDivByZeroException() throws Exception {
		try {
			idtc.comInvoke(VT_TEST_DIVBYZERO_EXCEPTION, "", 0, null, null);
			fail("COM Error/Exception excepted");
		} catch (COMError e) { /* expected */
		} catch (COMException e) { /* expected */ }
	}

	public void testStdException() throws Exception {
		try {
			idtc.comInvoke(VT_TEST_STD_EXCEPTION, "", 0, null, null);
			fail("COM Error/Exception excepted");
		} catch (COMError e) { /* expected */
		} catch (COMException e) { /* expected */ }
	}
}

⌨️ 快捷键说明

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