In this section, you will edit Java elements in the workbench.
1. |
In the Packages view, find junit.samples.VectorTest.java and double-click it to open this file in the Java editor. |
2. |
In the editor area, notice that this file is shown in the active editor. If it is not the active editor, click its tab to make it so. |
3. |
In the Packages view, open various resources in the editor area and notice the syntax highlighting. For example:
![]() |
4. |
Look at the Outline view. Notice how it is populated, displaying a hierarchy outline of the package itself plus import declarations, fields, classes, methods. ![]() |
5. |
Notice that the Outline view indicates whether a Java element is abstract, final, etc. Toggle the Show/Hide Fields, Show/Hide Non-Public Members, and Show/Hide Static Members buttons in the Outline view toolbar to filter the view's display. ![]() |
6. |
Toggle the Sort button in the Outline view to sort the Java elements by either sequential order (as indicated in the compilation unit) or alphabetical order. |
7. |
You can edit source code by viewing the full source code of the compilation unit, or you can narrow the view to a single element. Click the tab for VectorTest.java, and click the Show Source of Selected Element Only button in the toolbar. In the Outline view, select various elements and note that they are displayed individually in a segmented view in the editor. ![]() |
8. |
Click in the editor area again and click the same button ( Show Complete Source ) in the toolbar. In the Outline view, select various elements and note that they are once again displayed in a whole file view in the editor. Notice that now, the Outline view selection is marked in the editor with a range indicator in the marker bar. ![]() |
1. |
Make sure that the Sort button in the toolbar of the Outline view is toggled so that the view is sorted sequentially (instead of alphabetically). |
2. |
In the editor area, type the following at the very end of the VectorTest.java file (but before the closing brackets): public void testSizeIsThree (){ Notice that as soon as you type the method name in the editor area, the new method appears in the Outline view, at the very bottom (since the view is sorted sequentially). ![]() |
3. |
Click the Save button. Notice that because your workbench builds automatically, errors show up in the Packages view, the Tasks view, and the editor marker bar. Also notice that in the Packages view, the errors propagate up to the project of the compilation unit containing the error. ![]() |
4. |
Continue adding the new method by typing the following: assertTrue(fFull.size() == 3); } |
5. |
Click the Save button. Notice that the errors disappear. |
In this section, you will use local history support to easily switch to a previously-saved version of an individual Java element.
1. |
In the Outline view, select the testSizeIsThree() method that you just created and from its context menu, select Delete. |
2. |
In the editor, at the very end of the VectorTest.java file, add a new testSizeIsThree() method: public void testSizeIsThree() { fFull.add(0, new Integer(0)); fFull.remove(new Integer(3)); assertTrue(fFull.size() == 3); } Click Save when you are done. |
3. |
In the Outline view, select the testSizeIsThree() method, and from its context menu, select Replace from Local History. |
4. |
In the Replace Java Element from Local History dialog, the Local History list shows the various saved states of that element, and the Java Source Compare pane shows details of the differences between the selected history resource and the existing workbench resource. ![]() |
5. |
In the Local History pane, select the version that you deleted, then click Replace. |
6. |
The code in the editor is replaced with the history version. Click the Save button. |
1. |
Double-click junit.samples.VectorTest.java to open it in an editor. |
2. |
In the Outline view, select the testSizeIsThree() method to navigate in the editor to the code for that method. |
3. |
In the editor, 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 |
4. |
With your cursor at the end of the word assert, press Ctrl+Space to activate code assist. ![]() |
5. |
Scroll down a bit in the list to see the available choices. With the code assist window still active, type the letter t after assert (with no space between) to narrow the list. |
6. |
Select and then hover over various items in the list to view any available Javadoc help for each item. ![]() Note: You must first select the item, to view the hover help. |
7. |
Select the assertTrue(boolean) option from the lost and press Enter. |
8. |
After the code is inserted, complete the line so that it reads as follows: assertTrue(v.size() == fFull.size()); |
9. |
Click Save when you are done. |
1. |
If it is not already open, double-click junit.samples.VectorTest.java to open it in an editor. |
2. |
In the Outline view, select the import statements, and from their context menu, select Delete. ![]() |
3. |
From the context menu in the editor, select Organize Imports. |
4. |
The required import statements are added to the beginning of your code below the package declaration. ![]() Note: You can control the order of the import statement in the preferences pages (Window > Preferences > Java > Import Order). |
5. |
Click Save when you are done. |
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.
1. |
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(); } |
2. |
From the selection's context menu in the editor, select Extract Method. ![]() |
3. |
In the Method Name field, type collectInheritedTests, then click Next. ![]() |
4. |
The refactoring preview page displays the changes that will be made. ![]() |
5. |
The method is extracted. Select it in the Outline view to navigate to it in the editor. ![]() |
1. |
In the Packages view, double-click junit.samples.money.MoneyTest.java to open it in an editor. |
2. |
In first line of the MoneyTest class declaration in the editor, select the TestCase superclass specification and either
![]() Note: This command also works on methods and fields. |
3. |
The TestCase superclass opens in the editor area and is also represented in the Outline view. ![]() |
4. |
Click the TestCase.java editor tab to make it the active editor. Make sure that the class declaration is still selected, and:
![]() |
5. |
The Hierarchy view opens with the TestCase class displayed. ![]() You can also open editors on the types and methods in the Hierarchy view. |