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

📄 dispatchtestbase.java

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

package org.jawin;

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

import org.jawin.constants.DispatchConstants;
import org.jawin.io.LittleEndianInputStream;
import org.jawin.io.LittleEndianOutputStream;
import org.jawin.io.NakedByteStream;
import org.jawin.marshal.GenericStub;
import org.jawin.win32.COINIT;

/**
 * base testcase class for Dispatch style tests. Compare this with the Vtable based
 * tests in {@link org.jawin.VtableTestBase}.
 *
 * @version     $Revision: 1.4 $
 * @author      Stuart Halloway, http://www.relevancellc.com/halloway/weblog/
 */
public abstract class DispatchTestBase extends COMTestBase {

	protected IDualTestItf idtc;

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

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

	/**
	 * should be implemented by subclass and return the thread model for the test.
	 */
	protected abstract COINIT getThreadModel();
	
	public void testPropShortDispatch() throws Exception {
		idtc.put("propShort", new Short((short)117));
		assertTrue(117 == ((Short)idtc.get("propShort")).shortValue());
	}

	public void testPropFloatDispatch() throws Exception {
		idtc.put("propFloat", new Float(11.3f));
		assertTrue(11.3f == ((Float)idtc.get("propFloat")).floatValue());
	}

	public void testPropDoubleDispatch() throws Exception {
		idtc.put("propDouble", new Double(12.3));
		assertTrue(12.3 == ((Double)idtc.get("propDouble")).doubleValue());
	}

	public void testPropShortIndexDispatch() throws Exception {
		idtc.put("propShortIndexed", new Short((short)3), new Short((short)118));
		assertTrue(118 == ((Short)idtc.get("propShortIndexed", new Short((short)3))).shortValue());
	}

	public void testStringConcat() throws Exception {
		assertTrue("onetwo".equals((String)idtc.invoke("concat", stringArgs)));
	}

	public void testGreatExpectation() throws Exception {
		idtc.invoke("greatExpectations", expectedArgs);
	}

	public void testSumInts() throws Exception {
		assertTrue(15 == ((Integer)idtc.invoke("sumInts", intArgs)).intValue());
	}

	public void testSumShorts() throws Exception {
		assertTrue(-15 == ((Short)idtc.invoke("sumShorts", shortArgs)).shortValue());
	}

	public void testCountTrues() throws Exception {
		assertTrue(3 == ((Integer)idtc.invoke("countTrues", boolArgs)).intValue());
	}

	public void testPropDate() throws Exception {
		Date now = new Date();
		idtc.put("propDate", now);
		assertTrue(now.equals(idtc.get("propDate")));
	}

	public void testPropInts() throws Exception {
		assertTrue(Arrays.equals(intArgs, (int[])idtc.get("propInts")));
	}

	public void testPropStrings() throws Exception {
		assertTrue(Arrays.equals(stringArgs, (String[])idtc.get("propStrings")));
	}

	public void testVariantArray() throws Exception {
		Object[] data = (Object[])idtc.get("propVariantArray");
		assertEquals(data.length, 2);
		assertEquals(data[0], "hello");
		assertEquals(data[1], new Double(123.0));
		idtc.put("propVariantArray", data);
	}

	public void testMultiplySingleIntArray() throws Exception {
		Object out = new Variant.ByrefHolder(new int[0]);

		int[] expOut = { 7 , 14 , 21 , 28 , 35 };
		
		NakedByteStream nbs = new NakedByteStream();
		LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
		
		Variant.writeObject(out, leos);
		Variant.writeObject(intArgs, leos);
		Variant.writeObject(new Integer(7), leos);
		
		byte[] result = GenericStub.dispatchInvoke("vvv:H:VL16L16", 3, nbs.size(), nbs.getInternalBuffer(), "multiplySingleIntArray", DispatchConstants.DISPATCH_METHOD, idtc.getPeer(), idtc.getUnknown());
		LittleEndianInputStream leis = new LittleEndianInputStream(new ByteArrayInputStream(result));
		int[] out2 = (int[])Variant.readObject(leis);
		assertTrue(Arrays.equals(out2, expOut));
	}

	public void testMultiplySingleIntArrayInvoke() throws Exception {
		Variant.ByrefHolder out = new Variant.ByrefHolder(new int[0]);
		int[] expOut = { 7 , 14 , 21 , 28 , 35 };
		
		idtc.invokeN("multiplySingleIntArray", new Object[] { new Integer(7) , intArgs , out } );
		assertTrue(Arrays.equals((int[])out.getRef(), expOut));
	}

