1 16 package org.apache.commons.chain.impl; 17 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import junit.framework.Test; 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 import org.apache.commons.chain.Context; 30 31 32 33 39 40 public class ContextBaseTestCase extends TestCase { 41 42 43 45 46 49 protected Context context = null; 50 51 52 53 55 60 public ContextBaseTestCase(String name) { 61 super(name); 62 } 63 64 65 67 68 71 public void setUp() { 72 context = createContext(); 73 } 74 75 76 79 public static Test suite() { 80 return (new TestSuite(ContextBaseTestCase.class)); 81 } 82 83 86 public void tearDown() { 87 context = null; 88 } 89 90 91 93 94 public void testAttributes() { 96 97 Object value = null; 98 checkAttributeCount(0); 99 100 context.put("foo", "This is foo"); 101 checkAttributeCount(1); 102 value = context.get("foo"); 103 assertNotNull("Returned foo", value); 104 assertTrue("Returned foo type", value instanceof String ); 105 assertEquals("Returned foo value", "This is foo", 106 (String ) value); 107 108 context.put("bar", "This is bar"); 109 checkAttributeCount(2); 110 value = context.get("bar"); 111 assertNotNull("Returned bar", value); 112 assertTrue("Returned bar type", value instanceof String ); 113 assertEquals("Returned bar value", "This is bar", 114 (String ) value); 115 116 context.put("baz", "This is baz"); 117 checkAttributeCount(3); 118 value = context.get("baz"); 119 assertNotNull("Returned baz", value); 120 assertTrue("Returned baz type", value instanceof String ); 121 assertEquals("Returned baz value", "This is baz", 122 (String ) value); 123 124 context.put("baz", "This is new baz"); 125 checkAttributeCount(3); value = context.get("baz"); 127 assertNotNull("Returned baz", value); 128 assertTrue("Returned baz type", value instanceof String ); 129 assertEquals("Returned baz value", "This is new baz", 130 (String ) value); 131 132 context.remove("bar"); 133 checkAttributeCount(2); 134 assertNull("Did not return bar", 135 context.get("bar")); 136 assertNotNull("Still returned foo", 137 context.get("foo")); 138 assertNotNull("Still returned baz", 139 context.get("baz")); 140 141 context.clear(); 142 checkAttributeCount(0); 143 assertNull("Did not return foo", 144 context.get("foo")); 145 assertNull("Did not return bar", 146 context.get("bar")); 147 assertNull("Did not return baz", 148 context.get("baz")); 149 150 } 151 152 153 public void testContains() { 155 156 assertTrue(!context.containsKey("bop")); 157 assertTrue(!context.containsValue("bop value")); 158 context.put("bop", "bop value"); 159 assertTrue(context.containsKey("bop")); 160 assertTrue(context.containsValue("bop value")); 161 context.remove("bop"); 162 assertTrue(!context.containsKey("bop")); 163 assertTrue(!context.containsValue("bop value")); 164 165 } 166 167 168 public void testEquals() { 170 171 assertTrue(context.equals(context)); 173 assertTrue(context.hashCode() == context.hashCode()); 174 175 Context other = createContext(); 177 assertTrue(context.equals(other)); 178 assertTrue(context.hashCode() == other.hashCode()); 179 180 other.put("bop", "bop value"); 182 assertTrue(!context.equals(other)); 183 assertTrue(context.hashCode() != other.hashCode()); 184 185 other = createContext(); context.put("bop", "bop value"); 188 assertTrue(!context.equals(other)); 189 assertTrue(context.hashCode() != other.hashCode()); 190 191 } 192 193 194 public void testKeySet() { 196 197 Set keySet = null; 198 Collection all = new ArrayList (); 199 200 keySet = context.keySet(); 202 try { 203 keySet.add("bop"); 204 fail("Should have thrown UnsupportedOperationException"); 205 } catch (UnsupportedOperationException e) { 206 ; } 208 try { 209 Collection adds = new ArrayList (); 210 adds.add("bop"); 211 keySet.addAll(adds); 212 fail("Should have thrown UnsupportedOperationException"); 213 } catch (UnsupportedOperationException e) { 214 ; } 216 217 keySet = context.keySet(); 219 assertEquals(createContext().size(), keySet.size()); 220 assertTrue(!keySet.contains("foo")); 221 assertTrue(!keySet.contains("bar")); 222 assertTrue(!keySet.contains("baz")); 223 assertTrue(!keySet.contains("bop")); 224 225 context.put("foo", "foo value"); 227 context.put("bar", "bar value"); 228 context.put("baz", "baz value"); 229 all.add("foo"); 230 all.add("bar"); 231 all.add("baz"); 232 233 keySet = context.keySet(); 235 assertEquals(expectedAttributeCount() + 3, keySet.size()); 236 assertTrue(keySet.contains("foo")); 237 assertTrue(keySet.contains("bar")); 238 assertTrue(keySet.contains("baz")); 239 assertTrue(!keySet.contains("bop")); 240 assertTrue(keySet.containsAll(all)); 241 242 context.remove("bar"); 244 all.remove("bar"); 245 keySet = context.keySet(); 246 assertEquals(expectedAttributeCount() + 2, keySet.size()); 247 assertTrue(keySet.contains("foo")); 248 assertTrue(!keySet.contains("bar")); 249 assertTrue(keySet.contains("baz")); 250 assertTrue(!keySet.contains("bop")); 251 assertTrue(keySet.containsAll(all)); 252 253 keySet.remove("baz"); 255 all.remove("baz"); 256 keySet = context.keySet(); 257 assertEquals(expectedAttributeCount() + 1, keySet.size()); 258 assertTrue(keySet.contains("foo")); 259 assertTrue(!keySet.contains("bar")); 260 assertTrue(!keySet.contains("baz")); 261 assertTrue(!keySet.contains("bop")); 262 assertTrue(keySet.containsAll(all)); 263 264 keySet.clear(); 266 all.clear(); 267 assertEquals(expectedAttributeCount(), keySet.size()); 268 assertTrue(!keySet.contains("foo")); 269 assertTrue(!keySet.contains("bar")); 270 assertTrue(!keySet.contains("baz")); 271 assertTrue(!keySet.contains("bop")); 272 assertTrue(keySet.containsAll(all)); 273 274 context.put("foo", "foo value"); 276 context.put("bar", "bar value"); 277 context.put("baz", "baz value"); 278 all.add("foo"); 279 all.add("bar"); 280 all.add("baz"); 281 282 keySet = context.keySet(); 284 assertEquals(expectedAttributeCount() + 3, keySet.size()); 285 assertTrue(keySet.contains("foo")); 286 assertTrue(keySet.contains("bar")); 287 assertTrue(keySet.contains("baz")); 288 assertTrue(!keySet.contains("bop")); 289 assertTrue(keySet.containsAll(all)); 290 291 } 292 293 294 public void testPristine() { 296 297 checkAttributeCount(0); 298 assertNull("No 'foo' attribute", 299 context.get("foo")); 300 301 } 302 303 304 public void testPutAll() { 306 307 checkAttributeCount(0); 309 assertNull(context.get("foo")); 310 assertNull(context.get("bar")); 311 assertNull(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 Map adds = new HashMap (); 321 adds.put("foo", "foo value"); 322 adds.put("bar", "bar value"); 323 adds.put("baz", "baz value"); 324 context.putAll(adds); 325 326 checkAttributeCount(3); 328 assertEquals("foo value", (String ) context.get("foo")); 329 assertEquals("bar value", (String ) context.get("bar")); 330 assertEquals("baz value", (String ) context.get("baz")); 331 assertTrue(context.containsKey("foo")); 332 assertTrue(context.containsKey("bar")); 333 assertTrue(context.containsKey("baz")); 334 assertTrue(context.containsValue("foo value")); 335 assertTrue(context.containsValue("bar value")); 336 assertTrue(context.containsValue("baz value")); 337 338 } 339 340 341 343 344 protected void checkAttributeCount(int expected) { 346 int actual = 0; 347 Iterator keys = context.keySet().iterator(); 348 while (keys.hasNext()) { 349 Object key = (Object ) keys.next(); 350 actual++; 351 } 352 assertEquals("Correct attribute count", 353 expectedAttributeCount() + expected, actual); 354 if (expected == 0) { 355 assertTrue("Context should be empty", context.isEmpty()); 356 } else { 357 assertTrue("Context should not be empty", !context.isEmpty()); 358 } 359 } 360 361 362 protected Context createContext() { 364 return (new ContextBase()); 365 } 366 367 368 protected int expectedAttributeCount() { 370 return (createContext().size()); 371 } 372 373 374 } 375 | Popular Tags |