001: package org.sakaiproject.search.tool;
002:
003: //import java.util.ArrayList;
004: import java.util.List;
005: import java.util.Properties;
006:
007: import org.jmock.Mock;
008: import org.jmock.cglib.MockObjectTestCase;
009: import org.sakaiproject.search.api.SearchService;
010: import org.sakaiproject.tool.api.Placement;
011: import org.sakaiproject.tool.api.ToolManager;
012:
013: //import junit.framework.TestCase;
014:
015: public class SearchBeanImplTest extends MockObjectTestCase {
016:
017: SearchBeanImpl sbi = null;
018: Mock mockSearchService = null;
019: Mock mockToolManager = null;
020: Mock mockPlacement = null;
021: Mock mockProperties = null;
022: String defaultSiteId = "defaultSiteId";
023:
024: protected void setUp() throws Exception {
025: super .setUp();
026: // sbi = new SearchBeanImpl("defaultSiteId");
027: mockSearchService = mock(SearchService.class, "searchService");
028: mockToolManager = mock(ToolManager.class, "toolManager");
029: mockPlacement = mock(Placement.class, "placement");
030: mockProperties = mock(Properties.class, "properties");
031:
032: sbi = new SearchBeanImpl(defaultSiteId,
033: (SearchService) mockSearchService.proxy(),
034: "defaultSearch", (ToolManager) mockToolManager.proxy());
035: }
036:
037: // extractPropertiesFromTool
038:
039: public void testExtractPropertiesFromToolPlacement() {
040: mockToolManager.expects(once()).method("getCurrentPlacement")
041: .will(returnValue(mockPlacement.proxy()));
042: mockPlacement.expects(once()).method("getPlacementConfig")
043: .will(returnValue(mockProperties.proxy()));
044: mockProperties.expects(once()).method("isEmpty").will(
045: returnValue(true));
046: mockPlacement.expects(once()).method("getConfig").will(
047: returnValue(mockProperties.proxy()));
048: assertNotNull(sbi.extractPropertiesFromTool());
049: }
050:
051: public void testExtractPropertiesFromToolCurrentPlacement() {
052: mockToolManager.expects(once()).method("getCurrentPlacement")
053: .will(returnValue(mockPlacement.proxy()));
054: mockPlacement.expects(once()).method("getPlacementConfig")
055: .will(returnValue(mockProperties.proxy()));
056: mockProperties.expects(once()).method("isEmpty").will(
057: returnValue(false));
058:
059: assertNotNull(sbi.extractPropertiesFromTool());
060: }
061:
062: // extractSiteIdsFromToolProperty
063:
064: public void testExtractSiteIdsFromPropertiesNoProperties() {
065: mockProperties.expects(once()).method("getProperty").with(
066: eq("search_site_ids")).will(returnValue(null));
067: String[] s = sbi
068: .extractSiteIdsFromProperties((Properties) mockProperties
069: .proxy());
070: assertTrue("array of one empty siteIds", s.length == 1);
071: assertTrue("siteId is empty", "".equals(s[0]));
072: }
073:
074: public void testExtractSiteIdsFromPropertiesEmptyProperties() {
075: mockProperties.expects(once()).method("getProperty").with(
076: eq("search_site_ids")).will(returnValue(""));
077: String[] s = sbi
078: .extractSiteIdsFromProperties((Properties) mockProperties
079: .proxy());
080: assertTrue("array of one empty siteIds", s.length == 1);
081: assertTrue("siteId is empty", "".equals(s[0]));
082: }
083:
084: public void testExtractSiteIdsFromProperties1SiteId() {
085: mockProperties.expects(once()).method("getProperty").with(
086: eq("search_site_ids")).will(returnValue("SiteIdOne"));
087: String[] s = sbi
088: .extractSiteIdsFromProperties((Properties) mockProperties
089: .proxy());
090: assertTrue("array of one siteid", s.length == 1);
091: assertTrue("siteId is default", "SiteIdOne".equals(s[0]));
092: }
093:
094: public void testExtractSiteIdsFromProperties2SiteId() {
095: mockProperties.expects(once()).method("getProperty").with(
096: eq("search_site_ids")).will(
097: returnValue("SiteIdOne,SiteIdTwo"));
098: String[] s = sbi
099: .extractSiteIdsFromProperties((Properties) mockProperties
100: .proxy());
101: assertTrue("array of two siteIds", s.length == 2);
102: assertTrue("siteId has two entries", "SiteIdOne".equals(s[0]));
103: assertTrue("siteId has two entries", "SiteIdTwo".equals(s[1]));
104: }
105:
106: public void testExtractSiteIdsFromProperties2SiteIdBlanks() {
107: mockProperties.expects(once()).method("getProperty").with(
108: eq("search_site_ids")).will(
109: returnValue(" SiteIdOne , SiteIdTwo "));
110: String[] s = sbi
111: .extractSiteIdsFromProperties((Properties) mockProperties
112: .proxy());
113: assertTrue("array of siteIds with blanks", s.length == 2);
114: assertTrue("siteId has two entries", "SiteIdOne".equals(s[0]));
115: assertTrue("siteId has two entries", "SiteIdTwo".equals(s[1]));
116: }
117:
118: public void testExtractSiteIdsFromPropertiesJustBlank() {
119: mockProperties.expects(once()).method("getProperty").with(
120: eq("search_site_ids")).will(returnValue(" "));
121: String[] s = sbi
122: .extractSiteIdsFromProperties((Properties) mockProperties
123: .proxy());
124: assertTrue("single blank siteid", s.length == 1);
125: assertTrue("siteId is empty", "".equals(s[0]));
126: }
127:
128: // Not tested explicitly as very simple and just calls other tested methods.
129: // public void GetToolPropertySiteIds() {
130: // fail("not yet tested");
131: // }
132:
133: // If no ids from tool properties, will get only current site id
134: public void testGetSearchSitesNullSiteIdsArray() {
135: List l = sbi.getSearchSites(null);
136: assertTrue("no property site ids", l.size() == 1);
137: assertEquals("default site id", defaultSiteId, l.get(0));
138: }
139:
140: public void testGetSearchSitesEmptySiteIdsArray() {
141: List l = sbi.getSearchSites(new String[] {});
142: assertTrue("no property site ids", l.size() == 1);
143: assertEquals("default site id", defaultSiteId, l.get(0));
144: }
145:
146: public void testGetSearchSitesEmptyValueSiteIdsArray() {
147: List l = sbi.getSearchSites(new String[] { "" });
148: assertTrue("no property site ids", l.size() == 1);
149: assertEquals("default site id", defaultSiteId, l.get(0));
150: }
151:
152: public void testGetSearchSitesOneValueSiteIdsArray() {
153: List l = sbi.getSearchSites(new String[] { "toolProp1" });
154: assertTrue("no property site ids", l.size() == 2);
155: assertEquals("default site id", defaultSiteId, l.get(0));
156: assertEquals("toolProp1", "toolProp1", l.get(1));
157: }
158:
159: public void testGetSearchSitesTwoValueSiteIdsArray() {
160: List l = sbi.getSearchSites(new String[] { "toolSiteId1",
161: "toolSiteId2" });
162: assertTrue("no property site ids", l.size() == 3);
163: assertEquals("default site id", defaultSiteId, l.get(0));
164: assertEquals("toolSiteId1", "toolSiteId1", l.get(1));
165: assertEquals("toolSiteId2", "toolSiteId2", l.get(2));
166: }
167:
168: }
169:
170: //Mock mockSubscriber = mock(Subscriber.class);
171: //Publisher publisher = new Publisher();
172: //publisher.add( (Subscriber)mockSubscriber.proxy() );
|