use of com.sun.jna.platform.win32.WTypes.BSTR 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.WTypes.BSTR 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.WTypes.BSTR in project jna by java-native-access.
the class SAFEARRAYTest method testDataTypes.
@Test
public void testDataTypes() {
int idx = 1;
Pointer dataPointer;
SAFEARRAY sa;
long elementSize;
Object[] objectResult;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_BOOL), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(2L));
dataPointer = sa.accessData();
sa.putElement(true, idx);
short[] shortResult = dataPointer.getShortArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Boolean) sa.getElement(idx), equalTo(true));
assertThat(shortResult[idx], equalTo((short) 0xFFFF));
assertThat((Short) dataPointer.getShort(idx * elementSize), equalTo((short) 0xFFFF));
assertThat((Boolean) objectResult[idx], equalTo(true));
sa.unaccessData();
sa.destroy();
byte testByte = 67;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UI1), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(1L));
dataPointer = sa.accessData();
sa.putElement(testByte, idx);
byte[] byteResult = dataPointer.getByteArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Byte) sa.getElement(idx), equalTo(testByte));
assertThat(dataPointer.getByte(idx * elementSize), equalTo(testByte));
assertThat(byteResult[idx], equalTo(testByte));
assertThat((Byte) objectResult[idx], equalTo(testByte));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_I1), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(1L));
dataPointer = sa.accessData();
sa.putElement(testByte, idx);
byteResult = dataPointer.getByteArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Byte) sa.getElement(idx), equalTo(testByte));
assertThat(dataPointer.getByte(idx * elementSize), equalTo(testByte));
assertThat(byteResult[idx], equalTo(testByte));
assertThat((Byte) objectResult[idx], equalTo(testByte));
sa.unaccessData();
sa.destroy();
short testShort = Short.MAX_VALUE - 1;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UI2), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(2L));
dataPointer = sa.accessData();
sa.putElement(testShort, idx);
shortResult = dataPointer.getShortArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Short) sa.getElement(idx), equalTo(testShort));
assertThat(dataPointer.getShort(idx * elementSize), equalTo(testShort));
assertThat(shortResult[idx], equalTo(testShort));
assertThat((Short) objectResult[idx], equalTo(testShort));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_I2), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(2L));
dataPointer = sa.accessData();
sa.putElement(testShort, idx);
shortResult = dataPointer.getShortArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Short) sa.getElement(idx), equalTo(testShort));
assertThat(dataPointer.getShort(idx * elementSize), equalTo(testShort));
assertThat(shortResult[idx], equalTo(testShort));
assertThat((Short) objectResult[idx], equalTo(testShort));
sa.unaccessData();
sa.destroy();
int testInt = Integer.MAX_VALUE - 1;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UI4), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
int[] intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_I4), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UINT), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_INT), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
SCODE testSCODE = new SCODE(47);
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_ERROR), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testSCODE, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((SCODE) sa.getElement(idx), equalTo(testSCODE));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(47));
assertThat(intResult[idx], equalTo(47));
assertThat((SCODE) objectResult[idx], equalTo(testSCODE));
sa.unaccessData();
sa.destroy();
float testFloat = 42.23f;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_R4), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testFloat, idx);
float[] floatResult = dataPointer.getFloatArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Float) sa.getElement(idx), equalTo(testFloat));
assertThat((Float) dataPointer.getFloat(idx * elementSize), equalTo(testFloat));
assertThat(floatResult[idx], equalTo(testFloat));
assertThat((Float) objectResult[idx], equalTo(testFloat));
sa.unaccessData();
sa.destroy();
double testDouble = 42.23d;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_R8), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(8L));
dataPointer = sa.accessData();
sa.putElement(testDouble, idx);
double[] doubleResult = dataPointer.getDoubleArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Double) sa.getElement(idx), equalTo(testDouble));
assertThat((Double) dataPointer.getDouble(idx * elementSize), equalTo(testDouble));
assertThat(doubleResult[idx], equalTo(testDouble));
assertThat((Double) objectResult[idx], equalTo(testDouble));
sa.unaccessData();
sa.destroy();
Date testDate = new Date(1923, 1, 1, 5, 0, 0);
DATE testDATE = new DATE(testDate);
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_DATE), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(8L));
dataPointer = sa.accessData();
sa.putElement(testDATE, idx);
doubleResult = dataPointer.getDoubleArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat(((DATE) sa.getElement(idx)).date, equalTo(testDATE.date));
assertThat((Double) dataPointer.getDouble(idx * elementSize), equalTo(testDATE.date));
assertThat(((DATE) sa.getElement(idx)).getAsJavaDate(), equalTo(testDate));
assertThat(doubleResult[idx], equalTo(testDATE.date));
assertThat((Date) objectResult[idx], equalTo(testDate));
sa.unaccessData();
sa.destroy();
String testString = "äöüßAE!";
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_BSTR), 2);
elementSize = sa.getElemsize();
dataPointer = sa.accessData();
sa.putElement(testString, idx);
Pointer[] pointerResult = dataPointer.getPointerArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat(((String) sa.getElement(idx)), equalTo(testString));
assertThat(new BSTR(dataPointer.getPointer(idx * elementSize)).getValue(), equalTo(testString));
assertThat(new BSTR(pointerResult[idx]).getValue(), equalTo(testString));
assertThat((String) objectResult[idx], equalTo(testString));
sa.unaccessData();
sa.destroy();
// VT_VARIANT is tested in testADODB
// untested: VT_UNKNOWN
// untested: VT_DISPATCH
// untested: VT_CY
// untested: VT_DECIMAL
// unsupported: VT_RECORD
}
use of com.sun.jna.platform.win32.WTypes.BSTR in project jna by java-native-access.
the class WTypesTest method testBSTRBasic.
public void testBSTRBasic() {
String demoString = "inputÖäÜ?!";
// Allocation via system and the "correct" way
BSTR sysAllocated = OleAuto.INSTANCE.SysAllocString(demoString);
// Java based allocation - not suitable if passed via automation
BSTR javaAllocated = new BSTR(demoString);
// Ensure encoding roundtripping works
assertEquals(demoString, sysAllocated.getValue());
assertEquals(demoString, javaAllocated.getValue());
// BSTR is encoded as UTF-16/UCS2, so byte length is 2 * char count
assertEquals(demoString.length(), OleAuto.INSTANCE.SysStringLen(sysAllocated));
assertEquals(demoString.length(), OleAuto.INSTANCE.SysStringLen(javaAllocated));
assertEquals(2 * demoString.length(), OleAuto.INSTANCE.SysStringByteLen(sysAllocated));
assertEquals(2 * demoString.length(), OleAuto.INSTANCE.SysStringByteLen(javaAllocated));
// The BSTR Pointer points 4 bytes into the data itself (beginning of data
// string, the 4 preceding bytes code the string length (in bytes)
assertEquals(2 * demoString.length(), sysAllocated.getPointer().getInt(-4));
assertEquals(2 * demoString.length(), javaAllocated.getPointer().getInt(-4));
OleAuto.INSTANCE.SysFreeString(sysAllocated);
// javaAllocated is allocated via Memory and will be freeed by the
// garbadge collector automaticly
}
use of com.sun.jna.platform.win32.WTypes.BSTR in project jna by java-native-access.
the class WTypesTest method testBSTRNullPointerHandling.
public void testBSTRNullPointerHandling() {
// Allocation from NULL should return NULL
BSTR sysAllocated = OleAuto.INSTANCE.SysAllocString(null);
assertNull(sysAllocated);
// MSDN states, that the BSTR from Nullpointer represents the string with
// zero characters
BSTR bstr = new BSTR(Pointer.NULL);
assertEquals("", bstr.getValue());
}
Aggregations