From 8b5c3a37b6e234cab1af3713d88fa406081a2b9f Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Sat, 25 Aug 2018 20:41:36 -0700 Subject: [PATCH] Update tests for new exceptions and methods --- .../platform/win32/{ => COM}/WbemcliTest.java | 105 ++++++------------ 1 file changed, 31 insertions(+), 74 deletions(-) rename contrib/platform/test/com/sun/jna/platform/win32/{ => COM}/WbemcliTest.java (56%) diff --git a/contrib/platform/test/com/sun/jna/platform/win32/WbemcliTest.java b/contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java similarity index 56% rename from contrib/platform/test/com/sun/jna/platform/win32/WbemcliTest.java rename to contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java index bfb9b370d7..2a1ef6daec 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/WbemcliTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java @@ -21,7 +21,7 @@ * A copy is also included in the downloadable source code package * containing JNA, in file "AL2.0". */ -package com.sun.jna.platform.win32; +package com.sun.jna.platform.win32.COM; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -36,10 +36,12 @@ import org.junit.Before; import org.junit.Test; +import com.sun.jna.platform.win32.Ole32; +import com.sun.jna.platform.win32.Variant; +import com.sun.jna.platform.win32.COM.COMException; import com.sun.jna.platform.win32.COM.COMUtils; import com.sun.jna.platform.win32.COM.Wbemcli; import com.sun.jna.platform.win32.COM.WbemcliUtil; -import com.sun.jna.platform.win32.COM.Wbemcli.WbemcliException; import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery; import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult; @@ -99,8 +101,8 @@ public void testWmiExceptions() { try { WbemcliUtil.queryWMI(processQuery); fail("Win32_ClassDoesNotExist does not exist."); - } catch (WbemcliException expected) { - assertEquals(Wbemcli.WBEM_E_INVALID_CLASS, expected.getErrorCode()); + } catch (COMException expected) { + assertEquals(Wbemcli.WBEM_E_INVALID_CLASS, expected.getHresult()); } // Valid class but properties don't match the class @@ -108,8 +110,8 @@ public void testWmiExceptions() { try { WbemcliUtil.queryWMI(processQuery); fail("Properties in the process enum aren't in Win32_OperatingSystem"); - } catch (WbemcliException expected) { - assertEquals(Wbemcli.WBEM_E_INVALID_QUERY, expected.getErrorCode()); + } catch (COMException expected) { + assertEquals(Wbemcli.WBEM_E_INVALID_QUERY, expected.getHresult()); } // Invalid namespace @@ -117,8 +119,8 @@ public void testWmiExceptions() { try { WbemcliUtil.queryWMI(processQuery); fail("This is an invalid namespace."); - } catch (WbemcliException expected) { - assertEquals(Wbemcli.WBEM_E_INVALID_NAMESPACE, expected.getErrorCode()); + } catch (COMException expected) { + assertEquals(Wbemcli.WBEM_E_INVALID_NAMESPACE, expected.getHresult()); } } @@ -137,56 +139,30 @@ public void testWmiProcesses() { assertTrue(processes.getResultCount() > 0); int lastProcessIndex = processes.getResultCount() - 1; - // PID is UINT32 = VT_I4, should only work with getInteger - assertTrue(processes.getInteger(ProcessProperty.PROCESSID, lastProcessIndex) >= 0); - // Other types should throw exception with error code VT_I4 = 3 - try { - processes.getString(ProcessProperty.PROCESSID, lastProcessIndex); - fail("VT_I4 type cannot be returned as a String"); - } catch (WbemcliException expected) { - assertEquals(Variant.VT_I4, expected.getErrorCode()); - } + // PID is UINT32 = VT_I4 + assertEquals(Variant.VT_I4, processes.getVtType(ProcessProperty.PROCESSID)); + assertTrue((Integer) processes.getValue(ProcessProperty.PROCESSID, lastProcessIndex) >= 0); - // WSS is UINT64 = STRING, should only work with getString and parse to - // a long - String wssStr = processes.getString(ProcessProperty.WORKINGSETSIZE, lastProcessIndex); + // WSS is UINT64 = STRING + assertEquals(Variant.VT_BSTR, processes.getVtType(ProcessProperty.WORKINGSETSIZE)); + String wssStr = (String) processes.getValue(ProcessProperty.WORKINGSETSIZE, lastProcessIndex); assertTrue(Long.parseLong(wssStr) > 0); - // Other types should throw exception with error code VT_BSTR = 8 - try { - processes.getInteger(ProcessProperty.WORKINGSETSIZE, lastProcessIndex); - fail("VT_BSTR type cannot be returned as an Integer"); - } catch (WbemcliException expected) { - assertEquals(Variant.VT_BSTR, expected.getErrorCode()); - } - // EXECUTIONSTATE is always null, should only work with getValue + // EXECUTIONSTATE is always null + assertEquals(Variant.VT_NULL, processes.getVtType(ProcessProperty.EXECUTIONSTATE)); Object state = processes.getValue(ProcessProperty.EXECUTIONSTATE, lastProcessIndex); assertNull(state); - // Other types should return defaults - assertEquals("", processes.getString(ProcessProperty.EXECUTIONSTATE, lastProcessIndex)); - assertEquals((Integer) 0, processes.getInteger(ProcessProperty.EXECUTIONSTATE, lastProcessIndex)); - assertEquals((Short) (short) 0, processes.getShort(ProcessProperty.EXECUTIONSTATE, lastProcessIndex)); - assertEquals((Byte) (byte) 0, processes.getByte(ProcessProperty.EXECUTIONSTATE, lastProcessIndex)); - assertEquals((Float) 0f, processes.getFloat(ProcessProperty.EXECUTIONSTATE, lastProcessIndex)); - assertEquals((Double) 0d, processes.getDouble(ProcessProperty.EXECUTIONSTATE, lastProcessIndex)); - assertEquals(Boolean.FALSE, processes.getBoolean(ProcessProperty.EXECUTIONSTATE, lastProcessIndex)); - - // CreationDate is DATETIME = STRING, should only work with getString + + // CreationDate is DATETIME = STRING // and be in CIM_DATETIME format yyyymmddhhmmss.mmmmmm+zzz - String cdate = processes.getString(ProcessProperty.CREATIONDATE, lastProcessIndex); + assertEquals(Variant.VT_BSTR, processes.getVtType(ProcessProperty.CREATIONDATE)); + String cdate = (String) processes.getValue(ProcessProperty.CREATIONDATE, lastProcessIndex); assertEquals(25, cdate.length()); assertEquals('.', cdate.charAt(14)); assertTrue(Integer.parseInt(cdate.substring(0, 4)) > 1970); assertTrue(Integer.parseInt(cdate.substring(4, 6)) <= 12); assertTrue(Integer.parseInt(cdate.substring(6, 8)) <= 31); - // Other types should throw exception with error code VT_BSTR = 8 - try { - processes.getFloat(ProcessProperty.CREATIONDATE, lastProcessIndex); - fail("VT_BSTR type cannot be returned as a Float"); - } catch (WbemcliException expected) { - assertEquals(Variant.VT_BSTR, expected.getErrorCode()); - } } @Test @@ -198,35 +174,16 @@ public void testWmiOperatingSystem() { // There has to be at least one os (this one!) assertTrue(os.getResultCount() > 0); - // ForegroundApplicationBoost is UINT8 = VT_UI1, should only work with - // getByte - assertTrue(os.getByte(OperatingSystemProperty.FOREGROUNDAPPLICATIONBOOST, 0) >= 0); - // Other types should throw exception with error code VT_UI1 = 17 - try { - os.getInteger(OperatingSystemProperty.FOREGROUNDAPPLICATIONBOOST, 0); - fail("VT_UI1 type cannot be returned as an Integer"); - } catch (WbemcliException expected) { - assertEquals(Variant.VT_UI1, expected.getErrorCode()); - } + // ForegroundApplicationBoost is UINT8 = VT_UI1 + assertEquals(Variant.VT_UI1, os.getVtType(OperatingSystemProperty.FOREGROUNDAPPLICATIONBOOST)); + assertTrue((Byte) os.getValue(OperatingSystemProperty.FOREGROUNDAPPLICATIONBOOST, 0) >= 0); - // OSTYPE is UINT16 = VT_I4, should only work with getInteger - assertTrue(os.getInteger(OperatingSystemProperty.OSTYPE, 0) >= 0); - // Other types should throw exception with error code VT_I4 = 3 - try { - os.getByte(OperatingSystemProperty.OSTYPE, 0); - fail("VT_I4 type cannot be returned as a Byte"); - } catch (WbemcliException expected) { - assertEquals(Variant.VT_I4, expected.getErrorCode()); - } + // OSTYPE is UINT16 = VT_I4 + assertEquals(Variant.VT_I4, os.getVtType(OperatingSystemProperty.OSTYPE)); + assertTrue((Integer) os.getValue(OperatingSystemProperty.OSTYPE, 0) >= 0); - // PRIMARY is BOOLEAN = VT_BOOL, should only work with getBoolean - assertNotNull(os.getValue(OperatingSystemProperty.PRIMARY, 0)); - // Other types should throw exception with error code VT_BOOL = 11 - try { - os.getDouble(OperatingSystemProperty.PRIMARY, 0); - fail("VT_BOOL type cannot be returned as a Double"); - } catch (WbemcliException expected) { - assertEquals(Variant.VT_BOOL, expected.getErrorCode()); - } + // PRIMARY is BOOLEAN = VT_BOOL + assertEquals(Variant.VT_BOOL, os.getVtType(OperatingSystemProperty.PRIMARY)); + assertNotNull((Boolean) os.getValue(OperatingSystemProperty.PRIMARY, 0)); } }