Source Code Cross Referenced for AbstractCollectionTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » util » 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 » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.luni.tests.java.util;
019:
020:        import java.util.AbstractCollection;
021:        import java.util.Arrays;
022:        import java.util.Collection;
023:        import java.util.Iterator;
024:        import junit.framework.TestCase;
025:
026:        public class AbstractCollectionTest extends TestCase {
027:
028:            /**
029:             * @tests java.util.AbstractCollection#add(java.lang.Object)
030:             */
031:            public void test_addLjava_lang_Object() {
032:                AbstractCollection<Object> ac = new AbstractCollection<Object>() {
033:
034:                    @Override
035:                    public Iterator<Object> iterator() {
036:                        fail("iterator should not get called");
037:                        return null;
038:                    }
039:
040:                    @Override
041:                    public int size() {
042:                        fail("size should not get called");
043:                        return 0;
044:                    }
045:
046:                };
047:                try {
048:                    ac.add(null);
049:                } catch (UnsupportedOperationException e) {
050:                }
051:            }
052:
053:            /**
054:             * @tests java.util.AbstractCollection#addAll(java.util.Collection)
055:             */
056:            public void test_addAllLjava_util_Collection() {
057:                final Collection<String> fixtures = Arrays
058:                        .asList("0", "1", "2");
059:                AbstractCollection<String> ac = new AbstractCollection<String>() {
060:
061:                    @Override
062:                    public boolean add(String object) {
063:                        assertTrue(fixtures.contains(object));
064:                        return true;
065:                    }
066:
067:                    @Override
068:                    public Iterator<String> iterator() {
069:                        fail("iterator should not get called");
070:                        return null;
071:                    }
072:
073:                    @Override
074:                    public int size() {
075:                        fail("size should not get called");
076:                        return 0;
077:                    }
078:
079:                };
080:                assertTrue(ac.addAll(fixtures));
081:            }
082:
083:            /**
084:             * @tests java.util.AbstractCollection#containsAll(java.util.Collection)
085:             */
086:            public void test_containsAllLjava_util_Collection() {
087:                final Collection<String> fixtures = Arrays
088:                        .asList("0", "1", "2");
089:                AbstractCollection<String> ac = new AbstractCollection<String>() {
090:
091:                    @Override
092:                    public boolean contains(Object object) {
093:                        assertTrue(fixtures.contains(object));
094:                        return true;
095:                    }
096:
097:                    @Override
098:                    public Iterator<String> iterator() {
099:                        fail("iterator should not get called");
100:                        return null;
101:                    }
102:
103:                    @Override
104:                    public int size() {
105:                        fail("size should not get called");
106:                        return 0;
107:                    }
108:
109:                };
110:                assertTrue(ac.containsAll(fixtures));
111:            }
112:
113:            /**
114:             * @tests java.util.AbstractCollection#isEmpty()
115:             */
116:            public void test_isEmpty() {
117:                final boolean[] sizeCalled = new boolean[1];
118:                AbstractCollection<Object> ac = new AbstractCollection<Object>() {
119:                    @Override
120:                    public Iterator<Object> iterator() {
121:                        fail("iterator should not get called");
122:                        return null;
123:                    }
124:
125:                    @Override
126:                    public int size() {
127:                        sizeCalled[0] = true;
128:                        return 0;
129:                    }
130:                };
131:                assertTrue(ac.isEmpty());
132:                assertTrue(sizeCalled[0]);
133:            }
134:
135:            /**
136:             * @tests java.util.AbstractCollection#removeAll(java.util.Collection)
137:             */
138:            public void test_removeAllLjava_util_Collection() {
139:                final String[] removed = new String[3];
140:                AbstractCollection<String> ac = new AbstractCollection<String>() {
141:
142:                    @Override
143:                    public Iterator<String> iterator() {
144:                        return new Iterator<String>() {
145:                            String[] values = new String[] { "0", "1", "2" };
146:                            int index;
147:
148:                            public boolean hasNext() {
149:                                return index < values.length;
150:                            }
151:
152:                            public String next() {
153:                                return values[index++];
154:                            }
155:
156:                            public void remove() {
157:                                removed[index - 1] = values[index - 1];
158:                            }
159:
160:                        };
161:                    }
162:
163:                    @Override
164:                    public int size() {
165:                        fail("size should not get called");
166:                        return 0;
167:                    }
168:
169:                };
170:                assertTrue(ac.removeAll(Arrays.asList("0", "1", "2")));
171:                for (String r : removed) {
172:                    if (!"0".equals(r) && !"1".equals(r) && !"2".equals(r)) {
173:                        fail("an unexpected element was removed");
174:                    }
175:                }
176:            }
177:
178:            /**
179:             * @tests java.util.AbstractCollection#retainAll(java.util.Collection)
180:             */
181:            public void test_retainAllLjava_util_Collection() {
182:                final String[] removed = new String[1];
183:                AbstractCollection<String> ac = new AbstractCollection<String>() {
184:
185:                    @Override
186:                    public Iterator<String> iterator() {
187:                        return new Iterator<String>() {
188:                            String[] values = new String[] { "0", "1", "2" };
189:                            int index;
190:
191:                            public boolean hasNext() {
192:                                return index < values.length;
193:                            }
194:
195:                            public String next() {
196:                                return values[index++];
197:                            }
198:
199:                            public void remove() {
200:                                removed[index - 1] = values[index - 1];
201:                            }
202:
203:                        };
204:                    }
205:
206:                    @Override
207:                    public int size() {
208:                        fail("size should not get called");
209:                        return 0;
210:                    }
211:
212:                };
213:                assertTrue(ac.retainAll(Arrays.asList("1", "2")));
214:                assertEquals("0", removed[0]);
215:            }
216:
217:            /**
218:             * @tests java.util.AbstractCollection#toArray()
219:             */
220:            public void test_toArray() {
221:                AbstractCollection<String> ac = new AbstractCollection<String>() {
222:                    @Override
223:                    public Iterator<String> iterator() {
224:                        return new Iterator<String>() {
225:                            String[] values = new String[] { "0", "1", "2" };
226:                            int index;
227:
228:                            public boolean hasNext() {
229:                                return index < values.length;
230:                            }
231:
232:                            public String next() {
233:                                return values[index++];
234:                            }
235:
236:                            public void remove() {
237:                                fail("remove should not get called");
238:                            }
239:
240:                        };
241:                    }
242:
243:                    @Override
244:                    public int size() {
245:                        return 3;
246:                    }
247:                };
248:
249:                Object[] array = ac.toArray();
250:                assertEquals(3, array.length);
251:                for (Object o : array) {
252:                    if (!"0".equals(o) && !"1".equals(o) && !"2".equals(o)) {
253:                        fail("an unexpected element was removed");
254:                    }
255:                }
256:            }
257:
258:            /**
259:             * @tests java.util.AbstractCollection#toArray(java.lang.Object[])
260:             */
261:            public void test_toArray$Ljava_lang_Object() {
262:                AbstractCollection<String> ac = new AbstractCollection<String>() {
263:                    @Override
264:                    public Iterator<String> iterator() {
265:                        return new Iterator<String>() {
266:                            String[] values = new String[] { "0", "1", "2" };
267:                            int index;
268:
269:                            public boolean hasNext() {
270:                                return index < values.length;
271:                            }
272:
273:                            public String next() {
274:                                return values[index++];
275:                            }
276:
277:                            public void remove() {
278:                                fail("remove should not get called");
279:                            }
280:
281:                        };
282:                    }
283:
284:                    @Override
285:                    public int size() {
286:                        return 3;
287:                    }
288:                };
289:                try {
290:                    ac.toArray(null);
291:                    fail("No expected NullPointerException");
292:                } catch (NullPointerException e) {
293:                    // expected
294:                }
295:
296:                try {
297:                    ac.toArray(new StringBuffer[ac.size()]);
298:                    fail("No expected ArrayStoreException");
299:                } catch (ArrayStoreException e) {
300:                    // expected
301:                }
302:
303:                String[] a = new String[3];
304:                assertSame(a, ac.toArray(a));
305:
306:                a = new String[0];
307:                assertNotSame(a, ac.toArray(a));
308:                a = ac.toArray(a);
309:                assertEquals(3, a.length);
310:
311:                CharSequence[] csa = new CharSequence[3];
312:                ac.toArray(csa);
313:                assertEquals(3, csa.length);
314:                assertEquals("0", csa[0]);
315:                assertEquals("1", csa[1]);
316:                assertEquals("2", csa[2]);
317:            }
318:
319:            /**
320:             * @tests java.util.AbstractCollection#toString()
321:             */
322:            public void test_toString() {
323:                // see HARMONY-1522
324:                // collection that returns null iterator(this is against the spec.)
325:                AbstractCollection<?> c = new AbstractCollection<Object>() {
326:                    @Override
327:                    public int size() {
328:                        // return non-zero value to pass 'isEmpty' check
329:                        return 1;
330:                    }
331:
332:                    @Override
333:                    public Iterator<Object> iterator() {
334:                        // this violates the spec.
335:                        return null;
336:                    }
337:                };
338:
339:                try {
340:                    // AbstractCollection.toString() doesn't verify
341:                    // whether iterator() returns null value or not
342:                    c.toString();
343:                    fail("No expected NullPointerException");
344:                } catch (NullPointerException e) {
345:                }
346:            }
347:        }
w___ww_.ja___v__a2__s_._c_o__m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.