2

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?

3
  • Quite a lot of the workings of an editor depend on the exact details of the editor input. For example most editors like to have an IFileEditorInput rather than just an IEditorInput. So to really test the editor properly you need to give it a workspace file. Commented Feb 3, 2017 at 16:03
  • and how do I create a workspace file with a given string as input? Commented Feb 3, 2017 at 16:06
  • You use the create method of IFile. Commented Feb 3, 2017 at 16:07

1 Answer 1

2

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 );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.