use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.
the class VariantTest method testVariantConstructors.
public void testVariantConstructors() {
VARIANT variant;
// skipped: BSTRByReference constructor
// skipped: empty constructor
// skipped: pointer constructor
// skipped: IDispatch constructor
String testString = "TeST$รถ";
BSTR bstr = OleAuto.INSTANCE.SysAllocString(testString);
variant = new VARIANT(bstr);
assertThat(variant.getValue(), instanceOf(BSTR.class));
assertThat(((BSTR) variant.getValue()).getValue(), equalTo(testString));
assertThat(variant.stringValue(), equalTo(testString));
variant = new VARIANT(testString);
assertThat(variant.getValue(), instanceOf(BSTR.class));
assertThat(((BSTR) variant.getValue()).getValue(), equalTo(testString));
assertThat(variant.stringValue(), equalTo(testString));
OleAuto.INSTANCE.SysFreeString(bstr);
OleAuto.INSTANCE.SysFreeString((BSTR) variant.getValue());
BOOL boolTrue = new WinDef.BOOL(true);
variant = new VARIANT(Variant.VARIANT_TRUE);
assertThat(variant.getValue(), instanceOf(VARIANT_BOOL.class));
assertThat(((VARIANT_BOOL) variant.getValue()).shortValue(), equalTo((short) 0xFFFF));
assertThat(variant.booleanValue(), equalTo(true));
variant = new VARIANT(boolTrue);
assertThat(variant.getValue(), instanceOf(VARIANT_BOOL.class));
assertThat(((VARIANT_BOOL) variant.getValue()).shortValue(), equalTo((short) 0xFFFF));
assertThat(variant.booleanValue(), equalTo(true));
int testInt = 4223;
LONG testIntWin = new LONG(testInt);
variant = new VARIANT(testIntWin);
assertThat(variant.getValue(), instanceOf(LONG.class));
assertThat(((LONG) variant.getValue()).intValue(), equalTo(testInt));
assertThat(variant.intValue(), equalTo(testInt));
variant = new VARIANT(testInt);
assertThat(variant.getValue(), instanceOf(LONG.class));
assertThat(((LONG) variant.getValue()).intValue(), equalTo(testInt));
assertThat(variant.intValue(), equalTo(testInt));
short testShort = 23;
SHORT testShortWin = new SHORT(testShort);
variant = new VARIANT(testShortWin);
assertThat(variant.getValue(), instanceOf(SHORT.class));
assertThat(((SHORT) variant.getValue()).shortValue(), equalTo(testShort));
assertThat(variant.shortValue(), equalTo(testShort));
variant = new VARIANT(testShort);
assertThat(variant.getValue(), instanceOf(SHORT.class));
assertThat(((SHORT) variant.getValue()).shortValue(), equalTo(testShort));
assertThat(variant.shortValue(), equalTo(testShort));
long testLong = 4223L + Integer.MAX_VALUE;
variant = new VARIANT(testLong);
assertThat(variant.getValue(), instanceOf(LONGLONG.class));
assertThat(((LONGLONG) variant.getValue()).longValue(), equalTo(testLong));
assertThat(variant.longValue(), equalTo(testLong));
Date testDate = new Date(2042 - 1900, 2, 3, 23, 0, 0);
variant = new VARIANT(testDate);
assertThat(variant.getValue(), instanceOf(DATE.class));
assertThat(variant.dateValue(), equalTo(testDate));
byte testByte = 42;
BYTE testByteWin = new BYTE(testByte);
CHAR testByteWin2 = new CHAR(testByte);
variant = new VARIANT(testByte);
assertThat(variant.getValue(), instanceOf(BYTE.class));
assertThat(((BYTE) variant.getValue()).byteValue(), equalTo(testByte));
assertThat(variant.byteValue(), equalTo(testByte));
variant = new VARIANT(testByteWin);
assertThat(variant.getValue(), instanceOf(BYTE.class));
assertThat(((BYTE) variant.getValue()).byteValue(), equalTo(testByte));
assertThat(variant.byteValue(), equalTo(testByte));
variant = new VARIANT(testByteWin2);
assertThat(variant.getValue(), instanceOf(CHAR.class));
assertThat(((CHAR) variant.getValue()).byteValue(), equalTo(testByte));
assertThat(variant.byteValue(), equalTo(testByte));
variant = new VARIANT(testByteWin2);
assertThat(variant.getValue(), instanceOf(CHAR.class));
assertThat(((CHAR) variant.getValue()).byteValue(), equalTo(testByte));
assertThat(variant.byteValue(), equalTo(testByte));
double testDouble = 42.23;
variant = new VARIANT(testDouble);
assertThat(variant.getValue(), instanceOf(Double.class));
// If this fails introduce comparison with range
assertThat(variant.doubleValue(), equalTo(testDouble));
float testFloat = 42.23f;
variant = new VARIANT(testFloat);
assertThat(variant.getValue(), instanceOf(Float.class));
// If this fails introduce comparison with range
assertThat(variant.floatValue(), equalTo(testFloat));
char testChar = 42 + Short.MAX_VALUE;
variant = new VARIANT(testChar);
assertThat(variant.getValue(), instanceOf(USHORT.class));
assertThat(((USHORT) variant.getValue()).intValue(), equalTo((int) testChar));
assertThat(variant.intValue(), equalTo((int) testChar));
}
use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.
the class IFileSystem3 method testConvertIntTypes.
@Test
public void testConvertIntTypes() {
LONG testLONG = new LONG(42);
VARIANT resultLONG = Convert.toVariant(testLONG);
assertEquals(42, resultLONG.longValue());
assertEquals(Integer.class, Convert.toJavaObject(resultLONG, Object.class, fact, false, false).getClass());
assertEquals(42, Convert.toJavaObject(resultLONG, int.class, fact, false, false));
assertEquals(42, Convert.toJavaObject(resultLONG, Integer.class, fact, false, false));
SHORT testSHORT = new SHORT(42);
VARIANT resultSHORT = Convert.toVariant(testSHORT);
assertEquals(42, resultSHORT.longValue());
assertEquals(Short.class, Convert.toJavaObject(resultSHORT, Object.class, fact, false, false).getClass());
assertEquals((short) 42, Convert.toJavaObject(resultSHORT, short.class, fact, false, false));
assertEquals((short) 42, Convert.toJavaObject(resultSHORT, Short.class, fact, false, false));
BYTE testBYTE = new BYTE(42);
VARIANT resultBYTE = Convert.toVariant(testBYTE);
Byte testByteObj = 42;
VARIANT resultByteObj = Convert.toVariant(testByteObj);
byte testByte = 42;
VARIANT resultByte = Convert.toVariant(testByte);
assertEquals(42, resultBYTE.longValue());
assertEquals(42, resultByteObj.longValue());
assertEquals(42, resultByte.longValue());
assertEquals(Byte.class, Convert.toJavaObject(resultBYTE, Object.class, fact, false, false).getClass());
assertEquals(Byte.class, Convert.toJavaObject(resultByteObj, Object.class, fact, false, false).getClass());
assertEquals(Byte.class, Convert.toJavaObject(resultByte, Object.class, fact, false, false).getClass());
assertEquals((byte) 42, Convert.toJavaObject(resultBYTE, byte.class, fact, false, false));
assertEquals((byte) 42, Convert.toJavaObject(resultByteObj, byte.class, fact, false, false));
assertEquals((byte) 42, Convert.toJavaObject(resultByte, byte.class, fact, false, false));
assertEquals((byte) 42, Convert.toJavaObject(resultBYTE, Byte.class, fact, false, false));
assertEquals((byte) 42, Convert.toJavaObject(resultByteObj, Byte.class, fact, false, false));
assertEquals((byte) 42, Convert.toJavaObject(resultByte, Byte.class, fact, false, false));
Character testCharObj = 42;
VARIANT resultCharObj = Convert.toVariant(testCharObj);
char testChar = 42;
VARIANT resultChar = Convert.toVariant(testChar);
assertEquals(42, resultCharObj.longValue());
assertEquals(42, resultChar.longValue());
assertEquals(testCharObj, (Character) Convert.toJavaObject(resultCharObj, Object.class, fact, false, false));
assertEquals(testCharObj, (Character) Convert.toJavaObject(resultChar, Object.class, fact, false, false));
assertEquals(testCharObj, (Character) Convert.toJavaObject(resultCharObj, char.class, fact, false, false));
assertEquals(testCharObj, (Character) Convert.toJavaObject(resultChar, char.class, fact, false, false));
assertEquals(testCharObj, (Character) Convert.toJavaObject(resultCharObj, Character.class, fact, false, false));
assertEquals(testCharObj, (Character) Convert.toJavaObject(resultChar, Character.class, fact, false, false));
CHAR testCHAR = new CHAR(42);
VARIANT resultCHAR = Convert.toVariant(testCHAR);
assertEquals(42, resultCHAR.longValue());
assertEquals((byte) 42, Convert.toJavaObject(resultCHAR, Object.class, fact, false, false));
assertEquals((byte) 42, Convert.toJavaObject(resultCHAR, byte.class, fact, false, false));
assertEquals((byte) 42, Convert.toJavaObject(resultCHAR, Byte.class, fact, false, false));
Short testShortObj = 42;
VARIANT resultShortObj = Convert.toVariant(testShortObj);
short testShort = 42;
VARIANT resultShort = Convert.toVariant(testShort);
assertEquals(42, resultShortObj.longValue());
assertEquals(42, resultShort.longValue());
assertEquals((short) 42, Convert.toJavaObject(resultShortObj, Object.class, fact, false, false));
assertEquals((short) 42, Convert.toJavaObject(resultShort, Object.class, fact, false, false));
assertEquals((short) 42, Convert.toJavaObject(resultShortObj, short.class, fact, false, false));
assertEquals((short) 42, Convert.toJavaObject(resultShort, short.class, fact, false, false));
assertEquals((short) 42, Convert.toJavaObject(resultShortObj, Short.class, fact, false, false));
assertEquals((short) 42, Convert.toJavaObject(resultShort, Short.class, fact, false, false));
Integer testIntegerObj = 42;
VARIANT resultIntegerObj = Convert.toVariant(testIntegerObj);
int testInteger = 42;
VARIANT resultInteger = Convert.toVariant(testInteger);
assertEquals(42, resultIntegerObj.longValue());
assertEquals(42, resultInteger.longValue());
assertEquals((int) 42, Convert.toJavaObject(resultIntegerObj, Object.class, fact, false, false));
assertEquals((int) 42, Convert.toJavaObject(resultInteger, Object.class, fact, false, false));
assertEquals((int) 42, Convert.toJavaObject(resultIntegerObj, int.class, fact, false, false));
assertEquals((int) 42, Convert.toJavaObject(resultInteger, int.class, fact, false, false));
assertEquals((int) 42, Convert.toJavaObject(resultIntegerObj, Integer.class, fact, false, false));
assertEquals((int) 42, Convert.toJavaObject(resultInteger, Integer.class, fact, false, false));
Long testLongObj = 42L;
VARIANT resultLongObj = Convert.toVariant(testLongObj);
long testLong = 42;
VARIANT resultLong = Convert.toVariant(testLong);
assertEquals(42, resultLongObj.longValue());
assertEquals(42, resultLong.longValue());
assertEquals((long) 42, Convert.toJavaObject(resultLongObj, Object.class, fact, false, false));
assertEquals((long) 42, Convert.toJavaObject(resultLong, Object.class, fact, false, false));
assertEquals((long) 42, Convert.toJavaObject(resultLongObj, long.class, fact, false, false));
assertEquals((long) 42, Convert.toJavaObject(resultLong, long.class, fact, false, false));
assertEquals((long) 42, Convert.toJavaObject(resultLongObj, Long.class, fact, false, false));
assertEquals((long) 42, Convert.toJavaObject(resultLong, Long.class, fact, false, false));
}
use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.
the class IFileSystem3 method testConvertString.
@Test
public void testConvertString() {
// This test leaks the allocated BSTR -- this is tollerated here, as memory usage is minimal
String testString = "Hallo";
BSTR testValue = new BSTR(testString);
VARIANT resultVariant = Convert.toVariant(testValue);
assertEquals(testString, resultVariant.stringValue());
assertEquals(testString, Convert.toJavaObject(resultVariant, Object.class, fact, false, false));
assertEquals(testString, Convert.toJavaObject(resultVariant, String.class, fact, false, false));
resultVariant = Convert.toVariant(testString);
assertEquals(testString, resultVariant.stringValue());
assertEquals(testString, Convert.toJavaObject(resultVariant, Object.class, fact, false, false));
assertEquals(testString, Convert.toJavaObject(resultVariant, String.class, fact, false, false));
}
use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.
the class IFileSystem3 method testConvertFloat.
@Test
public void testConvertFloat() {
Float testFloatObj = 42.23f;
VARIANT resultFloatObj = Convert.toVariant(testFloatObj);
float testFloat = 42.23f;
VARIANT resultFloat = Convert.toVariant(testFloat);
assertEquals(42.23f, resultFloatObj.floatValue(), 0.01);
assertEquals(42.23f, resultFloat.floatValue(), 0.01);
assertEquals(42.23d, resultFloat.doubleValue(), 0.01);
assertEquals(42.23f, (Float) Convert.toJavaObject(resultFloatObj, Object.class, fact, false, false), 0.01);
assertEquals(42.23f, (Float) Convert.toJavaObject(resultFloat, Object.class, fact, false, false), 0.01);
assertEquals(42.23f, (Float) Convert.toJavaObject(resultFloatObj, float.class, fact, false, false), 0.01);
assertEquals(42.23f, (Float) Convert.toJavaObject(resultFloat, float.class, fact, false, false), 0.01);
assertEquals(42.23f, (Float) Convert.toJavaObject(resultFloatObj, Float.class, fact, false, false), 0.01);
assertEquals(42.23f, (Float) Convert.toJavaObject(resultFloat, Float.class, fact, false, false), 0.01);
assertEquals(42.23d, (Double) Convert.toJavaObject(resultFloat, double.class, fact, false, false), 0.01);
Double testDoubleObj = 42.23;
VARIANT resultDoubleObj = Convert.toVariant(testDoubleObj);
double testDouble = 42.23;
VARIANT resultDouble = Convert.toVariant(testDouble);
assertEquals(42.23, resultDoubleObj.doubleValue(), 0.01);
assertEquals(42.23, resultDouble.doubleValue(), 0.01);
assertEquals(42.23f, resultDouble.floatValue(), 0.01);
assertEquals(42.23, (Double) Convert.toJavaObject(resultDoubleObj, Object.class, fact, false, false), 0.01);
assertEquals(42.23, (Double) Convert.toJavaObject(resultDouble, Object.class, fact, false, false), 0.01);
assertEquals(42.23, (Double) Convert.toJavaObject(resultDoubleObj, double.class, fact, false, false), 0.01);
assertEquals(42.23, (Double) Convert.toJavaObject(resultDouble, double.class, fact, false, false), 0.01);
assertEquals(42.23, (Double) Convert.toJavaObject(resultDoubleObj, Double.class, fact, false, false), 0.01);
assertEquals(42.23, (Double) Convert.toJavaObject(resultDouble, Double.class, fact, false, false), 0.01);
assertEquals(42.23f, (Float) Convert.toJavaObject(resultDouble, float.class, fact, false, false), 0.01);
}
use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.
the class SAFEARRAYTest method testSafeArrayPutGetElement.
@Test
public void testSafeArrayPutGetElement() throws Exception {
int rowCount = 2;
int colCount = 10;
SAFEARRAY varArray = SAFEARRAY.createSafeArray(rowCount, colCount);
assertThat(varArray.getDimensionCount(), is(2));
assertThat(varArray.getUBound(0), equalTo(rowCount - 1));
assertThat(varArray.getUBound(1), equalTo(colCount - 1));
for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) {
for (int colIdx = 0; colIdx < colCount; colIdx++) {
VARIANT variant = new VARIANT(rowIdx + "#" + colIdx);
varArray.putElement(variant, rowIdx, colIdx);
}
}
for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) {
for (int colIdx = 0; colIdx < colCount; colIdx++) {
VARIANT element = (VARIANT) varArray.getElement(rowIdx, colIdx);
assertEquals(rowIdx + "#" + colIdx, element.stringValue());
OleAuto.INSTANCE.VariantClear(element);
}
}
}
Aggregations