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 junit.framework.*;
030:
031: import org.cougaar.core.service.community.CommunityService;
032: import org.cougaar.core.service.community.Community;
033: import org.cougaar.core.service.community.CommunityResponse;
034: import org.cougaar.core.service.community.CommunityResponseListener;
035:
036: import org.cougaar.community.util.Semaphore;
037:
038: import java.util.*;
039:
040: /**
041: * Test CommunityService operations.
042: *
043: */
044: public class BookkeepingTest extends TestBase {
045:
046: protected static final long TIMEOUT = 5000;
047: protected static final String AGENT = "Test_Agent";
048: protected static final String COMMUNITY = "Test_Community";
049:
050: protected static final int NUM_AGENTS = 20;
051: protected CommunityService commSvc[];
052: protected CommunityManagerTestImpl commMgr;
053: protected Set joinSet;
054:
055: protected static final String loggingProps[][] = {
056: { "log4j.category.org.cougaar.community", "INFO" },
057: //{"log4j.category.org.cougaar.community.CommunityCache","DEBUG"},
058: { "log4j.category.org.cougaar.community.test", "INFO" } };
059:
060: public BookkeepingTest(String name) {
061: super (name, loggingProps);
062: }
063:
064: protected void setUp() {
065: commSvc = new CommunityService[NUM_AGENTS];
066: for (int i = 0; i < NUM_AGENTS; i++) {
067: commSvc[i] = new CommunityServiceTestImpl(AGENT + i);
068: ((CommunityServiceTestImpl) commSvc[i]).getCache().clear();
069: }
070: commMgr = CommunityManagerTestImpl.getInstance();
071: commMgr.reset();
072: joinSet = Collections.synchronizedSet(new HashSet());
073: }
074:
075: public static Test suite() {
076: return new TestSuite(BookkeepingTest.class);
077: /* Use following to run specific tests only
078: TestSuite suite= new TestSuite();
079: suite.addTest(new BasicTests("testGetParentCommunities"));
080: return suite;*/
081: }
082:
083: public static void main(String[] args) {
084: junit.textui.TestRunner.run(suite());
085: }
086:
087: protected void join(final int agentNum) {
088: final Semaphore s = new Semaphore(0);
089: CommunityResponseListener crl = new CommunityResponseListener() {
090: public void getResponse(CommunityResponse resp) {
091: Community community = (Community) resp.getContent();
092: if (resp.getStatus() == CommunityResponse.SUCCESS) {
093: joinSet.add(AGENT + agentNum);
094: //System.out.println("agent=" + AGENT+agentNum + " community=" + community.toXml());
095: assertTrue(community != null
096: && community.hasEntity(AGENT + agentNum));
097: } else {
098: fail();
099: }
100: s.release();
101: }
102: };
103: //test one agent joins the community
104: try {
105: commSvc[agentNum].joinCommunity(COMMUNITY,
106: AGENT + agentNum, CommunityService.AGENT, null,
107: true, null, crl);
108: s.attempt(TIMEOUT);
109: } catch (Exception ex) {
110: ex.printStackTrace();
111: fail();
112: }
113: }
114:
115: protected void leave(final int agentNum) {
116: final Semaphore s = new Semaphore(0);
117: CommunityResponseListener crl = new CommunityResponseListener() {
118: public void getResponse(CommunityResponse resp) {
119: if (resp.getStatus() == CommunityResponse.SUCCESS) {
120: joinSet.remove(AGENT + agentNum);
121: }
122: s.release();
123: }
124: };
125: //test one agent joins the community
126: try {
127: commSvc[agentNum].leaveCommunity(COMMUNITY, AGENT
128: + agentNum, crl);
129: s.attempt(TIMEOUT);
130: } catch (Exception ex) {
131: ex.printStackTrace();
132: fail();
133: }
134: }
135:
136: /**
137: * Perform multiple join/leaves and ensure community state is accurately
138: * reflected.
139: */
140: public void test1() {
141: for (int i = 0; i < NUM_AGENTS; i++) {
142: join(i);
143: Community community = commSvc[i].getCommunity(COMMUNITY,
144: null);
145: assertTrue(community != null
146: && community.getEntities().size() == i + 1
147: && community.hasEntity(AGENT + i));
148: Collection parents = commSvc[i].listParentCommunities(AGENT
149: + i, (CommunityResponseListener) null);
150: assertTrue(parents != null && parents.size() == 1
151: && parents.contains(COMMUNITY));
152: }
153: assertTrue(joinSet.size() == NUM_AGENTS);
154: for (int i = 0; i < NUM_AGENTS; i++) {
155: leave(i);
156: Community community = commSvc[i].getCommunity(COMMUNITY,
157: null);
158: assertTrue(community != null
159: && community.getEntities().size() == NUM_AGENTS - i
160: - 1 && !community.hasEntity(AGENT + i));
161: Collection parents = commSvc[i].listParentCommunities(AGENT
162: + i, (CommunityResponseListener) null);
163: assertTrue(parents != null && parents.size() == 0);
164: }
165: assertTrue(joinSet.size() == 0);
166: }
167:
168: }
|