It's not a secret anymore!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes All of URLy Bird 1.1.3 about Junit test 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 "All of URLy Bird 1.1.3 about Junit test" Watch "All of URLy Bird 1.1.3 about Junit test" New topic
Author

All of URLy Bird 1.1.3 about Junit test

liqun chang
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Hi:
I am new user of JUnit.I have seen the content of the JUnit.and understand
TestCase and TestSuite and Fixture,i also can use objects do test.
But i want to know the simple example for thread test.Could you give me a
simple example with pseudocode?
Another question is test-first design term,Whehter test-before code,could you give me some general suggestion?
Because i have seen another topic in this forum,but there is not any valuable things for me?
Javini Javono
Ranch Hand

Joined: Dec 03, 2003
Posts: 286
Originally posted by liqun chang:

Another question is test-first design term,Whehter test-before code,could you give me some general suggestion?

Hi,
The following book will answer your above question and your questions dealing with
concurrency testing:
Author: Johannes Link with contributions by Peter Frohlich
Title: Unit Testing in Java: How Tests Drive the Code
I am new to JUnit. I decided that I would use this Sun exam as an opportunity to
use JUnit. Like you, prior to this point, I had played around with JUnit, but I had
not--presumably like you--applied it to a project I was actually working on.
Concerning concurrency: assuming my memory of my first read of the book is
correct: there is no JUnit test or add-on to prove your code is designed and implemented
in a thread-safe manner. For a good, beginning tutorial on threads see the
free on-line book: Thinking in Java.
Having said the above, however, it does no mean that some concurrency testing can't
be done. I'll let others answer this part of the question because I am currently working
on Data using JUnit, and have not yet gotten to lock, unlock, isLocked, and the
concurrency issues (though I've studied concurrency issues and know how the design
will unfold: i.e., which classes will and will not be made for concurrency).
Concerning how JUnit drives design. In a nutshell, and based upon the book,
Unit Testing in Java, and assuming I'm remembering correctly given that I've
only read the book once, you have this: in general: as things becomes more
difficult to test, you refactor, and in this way, very generally speaking, you
decouple the code, and in this way, some aspects of the design are driven
by the testing.
Also in general, I'm starting off by testing against the interface: DBMain
(or, to be more accurate as described in this post:
Topic: NX: Modifying DBMain (but slightly)
http://www.coderanch.com/t/185283/java-developer-SCJD/certification/NX-Modifying-DBMain-but-slightly
I'm testing against the SuperDBMain interface against three different
possible instantiations of "data".
That is, black-box testing. Simply jump in, and as you start testing,
although the interface is already given, you will find ways to enhance
the design. For instance, in trying to test the data.delete() method,
I wanted to undelete the file after deleting it; so, this led to a new design
idea: DBMainExtras extends DBMain, and Data implements DBMainExtras.
In this way, I can call new, more powerful methods instead of sticking with
the relatively low-level ones offered by DBMain. And, this was brought
about because I followed the general beginners rule: use JUnit to test
the interface.
All in all, as you may already know, I think that you are definitely on the
right track in using JUnit. I already feel very comfortable having a beginning
test-bed that I can rely on if, and when, I do further refactoring. By building
the tests as I refine the already written software (that is, I designed and
wrote the basics prior to using JUnit, but that was just how things happened
since I'm new to JUnit), I no longer have to worry or feel fear about making
any future software changes and enhancements, because the tests run
very quickly and accurately.
Also, as an aside, I'm currently using JUnit to test only Data. In my design,
Data is exposed to the client. And, Data has additional functionality (such
as possible primary keys) that the client will never use; so, it makes even
more sense to use JUnit, otherwise there is no software to test all the
functionality of Data.
Because DBMain is poorly designed, this is another reason to use JUnit,
so you "don't get lost" in the complexity of Sun's apriori design.
Finally, JUnit forces you to think about scenarios that you may have missed
during your detailed design steps.
Good luck with JUnit, and I hope others can fill in some of the blanks I may
have left out.
Thanks,
Javini Javono
[ March 06, 2004: Message edited by: Javini Javono ]
George Marinkovich
Ranch Hand

Joined: Apr 15, 2003
Posts: 619
Hi Liqun,
Originally posted by liqun chang:
Hi:
I am new user of JUnit.I have seen the content of the JUnit.and understand
TestCase and TestSuite and Fixture,i also can use objects do test.
But i want to know the simple example for thread test.Could you give me a
simple example with pseudocode?

Max gives a good example of a multi-threaded test harness in his book:The Sun Certified Java Developer Exam with J2SE 1.4
This code is based on the example in Max's book:

Here's the testing scenario in a nutshell:
Set all the owner fields in the database file to 0.
Run your application in server mode.
Open a network client on the machine.
Run the test harness on the same machine.
Press the search button and see a snapshot of the booking activity in the JTable.
When the test is over each record in the database should have an owner equal to 2000.
If all the owners aren't 2000, then look at the output and figure out what's going wrong.


Regards, George
SCJP, SCJD, SCWCD, SCBCD
Javini Javono
Ranch Hand

Joined: Dec 03, 2003
Posts: 286
Hi George,
Thanks much for that super software exerciser!
All three "pathways" (after sufficient refactoring and
debugging: absolute local mode (DBMain), local
mode (using factory), and RMI remote mode (using
factory), ran beautifully to completion.
I stand absolutely corrected in my suggestion that you
cannot know with certainty that your code is thread-safe.
Well, maybe you can't know with certainty. But, the above
super software exerciser (when targeting an outcome of
2000 in the resultant field) comes about as close to perfect
for our scenario as we might desire. For instance:
1. Removing the "synchronized" keyword from the low-level
RandomAccessFile reader/writer object always creates
juicy IOErrors.
2. Disabling the lock() and unlock() methods always creates
business logic errors (i.e., the resultant fields are nowhere close
to the expected result of 2000 due to "race conditions").
Your super software exerciser has helped considerable, and has
increased my confidence that my software now, and after I refactor
it, is 100% correct.
Thanks a million,
Javini Javono
[ March 10, 2004: Message edited by: Javini Javono ]
Sakke Wiik
Greenhorn

Joined: Mar 13, 2002
Posts: 10
Try out GroboUtils. It's a good threadtesting extension to JUnit. Here's a post with more info: http://www.coderanch.com/t/184291/java-developer-SCJD/certification/Test-Your-Effective-Locking-Mechanism
liqun chang
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Hi George i have some questions in another thread:
nx:All of URLy Bird 1.1.3 about delete and update method
please you help me.
liqun chang
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Hi George:a very hard question about your multiple thread test.
I have finished my Data class,but when i test multiple thread about it.
it go to wrong(singleton is fine)
I modify the test class as below:

you can see when i use another tool class set the owner fields to 0.
and run this test class it shows me the securityexception so that the
final owner is not same value.
[ March 28, 2004: Message edited by: liqun chang ]
liqun chang
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
my lock method is:
liqun chang
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
my update method is:
liqun chang
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
My question is whehter my update() method has any questions when concurrently access database file? please help me.
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hi liqun, it seems that you posted most of your lock code and update code.
FYI: We can post pseudo-code for discussion but not original working code.
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hi Liqun, I have gone through the update and lock code only. I could'nt find any reason why it should throw SecurityException. Maybe if you can run and print the stack trace and paste it here for a couple of scenario's then its possible to check on that.
Good Luck.
liqun chang
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Hi Satish thanks very much:
the stack trace is as below:

29:1:30 record read //this ok
security exception
java.lang.SecurityException:security exception
at suncertify.db.Data.update(Data.java:127)
at test.Update.run(testMutipleUpdate.java:44)
at suncertify.db.Data.unlock(Data.java:416)

and i see the source code:
127 line in Data is:throw new SecurityException("security exception");
44 line in testMutipleUpdate is:
data.update(recNo,fields,cookie);
416 line in Data is:
if(!lCookie.equals(lockMap.get(iRecNo)))throw new SecurityException("security exception");

please you and George help me.how to modify it?
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hi Liqun
Originally posted by liqun chang:
Hi Satish thanks very much:
the stack trace is as below:


I doubt here. See, you call update from Update.run method. But from the line_6 of the stack trace, you can see that it is arising somewhere in the unlock method. The exception flow from the given stack trace is like this: unlock(line_6)---->Update.run(line_5)---->update(line_4).
Why is the call to Update.run coming from unlock method?

and i see the source code:
127 line in Data is:throw new SecurityException("security exception");

This is from update method.

44 line in testMutipleUpdate is:
data.update(recNo,fields,cookie);

This is from Update.run method.

416 line in Data is:
if(!lCookie.equals(lockMap.get(iRecNo)))throw new SecurityException("security exception");

What I am not understanding is why is the above line initiating Update.run method(i.e. starting the thread)? Maybe it would help if you could post the complete stack trace here to know the exact flow and post relevant code lines only(Not the complete code). That might help to understand.

please you and George help me.how to modify it?
As such, I do not see anything wrong in the update method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: All of URLy Bird 1.1.3 about Junit test