The Application


Now that the view is in place, you can specify how the application should use it. It's time to tie things together.[2] Here's a new SisTest method, testAddCourse.

[2] Depending on your mindset, you might have found it easier to have started building from the application and driven down into the view. Even while I was constructing the view, I had in mind how I wanted the application to tie things together.

 package sis.ui; import junit.framework.*; import javax.swing.*; import java.awt.*; import sis.studentinfo.*; public class SisTest extends TestCase {    ...    public void testAddCourse() {       CoursesPanel panel =          (CoursesPanel)Util.getComponent(frame, CoursesPanel.NAME);       panel.setText(CoursesPanel.DEPARTMENT_FIELD_NAME, "MATH");       panel.setText(CoursesPanel.NUMBER_FIELD_NAME, "300");       JButton button = panel.getButton(CoursesPanel.ADD_BUTTON_NAME);       button.doClick();       Course course = panel.getCourse(0);       assertEquals("MATH", course.getDepartment());       assertEquals("300", course.getNumber());    } } 

The test drives the application from the perspective of an end user. It's almost an acceptance test.

First, the test sets values into the department and course number fields. It then emulates a click of the Add button using the button method click. In order to ensure that the application is behaving correctly, the tests asks the embedded CoursesPanel to return the first Course in its list.

You'll need to add a couple of methods to CoursesPanel to support the test:

 package sis.ui; ... public class CoursesPanel extends JPanel {    ...    Course getCourse(int index) {       Course adapter =          (CourseDisplayAdapter)coursesModel.getElementAt(index);       return adapter;    }    ...    void setText(String textFieldName, String text) {       getField(textFieldName).setText(text);    } } 

Since a CourseDisplayAdapter extends from Course, you can assign the extracted adapter object to a Course reference to return.

Code changes to Sis (including a few minor refactorings):

 package sis.ui; import javax.swing.*; import java.awt.event.*; import sis.studentinfo.*; public class Sis {    ...    private CoursesPanel panel;    ...    public Sis() {       initialize();    }    private void initialize() {       createCoursesPanel();       frame.setSize(WIDTH, HEIGHT);       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.getContentPane().add(panel);    }    ...    void createCoursesPanel() {       panel = new CoursesPanel();       panel.addCourseAddListener(          new ActionListener() {             public void actionPerformed(ActionEvent e) {                addCourse();             }          }       );    }    private void addCourse() {       Course course =          new Course(             panel.getText(CoursesPanel.DEPARTMENT_FIELD_NAME),             panel.getText(CoursesPanel.NUMBER_FIELD_NAME));       panel.addCourse(course);    } } 

The Sis class ties together the action listener and the ability to add a course to the panel. Most of the code should look familiarit is the client code you built in tests for CoursesPanel.

You'll need to add the getText method to CoursesPanel:

 String getText(String textFieldName) {    return getField(textFieldName).getText(); } 

You can now run Sis as a stand-alone application and experiment with adding courses. The screen shot in Figure 4 shows Sis after a user has entered five courses. It's still a mess!

Figure 4.




Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net