FileDocCategorySizeDatePackage
TestSearchModel.javaAPI DocExample1825Tue Oct 22 21:03:24 BST 2002com.oreilly.javaxp.junit

TestSearchModel.java

package com.oreilly.javaxp.junit;

import junit.framework.TestCase;

import java.util.List;

/**
 * @author Eric M. Burke
 * @version $Id: TestSearchModel.java,v 1.1 2002/10/23 02:03:24 jepc Exp $
 */
public class TestSearchModel extends TestCase {
    public TestSearchModel(String name) {
        super(name);
    }

    public void testSynchronousSearch() {
        SearchModel sm = new PersonSearchModel();

        // perform a search
        List results = sm.search("eric");
        assertEquals("Number of results", 1, results.size());
        Person p = (Person) results.get(0);
        assertEquals("Result", "Eric", p.getFirstName());

        // do another search
        results = sm.search("*");
        assertEquals("Number of results", 4, results.size());
    }

    public void testAsynchronousSearch() throws InterruptedException {
        MockSearchModelListener mockListener = new MockSearchModelListener();
        SearchModel sm = new PersonSearchModel();
        sm.search("eric", mockListener);

        synchronized (mockListener) {
            mockListener.wait(2000);
        }

        SearchModelEvent evt = mockListener.getSearchModelEvent();
        assertNotNull("Search timed out", evt);
        List results = evt.getSearchResult();
        assertEquals("Number of results", 1, results.size());
        Person p = (Person) results.get(0);
        assertEquals("Result", "Eric", p.getFirstName());
    }
}

class MockSearchModelListener implements SearchModelListener {
    SearchModelEvent evt;

    public void searchFinished(SearchModelEvent evt) {
        this.evt = evt;
        synchronized (this) {
            notifyAll();
        }
    }

    public SearchModelEvent getSearchModelEvent() {
        return this.evt;
    }
}