001: /*
002: * Copyright (C) 2006 Joe Walnes.
003: * Copyright (C) 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 06. July 2006 by Guilherme Silveira
011: */
012: package com.thoughtworks.xstream.persistence;
013:
014: import java.util.ArrayList;
015: import java.util.HashMap;
016: import java.util.HashSet;
017: import java.util.Iterator;
018: import java.util.List;
019: import java.util.Map;
020: import java.util.Set;
021:
022: import junit.framework.TestCase;
023:
024: public class XmlArrayListTest extends TestCase {
025: private MockedStrategy strategy;
026:
027: public void setUp() throws Exception {
028: super .setUp();
029: strategy = new MockedStrategy();
030: }
031:
032: public void testWritesASingleObject() {
033: XmlArrayList xmlList = new XmlArrayList(this .strategy);
034: xmlList.add("guilherme");
035: assertTrue(strategy.map.containsValue("guilherme"));
036: }
037:
038: public void testWritesASingleObjectInANegativePosition() {
039: XmlArrayList xmlList = new XmlArrayList(this .strategy);
040: try {
041: xmlList.add(-1, "guilherme");
042: fail();
043: } catch (IndexOutOfBoundsException ex) {
044: // ok
045: }
046: }
047:
048: public void testWritesASingleObjectInFirstPosition() {
049: XmlArrayList xmlList = new XmlArrayList(this .strategy);
050: xmlList.add("guilherme");
051: assertTrue(strategy.map.containsKey("0"));
052: }
053:
054: public void testWritesTwoObjects() {
055: XmlArrayList xmlList = new XmlArrayList(this .strategy);
056: xmlList.add("guilherme");
057: xmlList.add("silveira");
058: assertTrue(strategy.map.containsValue("guilherme"));
059: assertTrue(strategy.map.containsValue("silveira"));
060: assertTrue(strategy.map.containsKey("0"));
061: assertTrue(strategy.map.containsKey("1"));
062: }
063:
064: public void testWritesTwoObjectsGuaranteesItsEnumerationOrder() {
065: XmlArrayList xmlList = new XmlArrayList(this .strategy);
066: xmlList.add("guilherme");
067: xmlList.add("silveira");
068: assertEquals("guilherme", strategy.map.get("0"));
069: assertEquals("silveira", strategy.map.get("1"));
070: }
071:
072: public void testWritesASecondObjectInAPositionHigherThanTheListsSize() {
073: XmlArrayList xmlList = new XmlArrayList(this .strategy);
074: try {
075: xmlList.add("silveira");
076: xmlList.add(3, "guilherme");
077: fail();
078: } catch (IndexOutOfBoundsException ex) {
079: // ok
080: }
081: }
082:
083: public void testRemovesAWrittenObject() {
084: XmlArrayList xmlList = new XmlArrayList(this .strategy);
085: xmlList.add("guilherme");
086: boolean changed = xmlList.remove("guilherme");
087: assertFalse(strategy.map.containsValue("guilherme"));
088: }
089:
090: public void testRemovesAWrittenObjectImplyingInAChangeInTheList() {
091: XmlArrayList xmlList = new XmlArrayList(this .strategy);
092: xmlList.add("guilherme");
093: boolean changed = xmlList.remove("guilherme");
094: assertTrue(changed);
095: }
096:
097: public void testRemovesAnInvalidObjectWithoutAffectingTheList() {
098: XmlArrayList xmlList = new XmlArrayList(this .strategy);
099: boolean removed = xmlList.remove("guilherme");
100: assertFalse(removed);
101: }
102:
103: public void testHasZeroLengthWhenInstantiated() {
104: XmlArrayList xmlList = new XmlArrayList(this .strategy);
105: assertEquals(0, xmlList.size());
106: }
107:
108: public void testHasOneItem() {
109: XmlArrayList xmlList = new XmlArrayList(this .strategy);
110: xmlList.add("guilherme");
111: assertEquals(1, xmlList.size());
112: }
113:
114: public void testHasTwoItems() {
115: XmlArrayList xmlList = new XmlArrayList(this .strategy);
116: xmlList.add("guilherme");
117: xmlList.add("silveira");
118: assertEquals(2, xmlList.size());
119: }
120:
121: public void testIsNotEmpty() {
122: XmlArrayList xmlList = new XmlArrayList(this .strategy);
123: xmlList.add("guilherme");
124: assertFalse(xmlList.isEmpty());
125: }
126:
127: public void testDoesNotContainKey() {
128: XmlArrayList xmlList = new XmlArrayList(this .strategy);
129: assertFalse(xmlList.contains("guilherme"));
130: }
131:
132: public void testContainsKey() {
133: XmlArrayList xmlList = new XmlArrayList(this .strategy);
134: xmlList.add("guilherme");
135: assertTrue(xmlList.contains("guilherme"));
136: }
137:
138: public void testGetsAnObject() {
139: XmlArrayList xmlList = new XmlArrayList(this .strategy);
140: xmlList.add("guilherme");
141: Object onlyValue = xmlList.iterator().next();
142: assertEquals("guilherme", onlyValue);
143: }
144:
145: public void testGetsTheFirstObject() {
146: XmlArrayList xmlList = new XmlArrayList(this .strategy);
147: xmlList.add("guilherme");
148: assertEquals("guilherme", xmlList.get(0));
149: }
150:
151: public void testGetsTheSecondObject() {
152: XmlArrayList xmlList = new XmlArrayList(this .strategy);
153: xmlList.add("guilherme");
154: xmlList.add("silveira");
155: assertEquals("silveira", xmlList.get(1));
156: }
157:
158: public void testInsertsAnObjectInTheMiddleOfTheList() {
159: XmlArrayList xmlList = new XmlArrayList(this .strategy);
160: xmlList.add("guilherme");
161: xmlList.add("silveira");
162: xmlList.add(1, "de azevedo");
163: assertEquals("guilherme", xmlList.get(0));
164: assertEquals("de azevedo", xmlList.get(1));
165: assertEquals("silveira", xmlList.get(2));
166: }
167:
168: public void testIteratingGuaranteesItsEnumeration() {
169: XmlArrayList xmlList = new XmlArrayList(this .strategy);
170: xmlList.add("guilherme");
171: xmlList.add("silveira");
172: Iterator it = xmlList.iterator();
173: assertEquals("guilherme", it.next());
174: assertEquals("silveira", it.next());
175: }
176:
177: public void testIsEmpty() {
178: XmlArrayList xmlList = new XmlArrayList(this .strategy);
179: assertTrue(xmlList.isEmpty());
180: }
181:
182: public void testClearsItsObjects() {
183: XmlArrayList xmlList = new XmlArrayList(this .strategy);
184: xmlList.add("guilherme");
185: xmlList.add("silveira");
186: xmlList.clear();
187: assertEquals(0, xmlList.size());
188: }
189:
190: public void testPutsAllAddsTwoItems() {
191: Set original = new HashSet();
192: original.add("guilherme");
193: original.add("silveira");
194: XmlArrayList xmlList = new XmlArrayList(this .strategy);
195: xmlList.addAll(original);
196: assertEquals(2, xmlList.size());
197: }
198:
199: public void testContainsASpecificValue() {
200: XmlArrayList xmlList = new XmlArrayList(this .strategy);
201: xmlList.add("guilherme");
202: assertTrue(xmlList.contains("guilherme"));
203: }
204:
205: public void testDoesNotContainASpecificValue() {
206: XmlArrayList xmlList = new XmlArrayList(this .strategy);
207: assertFalse(xmlList.contains("zzzz"));
208: }
209:
210: public void testEntrySetContainsAllItems() {
211: Set original = new HashSet();
212: original.add("guilherme");
213: original.add("silveira");
214: XmlArrayList xmlList = new XmlArrayList(this .strategy);
215: xmlList.add("guilherme");
216: xmlList.add("silveira");
217: assertTrue(xmlList.containsAll(original));
218: }
219:
220: // actually an acceptance test?
221: public void testIteratesOverEntryAndChecksWithAnotherInstance() {
222: XmlArrayList xmlList = new XmlArrayList(this .strategy);
223: xmlList.add("guilherme");
224: xmlList.add("silveira");
225: XmlArrayList built = new XmlArrayList(this .strategy);
226: for (Iterator iter = xmlList.iterator(); iter.hasNext();) {
227: Object entry = iter.next();
228: assertTrue(built.contains(entry));
229: }
230: }
231:
232: public void testIteratesOverEntrySetContainingTwoItems() {
233: XmlArrayList xmlList = new XmlArrayList(this .strategy);
234: xmlList.add("guilherme");
235: xmlList.add("silveira");
236: List built = new ArrayList();
237: for (Iterator iter = xmlList.iterator(); iter.hasNext();) {
238: Object entry = iter.next();
239: built.add(entry);
240: }
241: assertEquals(xmlList, built);
242: }
243:
244: public void testRemovesAnItemThroughIteration() {
245: XmlArrayList xmlList = new XmlArrayList(this .strategy);
246: xmlList.add("guilherme");
247: xmlList.add("silveira");
248: for (Iterator iter = xmlList.iterator(); iter.hasNext();) {
249: Object entry = iter.next();
250: if (entry.equals("guilherme")) {
251: iter.remove();
252: }
253: }
254: assertFalse(xmlList.contains("guilherme"));
255: }
256:
257: private static class MockedStrategy implements StreamStrategy {
258:
259: private Map map = new HashMap();
260:
261: public Iterator iterator() {
262: return map.entrySet().iterator();
263: }
264:
265: public int size() {
266: return map.size();
267: }
268:
269: public Object get(Object key) {
270: return map.get(key);
271: }
272:
273: public Object put(Object key, Object value) {
274: return map.put(key, value);
275: }
276:
277: public Object remove(Object key) {
278: return map.remove(key);
279: }
280:
281: }
282:
283: }
|