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 javax.naming.directory.BasicAttributes;
031:
032: import junit.framework.*;
033:
034: import org.cougaar.core.service.community.Agent;
035: import org.cougaar.core.service.community.Community;
036: import org.cougaar.core.service.community.CommunityChangeEvent;
037: import org.cougaar.core.service.community.CommunityChangeListener;
038: import org.cougaar.core.service.community.CommunityResponse;
039: import org.cougaar.core.service.community.CommunityResponseListener;
040:
041: import org.cougaar.community.CommunityImpl;
042: import org.cougaar.community.AgentImpl;
043: import org.cougaar.community.CommunityMemberships;
044: import org.cougaar.community.CommunityMembershipsListener;
045: import org.cougaar.community.CommunityUtils;
046: import org.cougaar.community.util.Semaphore;
047:
048: /**
049: * Test automatic re-join capability when community memberships are unexpectedly
050: * lost.
051: *
052: */
053: public class MembershipWatcherTest extends TestBase {
054:
055: protected static final long TIMEOUT = 5000;
056: protected static final String AGENT = "Test_Agent";
057: protected static final String COMMUNITY = "Test_Community";
058: protected static final String SUBCOMMUNITY = "Test_SubCommunity";
059: protected CommunityServiceTestImpl commSvc;
060: protected CommunityManagerTestImpl commMgr;
061: protected CommunityResponse commResp; // Callback response
062: protected CommunityChangeEvent commChangeEvent;
063:
064: protected static final String loggingProps[][] = {
065: { "log4j.category.org.cougaar.community", "INFO" },
066: { "log4j.category.org.cougaar.community.MembershipWatcher",
067: "WARN" },
068: { "log4j.category.org.cougaar.community.test", "INFO" } };
069:
070: public MembershipWatcherTest(String name) {
071: super (name, loggingProps);
072: }
073:
074: protected void setUp() {
075: commSvc = new CommunityServiceTestImpl(AGENT);
076: commMgr = CommunityManagerTestImpl.getInstance();
077: commMgr.reset();
078: commResp = null; // Clear response before each test
079: commChangeEvent = null;
080: commSvc.getCache().clear();
081: }
082:
083: public static Test suite() {
084: return new TestSuite(MembershipWatcherTest.class);
085: //Use following to run specific tests only
086: //TestSuite suite= new TestSuite();
087: //suite.addTest(new MembershipWatcherTest("testJoinCommunity"));
088: //return suite;
089:
090: }
091:
092: /**
093: * Test ability of MembershipWatcher to detect the addition of a new
094: * agent to a community.
095: */
096: public void testJoinCommunity() {
097: final Semaphore s = new Semaphore(-1);
098: CommunityMemberships myCommunities = commSvc
099: .getCommunityMemberships();
100: myCommunities.addListener(new CommunityMembershipsListener() {
101: public void membershipsChanged() {
102: s.release();
103: }
104: });
105: CommunityResponseListener crl = new CommunityResponseListener() {
106: public void getResponse(CommunityResponse resp) {
107: commResp = resp;
108: s.release();
109: }
110: };
111: //test one agent joins the community
112: try {
113: commSvc.joinCommunity(COMMUNITY, AGENT, commSvc.AGENT,
114: null, true, null, crl);
115: s.attempt(TIMEOUT);
116: } catch (Exception ex) {
117: ex.printStackTrace();
118: fail();
119: }
120: Community community = (Community) commResp.getContent();
121: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
122: && community != null && community.hasEntity(AGENT)
123: && myCommunities.contains(COMMUNITY, AGENT));
124: }
125:
126: /**
127: * Test ability of MembershipWatcher to detect when an agent leaves a
128: * community.
129: */
130: public void testLeaveCommunity() {
131: final Semaphore s = new Semaphore(-1);
132:
133: Community comm = new CommunityImpl(COMMUNITY);
134: Agent agent = new AgentImpl(AGENT);
135: comm.addEntity(agent);
136: commMgr.addCommunity(comm);
137:
138: CommunityMemberships myCommunities = commSvc
139: .getCommunityMemberships();
140: myCommunities.add(COMMUNITY, agent);
141: myCommunities.addListener(new CommunityMembershipsListener() {
142: public void membershipsChanged() {
143: s.release();
144: }
145: });
146: CommunityResponseListener crl = new CommunityResponseListener() {
147: public void getResponse(CommunityResponse resp) {
148: commResp = resp;
149: s.release();
150: }
151: };
152:
153: try {
154: commSvc.leaveCommunity(COMMUNITY, AGENT, crl);
155: s.attempt(TIMEOUT);
156: } catch (Exception ex) {
157: ex.printStackTrace();
158: fail();
159: }
160: Community community = (Community) commResp.getContent();
161: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
162: && community != null && !community.hasEntity(AGENT)
163: && myCommunities.listCommunities().isEmpty());
164: }
165:
166: /**
167: * Test ability of MembershipWatcher to detect an inconsistency with
168: * memberships and community state. An automatic re-join of community
169: * is initiated.
170: */
171: public void testAutoRejoin() {
172: final Semaphore s = new Semaphore(-1);
173:
174: Community comm = new CommunityImpl(COMMUNITY);
175: commMgr.addCommunity(comm);
176:
177: CommunityMemberships myCommunities = commSvc
178: .getCommunityMemberships();
179: myCommunities.add(COMMUNITY, new AgentImpl(AGENT));
180: myCommunities.addListener(new CommunityMembershipsListener() {
181: public void membershipsChanged() {
182: s.release();
183: }
184: });
185:
186: commSvc.addListener(new CommunityChangeListener() {
187: public String getCommunityName() {
188: return COMMUNITY;
189: }
190:
191: public void communityChanged(CommunityChangeEvent cce) {
192: if (cce.getType() == cce.ADD_ENTITY
193: && cce.getCommunityName().equals(COMMUNITY)
194: && AGENT.equals(cce.getWhatChanged())) {
195: commChangeEvent = cce;
196: commSvc.removeListener(this );
197: s.release();
198: }
199: }
200: });
201:
202: try {
203: commSvc.getMembershipWatcher().validate();
204: s.attempt(TIMEOUT);
205: } catch (Exception ex) {
206: ex.printStackTrace();
207: fail();
208: }
209: Community community = commChangeEvent != null ? commChangeEvent
210: .getCommunity() : null;
211: assertTrue(community != null && community.hasEntity(AGENT)
212: && myCommunities.contains(COMMUNITY, AGENT));
213: }
214:
215: /**
216: * Test ability of MembershipWatcher to detect an inconsistency with
217: * memberships and community state. An automatic re-join of a nested community
218: * is initiated.
219: */
220: public void testAutoRejoinNestedCommunity() {
221: final Semaphore s = new Semaphore(-1);
222:
223: Community comm = new CommunityImpl(COMMUNITY);
224: commMgr.addCommunity(comm);
225:
226: Community nestedComm = new CommunityImpl(SUBCOMMUNITY,
227: new BasicAttributes("CommunityManager", AGENT));
228: nestedComm.addEntity(new AgentImpl(AGENT));
229: commMgr.addCommunity(nestedComm);
230:
231: CommunityMemberships myCommunities = commSvc
232: .getCommunityMemberships();
233: myCommunities.add(SUBCOMMUNITY, new AgentImpl(AGENT));
234: myCommunities.add(COMMUNITY, new CommunityImpl(SUBCOMMUNITY));
235:
236: myCommunities.addListener(new CommunityMembershipsListener() {
237: public void membershipsChanged() {
238: s.release();
239: }
240: });
241:
242: commSvc.addListener(new CommunityChangeListener() {
243: public String getCommunityName() {
244: return COMMUNITY;
245: }
246:
247: public void communityChanged(CommunityChangeEvent cce) {
248: if (cce.getType() == cce.ADD_ENTITY
249: && cce.getCommunityName().equals(COMMUNITY)
250: && SUBCOMMUNITY.equals(cce.getWhatChanged())) {
251: commChangeEvent = cce;
252: commSvc.removeListener(this );
253: s.release();
254: }
255: }
256: });
257:
258: try {
259: commSvc.getMembershipWatcher().validate();
260: s.attempt(TIMEOUT);
261: } catch (Exception ex) {
262: ex.printStackTrace();
263: fail();
264: }
265: //System.out.println(((CommunityServiceTestImpl)commSvc).getCache());
266: Community community = commChangeEvent != null ? commChangeEvent
267: .getCommunity() : null;
268: Community nestedCommunity = commSvc.getCommunity(SUBCOMMUNITY,
269: null);
270: assertTrue(community != null
271: && community.hasEntity(SUBCOMMUNITY)
272: && nestedCommunity != null
273: && CommunityUtils.hasAttribute(nestedCommunity
274: .getAttributes(), "Parent", COMMUNITY));
275: }
276:
277: }
|