In this section, you will edit Java elements in the workbench.
To open the file junit.samples.VectorTest.java in the Java editor, double click on the file in the Package Explorer view.
Notice the syntax highlighting. Examples of parts of java source which are rendered differently are:
Look at the Outline view.
It displays an outline of the Java file
including the package declaration, import declarations,
fields, types and methods.
The Outline view also indicates whether a Java element
is static, abstract, final, etc. The outline view also shows you whether
a method is overridden from a base class () or when it implements a method
from an interface (
).
Toggle the Hide Fields, Hide Static Members, and Hide Non-Public Members buttons in the Outline view toolbar to filter the view's display.
Toggle the Sort button in the Outline view to sort the Java elements in alphabetical order rather than in the order they appear in the Java file.
You can edit source code by viewing the whole Java file, or you can narrow the view to a single Java element. Click the Show Source of Selected Element Only button in the toolbar.
Next, In the Outline view, select any element and note that only the selected element is displayed in the Java editor.
Press the Show Source of Selected Element Only button again to see the whole Java file again.
In the Outline view, select different elements and note that they are once again displayed in a whole file view in the editor. The Outline view selection is now indicated with a range indicator on the vertical ruler on the left border of the Java editor.
Confirm that the alphabetical Sort button in the toolbar of the Outline view is toggled off.
Start adding a method by typing the following at the end of the VectorTest.java file (but before the closing brackets of the type) in the Java editor:
public void testSizeIsThree() {
As soon as you type the method name in the editor area, the new method appears at the bottom of the Outline view.
Click the Save button.
The compilation unit is compiled automatically and errors appear in the Package Explorer view, in the Tasks view and on the vertical ruler. In the Package Explorer view, the errors are propagated up to the project of the compilation unit containing the error.
Complete the new method by typing the following:
assertTrue(fFull.size() == 3); }
Save the file.
Notice that the error indicators disappear.
In this section, you will use the local history feature to switch to a previously saved version of an individual Java element.
Delete the method you just created by selecting it in the Outline view and clicking Delete from the context menu.
Add a new version of the method by typing the following at the end of the VectorTest.java file in the Java editor:
public void testSizeIsThree() { fFull.add(0, new Integer(0)); fFull.remove(new Integer(3)); assertTrue(fFull.size() == 3); }
Save the file.
In the Outline view, select the method testSizeIsThree()
,
and from its context menu, select
Replace from Local History.
In the Replace Java Element from Local History dialog, the Local History list shows the various saved states of that element. The Java Source Compare pane shows details of the differences between the selected history resource and the existing workbench resource.
In the Local History pane, select the version that you deleted, then click the Replace button.
In the Java editor, the method is replaced with the selected history version.
Save the file.
Open the junit.samples.VectorTest.java file in the Java editor.
Go to the testSizeIsThree()
method by selecting it
in the Outline view.
Add the following lines to the end of the method:
Vector v = new Vector(); for (int i=0; i<3; i++) v.addElement(new Object()); assert
With your cursor at the end of the word
assert
,
press Ctrl+Space to activate content assist.
The content assist window with a list of proposals will appear. Scroll the list to see the available choices.
With the content assist window still active, type the letter
't' after assert
(with no space in between).
The list is narrowed and only shows entries starting with
'assert'.
Select and then hover over various items in the list to view any available Javadoc help for each item.
Select assertTrue(boolean)
from the list and press
Enter.
The code for the assertTrue(boolean)
method is inserted.
Complete the line so that it reads as follows:
assertTrue(v.size() == fFull.size());
Save the file.
Open the junit.samples.VectorTest.java file in the Java editor.
Start adding a new method by typing the following:
public void testValues() { Integer[] expected= new Integer[3]; for
With the cursor at the end of for
, type Ctrl+Space
to enable content assist.
Choose the for - iterate over array
entry.
The template is inserted in the editor and i
is highlighted.
The local array name is guessed automatically.
Press Enter to confirm the template.
Complete the for
loop to:
public void testValues() { Integer[] expected= new Integer[3]; for (int i= 0; i < expected.length; i++) { expected[i]= i + 1; } Integer[] actual= to
With the cursor at the end of to
, type Ctrl+Space
to enable content assist. Pick toarray - convert collection to array
.
The template is inserted in the editor and type
is highlighted and selected.
Overwrite the selection by typing Integer
.
The type of the array constructor changes synchronously.
Type Tab to highlight and select collection
and Overwrite the selection by typing fFull
.
Type Enter to confirm the template.
Add the following lines of code to complete the method:
assertEquals(expected.length, actual.length); for (int i= 0; i < actual.length; i++) assertEquals(expected[i], actual[i]); }
Save the file.
Open the junit.samples.VectorTest.java file in the Java editor.
Delete the import declarations by selecting them in the Outline view and selecting Delete from the context menu.
From the context menu in the editor, select Organize Imports.
The required import statements are added to the beginning of your code below the package declaration.
Alternatively, Organize Imports can be invoked directly from the context menu of the import declarations in the Outline view.
Note: You can specify the order of the import declarations in Preferences (Window > Preferences > Java > Organize Imports).
Save the file.
In this section, you will improve the code of the constructor of junit.framework.TestSuite. To make the intent of the code more clear, you will extract the code that collects test cases from base classes into a new method called collectTestMethods.
In the junit.framework.TestSuite.java file, select the following range of code:
Class superClass= theClass; Vector names= new Vector(); while (Test.class.isAssignableFrom(superClass)) { Method[] methods= superClass.getDeclaredMethods(); for (int i= 0; i < methods.length; i++) { addTestMethod(methods[i], names, constructor); } superClass= superClass.getSuperclass(); }
Select the code range by using the Expand Selection to feature:
TestSuite(Class)
.while
.From the selection's context menu in the editor, select Refactor > Extract Method....
In the Method Name field, type collectInheritedTests, then click Next.
The refactoring preview page displays the changes that will be made.
Press Finish to extract the method.
Go to the extracted method by selecting it in the Outline view.
Open the junit.samples.money.MoneyTest.java file in the Java editor.
On the first line of the MoneyTest class declaration, select the TestCase superclass specification and either
Note: This command also works on methods and fields.
The TestCase superclass opens in the editor area and is also represented in the Outline view.
Click the TestCase.java editor tab to make it the active editor. Ensure that the class declaration is still selected, and:
The Hierarchy view opens with the TestCase class displayed.
Note: You can also open editors on types and methods in the Hierarchy view.
Java
views
Java
editor
Refactoring
support
Templates
Using the Java editor
Showing and hiding elements
Showing single elements or whole Java files
Sorting elements in Java views
Using the local history
Using content assist
Using templates
Managing import statements
Refactoring
Refactoring with preview
Using the Hierarchy view
Opening a type hierarchy on a Java element
Opening a type hierarchy on the current
text selection
Opening an editor for a selected element
Java Outline View
Java Content Assist
Templates Preferences
Organize Imports Preferences
Java Editor Preferences
Extract Method Errors
Type Hierarchy View