0001: /*
0002: * Copyright 2001-2004 The Apache Software Foundation
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.apache.commons.collections.list;
0017:
0018: import java.util.ArrayList;
0019: import java.util.ConcurrentModificationException;
0020: import java.util.HashSet;
0021: import java.util.Iterator;
0022: import java.util.List;
0023: import java.util.ListIterator;
0024: import java.util.NoSuchElementException;
0025:
0026: import junit.framework.Test;
0027:
0028: import org.apache.commons.collections.BulkTest;
0029:
0030: /**
0031: * Test class.
0032: *
0033: * @version $Revision: 219343 $ $Date: 2005-07-16 18:08:16 +0100 (Sat, 16 Jul 2005) $
0034: *
0035: * @author Rodney Waldhoff
0036: * @author Simon Kitching
0037: */
0038: public class TestCursorableLinkedList extends TestAbstractLinkedList {
0039: public TestCursorableLinkedList(String testName) {
0040: super (testName);
0041: }
0042:
0043: public static Test suite() {
0044: return BulkTest.makeSuite(TestCursorableLinkedList.class);
0045: }
0046:
0047: public static void main(String args[]) {
0048: String[] testCaseName = { TestCursorableLinkedList.class
0049: .getName() };
0050: junit.textui.TestRunner.main(testCaseName);
0051: }
0052:
0053: private CursorableLinkedList list = null;
0054:
0055: public void setUp() {
0056: list = new CursorableLinkedList();
0057: }
0058:
0059: public List makeEmptyList() {
0060: return new CursorableLinkedList();
0061: }
0062:
0063: public void testAdd() {
0064: assertEquals("[]", list.toString());
0065: assertTrue(list.add(new Integer(1)));
0066: assertEquals("[1]", list.toString());
0067: assertTrue(list.add(new Integer(2)));
0068: assertEquals("[1, 2]", list.toString());
0069: assertTrue(list.add(new Integer(3)));
0070: assertEquals("[1, 2, 3]", list.toString());
0071: assertTrue(list.addFirst(new Integer(0)));
0072: assertEquals("[0, 1, 2, 3]", list.toString());
0073: assertTrue(list.addLast(new Integer(4)));
0074: assertEquals("[0, 1, 2, 3, 4]", list.toString());
0075: list.add(0, new Integer(-2));
0076: assertEquals("[-2, 0, 1, 2, 3, 4]", list.toString());
0077: list.add(1, new Integer(-1));
0078: assertEquals("[-2, -1, 0, 1, 2, 3, 4]", list.toString());
0079: list.add(7, new Integer(5));
0080: assertEquals("[-2, -1, 0, 1, 2, 3, 4, 5]", list.toString());
0081:
0082: java.util.List list2 = new java.util.LinkedList();
0083: list2.add("A");
0084: list2.add("B");
0085: list2.add("C");
0086:
0087: assertTrue(list.addAll(list2));
0088: assertEquals("[-2, -1, 0, 1, 2, 3, 4, 5, A, B, C]", list
0089: .toString());
0090: assertTrue(list.addAll(3, list2));
0091: assertEquals("[-2, -1, 0, A, B, C, 1, 2, 3, 4, 5, A, B, C]",
0092: list.toString());
0093: }
0094:
0095: public void testClear() {
0096: assertEquals(0, list.size());
0097: assertTrue(list.isEmpty());
0098: list.clear();
0099: assertEquals(0, list.size());
0100: assertTrue(list.isEmpty());
0101:
0102: list.add("element");
0103: assertEquals(1, list.size());
0104: assertTrue(!list.isEmpty());
0105:
0106: list.clear();
0107: assertEquals(0, list.size());
0108: assertTrue(list.isEmpty());
0109:
0110: list.add("element1");
0111: list.add("element2");
0112: assertEquals(2, list.size());
0113: assertTrue(!list.isEmpty());
0114:
0115: list.clear();
0116: assertEquals(0, list.size());
0117: assertTrue(list.isEmpty());
0118:
0119: for (int i = 0; i < 1000; i++) {
0120: list.add(new Integer(i));
0121: }
0122: assertEquals(1000, list.size());
0123: assertTrue(!list.isEmpty());
0124:
0125: list.clear();
0126: assertEquals(0, list.size());
0127: assertTrue(list.isEmpty());
0128: }
0129:
0130: public void testContains() {
0131: assertTrue(!list.contains("A"));
0132: assertTrue(list.add("A"));
0133: assertTrue(list.contains("A"));
0134: assertTrue(list.add("B"));
0135: assertTrue(list.contains("A"));
0136: assertTrue(list.addFirst("a"));
0137: assertTrue(list.contains("A"));
0138: assertTrue(list.remove("a"));
0139: assertTrue(list.contains("A"));
0140: assertTrue(list.remove("A"));
0141: assertTrue(!list.contains("A"));
0142: }
0143:
0144: public void testContainsAll() {
0145: assertTrue(list.containsAll(list));
0146: java.util.List list2 = new java.util.LinkedList();
0147: assertTrue(list.containsAll(list2));
0148: list2.add("A");
0149: assertTrue(!list.containsAll(list2));
0150: list.add("B");
0151: list.add("A");
0152: assertTrue(list.containsAll(list2));
0153: list2.add("B");
0154: assertTrue(list.containsAll(list2));
0155: list2.add("C");
0156: assertTrue(!list.containsAll(list2));
0157: list.add("C");
0158: assertTrue(list.containsAll(list2));
0159: list2.add("C");
0160: assertTrue(list.containsAll(list2));
0161: assertTrue(list.containsAll(list));
0162: }
0163:
0164: public void testCursorNavigation() {
0165: list.add("1");
0166: list.add("2");
0167: list.add("3");
0168: list.add("4");
0169: list.add("5");
0170: CursorableLinkedList.Cursor it = list.cursor();
0171: assertTrue(it.hasNext());
0172: assertTrue(!it.hasPrevious());
0173: assertEquals("1", it.next());
0174: assertTrue(it.hasNext());
0175: assertTrue(it.hasPrevious());
0176: assertEquals("1", it.previous());
0177: assertTrue(it.hasNext());
0178: assertTrue(!it.hasPrevious());
0179: assertEquals("1", it.next());
0180: assertTrue(it.hasNext());
0181: assertTrue(it.hasPrevious());
0182: assertEquals("2", it.next());
0183: assertTrue(it.hasNext());
0184: assertTrue(it.hasPrevious());
0185: assertEquals("2", it.previous());
0186: assertTrue(it.hasNext());
0187: assertTrue(it.hasPrevious());
0188: assertEquals("2", it.next());
0189: assertTrue(it.hasNext());
0190: assertTrue(it.hasPrevious());
0191: assertEquals("3", it.next());
0192: assertTrue(it.hasNext());
0193: assertTrue(it.hasPrevious());
0194: assertEquals("4", it.next());
0195: assertTrue(it.hasNext());
0196: assertTrue(it.hasPrevious());
0197: assertEquals("5", it.next());
0198: assertTrue(!it.hasNext());
0199: assertTrue(it.hasPrevious());
0200: assertEquals("5", it.previous());
0201: assertTrue(it.hasNext());
0202: assertTrue(it.hasPrevious());
0203: assertEquals("4", it.previous());
0204: assertTrue(it.hasNext());
0205: assertTrue(it.hasPrevious());
0206: assertEquals("3", it.previous());
0207: assertTrue(it.hasNext());
0208: assertTrue(it.hasPrevious());
0209: assertEquals("2", it.previous());
0210: assertTrue(it.hasNext());
0211: assertTrue(it.hasPrevious());
0212: assertEquals("1", it.previous());
0213: assertTrue(it.hasNext());
0214: assertTrue(!it.hasPrevious());
0215: it.close();
0216: }
0217:
0218: public void testCursorSet() {
0219: list.add("1");
0220: list.add("2");
0221: list.add("3");
0222: list.add("4");
0223: list.add("5");
0224:
0225: CursorableLinkedList.Cursor it = list.cursor();
0226: assertEquals("1", it.next());
0227: it.set("a");
0228: assertEquals("a", it.previous());
0229: it.set("A");
0230: assertEquals("A", it.next());
0231: assertEquals("2", it.next());
0232: it.set("B");
0233: assertEquals("3", it.next());
0234: assertEquals("4", it.next());
0235: it.set("D");
0236: assertEquals("5", it.next());
0237: it.set("E");
0238: assertEquals("[A, B, 3, D, E]", list.toString());
0239: it.close();
0240: }
0241:
0242: public void testCursorRemove() {
0243: list.add("1");
0244: list.add("2");
0245: list.add("3");
0246: list.add("4");
0247: list.add("5");
0248:
0249: CursorableLinkedList.Cursor it = list.cursor();
0250: try {
0251: it.remove();
0252: fail();
0253: } catch (IllegalStateException e) {
0254: // expected
0255: }
0256: assertEquals("1", it.next());
0257: assertEquals("2", it.next());
0258: assertEquals("[1, 2, 3, 4, 5]", list.toString());
0259: it.remove();
0260: assertEquals("[1, 3, 4, 5]", list.toString());
0261: assertEquals("3", it.next());
0262: assertEquals("3", it.previous());
0263: assertEquals("1", it.previous());
0264: it.remove();
0265: assertEquals("[3, 4, 5]", list.toString());
0266: assertTrue(!it.hasPrevious());
0267: assertEquals("3", it.next());
0268: it.remove();
0269: assertEquals("[4, 5]", list.toString());
0270: try {
0271: it.remove();
0272: } catch (IllegalStateException e) {
0273: // expected
0274: }
0275: assertEquals("4", it.next());
0276: assertEquals("5", it.next());
0277: it.remove();
0278: assertEquals("[4]", list.toString());
0279: assertEquals("4", it.previous());
0280: it.remove();
0281: assertEquals("[]", list.toString());
0282: it.close();
0283: }
0284:
0285: public void testCursorAdd() {
0286: CursorableLinkedList.Cursor it = list.cursor();
0287: it.add("1");
0288: assertEquals("[1]", list.toString());
0289: it.add("3");
0290: assertEquals("[1, 3]", list.toString());
0291: it.add("5");
0292: assertEquals("[1, 3, 5]", list.toString());
0293: assertEquals("5", it.previous());
0294: it.add("4");
0295: assertEquals("[1, 3, 4, 5]", list.toString());
0296: assertEquals("4", it.previous());
0297: assertEquals("3", it.previous());
0298: it.add("2");
0299: assertEquals("[1, 2, 3, 4, 5]", list.toString());
0300: it.close();
0301: }
0302:
0303: public void testCursorConcurrentModification() {
0304: // this test verifies that cursors remain valid when the list
0305: // is modified via other means.
0306: list.add("1");
0307: list.add("2");
0308: list.add("3");
0309: list.add("5");
0310: list.add("7");
0311: list.add("9");
0312:
0313: CursorableLinkedList.Cursor c1 = list.cursor();
0314: CursorableLinkedList.Cursor c2 = list.cursor();
0315: Iterator li = list.iterator();
0316:
0317: // test cursors remain valid when list modified by std Iterator
0318: // test cursors skip elements removed via ListIterator
0319: assertEquals("1", li.next());
0320: assertEquals("2", li.next());
0321: li.remove();
0322: assertEquals("3", li.next());
0323: assertEquals("1", c1.next());
0324: assertEquals("3", c1.next());
0325: assertEquals("1", c2.next());
0326:
0327: // test cursor c1 can remove elements from previously modified list
0328: // test cursor c2 skips elements removed via different cursor
0329: c1.remove();
0330: assertEquals("5", c2.next());
0331: c2.add("6");
0332: assertEquals("5", c1.next());
0333: assertEquals("6", c1.next());
0334: assertEquals("7", c1.next());
0335:
0336: // test cursors remain valid when list mod via CursorableLinkedList
0337: // test cursor remains valid when elements inserted into list before
0338: // the current position of the cursor.
0339: list.add(0, "0");
0340:
0341: // test cursor remains valid when element inserted immediately after
0342: // current element of a cursor, and the element is seen on the
0343: // next call to the next method of that cursor.
0344: list.add(5, "8");
0345:
0346: assertEquals("8", c1.next());
0347: assertEquals("9", c1.next());
0348: c1.add("10");
0349: assertEquals("7", c2.next());
0350: assertEquals("8", c2.next());
0351: assertEquals("9", c2.next());
0352: assertEquals("10", c2.next());
0353:
0354: try {
0355: c2.next();
0356: fail();
0357: } catch (NoSuchElementException nse) {
0358: }
0359:
0360: try {
0361: li.next();
0362: fail();
0363: } catch (ConcurrentModificationException cme) {
0364: }
0365:
0366: c1.close(); // not necessary
0367: c2.close(); // not necessary
0368: }
0369:
0370: public void testCursorNextIndexMid() {
0371: list.add("1");
0372: list.add("2");
0373: list.add("3");
0374: list.add("5");
0375:
0376: CursorableLinkedList.Cursor c1 = list.cursor();
0377: Iterator li = list.iterator();
0378:
0379: // test cursors remain valid when list modified by std Iterator
0380: // test cursors skip elements removed via ListIterator
0381: assertEquals("1", li.next());
0382: assertEquals("2", li.next());
0383: li.remove();
0384: assertEquals(0, c1.nextIndex());
0385: assertEquals("1", c1.next());
0386: assertEquals(1, c1.nextIndex());
0387: assertEquals("3", c1.next());
0388: }
0389:
0390: public void testCursorNextIndexFirst() {
0391: list.add("1");
0392: list.add("2");
0393: list.add("3");
0394: list.add("5");
0395:
0396: CursorableLinkedList.Cursor c1 = list.cursor();
0397:
0398: assertEquals(0, c1.nextIndex());
0399: list.remove(0);
0400: assertEquals(0, c1.nextIndex());
0401: assertEquals("2", c1.next());
0402: assertEquals(1, c1.nextIndex());
0403: assertEquals("3", c1.next());
0404: }
0405:
0406: public void testCursorNextIndexAddBefore() {
0407: list.add("1");
0408: list.add("2");
0409: list.add("3");
0410: list.add("5");
0411:
0412: CursorableLinkedList.Cursor c1 = list.cursor();
0413:
0414: assertEquals(0, c1.nextIndex());
0415: assertEquals("1", c1.next());
0416: list.add(0, "0");
0417: assertEquals(2, c1.nextIndex());
0418: assertEquals("2", c1.next());
0419: }
0420:
0421: public void testCursorNextIndexAddNext() {
0422: list.add("1");
0423: list.add("2");
0424: list.add("3");
0425: list.add("5");
0426:
0427: CursorableLinkedList.Cursor c1 = list.cursor();
0428:
0429: assertEquals(0, c1.nextIndex());
0430: list.add(0, "0");
0431: assertEquals(0, c1.nextIndex());
0432: assertEquals("0", c1.next());
0433: assertEquals(1, c1.nextIndex());
0434: assertEquals("1", c1.next());
0435: }
0436:
0437: public void testCursorNextIndexAddAfter() {
0438: list.add("1");
0439: list.add("2");
0440: list.add("3");
0441: list.add("5");
0442:
0443: CursorableLinkedList.Cursor c1 = list.cursor();
0444:
0445: assertEquals(0, c1.nextIndex());
0446: list.add(1, "0");
0447: assertEquals(0, c1.nextIndex());
0448: assertEquals("1", c1.next());
0449: assertEquals(1, c1.nextIndex());
0450: assertEquals("0", c1.next());
0451: }
0452:
0453: //-----------------------------------------------------------------------
0454: public void testInternalState_CursorNextNextPreviousRemoveIndex1ByList() {
0455: list.add("A");
0456: list.add("B");
0457: list.add("C");
0458:
0459: CursorableLinkedList.Cursor c1 = list.cursor();
0460: assertEquals("A", c1.next());
0461: assertEquals("B", c1.next());
0462: assertEquals("B", c1.previous());
0463:
0464: assertEquals("B", list.remove(1));
0465:
0466: assertEquals(true, c1.nextIndexValid);
0467: assertEquals(1, c1.nextIndex);
0468: assertEquals(true, c1.currentRemovedByAnother);
0469: assertEquals(null, c1.current);
0470: assertEquals("C", c1.next.value);
0471:
0472: assertEquals("[A, C]", list.toString());
0473: c1.remove(); // works ok
0474: assertEquals("[A, C]", list.toString());
0475: try {
0476: c1.remove();
0477: fail();
0478: } catch (IllegalStateException ex) {
0479: }
0480: }
0481:
0482: public void testInternalState_CursorNextRemoveIndex1ByList() {
0483: list.add("A");
0484: list.add("B");
0485: list.add("C");
0486:
0487: CursorableLinkedList.Cursor c1 = list.cursor();
0488: assertEquals("A", c1.next());
0489:
0490: assertEquals("B", list.remove(1));
0491:
0492: assertEquals(true, c1.nextIndexValid);
0493: assertEquals(1, c1.nextIndex);
0494: assertEquals(false, c1.currentRemovedByAnother);
0495: assertEquals("A", c1.current.value);
0496: assertEquals("C", c1.next.value);
0497:
0498: assertEquals("[A, C]", list.toString());
0499: c1.remove(); // works ok
0500: assertEquals("[C]", list.toString());
0501: try {
0502: c1.remove();
0503: fail();
0504: } catch (IllegalStateException ex) {
0505: }
0506: }
0507:
0508: public void testInternalState_CursorNextNextRemoveIndex1ByList() {
0509: list.add("A");
0510: list.add("B");
0511: list.add("C");
0512:
0513: CursorableLinkedList.Cursor c1 = list.cursor();
0514: assertEquals("A", c1.next());
0515: assertEquals("B", c1.next());
0516:
0517: assertEquals("B", list.remove(1));
0518:
0519: assertEquals(true, c1.nextIndexValid);
0520: assertEquals(1, c1.nextIndex);
0521: assertEquals(true, c1.currentRemovedByAnother);
0522: assertEquals(null, c1.current);
0523: assertEquals("C", c1.next.value);
0524:
0525: assertEquals("[A, C]", list.toString());
0526: c1.remove(); // works ok
0527: assertEquals("[A, C]", list.toString());
0528: try {
0529: c1.remove();
0530: fail();
0531: } catch (IllegalStateException ex) {
0532: }
0533: }
0534:
0535: public void testInternalState_CursorNextNextNextRemoveIndex1ByList() {
0536: list.add("A");
0537: list.add("B");
0538: list.add("C");
0539: list.add("D");
0540:
0541: CursorableLinkedList.Cursor c1 = list.cursor();
0542: assertEquals("A", c1.next());
0543: assertEquals("B", c1.next());
0544: assertEquals("C", c1.next());
0545:
0546: assertEquals("B", list.remove(1));
0547:
0548: assertEquals(false, c1.nextIndexValid);
0549: assertEquals(false, c1.currentRemovedByAnother);
0550: assertEquals("C", c1.current.value);
0551: assertEquals("D", c1.next.value);
0552:
0553: assertEquals("[A, C, D]", list.toString());
0554: c1.remove(); // works ok
0555: assertEquals("[A, D]", list.toString());
0556: try {
0557: c1.remove();
0558: fail();
0559: } catch (IllegalStateException ex) {
0560: }
0561: }
0562:
0563: //-----------------------------------------------------------------------
0564: public void testInternalState_CursorNextNextPreviousRemoveByIterator() {
0565: list.add("A");
0566: list.add("B");
0567: list.add("C");
0568:
0569: CursorableLinkedList.Cursor c1 = list.cursor();
0570: assertEquals("A", c1.next());
0571: assertEquals("B", c1.next());
0572: assertEquals("B", c1.previous());
0573:
0574: c1.remove();
0575:
0576: assertEquals(true, c1.nextIndexValid);
0577: assertEquals(1, c1.nextIndex);
0578: assertEquals(false, c1.currentRemovedByAnother);
0579: assertEquals(null, c1.current);
0580: assertEquals("C", c1.next.value);
0581:
0582: assertEquals("[A, C]", list.toString());
0583: try {
0584: c1.remove();
0585: fail();
0586: } catch (IllegalStateException ex) {
0587: }
0588: }
0589:
0590: public void testInternalState_CursorNextNextRemoveByIterator() {
0591: list.add("A");
0592: list.add("B");
0593: list.add("C");
0594:
0595: CursorableLinkedList.Cursor c1 = list.cursor();
0596: assertEquals("A", c1.next());
0597: assertEquals("B", c1.next());
0598:
0599: c1.remove();
0600:
0601: assertEquals(true, c1.nextIndexValid);
0602: assertEquals(1, c1.nextIndex);
0603: assertEquals(false, c1.currentRemovedByAnother);
0604: assertEquals(null, c1.current);
0605: assertEquals("C", c1.next.value);
0606:
0607: assertEquals("[A, C]", list.toString());
0608: try {
0609: c1.remove();
0610: fail();
0611: } catch (IllegalStateException ex) {
0612: }
0613: }
0614:
0615: //-----------------------------------------------------------------------
0616: public void testInternalState_CursorNextNextPreviousAddIndex1ByList() {
0617: list.add("A");
0618: list.add("B");
0619: list.add("C");
0620:
0621: CursorableLinkedList.Cursor c1 = list.cursor();
0622: assertEquals("A", c1.next());
0623: assertEquals("B", c1.next());
0624: assertEquals("B", c1.previous());
0625:
0626: list.add(1, "Z");
0627:
0628: assertEquals(true, c1.nextIndexValid);
0629: assertEquals(1, c1.nextIndex);
0630: assertEquals("B", c1.current.value);
0631: assertEquals("Z", c1.next.value);
0632:
0633: assertEquals("[A, Z, B, C]", list.toString());
0634: c1.remove(); // works ok
0635: assertEquals("[A, Z, C]", list.toString());
0636: try {
0637: c1.remove();
0638: fail();
0639: } catch (IllegalStateException ex) {
0640: }
0641: }
0642:
0643: public void testInternalState_CursorNextAddIndex1ByList() {
0644: list.add("A");
0645: list.add("B");
0646: list.add("C");
0647:
0648: CursorableLinkedList.Cursor c1 = list.cursor();
0649: assertEquals("A", c1.next());
0650:
0651: list.add(1, "Z");
0652:
0653: assertEquals(true, c1.nextIndexValid);
0654: assertEquals(1, c1.nextIndex);
0655: assertEquals("A", c1.current.value);
0656: assertEquals("Z", c1.next.value);
0657:
0658: assertEquals("[A, Z, B, C]", list.toString());
0659: c1.remove(); // works ok
0660: assertEquals("[Z, B, C]", list.toString());
0661: try {
0662: c1.remove();
0663: fail();
0664: } catch (IllegalStateException ex) {
0665: }
0666: }
0667:
0668: public void testInternalState_CursorNextNextAddIndex1ByList() {
0669: list.add("A");
0670: list.add("B");
0671: list.add("C");
0672:
0673: CursorableLinkedList.Cursor c1 = list.cursor();
0674: assertEquals("A", c1.next());
0675: assertEquals("B", c1.next());
0676:
0677: list.add(1, "Z");
0678:
0679: assertEquals(false, c1.nextIndexValid);
0680: assertEquals("B", c1.current.value);
0681: assertEquals("C", c1.next.value);
0682:
0683: assertEquals("[A, Z, B, C]", list.toString());
0684: c1.remove(); // works ok
0685: assertEquals("[A, Z, C]", list.toString());
0686: try {
0687: c1.remove();
0688: fail();
0689: } catch (IllegalStateException ex) {
0690: }
0691: }
0692:
0693: //-----------------------------------------------------------------------
0694: public void testInternalState_CursorNextNextPreviousAddByIterator() {
0695: list.add("A");
0696: list.add("B");
0697: list.add("C");
0698:
0699: CursorableLinkedList.Cursor c1 = list.cursor();
0700: assertEquals("A", c1.next());
0701: assertEquals("B", c1.next());
0702: assertEquals("B", c1.previous());
0703:
0704: c1.add("Z");
0705:
0706: assertEquals(true, c1.nextIndexValid);
0707: assertEquals(2, c1.nextIndex);
0708: assertEquals(null, c1.current);
0709: assertEquals("B", c1.next.value);
0710:
0711: assertEquals("[A, Z, B, C]", list.toString());
0712: try {
0713: c1.remove();
0714: fail();
0715: } catch (IllegalStateException ex) {
0716: }
0717: }
0718:
0719: public void testInternalState_CursorNextNextAddByIterator() {
0720: list.add("A");
0721: list.add("B");
0722: list.add("C");
0723:
0724: CursorableLinkedList.Cursor c1 = list.cursor();
0725: assertEquals("A", c1.next());
0726: assertEquals("B", c1.next());
0727:
0728: c1.add("Z");
0729:
0730: assertEquals(true, c1.nextIndexValid);
0731: assertEquals(3, c1.nextIndex);
0732: assertEquals(false, c1.currentRemovedByAnother);
0733: assertEquals(null, c1.current);
0734: assertEquals("C", c1.next.value);
0735:
0736: assertEquals("[A, B, Z, C]", list.toString());
0737: try {
0738: c1.remove();
0739: fail();
0740: } catch (IllegalStateException ex) {
0741: }
0742: }
0743:
0744: //-----------------------------------------------------------------------
0745: public void testInternalState_CursorNextNextRemoveByListSetByIterator() {
0746: list.add("A");
0747: list.add("B");
0748: list.add("C");
0749:
0750: CursorableLinkedList.Cursor c1 = list.cursor();
0751: assertEquals("A", c1.next());
0752: assertEquals("B", c1.next());
0753:
0754: list.remove(1);
0755:
0756: assertEquals(true, c1.nextIndexValid);
0757: assertEquals(1, c1.nextIndex);
0758: assertEquals(null, c1.current);
0759: assertEquals("C", c1.next.value);
0760: assertEquals("[A, C]", list.toString());
0761:
0762: try {
0763: c1.set("Z");
0764: fail();
0765: } catch (IllegalStateException ex) {
0766: }
0767: }
0768:
0769: //-----------------------------------------------------------------------
0770: public void testInternalState_CursorNextNextPreviousSetByIterator() {
0771: list.add("A");
0772: list.add("B");
0773: list.add("C");
0774:
0775: CursorableLinkedList.Cursor c1 = list.cursor();
0776: assertEquals("A", c1.next());
0777: assertEquals("B", c1.next());
0778: assertEquals("B", c1.previous());
0779:
0780: c1.set("Z");
0781:
0782: assertEquals(true, c1.nextIndexValid);
0783: assertEquals(1, c1.nextIndex);
0784: assertEquals("Z", c1.current.value);
0785: assertEquals("Z", c1.next.value);
0786:
0787: assertEquals("[A, Z, C]", list.toString());
0788: c1.remove(); // works ok
0789: assertEquals("[A, C]", list.toString());
0790: try {
0791: c1.remove();
0792: fail();
0793: } catch (IllegalStateException ex) {
0794: }
0795: }
0796:
0797: public void testInternalState_CursorNextNextSetByIterator() {
0798: list.add("A");
0799: list.add("B");
0800: list.add("C");
0801:
0802: CursorableLinkedList.Cursor c1 = list.cursor();
0803: assertEquals("A", c1.next());
0804: assertEquals("B", c1.next());
0805:
0806: c1.set("Z");
0807:
0808: assertEquals(true, c1.nextIndexValid);
0809: assertEquals(2, c1.nextIndex);
0810: assertEquals("Z", c1.current.value);
0811: assertEquals("C", c1.next.value);
0812:
0813: assertEquals("[A, Z, C]", list.toString());
0814: c1.remove(); // works ok
0815: assertEquals("[A, C]", list.toString());
0816: try {
0817: c1.remove();
0818: fail();
0819: } catch (IllegalStateException ex) {
0820: }
0821: }
0822:
0823: //-----------------------------------------------------------------------
0824: public void testEqualsAndHashCode() {
0825: assertTrue(list.equals(list));
0826: assertEquals(list.hashCode(), list.hashCode());
0827: list.add("A");
0828: assertTrue(list.equals(list));
0829: assertEquals(list.hashCode(), list.hashCode());
0830:
0831: CursorableLinkedList list2 = new CursorableLinkedList();
0832: assertTrue(!list.equals(list2));
0833: assertTrue(!list2.equals(list));
0834:
0835: java.util.List list3 = new java.util.LinkedList();
0836: assertTrue(!list.equals(list3));
0837: assertTrue(!list3.equals(list));
0838: assertTrue(list2.equals(list3));
0839: assertTrue(list3.equals(list2));
0840: assertEquals(list2.hashCode(), list3.hashCode());
0841:
0842: list2.add("A");
0843: assertTrue(list.equals(list2));
0844: assertTrue(list2.equals(list));
0845: assertTrue(!list2.equals(list3));
0846: assertTrue(!list3.equals(list2));
0847:
0848: list3.add("A");
0849: assertTrue(list2.equals(list3));
0850: assertTrue(list3.equals(list2));
0851: assertEquals(list2.hashCode(), list3.hashCode());
0852:
0853: list.add("B");
0854: assertTrue(list.equals(list));
0855: assertTrue(!list.equals(list2));
0856: assertTrue(!list2.equals(list));
0857: assertTrue(!list.equals(list3));
0858: assertTrue(!list3.equals(list));
0859:
0860: list2.add("B");
0861: list3.add("B");
0862: assertTrue(list.equals(list));
0863: assertTrue(list.equals(list2));
0864: assertTrue(list2.equals(list));
0865: assertTrue(list2.equals(list3));
0866: assertTrue(list3.equals(list2));
0867: assertEquals(list2.hashCode(), list3.hashCode());
0868:
0869: list.add("C");
0870: list2.add("C");
0871: list3.add("C");
0872: assertTrue(list.equals(list));
0873: assertTrue(list.equals(list2));
0874: assertTrue(list2.equals(list));
0875: assertTrue(list2.equals(list3));
0876: assertTrue(list3.equals(list2));
0877: assertEquals(list.hashCode(), list2.hashCode());
0878: assertEquals(list2.hashCode(), list3.hashCode());
0879:
0880: list.add("D");
0881: list2.addFirst("D");
0882: assertTrue(list.equals(list));
0883: assertTrue(!list.equals(list2));
0884: assertTrue(!list2.equals(list));
0885: }
0886:
0887: public void testGet() {
0888: try {
0889: list.get(0);
0890: fail("shouldn't get here");
0891: } catch (IndexOutOfBoundsException e) {
0892: // expected
0893: }
0894:
0895: assertTrue(list.add("A"));
0896: assertEquals("A", list.get(0));
0897: assertTrue(list.add("B"));
0898: assertEquals("A", list.get(0));
0899: assertEquals("B", list.get(1));
0900:
0901: try {
0902: list.get(-1);
0903: fail("shouldn't get here");
0904: } catch (IndexOutOfBoundsException e) {
0905: // expected
0906: }
0907:
0908: try {
0909: list.get(2);
0910: fail("shouldn't get here");
0911: } catch (IndexOutOfBoundsException e) {
0912: // expected
0913: }
0914: }
0915:
0916: public void testIndexOf() {
0917: assertEquals(-1, list.indexOf("A"));
0918: assertEquals(-1, list.lastIndexOf("A"));
0919: list.add("A");
0920: assertEquals(0, list.indexOf("A"));
0921: assertEquals(0, list.lastIndexOf("A"));
0922: assertEquals(-1, list.indexOf("B"));
0923: assertEquals(-1, list.lastIndexOf("B"));
0924: list.add("B");
0925: assertEquals(0, list.indexOf("A"));
0926: assertEquals(0, list.lastIndexOf("A"));
0927: assertEquals(1, list.indexOf("B"));
0928: assertEquals(1, list.lastIndexOf("B"));
0929: list.addFirst("B");
0930: assertEquals(1, list.indexOf("A"));
0931: assertEquals(1, list.lastIndexOf("A"));
0932: assertEquals(0, list.indexOf("B"));
0933: assertEquals(2, list.lastIndexOf("B"));
0934: }
0935:
0936: public void testIsEmpty() {
0937: assertTrue(list.isEmpty());
0938: list.add("element");
0939: assertTrue(!list.isEmpty());
0940: list.remove("element");
0941: assertTrue(list.isEmpty());
0942: list.add("element");
0943: assertTrue(!list.isEmpty());
0944: list.clear();
0945: assertTrue(list.isEmpty());
0946: }
0947:
0948: public void testIterator() {
0949: list.add("1");
0950: list.add("2");
0951: list.add("3");
0952: list.add("4");
0953: list.add("5");
0954: Iterator it = list.iterator();
0955: assertTrue(it.hasNext());
0956: assertEquals("1", it.next());
0957: assertTrue(it.hasNext());
0958: assertEquals("2", it.next());
0959: assertTrue(it.hasNext());
0960: assertEquals("3", it.next());
0961: assertTrue(it.hasNext());
0962: assertEquals("4", it.next());
0963: assertTrue(it.hasNext());
0964: assertEquals("5", it.next());
0965: assertTrue(!it.hasNext());
0966:
0967: it = list.iterator();
0968: assertTrue(it.hasNext());
0969: assertEquals("1", it.next());
0970: it.remove();
0971: assertEquals("[2, 3, 4, 5]", list.toString());
0972: assertTrue(it.hasNext());
0973: assertEquals("2", it.next());
0974: it.remove();
0975: assertEquals("[3, 4, 5]", list.toString());
0976: assertTrue(it.hasNext());
0977: assertEquals("3", it.next());
0978: it.remove();
0979: assertEquals("[4, 5]", list.toString());
0980: assertTrue(it.hasNext());
0981: assertEquals("4", it.next());
0982: it.remove();
0983: assertEquals("[5]", list.toString());
0984: assertTrue(it.hasNext());
0985: assertEquals("5", it.next());
0986: it.remove();
0987: assertEquals("[]", list.toString());
0988: assertTrue(!it.hasNext());
0989: }
0990:
0991: public void testListIteratorNavigation() {
0992: list.add("1");
0993: list.add("2");
0994: list.add("3");
0995: list.add("4");
0996: list.add("5");
0997: ListIterator it = list.listIterator();
0998: assertTrue(it.hasNext());
0999: assertTrue(!it.hasPrevious());
1000: assertEquals(-1, it.previousIndex());
1001: assertEquals(0, it.nextIndex());
1002: assertEquals("1", it.next());
1003: assertTrue(it.hasNext());
1004: assertTrue(it.hasPrevious());
1005: assertEquals(0, it.previousIndex());
1006: assertEquals(1, it.nextIndex());
1007: assertEquals("1", it.previous());
1008: assertTrue(it.hasNext());
1009: assertTrue(!it.hasPrevious());
1010: assertEquals(-1, it.previousIndex());
1011: assertEquals(0, it.nextIndex());
1012: assertEquals("1", it.next());
1013: assertTrue(it.hasNext());
1014: assertTrue(it.hasPrevious());
1015: assertEquals(0, it.previousIndex());
1016: assertEquals(1, it.nextIndex());
1017: assertEquals("2", it.next());
1018: assertTrue(it.hasNext());
1019: assertTrue(it.hasPrevious());
1020: assertEquals(1, it.previousIndex());
1021: assertEquals(2, it.nextIndex());
1022: assertEquals("2", it.previous());
1023: assertTrue(it.hasNext());
1024: assertTrue(it.hasPrevious());
1025: assertEquals(0, it.previousIndex());
1026: assertEquals(1, it.nextIndex());
1027: assertEquals("2", it.next());
1028: assertTrue(it.hasNext());
1029: assertTrue(it.hasPrevious());
1030: assertEquals(1, it.previousIndex());
1031: assertEquals(2, it.nextIndex());
1032: assertEquals("3", it.next());
1033: assertTrue(it.hasNext());
1034: assertTrue(it.hasPrevious());
1035: assertEquals(2, it.previousIndex());
1036: assertEquals(3, it.nextIndex());
1037: assertEquals("4", it.next());
1038: assertTrue(it.hasNext());
1039: assertTrue(it.hasPrevious());
1040: assertEquals(3, it.previousIndex());
1041: assertEquals(4, it.nextIndex());
1042: assertEquals("5", it.next());
1043: assertTrue(!it.hasNext());
1044: assertTrue(it.hasPrevious());
1045: assertEquals(4, it.previousIndex());
1046: assertEquals(5, it.nextIndex());
1047: assertEquals("5", it.previous());
1048: assertTrue(it.hasNext());
1049: assertTrue(it.hasPrevious());
1050: assertEquals(3, it.previousIndex());
1051: assertEquals(4, it.nextIndex());
1052: assertEquals("4", it.previous());
1053: assertTrue(it.hasNext());
1054: assertTrue(it.hasPrevious());
1055: assertEquals(2, it.previousIndex());
1056: assertEquals(3, it.nextIndex());
1057: assertEquals("3", it.previous());
1058: assertTrue(it.hasNext());
1059: assertTrue(it.hasPrevious());
1060: assertEquals(1, it.previousIndex());
1061: assertEquals(2, it.nextIndex());
1062: assertEquals("2", it.previous());
1063: assertTrue(it.hasNext());
1064: assertTrue(it.hasPrevious());
1065: assertEquals(0, it.previousIndex());
1066: assertEquals(1, it.nextIndex());
1067: assertEquals("1", it.previous());
1068: assertTrue(it.hasNext());
1069: assertTrue(!it.hasPrevious());
1070: assertEquals(-1, it.previousIndex());
1071: assertEquals(0, it.nextIndex());
1072: }
1073:
1074: public void testListIteratorSet() {
1075: list.add("1");
1076: list.add("2");
1077: list.add("3");
1078: list.add("4");
1079: list.add("5");
1080:
1081: ListIterator it = list.listIterator();
1082: assertEquals("1", it.next());
1083: it.set("a");
1084: assertEquals("a", it.previous());
1085: it.set("A");
1086: assertEquals("A", it.next());
1087: assertEquals("2", it.next());
1088: it.set("B");
1089: assertEquals("3", it.next());
1090: assertEquals("4", it.next());
1091: it.set("D");
1092: assertEquals("5", it.next());
1093: it.set("E");
1094: assertEquals("[A, B, 3, D, E]", list.toString());
1095: }
1096:
1097: public void testListIteratorRemove() {
1098: list.add("1");
1099: list.add("2");
1100: list.add("3");
1101: list.add("4");
1102: list.add("5");
1103:
1104: ListIterator it = list.listIterator();
1105: try {
1106: it.remove();
1107: } catch (IllegalStateException e) {
1108: // expected
1109: }
1110: assertEquals("1", it.next());
1111: assertEquals("2", it.next());
1112: assertEquals("[1, 2, 3, 4, 5]", list.toString());
1113: it.remove();
1114: assertEquals("[1, 3, 4, 5]", list.toString());
1115: assertEquals("3", it.next());
1116: assertEquals("3", it.previous());
1117: assertEquals("1", it.previous());
1118: it.remove();
1119: assertEquals("[3, 4, 5]", list.toString());
1120: assertTrue(!it.hasPrevious());
1121: assertEquals("3", it.next());
1122: it.remove();
1123: assertEquals("[4, 5]", list.toString());
1124: try {
1125: it.remove();
1126: } catch (IllegalStateException e) {
1127: // expected
1128: }
1129: assertEquals("4", it.next());
1130: assertEquals("5", it.next());
1131: it.remove();
1132: assertEquals("[4]", list.toString());
1133: assertEquals("4", it.previous());
1134: it.remove();
1135: assertEquals("[]", list.toString());
1136: }
1137:
1138: public void testListIteratorAdd() {
1139: ListIterator it = list.listIterator();
1140: it.add("1");
1141: assertEquals("[1]", list.toString());
1142: it.add("3");
1143: assertEquals("[1, 3]", list.toString());
1144: it.add("5");
1145: assertEquals("[1, 3, 5]", list.toString());
1146: assertEquals("5", it.previous());
1147: it.add("4");
1148: assertEquals("[1, 3, 4, 5]", list.toString());
1149: assertEquals("4", it.previous());
1150: assertEquals("3", it.previous());
1151: it.add("2");
1152: assertEquals("[1, 2, 3, 4, 5]", list.toString());
1153: }
1154:
1155: public void testRemoveAll() {
1156: list.add("1");
1157: list.add("2");
1158: list.add("3");
1159: list.add("4");
1160: list.add("5");
1161:
1162: HashSet set = new HashSet();
1163: set.add("A");
1164: set.add("2");
1165: set.add("C");
1166: set.add("4");
1167: set.add("D");
1168:
1169: assertTrue(list.removeAll(set));
1170: assertEquals("[1, 3, 5]", list.toString());
1171: assertTrue(!list.removeAll(set));
1172: }
1173:
1174: public void testRemoveByIndex() {
1175: list.add("1");
1176: list.add("2");
1177: list.add("3");
1178: list.add("4");
1179: list.add("5");
1180: assertEquals("[1, 2, 3, 4, 5]", list.toString());
1181: assertEquals("1", list.remove(0));
1182: assertEquals("[2, 3, 4, 5]", list.toString());
1183: assertEquals("3", list.remove(1));
1184: assertEquals("[2, 4, 5]", list.toString());
1185: assertEquals("4", list.remove(1));
1186: assertEquals("[2, 5]", list.toString());
1187: assertEquals("5", list.remove(1));
1188: assertEquals("[2]", list.toString());
1189: assertEquals("2", list.remove(0));
1190: assertEquals("[]", list.toString());
1191: }
1192:
1193: public void testRemove() {
1194: list.add("1");
1195: list.add("1");
1196: list.add("2");
1197: list.add("3");
1198: list.add("4");
1199: list.add("5");
1200: list.add("2");
1201: list.add("3");
1202: list.add("4");
1203: list.add("5");
1204: assertEquals("[1, 1, 2, 3, 4, 5, 2, 3, 4, 5]", list.toString());
1205: assertTrue(!list.remove("6"));
1206: assertTrue(list.remove("5"));
1207: assertEquals("[1, 1, 2, 3, 4, 2, 3, 4, 5]", list.toString());
1208: assertTrue(list.remove("5"));
1209: assertEquals("[1, 1, 2, 3, 4, 2, 3, 4]", list.toString());
1210: assertTrue(!list.remove("5"));
1211: assertTrue(list.remove("1"));
1212: assertEquals("[1, 2, 3, 4, 2, 3, 4]", list.toString());
1213: assertTrue(list.remove("1"));
1214: assertEquals("[2, 3, 4, 2, 3, 4]", list.toString());
1215: assertTrue(list.remove("2"));
1216: assertEquals("[3, 4, 2, 3, 4]", list.toString());
1217: assertTrue(list.remove("2"));
1218: assertEquals("[3, 4, 3, 4]", list.toString());
1219: assertTrue(list.remove("3"));
1220: assertEquals("[4, 3, 4]", list.toString());
1221: assertTrue(list.remove("3"));
1222: assertEquals("[4, 4]", list.toString());
1223: assertTrue(list.remove("4"));
1224: assertEquals("[4]", list.toString());
1225: assertTrue(list.remove("4"));
1226: assertEquals("[]", list.toString());
1227: }
1228:
1229: public void testRetainAll() {
1230: list.add("1");
1231: list.add("1");
1232: list.add("2");
1233: list.add("2");
1234: list.add("3");
1235: list.add("3");
1236: list.add("4");
1237: list.add("4");
1238: list.add("5");
1239: list.add("5");
1240:
1241: HashSet set = new HashSet();
1242: set.add("A");
1243: set.add("2");
1244: set.add("C");
1245: set.add("4");
1246: set.add("D");
1247:
1248: assertTrue(list.retainAll(set));
1249: assertEquals("[2, 2, 4, 4]", list.toString());
1250: assertTrue(!list.retainAll(set));
1251: }
1252:
1253: public void testSet() {
1254: list.add("1");
1255: list.add("2");
1256: list.add("3");
1257: list.add("4");
1258: list.add("5");
1259: assertEquals("[1, 2, 3, 4, 5]", list.toString());
1260: list.set(0, "A");
1261: assertEquals("[A, 2, 3, 4, 5]", list.toString());
1262: list.set(1, "B");
1263: assertEquals("[A, B, 3, 4, 5]", list.toString());
1264: list.set(2, "C");
1265: assertEquals("[A, B, C, 4, 5]", list.toString());
1266: list.set(3, "D");
1267: assertEquals("[A, B, C, D, 5]", list.toString());
1268: list.set(4, "E");
1269: assertEquals("[A, B, C, D, E]", list.toString());
1270: }
1271:
1272: public void testSubList() {
1273: list.add("A");
1274: list.add("B");
1275: list.add("C");
1276: list.add("D");
1277: list.add("E");
1278:
1279: assertEquals("[A, B, C, D, E]", list.toString());
1280: assertEquals("[A, B, C, D, E]", list.subList(0, 5).toString());
1281: assertEquals("[B, C, D, E]", list.subList(1, 5).toString());
1282: assertEquals("[C, D, E]", list.subList(2, 5).toString());
1283: assertEquals("[D, E]", list.subList(3, 5).toString());
1284: assertEquals("[E]", list.subList(4, 5).toString());
1285: assertEquals("[]", list.subList(5, 5).toString());
1286: }
1287:
1288: public void testSubListAddEnd() {
1289: list.add("A");
1290: list.add("B");
1291: list.add("C");
1292: list.add("D");
1293: list.add("E");
1294:
1295: List sublist = list.subList(5, 5);
1296: sublist.add("F");
1297: assertEquals("[A, B, C, D, E, F]", list.toString());
1298: assertEquals("[F]", sublist.toString());
1299: sublist.add("G");
1300: assertEquals("[A, B, C, D, E, F, G]", list.toString());
1301: assertEquals("[F, G]", sublist.toString());
1302: }
1303:
1304: public void testSubListAddBegin() {
1305: list.add("A");
1306: list.add("B");
1307: list.add("C");
1308: list.add("D");
1309: list.add("E");
1310:
1311: List sublist = list.subList(0, 0);
1312: sublist.add("a");
1313: assertEquals("[a, A, B, C, D, E]", list.toString());
1314: assertEquals("[a]", sublist.toString());
1315: sublist.add("b");
1316: assertEquals("[a, b, A, B, C, D, E]", list.toString());
1317: assertEquals("[a, b]", sublist.toString());
1318: }
1319:
1320: public void testSubListAddMiddle() {
1321: list.add("A");
1322: list.add("B");
1323: list.add("C");
1324: list.add("D");
1325: list.add("E");
1326:
1327: List sublist = list.subList(1, 3);
1328: sublist.add("a");
1329: assertEquals("[A, B, C, a, D, E]", list.toString());
1330: assertEquals("[B, C, a]", sublist.toString());
1331: sublist.add("b");
1332: assertEquals("[A, B, C, a, b, D, E]", list.toString());
1333: assertEquals("[B, C, a, b]", sublist.toString());
1334: }
1335:
1336: public void testSubListRemove() {
1337: list.add("A");
1338: list.add("B");
1339: list.add("C");
1340: list.add("D");
1341: list.add("E");
1342:
1343: List sublist = list.subList(1, 4);
1344: assertEquals("[B, C, D]", sublist.toString());
1345: assertEquals("[A, B, C, D, E]", list.toString());
1346: sublist.remove("C");
1347: assertEquals("[B, D]", sublist.toString());
1348: assertEquals("[A, B, D, E]", list.toString());
1349: sublist.remove(1);
1350: assertEquals("[B]", sublist.toString());
1351: assertEquals("[A, B, E]", list.toString());
1352: sublist.clear();
1353: assertEquals("[]", sublist.toString());
1354: assertEquals("[A, E]", list.toString());
1355: }
1356:
1357: public void testToArray() {
1358: list.add("1");
1359: list.add("2");
1360: list.add("3");
1361: list.add("4");
1362: list.add("5");
1363:
1364: Object[] elts = list.toArray();
1365: assertEquals("1", elts[0]);
1366: assertEquals("2", elts[1]);
1367: assertEquals("3", elts[2]);
1368: assertEquals("4", elts[3]);
1369: assertEquals("5", elts[4]);
1370: assertEquals(5, elts.length);
1371:
1372: String[] elts2 = (String[]) (list.toArray(new String[0]));
1373: assertEquals("1", elts2[0]);
1374: assertEquals("2", elts2[1]);
1375: assertEquals("3", elts2[2]);
1376: assertEquals("4", elts2[3]);
1377: assertEquals("5", elts2[4]);
1378: assertEquals(5, elts2.length);
1379:
1380: String[] elts3 = new String[5];
1381: assertSame(elts3, list.toArray(elts3));
1382: assertEquals("1", elts3[0]);
1383: assertEquals("2", elts3[1]);
1384: assertEquals("3", elts3[2]);
1385: assertEquals("4", elts3[3]);
1386: assertEquals("5", elts3[4]);
1387: assertEquals(5, elts3.length);
1388:
1389: String[] elts4 = new String[3];
1390: String[] elts4b = (String[]) (list.toArray(elts4));
1391: assertTrue(elts4 != elts4b);
1392: assertEquals("1", elts4b[0]);
1393: assertEquals("2", elts4b[1]);
1394: assertEquals("3", elts4b[2]);
1395: assertEquals("4", elts4b[3]);
1396: assertEquals("5", elts4b[4]);
1397: assertEquals(5, elts4b.length);
1398: }
1399:
1400: public void testSerialization() throws Exception {
1401: list.add("A");
1402: list.add("B");
1403: list.add("C");
1404: list.add("D");
1405: list.add("E");
1406:
1407: java.io.ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
1408: java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(
1409: buf);
1410: out.writeObject(list);
1411: out.flush();
1412: out.close();
1413:
1414: java.io.ByteArrayInputStream bufin = new java.io.ByteArrayInputStream(
1415: buf.toByteArray());
1416: java.io.ObjectInputStream in = new java.io.ObjectInputStream(
1417: bufin);
1418: Object list2 = in.readObject();
1419:
1420: assertTrue(list != list2);
1421: assertTrue(list2.equals(list));
1422: assertTrue(list.equals(list2));
1423: }
1424:
1425: public void testSerializationWithOpenCursor() throws Exception {
1426: list.add("A");
1427: list.add("B");
1428: list.add("C");
1429: list.add("D");
1430: list.add("E");
1431: CursorableLinkedList.Cursor cursor = list.cursor();
1432:
1433: java.io.ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
1434: java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(
1435: buf);
1436: out.writeObject(list);
1437: out.flush();
1438: out.close();
1439:
1440: java.io.ByteArrayInputStream bufin = new java.io.ByteArrayInputStream(
1441: buf.toByteArray());
1442: java.io.ObjectInputStream in = new java.io.ObjectInputStream(
1443: bufin);
1444: Object list2 = in.readObject();
1445:
1446: assertTrue(list != list2);
1447: assertTrue(list2.equals(list));
1448: assertTrue(list.equals(list2));
1449: }
1450:
1451: public void testLongSerialization() throws Exception {
1452: // recursive serialization will cause a stack
1453: // overflow exception with long lists
1454: for (int i = 0; i < 10000; i++) {
1455: list.add(new Integer(i));
1456: }
1457:
1458: java.io.ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
1459: java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(
1460: buf);
1461: out.writeObject(list);
1462: out.flush();
1463: out.close();
1464:
1465: java.io.ByteArrayInputStream bufin = new java.io.ByteArrayInputStream(
1466: buf.toByteArray());
1467: java.io.ObjectInputStream in = new java.io.ObjectInputStream(
1468: bufin);
1469: Object list2 = in.readObject();
1470:
1471: assertTrue(list != list2);
1472: assertTrue(list2.equals(list));
1473: assertTrue(list.equals(list2));
1474: }
1475:
1476: /**
1477: * Ignore the serialization tests for sublists and sub-sublists.
1478: *
1479: * @return an array of sublist serialization test names
1480: */
1481: public String[] ignoredTests() {
1482: ArrayList list = new ArrayList();
1483: String prefix = "TestCursorableLinkedList";
1484: String bulk = ".bulkTestSubList";
1485: String[] ignored = new String[] {
1486: ".testEmptyListSerialization",
1487: ".testFullListSerialization",
1488: ".testEmptyListCompatibility",
1489: ".testFullListCompatibility",
1490: ".testSimpleSerialization",
1491: ".testCanonicalEmptyCollectionExists",
1492: ".testCanonicalFullCollectionExists",
1493: ".testSerializeDeserializeThenCompare" };
1494: for (int i = 0; i < ignored.length; i++) {
1495: list.add(prefix + bulk + ignored[i]);
1496: list.add(prefix + bulk + bulk + ignored[i]);
1497: }
1498: return (String[]) list.toArray(new String[0]);
1499: }
1500:
1501: }
|