aspose file tools*
The moose likes IDEs, Version Control and other tools and the fly likes Runnig test files with JUnit.core.run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Runnig test files with JUnit.core.run" Watch "Runnig test files with JUnit.core.run" New topic
Author

Runnig test files with JUnit.core.run

hadas yaari
Greenhorn

Joined: Dec 26, 2008
Posts: 11
Hello everybody, I have a little problem and I'm sure that you will solve it in a minute.
In my eclipse plug in - I let the user to choose java file with the fileChooser (it returns FILE)
afterwards I want to run the test with JUnit.core.run that receive Class parameter.
How do I get the Class from the java file I've chosen?
Thank you very much
hadas
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29261
    
140

Hadas,
If you read the file, the first non commented line is the package statement. And the classname is in the filename. Merging the two gives you a fully qualified class name.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
hadas yaari
Greenhorn

Joined: Dec 26, 2008
Posts: 11
Thank you very much
hadas yaari
Greenhorn

Joined: Dec 26, 2008
Posts: 11
Hi Jeanne,
I tried your idea and still I have a problem in run time.
I use Class.forName() that gets as parameter the class name (string) and suppose to create a class from it.
But I get an exception: class not found.
I suppose that Class.forName() doesn't look in the right place for the class.
Is there a way to tell it where to look?
The classes (test cases) are part of the current project.
I thought maybe I should paste a prefix to the class name but I don't know what prefix.
The purpuse is to give JUnit.core.run() a class to run.
In a lot of examples that I saw the class is given directly:

I'm helpless - Can you please direct me?
Thank you very much
Hadsa
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29261
    
140

Hadsa,
The file object has a method called getAbsolutePath(). This will give you the full name. For example, c:/workspace/com/javaranch/MyClass.class. You'll need to derive the package name from this path. Since package names typically start with com/org/net, you should be able to parse the string to get it. Then just turn the slashes into dots, remove the file extension and you've got a fully qualified class name!
hadas yaari
Greenhorn

Joined: Dec 26, 2008
Posts: 11
Hi Jeanne,
Sorry for being a little bit annoying but I think I didn't explain myself clearly.
We develop an eclipse plug-in.
A part of the plug-in is the option to run a unit tests from eclipse (when clicking on a button that is part of our plug-in).
The unit tests that the user would want to run are the unit tests of his project ( a project that he opened in eclipse that the plug in is installed in it).
When clicking on the button, the user can choose the wanted unit test files with a fileChooser and then when clicking "run tests", the tests should run by using JUnit.core.
When I run this plug in order to test and debug it, a new eclipse application( with the plug in installed in it) is opened and the user can work: open a java project, add classes, write unit tests etc.
When the user clicks on "run tests" I convert the file names into class names by replacing "/" with "." and removing the ".java" from the file name (as you suggested and as I did before). when I send this class name into Class.forName(), it searches the class in my project workspace (the project of the plug in) and not in the user's workspace (run time workspace) and that is the reason that the class is not found.
I want the possibility to run these unit tests in run-time from the user's workspace - this is the purpse of the plug-in: giving more services for the developer that uses the plug-in.
We want to run the unit tests like this in order to generate a special report.
Maybe I don't have to use Class.forName(). Maybe there is another way to load the class in order to give it to JUnit.core.run(). And of course the unit test have to run in the user's project environment because it tests the project classes.
Sorry for this long exhausting post.
I hope that now you understand the problem and maybe now you would be able to suggest me a solution.
Thank you very very much
Hadas
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29261
    
140

Oh! You have an Eclipse plugin question. I'll move this to our Eclipse forum.
hadas yaari
Greenhorn

Joined: Dec 26, 2008
Posts: 11
Thank you very mucn, I appriciate your help.
Can you please put the Eclipse Forum link here - I can't find it.
Thank you very much and have a great weekend.
Hadas
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29261
    
140

hadas yaari wrote:Thank you very mucn, I appriciate your help.
Can you please put the Eclipse Forum link here - I can't find it.
Thank you very much and have a great weekend.
Hadas

You are in it. The new software is subtle and provides links to the new location.

http://www.coderanch.com/forums/f-12/IDEs-Version-Control-other-tools
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29261
    
140

Zeca Pistolas,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runnig test files with JUnit.core.run
 
Similar Threads
Checking common files for password protection
JavaDoc problem
how to invoke a java program using shellscript
java compile trouble
Property File inside the jar