Source Code Cross Referenced for ContextBaseTestCase.java in  » Library » Apache-commons-chain-1.1-src » org » apache » commons » chain » impl » 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 commons chain 1.1 src » org.apache.commons.chain.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1999-2004 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.chain.impl;
017:
018:        import java.io.ByteArrayInputStream;
019:        import java.io.ByteArrayOutputStream;
020:        import java.io.ObjectInputStream;
021:        import java.io.ObjectOutputStream;
022:        import java.util.ArrayList;
023:        import java.util.Collection;
024:        import java.util.HashMap;
025:        import java.util.Iterator;
026:        import java.util.Map;
027:        import java.util.Set;
028:
029:        import junit.framework.Test;
030:        import junit.framework.TestCase;
031:        import junit.framework.TestSuite;
032:        import org.apache.commons.chain.Context;
033:        import org.apache.commons.chain.web.WebContext;
034:
035:        /**
036:         * <p>Test case for the <code>ContextBase</code> class.</p>
037:         *
038:         * @author Craig R. McClanahan
039:         * @version $Revision: 161600 $ $Date: 2005-04-16 20:55:37 +0100 (Sat, 16 Apr 2005) $
040:         */
041:
042:        public class ContextBaseTestCase extends TestCase {
043:
044:            // ---------------------------------------------------- Instance Variables
045:
046:            /**
047:             * The {@link Context} instance under test.
048:             */
049:            protected Context context = null;
050:
051:            // ---------------------------------------------------------- Constructors
052:
053:            /**
054:             * Construct a new instance of this test case.
055:             *
056:             * @param name Name of the test case
057:             */
058:            public ContextBaseTestCase(String name) {
059:                super (name);
060:            }
061:
062:            // -------------------------------------------------- Overall Test Methods
063:
064:            /**
065:             * Set up instance variables required by this test case.
066:             */
067:            public void setUp() {
068:                context = createContext();
069:            }
070:
071:            /**
072:             * Return the tests included in this test suite.
073:             */
074:            public static Test suite() {
075:                return (new TestSuite(ContextBaseTestCase.class));
076:            }
077:
078:            /**
079:             * Tear down instance variables required by this test case.
080:             */
081:            public void tearDown() {
082:                context = null;
083:            }
084:
085:            // ------------------------------------------------ Individual Test Methods
086:
087:            // Test ability to get, put, and remove attributes
088:            public void testAttributes() {
089:
090:                Object value = null;
091:                checkAttributeCount(0);
092:
093:                context.put("foo", "This is foo");
094:                checkAttributeCount(1);
095:                value = context.get("foo");
096:                assertNotNull("Returned foo", value);
097:                assertTrue("Returned foo type", value instanceof  String);
098:                assertEquals("Returned foo value", "This is foo",
099:                        (String) value);
100:
101:                context.put("bar", "This is bar");
102:                checkAttributeCount(2);
103:                value = context.get("bar");
104:                assertNotNull("Returned bar", value);
105:                assertTrue("Returned bar type", value instanceof  String);
106:                assertEquals("Returned bar value", "This is bar",
107:                        (String) value);
108:
109:                context.put("baz", "This is baz");
110:                checkAttributeCount(3);
111:                value = context.get("baz");
112:                assertNotNull("Returned baz", value);
113:                assertTrue("Returned baz type", value instanceof  String);
114:                assertEquals("Returned baz value", "This is baz",
115:                        (String) value);
116:
117:                context.put("baz", "This is new baz");
118:                checkAttributeCount(3); // Replaced, not added
119:                value = context.get("baz");
120:                assertNotNull("Returned baz", value);
121:                assertTrue("Returned baz type", value instanceof  String);
122:                assertEquals("Returned baz value", "This is new baz",
123:                        (String) value);
124:
125:                context.remove("bar");
126:                checkAttributeCount(2);
127:                assertNull("Did not return bar", context.get("bar"));
128:                assertNotNull("Still returned foo", context.get("foo"));
129:                assertNotNull("Still returned baz", context.get("baz"));
130:
131:                context.clear();
132:                checkAttributeCount(0);
133:                assertNull("Did not return foo", context.get("foo"));
134:                assertNull("Did not return bar", context.get("bar"));
135:                assertNull("Did not return baz", context.get("baz"));
136:
137:            }
138:
139:            // Test containsKey() and containsValue()
140:            public void testContains() {
141:
142:                assertTrue(!context.containsKey("bop"));
143:                assertTrue(!context.containsValue("bop value"));
144:                context.put("bop", "bop value");
145:                assertTrue(context.containsKey("bop"));
146:                assertTrue(context.containsValue("bop value"));
147:                context.remove("bop");
148:                assertTrue(!context.containsKey("bop"));
149:                assertTrue(!context.containsValue("bop value"));
150:
151:            }
152:
153:            // Test equals() and hashCode()
154:            public void testEquals() {
155:
156:                // Compare to self
157:                assertTrue(context.equals(context));
158:                assertTrue(context.hashCode() == context.hashCode());
159:
160:                // Compare to equivalent instance
161:                Context other = createContext();
162:                assertTrue(context.equals(other));
163:                assertTrue(context.hashCode() == other.hashCode());
164:
165:                // Compare to non-equivalent instance - other modified
166:                other.put("bop", "bop value");
167:                assertTrue(!context.equals(other));
168:                assertTrue(context.hashCode() != other.hashCode());
169:
170:                // Compare to non-equivalent instance - self modified
171:                other = createContext(); // reset to equivalence
172:                context.put("bop", "bop value");
173:                assertTrue(!context.equals(other));
174:                assertTrue(context.hashCode() != other.hashCode());
175:
176:            }
177:
178:            // Test keySet()
179:            public void testKeySet() {
180:
181:                Set keySet = null;
182:                Collection all = new ArrayList();
183:
184:                // Unsupported operations
185:                keySet = context.keySet();
186:                try {
187:                    keySet.add("bop");
188:                    fail("Should have thrown UnsupportedOperationException");
189:                } catch (UnsupportedOperationException e) {
190:                    ; // Expected result
191:                }
192:                try {
193:                    Collection adds = new ArrayList();
194:                    adds.add("bop");
195:                    keySet.addAll(adds);
196:                    fail("Should have thrown UnsupportedOperationException");
197:                } catch (UnsupportedOperationException e) {
198:                    ; // Expected result
199:                }
200:
201:                // Before-modification checks
202:                keySet = context.keySet();
203:                assertEquals(createContext().size(), keySet.size());
204:                assertTrue(!keySet.contains("foo"));
205:                assertTrue(!keySet.contains("bar"));
206:                assertTrue(!keySet.contains("baz"));
207:                assertTrue(!keySet.contains("bop"));
208:
209:                // Add the new elements
210:                context.put("foo", "foo value");
211:                context.put("bar", "bar value");
212:                context.put("baz", "baz value");
213:                all.add("foo");
214:                all.add("bar");
215:                all.add("baz");
216:
217:                // After-modification checks
218:                keySet = context.keySet();
219:                assertEquals(expectedAttributeCount() + 3, keySet.size());
220:                assertTrue(keySet.contains("foo"));
221:                assertTrue(keySet.contains("bar"));
222:                assertTrue(keySet.contains("baz"));
223:                assertTrue(!keySet.contains("bop"));
224:                assertTrue(keySet.containsAll(all));
225:
226:                // Remove a single element via remove()
227:                context.remove("bar");
228:                all.remove("bar");
229:                keySet = context.keySet();
230:                assertEquals(expectedAttributeCount() + 2, keySet.size());
231:                assertTrue(keySet.contains("foo"));
232:                assertTrue(!keySet.contains("bar"));
233:                assertTrue(keySet.contains("baz"));
234:                assertTrue(!keySet.contains("bop"));
235:                assertTrue(keySet.containsAll(all));
236:
237:                // Remove a single element via keySet.remove()
238:                keySet.remove("baz");
239:                all.remove("baz");
240:                keySet = context.keySet();
241:                assertEquals(expectedAttributeCount() + 1, keySet.size());
242:                assertTrue(keySet.contains("foo"));
243:                assertTrue(!keySet.contains("bar"));
244:                assertTrue(!keySet.contains("baz"));
245:                assertTrue(!keySet.contains("bop"));
246:                assertTrue(keySet.containsAll(all));
247:
248:                // Remove all elements via keySet.clear()
249:                keySet.clear();
250:                all.clear();
251:                assertEquals(expectedAttributeCount(), keySet.size());
252:                assertTrue(!keySet.contains("foo"));
253:                assertTrue(!keySet.contains("bar"));
254:                assertTrue(!keySet.contains("baz"));
255:                assertTrue(!keySet.contains("bop"));
256:                assertTrue(keySet.containsAll(all));
257:
258:                // Add the new elements #2
259:                context.put("foo", "foo value");
260:                context.put("bar", "bar value");
261:                context.put("baz", "baz value");
262:                all.add("foo");
263:                all.add("bar");
264:                all.add("baz");
265:
266:                // After-modification checks #2
267:                keySet = context.keySet();
268:                assertEquals(expectedAttributeCount() + 3, keySet.size());
269:                assertTrue(keySet.contains("foo"));
270:                assertTrue(keySet.contains("bar"));
271:                assertTrue(keySet.contains("baz"));
272:                assertTrue(!keySet.contains("bop"));
273:                assertTrue(keySet.containsAll(all));
274:
275:            }
276:
277:            // Test state of newly created instance
278:            public void testPristine() {
279:
280:                checkAttributeCount(0);
281:                assertNull("No 'foo' attribute", context.get("foo"));
282:
283:            }
284:
285:            // Test putAll()
286:            public void testPutAll() {
287:
288:                // Check preconditions
289:                checkAttributeCount(0);
290:                assertNull(context.get("foo"));
291:                assertNull(context.get("bar"));
292:                assertNull(context.get("baz"));
293:                assertTrue(!context.containsKey("foo"));
294:                assertTrue(!context.containsKey("bar"));
295:                assertTrue(!context.containsKey("baz"));
296:                assertTrue(!context.containsValue("foo value"));
297:                assertTrue(!context.containsValue("bar value"));
298:                assertTrue(!context.containsValue("baz value"));
299:
300:                // Call putAll()
301:                Map adds = new HashMap();
302:                adds.put("foo", "foo value");
303:                adds.put("bar", "bar value");
304:                adds.put("baz", "baz value");
305:                context.putAll(adds);
306:
307:                // Check postconditions
308:                checkAttributeCount(3);
309:                assertEquals("foo value", (String) context.get("foo"));
310:                assertEquals("bar value", (String) context.get("bar"));
311:                assertEquals("baz value", (String) context.get("baz"));
312:                assertTrue(context.containsKey("foo"));
313:                assertTrue(context.containsKey("bar"));
314:                assertTrue(context.containsKey("baz"));
315:                assertTrue(context.containsValue("foo value"));
316:                assertTrue(context.containsValue("bar value"));
317:                assertTrue(context.containsValue("baz value"));
318:
319:            }
320:
321:            // Test serialization
322:            public void testSeriaization() throws Exception {
323:
324:                // ContextBase is implicitly declared Serializable because it
325:                // extends HashMap.  However, it is not possible to make
326:                // the concrete subclasses of WebContext Serializable, because
327:                // the underlying container objects that they wrap will not be.
328:                // Therefore, skip testing serializability of these implementations
329:                if (context instanceof  WebContext) {
330:                    return;
331:                }
332:
333:                // Set up the context with some parameters
334:                context.put("foo", "foo value");
335:                context.put("bar", "bar value");
336:                context.put("baz", "baz value");
337:                checkAttributeCount(3);
338:
339:                // Serialize to a byte array
340:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
341:                ObjectOutputStream oos = new ObjectOutputStream(baos);
342:                oos.writeObject(context);
343:                oos.close();
344:
345:                // Deserialize back to a new object
346:                ByteArrayInputStream bais = new ByteArrayInputStream(baos
347:                        .toByteArray());
348:                ObjectInputStream ois = new ObjectInputStream(bais);
349:                context = (Context) ois.readObject();
350:                ois.close();
351:
352:                // Do some rudimentary checks to make sure we have the same contents
353:                assertTrue(context.containsKey("foo"));
354:                assertTrue(context.containsKey("bar"));
355:                assertTrue(context.containsKey("baz"));
356:                checkAttributeCount(3);
357:
358:            }
359:
360:            // -------------------------------------------------------- Support Methods
361:
362:            // Verify the number of defined attributes
363:            protected void checkAttributeCount(int expected) {
364:                int actual = 0;
365:                Iterator keys = context.keySet().iterator();
366:                while (keys.hasNext()) {
367:                    Object key = (Object) keys.next();
368:                    actual++;
369:                }
370:                assertEquals("Correct attribute count",
371:                        expectedAttributeCount() + expected, actual);
372:                if (expected == 0) {
373:                    assertTrue("Context should be empty", context.isEmpty());
374:                } else {
375:                    assertTrue("Context should not be empty", !context
376:                            .isEmpty());
377:                }
378:            }
379:
380:            // Create a new instance of the appropriate Context type for this test case
381:            protected Context createContext() {
382:                return (new ContextBase());
383:            }
384:
385:            // Return the expected size() for a Context for this test case
386:            protected int expectedAttributeCount() {
387:                return (createContext().size());
388:            }
389:
390:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.