📄 searchtest.java
字号:
import junit.framework.*;
import java.util.*;
import edu.uiuc.cs.cs327.linuxwifi.app.*;
import edu.uiuc.cs.cs327.linuxwifi.services.*;
import edu.uiuc.cs.cs327.linuxwifi.util.*;
public class SearchTest extends TestCase implements FileInfoListener,
MusicProfileListener
{
private int _fileInfoHits;
private int _musicProfileHits;
/***************************************
* These two methods are standard JUnit boilerplate
**********************************/
public SearchTest(String name)
{
super(name);
}
public static Test suite()
{
return new TestSuite(SearchTest.class);
}
/*********************************************
* A convenience method that lets this Test Suite
* be run as a standalone class
**************************************/
public static void main(String[] args)
{
junit.textui.TestRunner.run(suite());
}
/*********************************************
* These three methods are white-box tests aimed at
* ensuring that our custom events are handled
* properly. We elected to have lower-level classes
* communicate back to their callers through an event
* system, and want to ensure that fired events are
* received by their listeners
********************************************/
public void testAppFileInfoEvent()
{
_fileInfoHits = 0;
AppAPI appAPI = new AppAPI();
appAPI.addFileInfoListener(this);
appAPI.fireFileInfoReceivedEvent(null);
assertEquals(1, _fileInfoHits);
}
public void testServicesFileInfoEvent()
{
_fileInfoHits = 0;
ServicesApi srvAPI = new ServicesApi();
srvAPI.addFileInfoListener(this);
srvAPI.fireFileInfoReceivedEvent(null);
assertEquals(1, _fileInfoHits);
}
public void testAppMusicProfileEvent()
{
_musicProfileHits = 0;
AppAPI appAPI = new AppAPI();
appAPI.addMusicProfileListener(this);
appAPI.fireMusicRequestEvent(null);
assertEquals(1, _musicProfileHits);
}
/***************************************************
* This black-box test ensures that the Search function
* sends a response when it is asked for a file that is
* available.
* The expected nuber of hits is two, because the ServicesApi fires
* a null event in order to update the client when a search begins
* and clears any existing results.
********************************************/
public void testSearchResponse()
{
_fileInfoHits = 0;
ServicesApi srvAPI = new ServicesApi();
srvAPI.addFileInfoListener(this);
searchForBeatles(srvAPI);
assertEquals("Match not found - be sure the test directory is set up properly", 2, _fileInfoHits);
}
/*****************************************************
* This is a black-box test that ensures fulfillment of the
* requirement that a new search will clear the previous results.
* a successful seacrh is run to prepopulate the searchResults, and
* then a false seacrh is started. The results of the first test
* should be cleared.
***************************************************/
public void testNewSearchClearsResults()
{
ServicesApi srvAPI = new ServicesApi();
searchForBeatles(srvAPI);
assertEquals(1, srvAPI.getSearchResponses().size());
searchForFake(srvAPI);
assertEquals("Match found - be sure you haven't created an improbable file", 0, srvAPI.getSearchResponses().size());
}
/****************************************************
* This is an automated system test for the search functionality.
* While the call is simple, this executes a full search:
* 1) The request is generated and sent downstream
* 2) The system notes that a request has been made and sees
* if it can fulfill it
* 3) The system makes a positive response
* 4) The requesting party is notified of a match
*********************************************/
public void testBeatlesSearch()
{
ServicesApi srvAPI = new ServicesApi();
searchForBeatles(srvAPI);
assertEquals("Match not found - be sure the test directory is set up properly", 1, srvAPI.getSearchResponses().size());
}
/****************************************************
* These two helper methods execute a seacrh for the
* Beatles song "Let it Be" and a fake song that shouldn't exist
*********************************************/
private void searchForBeatles(ServicesApi srvAPI)
{
srvAPI.searchPeers(getBeatlesProfile());
}
private void searchForFake(ServicesApi srvAPI)
{
srvAPI.searchPeers(getFakeProfile());
}
/************************************************
* This helper method generates a music profile
* that will match The Beatles song "Let It Be"
**************************************/
private MusicProfile getBeatlesProfile()
{
MusicProfile mp = new MusicProfile();
mp.setArtist("The Beatles");
mp.setMaxFileSize(0);
mp.setSong("Let It Be");
mp.setGenre("");
mp.setFileFormat("mp3");
return mp;
}
/********************************************
* This helper method generates a music profile
* that shouldn't match anything
*************************************/
private MusicProfile getFakeProfile()
{
MusicProfile mp = new MusicProfile();
mp.setArtist("No such artist as This");
mp.setMaxFileSize(-200);
mp.setSong("A fake song name");
mp.setGenre("No genre I've ever heard of");
mp.setFileFormat("");
return mp;
}
/************************************************
* These two event handlers assist the
* tests that verify that the event handling system
* is wired up properly
*******************************************/
public void fileInfoReceived(FileInfoEvent e)
{
_fileInfoHits++;
}
public void musicRequestReceived(MusicProfileEvent e)
{
_musicProfileHits++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -