Update of /cvsroot/mockobjects/no-stone-unturned/src/nostone/gui
In directory usw-pr-cvs1:/tmp/cvs-serv27828/src/nostone/gui
Added Files:
SearcherTest.java TestHelper.java
Log Message:
moved tests to gui package
--- 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: 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);
}
}
|