Search code examples
junitcomexceptionjawin

Jawin and JUnit


I am using Jawin to access a Windows application though COM. My code works fine. I've written a simple test in Java and it executes as expected. However, when I annotate this method using the @Test and execute it through the JUnit environment the code throws a COMException:

org.jawin.COMException: 80020009: Failed to get item[src=OMTE.Projects.6,guid={C9FF8402-BB2E-11D0-8475-0080C82BFA0C}]
at org.jawin.marshal.GenericStub.dispatchInvoke0(Native Method)
at org.jawin.marshal.GenericStub.dispatchInvoke(GenericStub.java:201)
at org.jawin.DispatchPtr.invokeN(DispatchPtr.java:587)
at org.jawin.DispatchPtr.invokeN(DispatchPtr.java:555)
at org.eclipse.epsilon.emc.ptcim.AttributeRelatedTests.getProject(AttributeRelatedTests.java:42)
at org.eclipse.epsilon.emc.ptcim.AttributeRelatedTests.testModelId(AttributeRelatedTests.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Java code:

public static void main (String args[]) {
        testModelId(); // try/catch was removed for better presentation
}

public static void testModelId() throws COMException {
    Ole32.CoInitialize();
    DispatchPtr theProject = getProject("Traffic Lights");
    DispatchPtr model = load(theProject);
    DispatchPtr rootItem = (DispatchPtr) model.invoke("Item", "Package", "Software");
    String strObjId = (String) rootItem.get("Property", "Id");
    assertEquals("80a27e73-0121-436a-abf7-1e01ebb33c7e", strObjId);
}


protected static DispatchPtr getProject(String name) throws COMException {
    Ole32.CoInitialize();
    DispatchPtr app = new DispatchPtr("OMTE.Projects");
    String Role = "Project";
    DispatchPtr dispPtr = (DispatchPtr) app.invokeN("Item", new Object[] {
            Role, name});
    DispatchPtr modelRef = new DispatchPtr();
    modelRef.stealUnknown(dispPtr);
    return modelRef;
}

// load method is not included as the exception is thrown at getProject method

JUnit code:

@Test
public void testModelId() throws COMException {
    Ole32.CoInitialize();
    DispatchPtr theProject = getProject("Traffic Lights");
    DispatchPtr model = load(theProject);
    DispatchPtr rootItem = (DispatchPtr) model.invoke("Item", "Package", "Software");
    String strObjId = (String) rootItem.get("Property", "Id");
    assertEquals("80a27e73-0121-436a-abf7-1e01ebb33c7e", strObjId);
}


protected static DispatchPtr getProject(String name) throws COMException {
    Ole32.CoInitialize();
    DispatchPtr app = new DispatchPtr("OMTE.Projects");
    String Role = "Project";
    DispatchPtr dispPtr = (DispatchPtr) app.invokeN("Item", new Object[] {
            Role, name});
    DispatchPtr modelRef = new DispatchPtr();
    modelRef.stealUnknown(dispPtr);
    return modelRef;
}

The first is run as Java application, the second as JUnit4 test configuration.


Solution

  • This is what I did and this is what worked for me:

    I tried running the same tests from a different machine using the JUnit run configuration and they ran successfully. There was one major difference between the two machines, i.e. the operating system (Windows 10 vs Windows 7). Both machines had Eclipse Mars 2 installed. Downgrading my OS (from Windows 10 that wasn't working to Windows 7 that was working) was an overkill so I decided to try a newer Eclipse version hoping that there was some incompatibility between Win 10 + Eclipse Mars 2 + JUnit which was fixed in newer versions. Indeed, the same tests run perfectly in Eclipse Neon in Windows 10.

    From the actions taken to solve the problem my understanding is that Eclipse Mars 2 + Windows 10 (64bit) + JUnit are, under specific circumstances, not compatible (these circumstances might or might not involve the use of library Jawin 2.0 alpha).