Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » ServerTools (WTP) » Problem with JUnit testing on XML Editor Plugin
Problem with JUnit testing on XML Editor Plugin [message #225960] Mon, 05 January 2009 22:53 Go to next message
Xavier Coulon is currently offline Xavier CoulonFriend
Messages: 58
Registered: July 2009
Member
Hello,

Before all : happy new year to the readers of this post :-)

I'm trying to write some JUnit tests on an extension to the WTP XML Editor
(version 3.0.3).
For now, the extension provides content assist and hyperlinks to the
editor, and while doing some "hand testing" works well, this is not a
satisfying/agile approach (and btw, I would like to apply code coverage
tools such as Clover and EclEmma on my source code).

When I launch the Junit Plugin TestCase, a second instance of Eclipse
starts in a new (and cleaned) workspace, the test setup (creating
JavaProject with build path, etc..) works, but then I get the following
error :

Exception in thread "WorkbenchTestable" org.eclipse.swt.SWTException:
Device is disposed
at org.eclipse.swt.SWT.error(SWT.java:3777)
at org.eclipse.swt.SWT.error(SWT.java:3695)
at org.eclipse.swt.SWT.error(SWT.java:3666)
at org.eclipse.swt.widgets.Display.error(Display.java:1126)
at org.eclipse.swt.widgets.Display.syncExec(Display.java:3952)
at
org.eclipse.ui.internal.testing.WorkbenchTestable.testingFin ished(WorkbenchTestable.java:119)
at
org.eclipse.pde.internal.junit.runtime.UITestApplication.run Tests(UITestApplication.java:117)
at
org.eclipse.ui.internal.testing.WorkbenchTestable$1.run(Work benchTestable.java:68)
at java.lang.Thread.run(Thread.java:613)


This error occurs in the following utility method of a class I developped :

/**
* Retrieve the JavaProject the current edited resource belongs to
*
* @return instance of IJavaProject containing the edited resource
*/
public static final IJavaProject
getCurrentJavaProject(IStructuredDocument document) {
IJavaProject javaProject = null;
IEditorInput input = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage().getActiveEditor( )
.getEditorInput();
if (input instanceof FileEditorInput) {
IProject project = ((FileEditorInput) input).getFile().getProject();
javaProject = (JavaProject) JavaCore.create(project);
}
return javaProject;
}

This method should return the current Java Project in which the document
given in parameter is located. I need this JavaProject to retrieve
IType(s) in its classpath...

This method woks fine when I perform some "hand testing" since the
workbench is active, but throws a NullPointerException at the following
line :
IEditorInput input = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage().getActiveEditor( )
.getEditorInput();


For now :

- The instance of document given in parameter of this method is retrieved
from the ContentAssistRequest when the extension provides some custom
content assist :

Util.getCurrentJavaProject(contentAssistRequest.getDocumentR egion().getParentDocument());

- The instance of document given in parameter of this method is retrieved
from the TextViewer when the extension provides hyperlinks :
IStructuredDocumentRegion regions =
ContentAssistUtils.getStructuredDocumentRegion(textViewer,
region.getOffset());
Util.getCurrentJavaProject(regions.getParentDocument())



Is there another way to retrieve the current JavaProject from the given
input (ITextViewer and ContentAssistRequest) so that my code works *both*
in JUnit Plugin Tests and User Runtime ?

Thank you in advance
Regards
Xavier
Re: Problem with JUnit testing on XML Editor Plugin [message #225996 is a reply to message #225960] Tue, 06 January 2009 16:54 Go to previous messageGo to next message
Xavier Coulon is currently offline Xavier CoulonFriend
Messages: 58
Registered: July 2009
Member
Hello again,

Does anyone know how to solve this problem ? ie, how can you run JUnit
Tests on Eclipse plugins when you need to find the JavaProject associated
with the current input/source/document in an editor ?

Thank you in advance
Regards,
Xavier
Re: Problem with JUnit testing on XML Editor Plugin [message #226014 is a reply to message #225996] Tue, 06 January 2009 17:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: dcarver.starstandard.org

Xavier Coulon wrote:
> Hello again,
>
> Does anyone know how to solve this problem ? ie, how can you run JUnit
> Tests on Eclipse plugins when you need to find the JavaProject
> associated with the current input/source/document in an editor ?

I'd recommend taking a look at the Unit tests that are in the
org.eclipse.jdt.ui.test package.

They should give you some information on how to retrieve the particular
project given an IFile from an editor.

Dave
Re: Problem with JUnit testing on XML Editor Plugin [message #226074 is a reply to message #226014] Tue, 06 January 2009 22:30 Go to previous messageGo to next message
Xavier Coulon is currently offline Xavier CoulonFriend
Messages: 58
Registered: July 2009
Member
Thank you for your answer David,

The root problem was due to some stupid lien of code I wrote between
Christmas and New Year eve..
The code was
PlatformUI.getWorkbench().getActiveWorkbenchWindow().close() ;

I wanted to close the 'Welcome' view you get when you start Eclipse in a
new workbench, which is the case with JUnit Plugin Tests by default.
Removing this line now gives me a NullPointerException since there is no
ActiveEditor (probably because of this Welcome Page..)


I followed your suggestion, yet I didn't find the piece of code in the
plugin/package you mentionned (their must be something related to this,
but I was searching with the CVS Web Browser interface)

I finally found the following piece of code in another open source plugin
(Spring IDE). The trick is to locate the IFile from the document using the
StructuredModelManager.

/**
* Retrieve the JavaProject the current edited resource belongs to
*
* @return instance of IJavaProject containing the edited resource
*/
public static IJavaProject getJavaProject(IStructuredDocument document) {
IFile resource = getFile(document);
if(resource != null) {
IJavaProject project = JavaCore.create(resource.getProject());
return project;
}
return null;
}

public static IFile getFile(IStructuredDocument document) {
if (document != null) {
try {
IStructuredModel model = StructuredModelManager
.getModelManager().getModelForRead(document);
IFile resource = null;

try {
String baselocation = model.getBaseLocation();
if (baselocation != null) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
.getRoot();
IPath filePath = new Path(baselocation);
if (filePath.segmentCount() > 0) {
resource = root.getFile(filePath);
}
}
}
finally {
if (model != null) {
model.releaseFromRead();
}
}

return resource;
}
catch (Exception e) {
// Sometime WTP fails for no good reason
}
}

return null;
}


Regards,
Xavier
Re: Problem with JUnit testing on XML Editor Plugin [message #226082 is a reply to message #225996] Tue, 06 January 2009 23:40 Go to previous message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 4434
Registered: July 2009
Senior Member

Xavier Coulon wrote:
> Hello again,
>
> Does anyone know how to solve this problem ? ie, how can you run JUnit
> Tests on Eclipse plugins when you need to find the JavaProject
> associated with the current input/source/document in an editor ?

While I'm only guessing, it's possible that there isn't an active
editor part during the test, but if you go back to the model manager
through
StructuredModelManager.getModelManager().getExistingModelFor Read(),
with a little maneuvering you can get the resource's path, which
should let you get to the project.

--
---
Nitin Dahyabhai
Eclipse WTP Source Editing
IBM Rational


_
Nitin Dahyabhai
Eclipse Web Tools Platform
Previous Topic:[JSDT] Opening a javascript file that is outside the workspace
Next Topic:WYSIWYG editor for html
Goto Forum:
  


Current Time: Sat Apr 20 04:04:25 GMT 2024

Powered by FUDForum. Page generated in 0.03188 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top