File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Testing" Watch "Testing" New topic


GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
Hi guys, will the following code be good enough to test locking/unlocking. I am getting unexcepted results like Secruity Excpetion, just wondering whether I am on the right track.

public class LockTesting extends Thread {
private static Data data1;

public static void main(String[] args) {
data1 = new Data();

LockTesting test1 = new LockTesting();
LockTesting test2 = new LockTesting();
LockTesting test3 = new LockTesting();
LockTesting test4 = new LockTesting();
LockTesting test5 = new LockTesting();




public void run() {

for (int x = 0; x < 30; x++) {
long cookie1 = data1.lockRecord(1);
data1.unlock(1, cookie1);
long cookie2 = data1.lockRecord(2);
data1.unlock(2, cookie2);
long cookie3 = data1.lockRecord(3);
data1.unlock(3, cookie3);

long cookie4 = data1.lockRecord(4);
data1.unlock(4, cookie4);

long cookie5 = data1.lockRecord(5);
data1.unlock(5, cookie5);

long cookie6 = data1.lockRecord(6);

data1.unlock(6, cookie6);
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
If you execute this how you know whether it is working right or not?

Can you purposely create conditions where

1. a thread is locking an already locked record using same cookie and different cookie.

2. two threads are trying to update a record simultaneously

3. two threads deleting a record

4. A thread is unlocking a record not locked by other and same cookie.

5. a thread deleting and another inserting


Then you know what to expect.

A test should address a finite issues and should yield a finite set of results.
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
like jiju said.
A test should creat a set of conditions and check the expected output. The condions can be a valid operation, or an error in which case if the expected error condition occurs the tests passes.

You should write small tests(unit level) to check the main classes in our case(Data and whatever fasade/adapter you use, and the networking). Once all the corner cases are checked using unit tests, its good to have a good highlevel random test(s) run for a while to check everything(functional tests in hardware not sure about the softare term is) you havent thought of.

I did a bunch of read write type tests, that is write with multiple thread instance to the same reacord, and check for the expected value. The testfailed when the expected value was not recived. I also created error conditions, that caused exceptions, for example I deleted a record while it was locked and waited for a RNF exception from the next waiting thread.

Here your not check for anything, and whats more your main doesnt wait for the threads to finish executing before exiting. Creat a set of operations that create a condition you expect, and then check for it. I would highly recomend that you use the JUnit test toolkit, its very simple to use, and useful.
[ November 22, 2004: Message edited by: Inuka Vincit ]

MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by GD Deepz:
Hi guys, will the following code be good enough to test locking/unlocking. I am getting unexcepted results like Secruity Excpetion, just wondering whether I am on the right track.

I use a test like that to test that there are no obvious problems such as deadlocks or incorrect cookies when using multiple threads. I do make the number of theads, number of iterations and number of records configurable. Since my LockManager locks arbitrary numeric resources I don't need a file to run this test.

I use a second test that does random database operations against a file, since it does inserts and deletes randomly, there is no way to confirm that its actually producing the correct results.

A third test I use is to do the following sequence repeatedly over the whole file using several threads: {lock record, read record, random delay, increment a numeric field in the record, replace record, unlock record}. If the file starts out with the numeric field set to 0, you should find that all the records have the number of passes*threads as their value.

A forth test is to run several clients, with at least one configured to pause awaiting a user input after it locks a record. This will allow you to try various specific controlled lock tests including unplugging the network while locked.

While running all these I use Emma to keep code coverage statistics. If you can get these numbers close to 100% for all your code, without any failures, you probably have pretty solid code. Getting coverage numbers over 90% usually requires an extensive test plan.
I agree. Here's the link:
subject: Testing
jQuery in Action, 3rd edition