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

📄 varianttest.java

📁 java与windows的com桥,可以用来操作所有的com程序如word或者excel等
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.jacob.com;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

import com.jacob.test.BaseTestCase;

/**
 * runs through some of the get and set methods on Variant
 * 
 * <p>
 * May need to run with some command line options (including from inside
 * Eclipse). Look in the docs area at the Jacob usage document for command line
 * options.
 */
public class VariantTest extends BaseTestCase {

	/**
	 * This verifies that toJavaObject() works for all of the main data types
	 * when they exist as a byRef version.
	 * <p>
	 * It compares the toJavaObject() for a byref against the toJavaObject() for
	 * the regular.
	 * 
	 */
	public void testByRefToJavaObject() {
		Variant v = null;
		Variant vByRef = null;

		v = new Variant(new Float(53.3), false);
		vByRef = new Variant(new Float(53.3), true);
		if (!v.toJavaObject().equals(vByRef.toJavaObject())) {
			fail(v.toString() + " could not make type " + v.getvt() + " and "
					+ vByRef.getvt() + " java objects come out the same");
		}
		v = new Variant(new Double(53.3), false);
		vByRef = new Variant(new Double(53.3), true);
		if (!v.toJavaObject().equals(vByRef.toJavaObject())) {
			fail(v.toString() + " could not make type " + v.getvt() + " and "
					+ vByRef.getvt() + " java objects come out the same");
		}

		v = new Variant(new Boolean(true), false);
		vByRef = new Variant(new Boolean(true), true);
		if (!v.toJavaObject().equals(vByRef.toJavaObject())) {
			fail(v.toString() + " could not make type " + v.getvt() + " and "
					+ vByRef.getvt() + " java objects come out the same");
		}

		v = new Variant(new Integer(53), false);
		vByRef = new Variant(new Integer(53), true);
		if (!v.toJavaObject().equals(vByRef.toJavaObject())) {
			fail(v.toString() + " could not make type " + v.getvt() + " and "
					+ vByRef.getvt() + " java objects come out the same");
		}

		v = new Variant(new Short((short) 53), false);
		vByRef = new Variant(new Short((short) 53), true);
		if (!v.toJavaObject().equals(vByRef.toJavaObject())) {
			fail(v.toString() + " could not make type " + v.getvt() + " and "
					+ vByRef.getvt() + " java objects come out the same");
		}

		v = new Variant("53.33", false);
		vByRef = new Variant("53.33", true);
		if (!v.toJavaObject().equals(vByRef.toJavaObject())) {
			fail(v.toString() + " could not make type " + v.getvt() + " and "
					+ vByRef.getvt() + " java objects come out the same");
		}

		// Ugh, you have to pick a magic number whose scale is less than 28
		// 53.53 had a scale of 64 and 53.52 had a scale of 47
		BigDecimal testDecimal = new BigDecimal(53.50);
		v = new Variant(testDecimal, false);
		vByRef = new Variant(testDecimal, true);
		if (!v.toJavaObject().equals(vByRef.toJavaObject())) {
			fail(v.toString() + " could not make type " + v.getvt() + " and "
					+ vByRef.getvt() + " java objects come out the same");
		}

		Date now = new Date();
		v = new Variant(now, false);
		vByRef = new Variant(now, true);
		if (!v.toJavaObject().equals(vByRef.toJavaObject())) {
			fail(v.toString() + " could not make type " + v.getvt() + " and "
					+ vByRef.getvt() + " java objects come out the same");
		}
	}

	/**
	 * try and test VT_I8. This should only work on 64 bit machines
	 */
	public void testLong() {
		Variant v = null;
		Variant vByRef = null;

		long longNumber = 1L << 40;
		v = new Variant(new Long(longNumber), false);
		vByRef = new Variant(new Long(longNumber), true);
		assertEquals("Could recover long number " + longNumber, v.getLong(),
				longNumber);
		assertEquals("Could not make long number " + longNumber
				+ " come out the same for get and getByRef()",
				v.toJavaObject(), vByRef.toJavaObject());
		v = new Variant("" + longNumber);
		v.changeType(Variant.VariantLongInt);
		assertEquals("Conversion from string to long didn't work ",
				v.getLong(), longNumber);
	}

	/**
	 * do some testing around currencies
	 */
	public void testCurrencyHandling() {
		Variant v = null;
		Variant vByRef = null;

		// need to do currency also
		// currency is an integer scaled up by 10,000 to give 4 digits to the
		// right of the decimal
		int currencyScale = 10000;
		long twentyThousand = 20000 * currencyScale;
		Currency twentyThousandAsCurrency = new Currency(twentyThousand);
		v = new Variant(twentyThousandAsCurrency, false);
		vByRef = new Variant(twentyThousandAsCurrency, true);
		if (!(v.toJavaObject() instanceof Currency)) {
			fail("v.toJavaObject was not Long for currency but was: "
					+ v.toJavaObject());
		}
		if (!v.toJavaObject().equals(vByRef.toJavaObject())) {
			fail(v.toString() + " could not make type " + v.getvt() + " and "
					+ vByRef.getvt() + " java objects come out the same");
		}
		long twentyThousandDotSeven = twentyThousand + 700;
		Currency twentyThousandDotSevenAsCurrency = new Currency(
				twentyThousandDotSeven);
		// use the primitive constructor
		v = new Variant(twentyThousandDotSevenAsCurrency);
		assertEquals("failed test with " + twentyThousandDotSeven,
				twentyThousandDotSeven, v.getCurrency().longValue());

	}

