From: Steve F. <sm...@us...> - 2002-08-31 10:55:27
|
Update of /cvsroot/mockobjects/no-stone-unturned/src/java/nostone/gui In directory usw-pr-cvs1:/tmp/cvs-serv26200/src/java/nostone/gui Added Files: TestHelper.java SearcherTest.java Searcher.java Directory.java Log Message: Restructured source code under single src directory --- NEW FILE: TestHelper.java --- package nostone.gui; import java.awt.*; public class TestHelper { public Component depthFirst(Container container, ComponentVisitor visitor) { Component[] components = container.getComponents(); for (int i = 0; i < components.length; i++) { Component component = components[i]; Component applied = visitor.apply(component); if (null != applied) { return applied; } if (component instanceof Container) { applied = depthFirst((Container) component, visitor); if (null != applied) { return applied; } } } return null; } public interface ComponentVisitor { public Component apply(Component aComponent); } } --- NEW FILE: SearcherTest.java --- package nostone.gui; import junit.framework.TestCase; import javax.swing.*; import java.awt.*; import com.mockobjects.ExpectationValue; public class SearcherTest extends TestCase { public SearcherTest(String name) { super(name); } public void testNoMatchesFound() { Searcher searcher = new Searcher(new Directory() { public String searchFor(String searchString) { return null; } }); assertEquals("Should be status", "", ((JLabel)findNamedComponent(searcher, "status")).getText().trim()); ((JButton)findNamedComponent(searcher, "search button")).doClick(); assertEquals("Should be status", "No entries found", ((JLabel)findNamedComponent(searcher, "status")).getText()); } public void testOneMatchFound() { final ExpectationValue searchString = new ExpectationValue("search string"); Searcher searcher = new Searcher(new Directory() { public String searchFor(String aSearchString) { searchString.setActual(aSearchString); return "One Result"; } }); ((JTextField)findNamedComponent(searcher, "search criterion")).setText("Search String"); searchString.setExpected("Search String"); ((JButton)findNamedComponent(searcher, "search button")).doClick(); assertEquals("Should be result", "One Result", ((JTextArea)findNamedComponent(searcher, "results")).getText()); assertEquals("Should be status", "", ((JLabel)findNamedComponent(searcher, "status")).getText().trim()); } private Component findNamedComponent(final Container container, final String name) { return new TestHelper().depthFirst(container, new TestHelper.ComponentVisitor() { public Component apply(Component aComponent) { return name.equals(aComponent.getName()) ? aComponent : null; } }); } } --- NEW FILE: Searcher.java --- /* */ package nostone.gui; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Searcher extends JFrame { public Searcher(final Directory directory) throws HeadlessException { super("Searcher"); JButton searchButton = new JButton("Search"); searchButton.setName("search button"); JTextField searchString = new JTextField(); searchString.setName("search criterion"); searchString.setColumns(15); final JLabel statusBar = new JLabel(" "); statusBar.setName("status"); final JTextArea results = new JTextArea(); results.setName("results"); searchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String result = directory.searchFor(""); if (null == result) { statusBar.setText("No entries found"); } else { results.setText(result); } } }); Box searchBar = Box.createHorizontalBox(); searchBar.add(searchString); searchBar.add(searchButton); getContentPane().add(searchBar, BorderLayout.NORTH); getContentPane().add(results, BorderLayout.CENTER); getContentPane().add(statusBar, BorderLayout.SOUTH); pack(); } static public void main(String[] args) { Searcher searcher = new Searcher(new Directory() { public String searchFor(String searchString) { return "One Result"; } }); searcher.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { Window w = e.getWindow(); w.setVisible(false); w.dispose(); System.exit(0); } }); searcher.setVisible(true); } } --- NEW FILE: Directory.java --- package nostone.gui; public interface Directory { String searchFor(String searchString); } |