问题
What I want: I want to write JUnit tests for my Eclipse editor plug-in. For this I want to set up an instance of my TextEditor
extension. Then I want to set the input of this TextEditor
from a String, so that the content of the String is the input of the editor instance. Then I want to run some tests and assert that the editor marks errors and so on.
Where I fail: I don't know how to set up the input of the editor from a String. The only function the editor has to do so is setInput(IEditorInput input)
, but I don't know how to create an ÌEditorInput
with a String as it's content.
Is there any possibility to do so and, if not, any other way to set the input of the editor to a given String?
回答1:
You can either create a (workspace) file as Greg suggested or implement a specialized IEditorInput
and open a text editor with this input.
For example:
// uninteresting methods left out for brevity
class StringEditorInput implements IStorageEditorInput {
@Override
public boolean exists() {
return true;
}
@Override
public IStorage getStorage() throws CoreException {
return new StringStorage();
}
}
class StringStorage implements IStorage {
@Override
public InputStream getContents() throws CoreException {
return new ByteArrayInputStream( "Hello editor!".getBytes( StandardCharsets.UTF_8 ) );
}
}
String editorId = "org.eclipse.ui.DefaultTextEditor";
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editor = IDE.openEditor( page , new StringEditorInput(), editorId );
来源:https://stackoverflow.com/questions/42027933/how-to-programatically-set-string-input-of-texteditor-in-eclipse-plug-in