1 18 package org.apache.activemq.kaha; 19 20 import java.io.IOException ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 import java.util.ListIterator ; 25 import junit.framework.TestCase; 26 public class ListContainerTest extends TestCase{ 27 protected String name="test"; 28 protected Store store; 29 protected ListContainer container; 30 protected LinkedList testList; 31 protected static final int COUNT=10; 32 33 36 public void testSize() throws Exception { 37 container.addAll(testList); 38 assertEquals(container.size(),testList.size()); 39 } 40 41 44 public void testAddFirst() throws Exception { 45 container.addAll(testList); 46 assertEquals(container.size(),testList.size()); 47 String first="first"; 48 container.addFirst(first); 49 assertEquals(first,container.get(0)); 50 assertEquals(container.size(),testList.size()+1); 51 } 52 53 56 public void testAddLast() throws Exception { 57 container.addAll(testList); 58 assertEquals(container.size(),testList.size()); 59 String last="last"; 60 container.addLast(last); 61 assertEquals(last,container.get(testList.size())); 62 assertEquals(container.size(),testList.size()+1); 63 } 64 65 68 public void testRemoveFirst() throws Exception { 69 container.addAll(testList); 70 assertEquals(container.size(),testList.size()); 71 assertEquals(testList.get(0),container.removeFirst()); 72 assertEquals(container.size(),testList.size()-1); 73 for(int i=1;i<testList.size();i++){ 74 assertEquals(testList.get(i),container.get(i-1)); 75 } 76 } 77 78 81 public void testRemoveLast() throws Exception { 82 container.addAll(testList); 83 assertEquals(container.size(),testList.size()); 84 assertEquals(testList.get(testList.size()-1),container.removeLast()); 85 assertEquals(container.size(),testList.size()-1); 86 for(int i=0;i<testList.size()-1;i++){ 87 assertEquals(testList.get(i),container.get(i)); 88 } 89 } 90 91 94 public void testIterator() throws Exception { 95 container.addAll(testList); 96 for(Iterator i=testList.iterator(),j=container.iterator();i.hasNext();){ 97 assertEquals(i.next(),j.next()); 98 } 99 for(Iterator i=container.iterator();i.hasNext();){ 100 i.next(); 101 i.remove(); 102 } 103 assert (container.isEmpty()); 104 } 105 106 109 public void testIsEmpty() throws Exception { 110 assertTrue(container.isEmpty()); 111 } 112 113 116 public void testContains() throws Exception { 117 container.addAll(testList); 118 for(Iterator i=testList.iterator(),j=container.iterator();i.hasNext();){ 119 assertTrue(container.contains(i.next())); 120 } 121 } 122 123 126 public void testToArray() throws Exception { 127 container.addAll(testList); 128 Object [] a=testList.toArray(); 129 Object [] b=container.toArray(); 130 assertEquals(a.length,b.length); 131 for(int i=0;i<a.length;i++){ 132 assertEquals(a[i],b[i]); 133 } 134 } 135 136 139 public void testRemoveObject() throws Exception { 140 container.addAll(testList); 141 assertEquals(container.size(),testList.size()); 142 for(int i=0;i<testList.size();i++){ 143 container.remove(testList.get(i)); 144 } 145 assertTrue(container.isEmpty()); 146 } 147 148 151 public void testContainsAll() throws Exception { 152 container.addAll(testList); 153 assertTrue(container.containsAll(testList)); 154 } 155 156 159 public void testRemoveAll() throws Exception { 160 container.addAll(testList); 161 assertEquals(testList.size(),container.size()); 162 container.removeAll(testList); 163 assertTrue(container.isEmpty()); 164 } 165 166 169 public void testRetainAll() throws Exception { 170 container.addAll(testList); 171 assertEquals(testList.size(),container.size()); 172 testList.remove(0); 173 container.retainAll(testList); 174 assertEquals(testList.size(),container.size()); 175 } 176 177 180 public void testClear() throws Exception { 181 container.addAll(testList); 182 assertEquals(testList.size(),container.size()); 183 container.clear(); 184 assertTrue(container.isEmpty()); 185 } 186 187 190 public void testGet() throws Exception { 191 container.addAll(testList); 192 for(int i=0;i<testList.size();i++){ 193 assertEquals(container.get(i),testList.get(i)); 194 } 195 } 196 197 200 public void testSet() throws Exception { 201 container.addAll(testList); 202 } 203 204 207 public void testAddIntE() throws Exception { 208 container.addAll(testList); 209 assertTrue(container.equals(testList)); 210 Object testObj="testObj"; 211 int index=0; 212 testList.set(index,testObj); 213 container.set(index,testObj); 214 assertTrue(container.equals(testList)); 215 index=testList.size()-1; 216 testList.set(index,testObj); 217 container.set(index,testObj); 218 assertTrue(container.equals(testList)); 219 } 220 221 224 public void testRemoveInt() throws Exception { 225 container.addAll(testList); 226 assertTrue(container.equals(testList)); 227 testList.remove(0); 228 container.remove(0); 229 assertTrue(container.equals(testList)); 230 int pos=testList.size()-1; 231 testList.remove(pos); 232 container.remove(pos); 233 assertTrue(container.equals(testList)); 234 } 235 236 239 public void testIndexOf() throws Exception { 240 container.addAll(testList); 241 assertTrue(container.equals(testList)); 242 for(int i=0;i<testList.size();i++){ 243 Object o=testList.get(i); 244 assertEquals(i,container.indexOf(o)); 245 } 246 } 247 248 251 public void testListIterator() throws Exception { 252 container.addAll(testList); 253 ListIterator containerIter=container.listIterator(); 254 ListIterator testIter=testList.listIterator(); 255 assertTrue(testIter.hasNext()); 256 assertTrue(containerIter.hasNext()); 257 while(testIter.hasNext()){ 258 Object o1=testIter.next(); 259 Object o2=containerIter.next(); 260 assertEquals(o1,o2); 261 testIter.remove(); 262 containerIter.remove(); 263 } 264 assertTrue(testList.isEmpty()); 265 assertTrue(container.isEmpty()); 266 } 267 268 271 public void testListIteratorInt() throws Exception { 272 container.addAll(testList); 273 int start=testList.size()/2; 274 ListIterator containerIter=container.listIterator(start); 275 ListIterator testIter=testList.listIterator(start); 276 assertTrue(testIter.hasNext()); 277 assertTrue(containerIter.hasNext()); 278 while(testIter.hasNext()){ 279 Object o1=testIter.next(); 280 Object o2=containerIter.next(); 281 assertEquals(o1,o2); 282 } 283 } 284 285 288 public void testSubList() throws Exception { 289 container.addAll(testList); 290 int start=testList.size()/2; 291 List l1=testList.subList(start,testList.size()); 292 List l2=container.subList(start,testList.size()); 293 assertEquals(l1.size(),l2.size()); 294 assertEquals(l1,l2); 295 } 296 297 protected Store getStore() throws IOException { 298 return StoreFactory.open(name,"rw"); 299 } 300 301 protected void setUp() throws Exception { 302 super.setUp(); 303 name = System.getProperty("basedir", ".")+"/target/activemq-data/list-container.db"; 304 StoreFactory.delete(name); 305 store=getStore(); 306 store.deleteListContainer(name); 307 container=store.getListContainer(name); 308 container.load(); 309 testList=new LinkedList (); 310 for(int i=0;i<COUNT;i++){ 311 String value="value:"+i; 312 testList.add(value); 313 } 314 } 315 316 protected void tearDown() throws Exception { 317 super.tearDown(); 318 if(store!=null){ 319 store.close(); 320 } 321 assertTrue(StoreFactory.delete(name)); 322 } 323 } 324 | Popular Tags |