1 package org.jboss.cache.factories; 2 3 import org.jboss.cache.CacheSPI; 4 import org.jboss.cache.config.Configuration; 5 import org.jboss.cache.interceptors.Interceptor; 6 7 import java.util.List ; 8 9 12 public class CustomInterceptorChainTest extends InterceptorChainTestBase 13 { 14 private CacheSPI cache; 15 16 protected void setUp() throws Exception 17 { 18 Configuration c = new Configuration(); 19 cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(c); 20 cache.create(); 21 } 22 23 protected void tearDown() 24 { 25 if (cache != null) 26 { 27 cache.stop(); 28 cache = null; 29 } 30 } 31 32 public void testChainImmutability() 33 { 34 try 35 { 36 cache.getInterceptorChain().add(new TestInterceptor()); 37 fail("unsupportedException should have been thrown as the chain obtained from the cache should be immutable"); 38 } 39 catch (UnsupportedOperationException uoe) 40 { 41 } 43 } 44 45 public void testInjectionAtHead() 46 { 47 List <Interceptor> interceptors = cache.getInterceptorChain(); 48 assertEquals("Expecting 6 interceptors", 7, interceptors.size()); 49 assertInterceptorLinkage(interceptors); 50 51 Interceptor x = new TestInterceptor(); 52 cache.addInterceptor(x, 0); 53 54 interceptors = cache.getInterceptorChain(); 55 assertEquals("Expecting 7 interceptors", 8, interceptors.size()); 56 assertInterceptorLinkage(interceptors); 57 58 assertEquals(x, interceptors.get(0)); 59 } 60 61 public void testInjectionAtTail() 62 { 63 List <Interceptor> interceptors = cache.getInterceptorChain(); 64 assertEquals("Expecting 6 interceptors", 7, interceptors.size()); 65 assertInterceptorLinkage(interceptors); 66 67 Interceptor x = new TestInterceptor(); 68 cache.addInterceptor(x, 6); 69 70 interceptors = cache.getInterceptorChain(); 71 assertEquals("Expecting 7 interceptors", 8, interceptors.size()); 72 assertInterceptorLinkage(interceptors); 73 74 assertEquals(x, interceptors.get(6)); 75 } 76 77 public void testInjectionInMiddle() 78 { 79 List <Interceptor> interceptors = cache.getInterceptorChain(); 80 assertEquals("Expecting 6 interceptors", 7, interceptors.size()); 81 assertInterceptorLinkage(interceptors); 82 83 Interceptor x = new TestInterceptor(); 84 cache.addInterceptor(x, 3); 85 86 interceptors = cache.getInterceptorChain(); 87 assertEquals("Expecting 7 interceptors", 8, interceptors.size()); 88 assertInterceptorLinkage(interceptors); 89 90 assertEquals(x, interceptors.get(3)); 91 } 92 93 public void testInjectionBeyondTail() 94 { 95 List <Interceptor> interceptors = cache.getInterceptorChain(); 96 assertEquals("Expecting 6 interceptors", 7, interceptors.size()); 97 assertInterceptorLinkage(interceptors); 98 99 Interceptor x = new TestInterceptor(); 100 try 101 { 102 cache.addInterceptor(x, 8); 103 fail("Should throw an exception"); 104 } 105 catch (IndexOutOfBoundsException e) 106 { 107 } 109 } 110 111 public void testRemoveAtHead() 112 { 113 List <Interceptor> interceptors = cache.getInterceptorChain(); 114 Interceptor afterHead = interceptors.get(1); 115 assertEquals("Expecting 6 interceptors", 7, interceptors.size()); 116 assertInterceptorLinkage(interceptors); 117 118 cache.removeInterceptor(0); 119 120 interceptors = cache.getInterceptorChain(); 121 assertEquals("Expecting 5 interceptors", 6, interceptors.size()); 122 assertInterceptorLinkage(interceptors); 123 124 assertEquals(afterHead, interceptors.get(0)); 125 } 126 127 public void testRemoveAtTail() 128 { 129 List <Interceptor> interceptors = cache.getInterceptorChain(); 130 Interceptor beforeTail = interceptors.get(4); 131 assertEquals("Expecting 6 interceptors", 7, interceptors.size()); 132 assertInterceptorLinkage(interceptors); 133 134 cache.removeInterceptor(5); 135 136 interceptors = cache.getInterceptorChain(); 137 138 System.out.println(interceptors); 139 140 assertEquals("Expecting 5 interceptors", 6, interceptors.size()); 141 assertInterceptorLinkage(interceptors); 142 143 assertEquals(beforeTail, interceptors.get(4)); 144 } 145 146 public void testRemoveAtMiddle() 147 { 148 List <Interceptor> interceptors = cache.getInterceptorChain(); 149 assertEquals("Expecting 6 interceptors", 7, interceptors.size()); 150 assertInterceptorLinkage(interceptors); 151 152 cache.removeInterceptor(3); 153 154 interceptors = cache.getInterceptorChain(); 155 assertEquals("Expecting 5 interceptors", 6, interceptors.size()); 156 assertInterceptorLinkage(interceptors); 157 } 158 159 public void testRemoveBeyondTail() 160 { 161 List <Interceptor> interceptors = cache.getInterceptorChain(); 162 assertEquals("Expecting 6 interceptors", 7, interceptors.size()); 163 assertInterceptorLinkage(interceptors); 164 165 try 166 { 167 cache.removeInterceptor(8); 168 fail("Should throw an exception"); 169 } 170 catch (IndexOutOfBoundsException e) 171 { 172 } 174 } 175 176 177 public static class TestInterceptor extends Interceptor 178 { 179 } 180 } 181 | Popular Tags |