	public void testMultiplySeveralIntArrays() throws Exception {
		int[] intArgs2 = { 11 , 12 , 13 , 14 , 15 , 16 , 17 };
		Object out = new Variant.ByrefHolder(new int[0]);
		Object out2 = new Variant.ByrefHolder(new int[0]);

		int[] expOut = { 7 , 14 , 21 , 28 , 35 };
		int[] expOut2 = { 77 , 84 , 91 , 98 , 105 , 112 , 119 };
		
		NakedByteStream nbs = new NakedByteStream();
		LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
		
		Variant.writeObject(out2, leos);
		Variant.writeObject(intArgs2, leos);
		Variant.writeObject(out, leos);
		Variant.writeObject(intArgs, leos);
		Variant.writeObject(new Integer(7), leos);
		
		byte[] result = GenericStub.dispatchInvoke("vvvvv:H:VL16VL16L16", 5, nbs.size(), nbs.getInternalBuffer(), "multiplySeveralIntArrays", DispatchConstants.DISPATCH_METHOD, idtc.getPeer(), idtc.getUnknown());
		LittleEndianInputStream leis = new LittleEndianInputStream(new ByteArrayInputStream(result));
		int[] mout2 = (int[])Variant.readObject(leis);
		int[] mout = (int[])Variant.readObject(leis);
		assertTrue(Arrays.equals(mout, expOut));
		assertTrue(Arrays.equals(mout2, expOut2));
	}

	public void testMultiplySeveralIntArraysInvoke() throws COMException {
		int[] intArgs2 = { 11 , 12 , 13 , 14 , 15 , 16 , 17 };
		Variant.ByrefHolder out = new Variant.ByrefHolder(new int[0]);
		Variant.ByrefHolder out2 = new Variant.ByrefHolder(new int[0]);

		int[] expOut = { 7 , 14 , 21 , 28 , 35 };
		int[] expOut2 = { 77 , 84 , 91 , 98 , 105 , 112 , 119 };
		
		idtc.invokeN("multiplySeveralIntArrays", new Object[] {
			new Integer(7) , intArgs , out , intArgs2 , out2
		} );
		
		assertTrue(Arrays.equals((int[])out.getRef(), expOut));
		assertTrue(Arrays.equals((int[])out2.getRef(), expOut2));
	}

	public void testOutInt() throws COMException {
		NakedByteStream nbs = new NakedByteStream();
		LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
		Variant.writeObject(new Variant.ByrefHolder(new Integer(0)), leos);
		Variant.writeObject(new Integer(7), leos);
		
		byte[] result = GenericStub.dispatchInvoke("vv:H:VL16", 2, nbs.size(), nbs.getInternalBuffer(), "outInt", DispatchConstants.DISPATCH_METHOD, idtc.getPeer(), idtc.getUnknown());
		LittleEndianInputStream leis = new LittleEndianInputStream(new ByteArrayInputStream(result));
		int out = ((Integer)Variant.readObject(leis)).intValue();
		assertEquals(14, out);
	}

	public void testOutIntInvoke() throws COMException {
		Variant.ByrefHolder out = new Variant.ByrefHolder(new Integer(0));
		idtc.invoke("outInt", new Integer(7), out);
		assertEquals(14, ((Integer)out.getRef()).intValue());
	}

	/**
	 * testing a safearray with 100 elements, each element a new SafeArray(int) with 100
	 * integers.
	 */
	public void testMultiplyEmbeddedSafeArraysInvoke() throws COMException {
		int dim1 = 100;
		int dim2 = 100;
		Object[] matrix = new Object[dim1];
		int[] expOut = new int[dim2];
		for (int i = 0; i < dim1; i++) {
			matrix[i] = new int[dim2];
			for (int j = 0; j < dim2; j++) {
				((int[])matrix[i])[j] = i + j;
				expOut[j] += i + j;
			}
		}
		
		int[] res = (int[])idtc.invoke("multiplyEmbeddedSafeArrays", matrix, new Integer(dim2));
		assertTrue(Arrays.equals(res, expOut)); 
	}

	/**
	 * testing 10 * 10 * 10 * 10 embedded safearrays, each containing 20 ints.
	 * Gives a NakedByteStream of around 1 mb. We actually perform pretty well.
	 */
	public void testMultiplyEmbeddedSafeArrays5DimsInvoke() throws COMException {
		int dimSizes = 10;
		int resDim = 20;
		Object[][][][] matrix = new Object[dimSizes][dimSizes][dimSizes][dimSizes];
		int[] expOut = new int[resDim];
		for (int i = 0; i < dimSizes; i++) {
			for (int j = 0; j < dimSizes; j++) {
				for (int k = 0; k < dimSizes; k++) {
					for (int l = 0; l < dimSizes; l++) {
						matrix[i][j][k][l] = new int[resDim];
						for (int m = 0; m < resDim; m++) {
							((int[])matrix[i][j][k][l])[m] = i + j + k + l + m;
							expOut[m] += i + j + k + l + m;
						}
					}
				}
			}
		}
		
		int[] res = (int[])idtc.invoke("multiplyEmbeddedSafeArrays", matrix, new Integer(resDim));
		assertTrue(Arrays.equals(res, expOut)); 
	}

	public void testNullPointerException() throws Exception {
		try {
			idtc.invoke("testNullPointerException");
			fail("COM Error/Exception excepted");
		} catch (COMError e) { /* expected */
		} catch (COMException e) { /* expected */ }
	}

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

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

	protected void setUp() throws Exception {
		super.setUp(getThreadModel());
		idtc = initDisp();
	}

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

}

⌨️ 快捷键说明

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