Source Code Cross Referenced for MembershipWatcherTest.java in  » Science » Cougaar12_4 » org » cougaar » community » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Cougaar12_4 » org.cougaar.community.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.