Source Code Cross Referenced for IoFilterChainTest.java in  » Net » mina-2.0.0-M1 » org » apache » mina » common » 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 » Net » mina 2.0.0 M1 » org.apache.mina.common 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one
003:         *  or more contributor license agreements.  See the NOTICE file
004:         *  distributed with this work for additional information
005:         *  regarding copyright ownership.  The ASF licenses this file
006:         *  to you under the Apache License, Version 2.0 (the
007:         *  "License"); you may not use this file except in compliance
008:         *  with the License.  You may obtain a copy of the License at
009:         *
010:         *    http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         *  Unless required by applicable law or agreed to in writing,
013:         *  software distributed under the License is distributed on an
014:         *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         *  KIND, either express or implied.  See the License for the
016:         *  specific language governing permissions and limitations
017:         *  under the License.
018:         *
019:         */
020:        package org.apache.mina.common;
021:
022:        import junit.framework.Assert;
023:        import junit.framework.TestCase;
024:
025:        import org.apache.mina.common.IoFilterChain.Entry;
026:
027:        /**
028:         * Tests {@link DefaultIoFilterChain}.
029:         *
030:         * @author The Apache MINA Project (dev@mina.apache.org)
031:         * @version $Rev: 576271 $, $Date: 2007-09-17 00:43:04 -0600 (Mon, 17 Sep 2007) $
032:         */
033:        public class IoFilterChainTest extends TestCase {
034:            private DummySession session;
035:            private IoFilterChain chain;
036:            private String result;
037:
038:            private final IoHandler handler = new IoHandlerAdapter() {
039:                @Override
040:                public void sessionCreated(IoSession session) {
041:                    result += "HS0";
042:                }
043:
044:                @Override
045:                public void sessionOpened(IoSession session) {
046:                    result += "HSO";
047:                }
048:
049:                @Override
050:                public void sessionClosed(IoSession session) {
051:                    result += "HSC";
052:                }
053:
054:                @Override
055:                public void sessionIdle(IoSession session, IdleStatus status) {
056:                    result += "HSI";
057:                }
058:
059:                @Override
060:                public void exceptionCaught(IoSession session, Throwable cause) {
061:                    result += "HEC";
062:                    if (cause.getClass() != Exception.class) {
063:                        cause.printStackTrace(System.out);
064:                    }
065:                }
066:
067:                @Override
068:                public void messageReceived(IoSession session, Object message) {
069:                    result += "HMR";
070:                }
071:
072:                @Override
073:                public void messageSent(IoSession session, Object message) {
074:                    result += "HMS";
075:                }
076:            };
077:
078:            @Override
079:            public void setUp() {
080:                session = new DummySession();
081:                session.setHandler(handler);
082:                chain = session.getFilterChain();
083:                result = "";
084:            }
085:
086:            @Override
087:            public void tearDown() {
088:            }
089:
090:            public void testAdd() throws Exception {
091:                chain.addFirst("A", new EventOrderTestFilter('A'));
092:                chain.addLast("B", new EventOrderTestFilter('A'));
093:                chain.addFirst("C", new EventOrderTestFilter('A'));
094:                chain.addLast("D", new EventOrderTestFilter('A'));
095:                chain.addBefore("B", "E", new EventOrderTestFilter('A'));
096:                chain.addBefore("C", "F", new EventOrderTestFilter('A'));
097:                chain.addAfter("B", "G", new EventOrderTestFilter('A'));
098:                chain.addAfter("D", "H", new EventOrderTestFilter('A'));
099:
100:                String actual = "";
101:                for (Entry e : chain.getAll()) {
102:                    actual += e.getName();
103:                }
104:
105:                Assert.assertEquals("FCAEBGDH", actual);
106:            }
107:
108:            public void testGet() throws Exception {
109:                IoFilter filterA = new IoFilterAdapter();
110:                IoFilter filterB = new IoFilterAdapter();
111:                IoFilter filterC = new IoFilterAdapter();
112:                IoFilter filterD = new IoFilterAdapter();
113:
114:                chain.addFirst("A", filterA);
115:                chain.addLast("B", filterB);
116:                chain.addBefore("B", "C", filterC);
117:                chain.addAfter("A", "D", filterD);
118:
119:                Assert.assertSame(filterA, chain.get("A"));
120:                Assert.assertSame(filterB, chain.get("B"));
121:                Assert.assertSame(filterC, chain.get("C"));
122:                Assert.assertSame(filterD, chain.get("D"));
123:            }
124:
125:            public void testRemove() throws Exception {
126:                chain.addLast("A", new EventOrderTestFilter('A'));
127:                chain.addLast("B", new EventOrderTestFilter('A'));
128:                chain.addLast("C", new EventOrderTestFilter('A'));
129:                chain.addLast("D", new EventOrderTestFilter('A'));
130:                chain.addLast("E", new EventOrderTestFilter('A'));
131:
132:                chain.remove("A");
133:                chain.remove("E");
134:                chain.remove("C");
135:                chain.remove("B");
136:                chain.remove("D");
137:
138:                Assert.assertEquals(0, chain.getAll().size());
139:            }
140:
141:            public void testClear() throws Exception {
142:                chain.addLast("A", new EventOrderTestFilter('A'));
143:                chain.addLast("B", new EventOrderTestFilter('A'));
144:                chain.addLast("C", new EventOrderTestFilter('A'));
145:                chain.addLast("D", new EventOrderTestFilter('A'));
146:                chain.addLast("E", new EventOrderTestFilter('A'));
147:
148:                chain.clear();
149:
150:                Assert.assertEquals(0, chain.getAll().size());
151:            }
152:
153:            public void testToString() throws Exception {
154:                // When the chain is empty
155:                Assert.assertEquals("{ empty }", chain.toString());
156:
157:                // When there's one filter
158:                chain.addLast("A", new IoFilterAdapter() {
159:                    @Override
160:                    public String toString() {
161:                        return "B";
162:                    }
163:                });
164:                Assert.assertEquals("{ (A:B) }", chain.toString());
165:
166:                // When there are two
167:                chain.addLast("C", new IoFilterAdapter() {
168:                    @Override
169:                    public String toString() {
170:                        return "D";
171:                    }
172:                });
173:                Assert.assertEquals("{ (A:B), (C:D) }", chain.toString());
174:            }
175:
176:            public void testDefault() {
177:                run("HS0 HSO HMR HMS HSI HEC HSC");
178:            }
179:
180:            public void testChained() throws Exception {
181:                chain.addLast("A", new EventOrderTestFilter('A'));
182:                chain.addLast("B", new EventOrderTestFilter('B'));
183:                run("AS0 BS0 HS0" + "ASO BSO HSO" + "AMR BMR HMR"
184:                        + "BFW AFW AMS BMS HMS" + "ASI BSI HSI" + "AEC BEC HEC"
185:                        + "ASC BSC HSC");
186:            }
187:
188:            public void testAddRemove() throws Exception {
189:                IoFilter filter = new AddRemoveTestFilter();
190:
191:                chain.addFirst("A", filter);
192:                assertEquals("ADDED", result);
193:
194:                chain.remove("A");
195:                assertEquals("ADDEDREMOVED", result);
196:            }
197:
198:            private void run(String expectedResult) {
199:                chain.fireSessionCreated();
200:                chain.fireSessionOpened();
201:                chain.fireMessageReceived(new Object());
202:                chain.fireFilterWrite(new DefaultWriteRequest(new Object()));
203:                chain.fireSessionIdle(IdleStatus.READER_IDLE);
204:                chain.fireExceptionCaught(new Exception());
205:                chain.fireSessionClosed();
206:
207:                result = formatResult(result);
208:                expectedResult = formatResult(expectedResult);
209:
210:                System.out.println("Expected: " + expectedResult);
211:                System.out.println("Actual:   " + result);
212:                Assert.assertEquals(expectedResult, result);
213:            }
214:
215:            private String formatResult(String result) {
216:                result = result.replaceAll("\\s", "");
217:                StringBuffer buf = new StringBuffer(result.length() * 4 / 3);
218:                for (int i = 0; i < result.length(); i++) {
219:                    buf.append(result.charAt(i));
220:                    if (i % 3 == 2) {
221:                        buf.append(' ');
222:                    }
223:                }
224:
225:                return buf.toString();
226:            }
227:
228:            private class EventOrderTestFilter extends IoFilterAdapter {
229:                private final char id;
230:
231:                private EventOrderTestFilter(char id) {
232:                    this .id = id;
233:                }
234:
235:                @Override
236:                public void sessionCreated(NextFilter nextFilter,
237:                        IoSession session) {
238:                    result += id + "S0";
239:                    nextFilter.sessionCreated(session);
240:                }
241:
242:                @Override
243:                public void sessionOpened(NextFilter nextFilter,
244:                        IoSession session) {
245:                    result += id + "SO";
246:                    nextFilter.sessionOpened(session);
247:                }
248:
249:                @Override
250:                public void sessionClosed(NextFilter nextFilter,
251:                        IoSession session) {
252:                    result += id + "SC";
253:                    nextFilter.sessionClosed(session);
254:                }
255:
256:                @Override
257:                public void sessionIdle(NextFilter nextFilter,
258:                        IoSession session, IdleStatus status) {
259:                    result += id + "SI";
260:                    nextFilter.sessionIdle(session, status);
261:                }
262:
263:                @Override
264:                public void exceptionCaught(NextFilter nextFilter,
265:                        IoSession session, Throwable cause) {
266:                    result += id + "EC";
267:                    nextFilter.exceptionCaught(session, cause);
268:                }
269:
270:                @Override
271:                public void filterWrite(NextFilter nextFilter,
272:                        IoSession session, WriteRequest writeRequest) {
273:                    result += id + "FW";
274:                    nextFilter.filterWrite(session, writeRequest);
275:                }
276:
277:                @Override
278:                public void messageReceived(NextFilter nextFilter,
279:                        IoSession session, Object message) {
280:                    result += id + "MR";
281:                    nextFilter.messageReceived(session, message);
282:                }
283:
284:                @Override
285:                public void messageSent(NextFilter nextFilter,
286:                        IoSession session, WriteRequest writeRequest) {
287:                    result += id + "MS";
288:                    nextFilter.messageSent(session, writeRequest);
289:                }
290:
291:                @Override
292:                public void filterClose(NextFilter nextFilter, IoSession session)
293:                        throws Exception {
294:                    nextFilter.filterClose(session);
295:                }
296:            }
297:
298:            private class AddRemoveTestFilter extends IoFilterAdapter {
299:                @Override
300:                public void onPostAdd(IoFilterChain parent, String name,
301:                        NextFilter nextFilter) {
302:                    result += "ADDED";
303:                }
304:
305:                @Override
306:                public void onPostRemove(IoFilterChain parent, String name,
307:                        NextFilter nextFilter) {
308:                    result += "REMOVED";
309:                }
310:            }
311:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.