1761727 converted unit test programs to JUnit tests and updated the build targets
This commit is contained in:
@@ -1,98 +1,98 @@
|
||||
package com.jacob.test.vbscript;
|
||||
|
||||
import com.jacob.com.*;
|
||||
import com.jacob.test.BaseTestCase;
|
||||
import com.jacob.activeX.*;
|
||||
|
||||
/**
|
||||
* This example demonstrates how to make calls between
|
||||
* two different STA's.
|
||||
* First, to create an STA, you need to extend the STA class
|
||||
* and override its OnInit() method. This method will be called
|
||||
* in the STA's thread so you can use it to create your COM
|
||||
* components that will run in that STA.
|
||||
* If you then try to call methods on those components from other
|
||||
* threads (STA or MTA) - this will fail.
|
||||
* You cannot create a component in an STA and call its methods
|
||||
* from another thread.
|
||||
* You can use the DispatchProxy to get a proxy to any Dispatch
|
||||
* that lives in another STA. This object has to be created in the
|
||||
* STA that houses the Dispatch (in this case it's created in the
|
||||
* OnInit method). Then, another thread can call the toDispatch()
|
||||
* method of DispatchProxy to get a local proxy. At most ONE (!)
|
||||
* thread can call toDispatch(), and the call can be made only once.
|
||||
* This is because a IStream object is used to pass the proxy, and
|
||||
* it is only written once and closed when you read it.
|
||||
* If you need multiple threads to access a Dispatch pointer, then
|
||||
* create that many DispatchProxy objects.
|
||||
* This example demonstrates how to make calls between two different STA's.
|
||||
* First, to create an STA, you need to extend the STA class and override its
|
||||
* OnInit() method. This method will be called in the STA's thread so you can
|
||||
* use it to create your COM components that will run in that STA. If you then
|
||||
* try to call methods on those components from other threads (STA or MTA) -
|
||||
* this will fail. You cannot create a component in an STA and call its methods
|
||||
* from another thread. You can use the DispatchProxy to get a proxy to any
|
||||
* Dispatch that lives in another STA. This object has to be created in the STA
|
||||
* that houses the Dispatch (in this case it's created in the OnInit method).
|
||||
* Then, another thread can call the toDispatch() method of DispatchProxy to get
|
||||
* a local proxy. At most ONE (!) thread can call toDispatch(), and the call can
|
||||
* be made only once. This is because a IStream object is used to pass the
|
||||
* proxy, and it is only written once and closed when you read it. If you need
|
||||
* multiple threads to access a Dispatch pointer, then create that many
|
||||
* DispatchProxy objects.
|
||||
* <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.
|
||||
*/
|
||||
class ScriptTest2 extends STA
|
||||
{
|
||||
public static ActiveXComponent sC;
|
||||
public static DispatchEvents de = null;
|
||||
public static Dispatch sControl = null;
|
||||
public static DispatchProxy sCon = null;
|
||||
|
||||
public boolean OnInit()
|
||||
{
|
||||
try
|
||||
{
|
||||
System.out.println("OnInit");
|
||||
System.out.println(Thread.currentThread());
|
||||
String lang = "VBScript";
|
||||
sC = new ActiveXComponent("ScriptControl");
|
||||
sControl = (Dispatch)sC.getObject();
|
||||
public class ScriptTest2 extends BaseTestCase {
|
||||
public void testScript2() {
|
||||
try {
|
||||
ComThread.InitSTA();
|
||||
ScriptTestSTA script = new ScriptTestSTA();
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ie){
|
||||
// should we get this?
|
||||
}
|
||||
|
||||
// sCon can be called from another thread
|
||||
sCon = new DispatchProxy(sControl);
|
||||
String scriptCommand = getSampleVPScriptForEval();
|
||||
// get a thread-local Dispatch from sCon
|
||||
Dispatch sc = script.sCon.toDispatch();
|
||||
|
||||
Dispatch.put(sControl, "Language", lang);
|
||||
ScriptTestErrEvents te = new ScriptTestErrEvents();
|
||||
de = new DispatchEvents(sControl, te);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// call a method on the thread-local Dispatch obtained
|
||||
// from the DispatchProxy. If you try to make the same
|
||||
// method call on the sControl object - you will get a
|
||||
// ComException.
|
||||
Variant result = Dispatch.call(sc, "Eval", scriptCommand);
|
||||
System.out.println("eval(" + scriptCommand + ") = " + result);
|
||||
script.quit();
|
||||
System.out.println("called quit");
|
||||
} catch (ComException e) {
|
||||
e.printStackTrace();
|
||||
fail("caught exception"+e);
|
||||
} finally {
|
||||
Integer I = null;
|
||||
for (int i = 1; i < 1000000; i++) {
|
||||
I = new Integer(i);
|
||||
}
|
||||
System.out.println(I);
|
||||
ComThread.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnQuit()
|
||||
{
|
||||
System.out.println("OnQuit");
|
||||
}
|
||||
public class ScriptTestSTA extends STA {
|
||||
|
||||
public static void main(String args[]) throws Exception
|
||||
{
|
||||
try {
|
||||
ComThread.InitSTA();
|
||||
ScriptTest2 script = new ScriptTest2();
|
||||
Thread.sleep(1000);
|
||||
public DispatchEvents de = null;
|
||||
|
||||
// get a thread-local Dispatch from sCon
|
||||
Dispatch sc = sCon.toDispatch();
|
||||
public Dispatch sControl = null;
|
||||
|
||||
public DispatchProxy sCon = null;
|
||||
|
||||
public boolean OnInit() {
|
||||
try {
|
||||
System.out.println("OnInit");
|
||||
System.out.println(Thread.currentThread());
|
||||
String lang = "VBScript";
|
||||
sControl = new ActiveXComponent("ScriptControl");
|
||||
|
||||
// sCon can be called from another thread
|
||||
sCon = new DispatchProxy(sControl);
|
||||
|
||||
Dispatch.put(sControl, "Language", lang);
|
||||
ScriptTestErrEvents te = new ScriptTestErrEvents();
|
||||
de = new DispatchEvents(sControl, te);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnQuit() {
|
||||
System.out.println("OnQuit");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// call a method on the thread-local Dispatch obtained
|
||||
// from the DispatchProxy. If you try to make the same
|
||||
// method call on the sControl object - you will get a
|
||||
// ComException.
|
||||
Variant result = Dispatch.call(sc, "Eval", args[0]);
|
||||
System.out.println("eval("+args[0]+") = "+ result);
|
||||
script.quit();
|
||||
System.out.println("called quit");
|
||||
} catch (ComException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Integer I = null;
|
||||
for(int i=1;i<1000000;i++)
|
||||
{
|
||||
I = new Integer(i);
|
||||
}
|
||||
System.out.println(I);
|
||||
ComThread.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user