Home Corporate Contacts

TETware Knowledgebase


Products
Solutions

Information

Datasheet
Documentation
FAQ
Knowledgebase

Return to Knowledgebase Index

39. How to create a new thread in a Java API-conforming test case

 

Question

I have a test program looks like this:


public class test {
	public static void main(String args[]) {
	new testThread().start();
	}
}

class testThread extends Thread {
	public void run()
	{
	// the test program is here.
	}
}
How can I convert it to use the TETware Java API?

 

Answer

When you create a new thread in a program that uses the TETware Java API, instead of saying


class testThread extends Thread
you need to say

class testThread extends TetThread


Here is an example of a simple test case that creates a new thread:

import java.lang.*;
import TET.*;

/* * Test case for threads. */ public class ThrTest extends SimpleTestCase { public static final int SLEEP_TIME = 5;

public static void main(String[] args) { main("ThrTest", args, new ThrTest()); }

public void i1t1(TestSession ts) { TestThread thread;

thread = new TestThread(ts, 1, SLEEP_TIME); thread.start(); ts.tet_infoline("This is the parent thread: " + Thread.currentThread()); ts.tet_infoline("Parent printing child thread: " + thread);

try { thread.join(2 * SLEEP_TIME); } catch (InterruptedException e) { ts.tet_infoline("Caught exception \ while joining child thread:"); ts.tet_infoline(e.toString()); ts.tet_result(ts.TET_UNRESOLVED); }

ts.tet_infoline("parent thread returns"); ts.tet_result(ts.TET_PASS); }

}

class TestThread extends TetThread { private TestSession ts; private int sleepTime;

TestThread(TestSession ts, int id, int sleepTime) { super(ts, "TestThread() #" + id, sleepTime * 1000L); this.ts = ts; this.sleepTime = sleepTime; }

public void run() { ts.tet_infoline("Child thread (" + Thread.currentThread() + "): starting to run");

try { sleep(sleepTime * 1000L); } catch (InterruptedException e) { ts.tet_infoline("Child thread (" + Thread.currentThread() + ") caught exception while sleeping: "); ts.tet_infoline(" " + e); ts.tet_result(ts.TET_UNRESOLVED); }

ts.tet_infoline("Child thread (" + Thread.currentThread() + ") exits"); } }




Here is the journal output when this test case is run:

0|3.4ea 12:38:06 19990928|User: tet (106) TCC Start, \
	Command line: tcc -epl /ts/tc1/ThrTest
5|Linux fir 2.0.35 #6 Sat Mar 13 09:21:49 GMT 1999 i686| \
	Local System Information
20|/home/tet/jtests/tetexec.cfg 1|Config Start
30||TET_EXPAND_CONF_VARS=true
30||TET_OUTPUT_CAPTURE=true
30||TET_API_COMPLIANT=true
30||TET_EXEC_IN_PLACE=true
30||TET_EXEC_TOOL=ksh
30||TET_EXEC_FILE=/home/tet/jtests/exectool.ksh
30||TET_JAVA_PATH=/usr/local/java/bin/java
30||TET_PASS_TC_NAME=True
30||TET_VERSION=3.4ea
40||Config End
10|0 /ts/tc1/ThrTest 12:38:06|TC Start, scenario ref 1-0
15|0 3.4ea 1|TCM Start
400|0 1 1 12:38:06|IC Start
200|0 1 12:38:06|TP Start
520|0 1 00030029 1 1|This is the parent thread: \
	Thread[main,5,main]
520|0 1 00030029 1 2|Parent printing child thread: \
	Thread[TestThread() #1,5,TET:ThrTest:IC1TP1]
520|0 1 00030029 1 3|parent thread returns
520|0 1 00030029 2 1|Child thread \
	(Thread[TestThread() #1,5,TET:ThrTest:IC1TP1]): \
	starting to run
520|0 1 00030029 2 2|Child thread \
	(Thread[TestThread() #1,5,TET:ThrTest:IC1TP1]) exits
220|0 1 0 12:38:11|PASS
410|0 1 1 12:38:11|IC End
80|0 0 12:38:12|TC End, scenario ref 1-0
900|12:38:12|TCC End

 

See also

  • "Using Threads'' in the chapter that describes the Java API in the TETware Programmers Guide.

 


Home Contacts Legal Copyright Corporate News

Copyright © The Open Group 1995-2012, All Rights Reserved