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:
031: import junit.framework.*;
032:
033: import javax.naming.directory.BasicAttribute;
034: import javax.naming.directory.Attributes;
035: import javax.naming.directory.BasicAttributes;
036: import javax.naming.directory.ModificationItem;
037:
038: import org.cougaar.core.service.community.CommunityService;
039: import org.cougaar.core.service.community.Community;
040: import org.cougaar.core.service.community.Agent;
041: import org.cougaar.core.service.community.CommunityChangeListener;
042: import org.cougaar.core.service.community.CommunityChangeEvent;
043: import org.cougaar.core.service.community.CommunityResponse;
044: import org.cougaar.core.service.community.CommunityResponseListener;
045:
046: import org.cougaar.community.CommunityImpl;
047: import org.cougaar.community.AgentImpl;
048: import org.cougaar.community.CommunityUtils;
049: import org.cougaar.community.util.Semaphore;
050:
051: /**
052: * Test Change notification operations.
053: *
054: */
055: public class ChangeListenerTest extends TestBase {
056:
057: protected static final String AGENT = "Test_Agent";
058: protected static final String COMMUNITY = "Test_Community";
059: protected CommunityService 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.CommunityCache","DEBUG"},
067: { "log4j.category.org.cougaar.community.test", "INFO" } };
068:
069: public ChangeListenerTest(String name) {
070: super (name, loggingProps);
071: }
072:
073: protected void setUp() {
074: commSvc = new CommunityServiceTestImpl(AGENT);
075: commMgr = CommunityManagerTestImpl.getInstance();
076: commMgr.reset();
077: commResp = null; // Clear response before each test
078: commChangeEvent = null;
079: ((CommunityServiceTestImpl) commSvc).getCache().clear();
080: }
081:
082: public static Test suite() {
083: return new TestSuite(ChangeListenerTest.class);
084: //Use following to run specific tests only
085: //TestSuite suite= new TestSuite();
086: //suite.addTest(new ChangeListenerTest("testAddCommunity"));
087: //return suite;
088: }
089:
090: /**
091: * Test ADD_COMMUNITY change event.
092: */
093: public void testAddCommunity() {
094: final Semaphore s = new Semaphore(-1);
095: commSvc.addListener(new CommunityChangeListener() {
096: public String getCommunityName() {
097: return COMMUNITY;
098: }
099:
100: public void communityChanged(CommunityChangeEvent cce) {
101: //System.out.println(cce);
102: if (cce.getType() == cce.ADD_COMMUNITY
103: && cce.getCommunityName().equals(COMMUNITY)) {
104: commChangeEvent = cce;
105: commSvc.removeListener(this );
106: s.release();
107: }
108: }
109: });
110: CommunityResponseListener crl = new CommunityResponseListener() {
111: public void getResponse(CommunityResponse resp) {
112: commResp = resp;
113: s.release();
114: }
115: };
116: try {
117: commSvc.joinCommunity(COMMUNITY, AGENT, commSvc.AGENT,
118: null, true, null, crl);
119: s.attempt(5000);
120: } catch (Exception ex) {
121: ex.printStackTrace();
122: fail();
123: }
124: Community communityFromResponse = (Community) commResp
125: .getContent();
126: //System.out.println(communityFromResponse.toXml());
127: Community communityFromEvent = commChangeEvent.getCommunity();
128: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
129: && communityFromResponse != null
130: && communityFromEvent != null
131: && commChangeEvent.getType() == commChangeEvent.ADD_COMMUNITY);
132: }
133:
134: /**
135: * Test ADD_ENTITY change event.
136: */
137: public void testAddEntity() {
138: final Semaphore s = new Semaphore(-1);
139: commSvc.addListener(new CommunityChangeListener() {
140: public String getCommunityName() {
141: return COMMUNITY;
142: }
143:
144: public void communityChanged(CommunityChangeEvent cce) {
145: if (cce.getType() == cce.ADD_ENTITY
146: && cce.getCommunityName().equals(COMMUNITY)
147: && cce.getWhatChanged().equals(AGENT)) {
148: commChangeEvent = cce;
149: commSvc.removeListener(this );
150: s.release();
151: }
152: }
153: });
154: CommunityResponseListener crl = new CommunityResponseListener() {
155: public void getResponse(CommunityResponse resp) {
156: commResp = resp;
157: s.release();
158: }
159: };
160: try {
161: commSvc.joinCommunity(COMMUNITY, AGENT, commSvc.AGENT,
162: null, true, null, crl);
163: s.attempt(5000);
164: } catch (Exception ex) {
165: ex.printStackTrace();
166: fail();
167: }
168: Community communityFromResponse = (Community) commResp
169: .getContent();
170: Community communityFromEvent = commChangeEvent.getCommunity();
171: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
172: && communityFromResponse != null
173: && communityFromEvent != null
174: && commChangeEvent.getType() == commChangeEvent.ADD_ENTITY
175: && AGENT.equals(commChangeEvent.getWhatChanged()));
176: }
177:
178: /**
179: * Test REMOVE_ENTITY change event.
180: */
181: public void testRemoveEntity() {
182: Community comm = new CommunityImpl(COMMUNITY);
183: comm.addEntity(new AgentImpl(AGENT));
184: commMgr.addCommunity(comm);
185:
186: final Semaphore s = new Semaphore(-1);
187: commSvc.addListener(new CommunityChangeListener() {
188: public String getCommunityName() {
189: return COMMUNITY;
190: }
191:
192: public void communityChanged(CommunityChangeEvent cce) {
193: if (cce.getType() == cce.REMOVE_ENTITY
194: && cce.getCommunityName().equals(COMMUNITY)) {
195: commChangeEvent = cce;
196: commSvc.removeListener(this );
197: s.release();
198: }
199: }
200: });
201: CommunityResponseListener crl = new CommunityResponseListener() {
202: public void getResponse(CommunityResponse resp) {
203: commResp = resp;
204: s.release();
205: }
206: };
207: try {
208: commSvc.leaveCommunity(COMMUNITY, AGENT, crl);
209: s.attempt(5000);
210: } catch (Exception ex) {
211: ex.printStackTrace();
212: fail();
213: }
214: Community communityFromResponse = (Community) commResp
215: .getContent();
216: Community communityFromEvent = commChangeEvent.getCommunity();
217: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
218: && communityFromResponse != null
219: && communityFromEvent != null
220: && commChangeEvent.getType() == commChangeEvent.REMOVE_ENTITY
221: && AGENT.equals(commChangeEvent.getWhatChanged()));
222: }
223:
224: /**
225: * Test ENTITY_ATTRIBUTES_CHANGED change event.
226: */
227: public void testEntityAttributesChanged() {
228: Community comm = new CommunityImpl(COMMUNITY);
229: Attributes entityAttrs = new BasicAttributes();
230: entityAttrs.put(new BasicAttribute("TestAttr1", "Val1"));
231: Agent agent = new AgentImpl(AGENT, entityAttrs);
232: comm.addEntity(agent);
233: commMgr.addCommunity(comm);
234:
235: Attributes changes = new BasicAttributes();
236: changes.put(new BasicAttribute("TestAttr1", "Val1"));
237: changes.put(new BasicAttribute("TestAttr2", "Val2"));
238:
239: ModificationItem[] attrMods = CommunityUtils
240: .getAttributeModificationItems(entityAttrs, changes);
241:
242: final Semaphore s = new Semaphore(-1);
243: commSvc.addListener(new CommunityChangeListener() {
244: public String getCommunityName() {
245: return COMMUNITY;
246: }
247:
248: public void communityChanged(CommunityChangeEvent cce) {
249: if (cce.getType() == cce.ENTITY_ATTRIBUTES_CHANGED
250: && cce.getCommunityName().equals(COMMUNITY)) {
251: commChangeEvent = cce;
252: commSvc.removeListener(this );
253: s.release();
254: }
255: }
256: });
257: CommunityResponseListener crl = new CommunityResponseListener() {
258: public void getResponse(CommunityResponse resp) {
259: commResp = resp;
260: s.release();
261: }
262: };
263: try {
264: commSvc.modifyAttributes(COMMUNITY, AGENT, attrMods, crl);
265: s.attempt(5000);
266: } catch (Exception ex) {
267: ex.printStackTrace();
268: fail();
269: }
270: Community communityFromResponse = (Community) commResp
271: .getContent();
272: Community communityFromEvent = commChangeEvent.getCommunity();
273:
274: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
275: && communityFromResponse != null
276: && communityFromEvent != null
277: && commChangeEvent.getType() == commChangeEvent.ENTITY_ATTRIBUTES_CHANGED
278: && AGENT.equals(commChangeEvent.getWhatChanged()));
279: }
280:
281: /**
282: * Test COMMUNITY_ATTRIBUTES_CHANGED change event.
283: */
284: public void testCommunityAttributesChanged() {
285: Attributes commAttrs = new BasicAttributes();
286:
287: commAttrs.put(new BasicAttribute("TestAttr1", "Val1"));
288: Community comm = new CommunityImpl(COMMUNITY, commAttrs);
289:
290: comm.addEntity(new AgentImpl(AGENT));
291: commMgr.addCommunity(comm);
292:
293: Attributes changes = new BasicAttributes();
294: changes.put(new BasicAttribute("TestAttr1", "Val1"));
295: changes.put(new BasicAttribute("TestAttr2", "Val2"));
296:
297: ModificationItem[] attrMods = CommunityUtils
298: .getAttributeModificationItems(commAttrs, changes);
299:
300: final Semaphore s = new Semaphore(-1);
301: commSvc.addListener(new CommunityChangeListener() {
302: public String getCommunityName() {
303: return COMMUNITY;
304: }
305:
306: public void communityChanged(CommunityChangeEvent cce) {
307: if (cce.getType() == cce.COMMUNITY_ATTRIBUTES_CHANGED
308: && cce.getCommunityName().equals(COMMUNITY)) {
309: commChangeEvent = cce;
310: commSvc.removeListener(this );
311: s.release();
312: }
313: }
314: });
315: CommunityResponseListener crl = new CommunityResponseListener() {
316: public void getResponse(CommunityResponse resp) {
317: commResp = resp;
318: s.release();
319: }
320: };
321: try {
322: commSvc.modifyAttributes(COMMUNITY, null, attrMods, crl);
323: s.attempt(5000);
324: } catch (Exception ex) {
325: ex.printStackTrace();
326: fail();
327: }
328: Community communityFromResponse = (Community) commResp
329: .getContent();
330: Community communityFromEvent = commChangeEvent.getCommunity();
331:
332: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
333: && communityFromResponse != null
334: && communityFromEvent != null
335: && commChangeEvent.getType() == commChangeEvent.COMMUNITY_ATTRIBUTES_CHANGED
336: && COMMUNITY.equals(commChangeEvent.getWhatChanged()));
337: }
338:
339: }
|