	/**
	 * 4/2007 bug report toObject on dispatch tries to call getDispatchRef
	 * instead of getDispatch so toString() on dispatch blows up.
	 * 
	 */
	public void testDispatchToJavaObject() {
		Variant v2 = new Variant();
		v2.putNothing();
		// this test fails even though the exact same code below works fine
		// v2.toJavaObject();
	}

	/**
	 * see what happens when we conver to by ref
	 * 
	 */
	public void testSomeChangeVT() {
		Variant v;
		// the code shows e shouldn't need to use a returned Variant but the
		// test says we do
		Variant vConverted;
		v = new Variant(53.3);
		short originalVT = v.getvt();
		short modifier;

		modifier = Variant.VariantShort;
		vConverted = v.changeType(modifier);
		if (vConverted.getvt() != modifier) {
			fail("Failed to change Variant " + originalVT + " using mask "
					+ modifier + " resulted in " + vConverted.getvt());
		}

		modifier = Variant.VariantString;
		vConverted = v.changeType(modifier);
		if (vConverted.getvt() != modifier) {
			fail("Failed to change Variant " + originalVT + " using mask "
					+ modifier + " resulted in " + vConverted.getvt());
		}

		// can't convert to byref!
		modifier = Variant.VariantByref | Variant.VariantShort;
		vConverted = v.changeType(modifier);
		if (vConverted.getvt() == modifier) {
			fail("Should not have been able to change Variant " + originalVT
					+ " using mask " + modifier + " resulted in "
					+ vConverted.getvt());
		}
	}

	/**
	 * make sure variant with no backing store works.
	 * 
	 */
	public void testUninitializedVariant() {
		Variant v;
		// Variants created without parameters are auto set to VariantEmpty
		v = new Variant();
		try {
			if (v.getvt() == Variant.VariantEmpty) {
				// successful
				// System.out.println("Variant initialized without parameters
				// correctly set to empty");
			} else {
				throw new RuntimeException(
						"getvt() on uninitialized variant shoud have returned VariantEmpty, instead returned "
								+ v.getvt());
			}
		} catch (IllegalStateException ise) {
			throw new RuntimeException(
					"getvt() on uninitialized variant shoud have succeeded, but instead threw exception");
		}
		try {
			v.toString();
		} catch (IllegalStateException ise) {
			fail("toString() should never throw a runtime exception");
			throw new RuntimeException(
					"toString() should not blow up even with uninitialized Variant");
		}

	}

	/**
	 * 
	 * verify the toString() method does not do type conversion
	 */
	public void testToStringDoesNotConvert() {
		Variant v;
		v = new Variant(true);
		v.toString();
		if (v.getvt() != Variant.VariantBoolean) {
			throw new RuntimeException(
					"toString() converted boolean to something else");
		} else {
			// fail("toString() correctly does not convert type");
		}
		if (v.getBoolean() != true) {
			fail("toString() converted boolean true to " + v.getBoolean());
		}
		v = new Variant(false);
		v.toString();
		if (v.getvt() != Variant.VariantBoolean) {
			throw new RuntimeException(
					"toString() converted boolean to something else");
		} else {
			// fail("toString() correctly does not convert type");
		}
		if (v.getBoolean() != false) {
			fail("toString() converted boolean false to " + v.getBoolean());
		}
	}

	/**
	 * Verify that booleans can be released. Part of the suite that checks all
	 * types.
	 */
	public void testSafeReleaseBoolean() {
		Variant v;
		v = new Variant(true);
		// System.out.println("Newly created Variant ("+ v.getBoolean()+") "+
		// "trying to create access violation but it doesn't seem to be easy");
		v.safeRelease();
		try {
			v.getBoolean();
			fail("IllegalStateException should have been thrown when querying safeReleased object");
			throw new RuntimeException("test failed");
		} catch (IllegalStateException ise) {
			// System.out.println("IllegalStateException correctly thrown after
			// safeRelease");
		}
		v = new Variant(true);
		for (int i = 0; i < 10; i++) {
			new Variant("xxx" + i);
			new Variant(i);
			new Variant("yyy" + i);
		}
		ComThread.Release();
		try {
			v.getBoolean();
			fail("IllegalStateException should have been thrown when querying ComThread.Release");
			throw new RuntimeException("test failed");
		} catch (IllegalStateException ise) {
			// System.out.println("IllegalStateException correctly thrown after
			// ComThread.Release");
		}
	}

	/**
	 * verify the constant values aren't released with safeRelease
	 * 
	 */
	public void testSafeReleaseConstant() {
		// System.out.println("Using Static constant Variant - should never
		// throw access violation");
		Variant.VT_TRUE.safeRelease();
		if (Variant.VT_TRUE.getBoolean() != true) {
			fail("VT_TRUE has been broken by SafeRelease()");
			throw new RuntimeException("test failed");
		} else {
			// System.out.println("VT_TRUE survived SafeRelease()");
		}

		for (int i = 0; i < 10; i++) {
			new Variant("xxx" + i);
			new Variant(i);
			new Variant("yyy" + i);
		}
		ComThread.Release();

		if (Variant.VT_TRUE.getBoolean() != true) {
			fail("VT_TRUE has been broken by ComThread.Release()");
			throw new RuntimeException("test failed");
		} else {
			// System.out.println("VT_TRUE survived ComThread.Release()");
		}

	}

	/**
	 * this used to try and and create an access violation but that didn't work
	 * and now the methods on the Variant are smarter about working after a
	 * release
	 * 
	 */
	public void testSafeReleaseString() {
		String mTestString = "Guitar Hero";
		Variant v = new Variant(mTestString);
		// System.out.println("Newly created Variant ("+ v.getString()+") "+
		// "about to safe release and then access");
		v.safeRelease();
		try {

⌨️ 快捷键说明

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