Source Code Cross Referenced for TestBoundedBuffer.java in  » Library » Apache-common-Collections » org » apache » commons » collections » buffer » 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 » Library » Apache common Collections » org.apache.commons.collections.buffer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright 2005-2006 The Apache Software Foundation
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */
016:        package org.apache.commons.collections.buffer;
017:
018:        import org.apache.commons.collections.AbstractTestObject;
019:        import org.apache.commons.collections.BoundedCollection;
020:        import org.apache.commons.collections.Buffer;
021:        import org.apache.commons.collections.BufferOverflowException;
022:
023:        import java.util.Iterator;
024:        import java.util.Collections;
025:        import java.util.Arrays;
026:
027:        import junit.framework.Test;
028:        import junit.framework.TestSuite;
029:
030:        public class TestBoundedBuffer extends AbstractTestObject {
031:
032:            public TestBoundedBuffer(String testName) {
033:                super (testName);
034:            }
035:
036:            public static Test suite() {
037:                return new TestSuite(TestBoundedBuffer.class);
038:            }
039:
040:            public static void main(String args[]) {
041:                String[] testCaseName = { TestBoundedBuffer.class.getName() };
042:                junit.textui.TestRunner.main(testCaseName);
043:            }
044:
045:            public String getCompatibilityVersion() {
046:                return "3.2";
047:            }
048:
049:            public boolean isEqualsCheckable() {
050:                return false;
051:            }
052:
053:            public Object makeObject() {
054:                return BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
055:            }
056:
057:            //-----------------------------------------------------------------------
058:            public void testMaxSize() {
059:                final Buffer bounded = BoundedBuffer.decorate(
060:                        new UnboundedFifoBuffer(), 2, 500);
061:                BoundedCollection bc = (BoundedCollection) bounded;
062:                assertEquals(2, bc.maxSize());
063:                assertEquals(false, bc.isFull());
064:                bounded.add("A");
065:                assertEquals(false, bc.isFull());
066:                bounded.add("B");
067:                assertEquals(true, bc.isFull());
068:                bounded.remove();
069:                assertEquals(false, bc.isFull());
070:                try {
071:                    BoundedBuffer.decorate(new UnboundedFifoBuffer(), 0);
072:                    fail();
073:                } catch (IllegalArgumentException ex) {
074:                }
075:                try {
076:                    BoundedBuffer.decorate(new UnboundedFifoBuffer(), -1);
077:                    fail();
078:                } catch (IllegalArgumentException ex) {
079:                }
080:            }
081:
082:            public void testAddToFullBufferNoTimeout() {
083:                final Buffer bounded = BoundedBuffer.decorate(
084:                        new UnboundedFifoBuffer(), 1);
085:                bounded.add("Hello");
086:                try {
087:                    bounded.add("World");
088:                    fail();
089:                } catch (BufferOverflowException e) {
090:                }
091:            }
092:
093:            public void testAddAllToFullBufferNoTimeout() {
094:                final Buffer bounded = BoundedBuffer.decorate(
095:                        new UnboundedFifoBuffer(), 1);
096:                bounded.add("Hello");
097:                try {
098:                    bounded.addAll(Collections.singleton("World"));
099:                    fail();
100:                } catch (BufferOverflowException e) {
101:                }
102:            }
103:
104:            public void testAddAllToEmptyBufferExceedMaxSizeNoTimeout() {
105:                final Buffer bounded = BoundedBuffer.decorate(
106:                        new UnboundedFifoBuffer(), 1);
107:                try {
108:                    bounded.addAll(Collections.nCopies(2, "test"));
109:                    fail();
110:                } catch (BufferOverflowException e) {
111:                }
112:            }
113:
114:            public void testAddToFullBufferRemoveViaIterator() {
115:                final Buffer bounded = BoundedBuffer.decorate(
116:                        new UnboundedFifoBuffer(), 1, 500);
117:                bounded.add("Hello");
118:                new DelayedIteratorRemove(bounded, 200).start();
119:                bounded.add("World");
120:                assertEquals(1, bounded.size());
121:                assertEquals("World", bounded.get());
122:
123:            }
124:
125:            public void testAddAllToFullBufferRemoveViaIterator() {
126:                final Buffer bounded = BoundedBuffer.decorate(
127:                        new UnboundedFifoBuffer(), 2, 500);
128:                bounded.add("Hello");
129:                bounded.add("World");
130:                new DelayedIteratorRemove(bounded, 200, 2).start();
131:                bounded.addAll(Arrays.asList(new String[] { "Foo", "Bar" }));
132:                assertEquals(2, bounded.size());
133:                assertEquals("Foo", bounded.remove());
134:                assertEquals("Bar", bounded.remove());
135:            }
136:
137:            public void testAddToFullBufferWithTimeout() {
138:                final Buffer bounded = BoundedBuffer.decorate(
139:                        new UnboundedFifoBuffer(), 1, 500);
140:                bounded.add("Hello");
141:                new DelayedRemove(bounded, 200).start();
142:                bounded.add("World");
143:                assertEquals(1, bounded.size());
144:                assertEquals("World", bounded.get());
145:                try {
146:                    bounded.add("!");
147:                    fail();
148:                } catch (BufferOverflowException e) {
149:                }
150:            }
151:
152:            public void testAddAllToFullBufferWithTimeout() {
153:                final Buffer bounded = BoundedBuffer.decorate(
154:                        new UnboundedFifoBuffer(), 2, 500);
155:                bounded.add("Hello");
156:                bounded.add("World");
157:                new DelayedRemove(bounded, 200, 2).start();
158:
159:                bounded.addAll(Arrays.asList(new String[] { "Foo", "Bar" }));
160:                assertEquals(2, bounded.size());
161:                assertEquals("Foo", bounded.get());
162:                try {
163:                    bounded.add("!");
164:                    fail();
165:                } catch (BufferOverflowException e) {
166:                }
167:            }
168:
169:            private class DelayedIteratorRemove extends Thread {
170:
171:                private final Buffer buffer;
172:
173:                private final long delay;
174:
175:                private final int nToRemove;
176:
177:                public DelayedIteratorRemove(Buffer buffer, long delay,
178:                        int nToRemove) {
179:                    this .buffer = buffer;
180:                    this .delay = delay;
181:                    this .nToRemove = nToRemove;
182:                }
183:
184:                public DelayedIteratorRemove(Buffer buffer, long delay) {
185:                    this (buffer, delay, 1);
186:                }
187:
188:                public void run() {
189:                    try {
190:                        Thread.sleep(delay);
191:                        Iterator iter = buffer.iterator();
192:                        for (int i = 0; i < nToRemove; ++i) {
193:                            iter.next();
194:                            iter.remove();
195:                        }
196:
197:                    } catch (InterruptedException e) {
198:                    }
199:                }
200:            }
201:
202:            private class DelayedRemove extends Thread {
203:
204:                private final Buffer buffer;
205:
206:                private final long delay;
207:
208:                private final int nToRemove;
209:
210:                public DelayedRemove(Buffer buffer, long delay, int nToRemove) {
211:                    this .buffer = buffer;
212:                    this .delay = delay;
213:                    this .nToRemove = nToRemove;
214:                }
215:
216:                public DelayedRemove(Buffer buffer, long delay) {
217:                    this (buffer, delay, 1);
218:                }
219:
220:                public void run() {
221:                    try {
222:                        Thread.sleep(delay);
223:                        for (int i = 0; i < nToRemove; ++i) {
224:                            buffer.remove();
225:                        }
226:                    } catch (InterruptedException e) {
227:                    }
228:                }
229:            }
230:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.