001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 Mobile Intelligence Corp
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.community.test;
028:
029: import java.util.Properties;
030: import java.util.Collection;
031: import java.util.Iterator;
032:
033: import junit.framework.*;
034:
035: import javax.naming.directory.Attributes;
036: import javax.naming.directory.Attribute;
037: import javax.naming.directory.BasicAttributes;
038:
039: import org.cougaar.core.service.community.Entity;
040: import org.cougaar.core.service.community.CommunityService;
041: import org.cougaar.core.service.community.Community;
042: import org.cougaar.core.service.community.CommunityResponse;
043: import org.cougaar.core.service.community.CommunityResponseListener;
044:
045: import org.cougaar.community.CommunityImpl;
046: import org.cougaar.community.AgentImpl;
047: import org.cougaar.community.util.Semaphore;
048:
049: /**
050: * Test operations involving nested communities.
051: *
052: */
053: public class NestedCommunityTest extends TestBase {
054:
055: protected static final String AGENT = "Test_Agent";
056: protected static final String COMMUNITY = "Test_Community";
057: protected static final String NESTED_COMMUNITY = "Nested_Community";
058: protected CommunityService commSvc;
059: protected CommunityManagerTestImpl commMgr;
060: protected CommunityResponse topCommResp;
061: protected CommunityResponse nestedCommResp;
062: protected Collection searchResults;
063:
064: protected static final String loggingProps[][] = {
065: { "log4j.category.org.cougaar.community", "INFO" },
066: { "log4j.category.org.cougaar.community.test", "INFO" } };
067:
068: public NestedCommunityTest(String name) {
069: super (name, loggingProps);
070: }
071:
072: protected void setUp() {
073: commSvc = new CommunityServiceTestImpl(AGENT);
074: commMgr = CommunityManagerTestImpl.getInstance();
075: commMgr.reset();
076: topCommResp = null; // Clear response before each test
077: nestedCommResp = null;
078: ((CommunityServiceTestImpl) commSvc).getCache().clear();
079: searchResults = null;
080: }
081:
082: public static Test suite() {
083: return new TestSuite(NestedCommunityTest.class);
084: //Use following to run specific tests only
085: //TestSuite suite= new TestSuite();
086: //suite.addTest(new NestedCommunityTest("testLeaveCommunity"));
087: //return suite;
088: }
089:
090: /**
091: * Test add of a nested community to a parent community.
092: */
093: public void testAddCommunity() {
094: // Setup test state
095: Community top = new CommunityImpl(COMMUNITY);
096: commMgr.addCommunity(top);
097:
098: Attributes attrs = new BasicAttributes();
099: attrs.put("CommunityManager", AGENT);
100: Community nested = new CommunityImpl(NESTED_COMMUNITY, attrs);
101: nested.addEntity(new AgentImpl(AGENT));
102: commMgr.addCommunity(nested);
103:
104: Community topComm = null;
105: Community nestedComm = null;
106: final Semaphore s = new Semaphore(0);
107: CommunityResponseListener crl = new CommunityResponseListener() {
108: public void getResponse(CommunityResponse resp) {
109: topCommResp = resp;
110: s.release();
111: }
112: };
113: try {
114: commSvc.joinCommunity(COMMUNITY, NESTED_COMMUNITY,
115: commSvc.COMMUNITY, null, true, null, crl);
116: s.attempt(5000);
117: nestedComm = commSvc.getCommunity(NESTED_COMMUNITY, null);
118: } catch (Exception ex) {
119: ex.printStackTrace();
120: fail();
121: }
122: //System.out.println(CommunityCache.getCache());
123: topComm = (topCommResp != null) ? (Community) topCommResp
124: .getContent() : null;
125: Attribute parentAttr = (nestedComm != null) ? nestedComm
126: .getAttributes().get("Parent") : null;
127: assertTrue(topCommResp.getStatus() == CommunityResponse.SUCCESS
128: && topComm != null && nestedComm != null
129: && topComm.hasEntity(NESTED_COMMUNITY)
130: && parentAttr != null && parentAttr.contains(COMMUNITY));
131: }
132:
133: /**
134: * Test removal of a nested community from its parent community.
135: */
136: public void testLeaveCommunity() {
137: // Setup test state
138: Community top = new CommunityImpl(COMMUNITY);
139: top.addEntity(new CommunityImpl(NESTED_COMMUNITY));
140: commMgr.addCommunity(top);
141:
142: Attributes attrs = new BasicAttributes();
143: attrs.put("CommunityManager", AGENT);
144: attrs.put("Parent", COMMUNITY);
145: Community nested = new CommunityImpl(NESTED_COMMUNITY, attrs);
146: nested.addEntity(new AgentImpl(AGENT));
147: commMgr.addCommunity(nested);
148:
149: Community topComm = null;
150: Community nestedComm = null;
151: final Semaphore s = new Semaphore(0);
152: CommunityResponseListener crl = new CommunityResponseListener() {
153: public void getResponse(CommunityResponse resp) {
154: topCommResp = resp;
155: s.release();
156: }
157: };
158: try {
159: commSvc.leaveCommunity(COMMUNITY, NESTED_COMMUNITY, crl);
160: s.attempt(5000);
161: nestedComm = commSvc.getCommunity(NESTED_COMMUNITY, null);
162: } catch (Exception ex) {
163: ex.printStackTrace();
164: fail();
165: }
166: //System.out.println(CommunityCache.getCache());
167: topComm = (topCommResp != null) ? (Community) topCommResp
168: .getContent() : null;
169: Attribute parentAttr = (nestedComm != null) ? nestedComm
170: .getAttributes().get("Parent") : null;
171: assertTrue(topCommResp.getStatus() == CommunityResponse.SUCCESS
172: && topComm != null
173: && nestedComm != null
174: && !topComm.hasEntity(NESTED_COMMUNITY)
175: && (parentAttr == null || !parentAttr
176: .contains(COMMUNITY)));
177: }
178:
179: /**
180: * Test flat search.
181: */
182: public void testFlatSearch() {
183: // Setup test state
184: Community top = new CommunityImpl(COMMUNITY);
185: top.addEntity(new CommunityImpl(NESTED_COMMUNITY));
186: commMgr.addCommunity(top);
187:
188: Attributes attrs = new BasicAttributes();
189: attrs.put("CommunityManager", AGENT);
190: attrs.put("Parent", COMMUNITY);
191: Community nested = new CommunityImpl(NESTED_COMMUNITY, attrs);
192: nested.addEntity(new AgentImpl("Agent1", new BasicAttributes(
193: "Attr1", "Val1")));
194: nested.addEntity(new AgentImpl("Agent2", new BasicAttributes(
195: "Attr2", "Val2")));
196: nested.addEntity(new AgentImpl("Agent3", new BasicAttributes(
197: "Attr3", "Val3")));
198: commMgr.addCommunity(nested);
199:
200: final Semaphore s = new Semaphore(0);
201: try {
202: searchResults = commSvc.searchCommunity(COMMUNITY,
203: "(Attr2=Val2)",
204: false, // Flat search
205: Community.ALL_ENTITIES,
206: new CommunityResponseListener() {
207: public void getResponse(CommunityResponse resp) {
208: if (resp.getStatus() == CommunityResponse.SUCCESS) {
209: searchResults = (Collection) resp
210: .getContent();
211: }
212: s.release();
213: }
214: });
215: if (searchResults != null) {
216: s.release();
217: }
218: s.attempt(5000);
219: } catch (Exception ex) {
220: ex.printStackTrace();
221: fail();
222: }
223: assertTrue(searchResults != null && searchResults.size() == 0);
224: }
225:
226: /**
227: * Test nested search.
228: */
229: public void testNestedSearch() {
230: // Setup test state
231: Community top = new CommunityImpl(COMMUNITY);
232: top.addEntity(new CommunityImpl(NESTED_COMMUNITY));
233: commMgr.addCommunity(top);
234:
235: Attributes attrs = new BasicAttributes();
236: attrs.put("CommunityManager", AGENT);
237: attrs.put("Parent", COMMUNITY);
238: Community nested = new CommunityImpl(NESTED_COMMUNITY, attrs);
239: nested.addEntity(new AgentImpl("Agent1", new BasicAttributes(
240: "Attr1", "Val1")));
241: nested.addEntity(new AgentImpl("Agent2", new BasicAttributes(
242: "Attr2", "Val2")));
243: nested.addEntity(new AgentImpl("Agent3", new BasicAttributes(
244: "Attr3", "Val3")));
245: commMgr.addCommunity(nested);
246:
247: final Semaphore s = new Semaphore(0);
248: try {
249: searchResults = commSvc.searchCommunity(COMMUNITY,
250: "(Attr2=Val2)",
251: true, // Nested search
252: Community.ALL_ENTITIES,
253: new CommunityResponseListener() {
254: public void getResponse(CommunityResponse resp) {
255: if (resp.getStatus() == CommunityResponse.SUCCESS) {
256: searchResults = (Collection) resp
257: .getContent();
258: }
259: s.release();
260: }
261: });
262: if (searchResults != null) {
263: s.release();
264: }
265: s.attempt(5000);
266: } catch (Exception ex) {
267: ex.printStackTrace();
268: fail();
269: }
270: assertTrue(searchResults != null && searchResults.size() == 1
271: && resultsContains(searchResults, "Agent2"));
272: }
273:
274: /**
275: * Test listParentCommunities of one nested community
276: */
277: public void testListParentCommunities() {
278: // Setup test state
279: Community top = new CommunityImpl(COMMUNITY);
280: top.addEntity(new CommunityImpl(NESTED_COMMUNITY));
281: commMgr.addCommunity(top);
282:
283: Attributes attrs = new BasicAttributes();
284: attrs.put("CommunityManager", AGENT);
285: attrs.put("Parent", COMMUNITY);
286: Community nested = new CommunityImpl(NESTED_COMMUNITY, attrs);
287: nested.addEntity(new AgentImpl(AGENT));
288: commMgr.addCommunity(nested);
289:
290: final Semaphore s = new Semaphore(0);
291: //Get parents of the nested community, result should be 1 community.
292: try {
293: searchResults = commSvc.listParentCommunities(
294: NESTED_COMMUNITY, new CommunityResponseListener() {
295: public void getResponse(CommunityResponse resp) {
296: if (resp.getStatus() == CommunityResponse.SUCCESS) {
297: searchResults = (Collection) resp
298: .getContent();
299: }
300: s.release();
301: }
302: });
303: if (searchResults != null) {
304: s.release();
305: }
306: s.attempt(5000);
307: } catch (Exception ex) {
308: ex.printStackTrace();
309: fail();
310: }
311: assertTrue(searchResults != null && searchResults.size() == 1
312: && searchResults.contains(COMMUNITY));
313:
314: //Get parents of entity in nested community, result should be 1 community.
315: try {
316: searchResults = commSvc.listParentCommunities(AGENT,
317: new CommunityResponseListener() {
318: public void getResponse(CommunityResponse resp) {
319: if (resp.getStatus() == CommunityResponse.SUCCESS) {
320: searchResults = (Collection) resp
321: .getContent();
322: }
323: s.release();
324: }
325: });
326: if (searchResults != null) {
327: s.release();
328: }
329: s.attempt(5000);
330: } catch (Exception ex) {
331: ex.printStackTrace();
332: fail();
333: }
334: assertTrue(searchResults != null && searchResults.size() == 1
335: && searchResults.contains(NESTED_COMMUNITY));
336:
337: }
338:
339: private boolean resultsContains(Collection results,
340: String entityName) {
341: if (results != null) {
342: for (Iterator it = results.iterator(); it.hasNext();) {
343: Entity entity = (Entity) it.next();
344: if (entity.getName().equals(entityName))
345: return true;
346: }
347: }
348: return false;
349: }
350: }
|