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 13. June 2006 by Guilherme Silveira
011: */
012: package com.thoughtworks.xstream.persistence;
013:
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import java.util.Map;
017: import java.util.Set;
018:
019: import junit.framework.TestCase;
020:
021: public class XmlMapTest extends TestCase {
022:
023: private MockedStrategy strategy;
024:
025: public void setUp() throws Exception {
026: super .setUp();
027: strategy = new MockedStrategy();
028: }
029:
030: public void testWritesASingleObject() {
031: XmlMap map = new XmlMap(this .strategy);
032: map.put("guilherme", "aCuteString");
033: assertTrue(strategy.map.containsKey("guilherme"));
034: }
035:
036: public void testWritesTwoObjects() {
037: XmlMap map = new XmlMap(this .strategy);
038: map.put("guilherme", "aCuteString");
039: map.put("silveira", "anotherCuteString");
040: assertTrue(strategy.map.containsKey("guilherme"));
041: assertTrue(strategy.map.containsKey("silveira"));
042: }
043:
044: public void testRemovesAWrittenObject() {
045: XmlMap map = new XmlMap(this .strategy);
046: map.put("guilherme", "aCuteString");
047: assertTrue(strategy.map.containsKey("guilherme"));
048: String aCuteString = (String) map.remove("guilherme");
049: assertEquals("aCuteString", aCuteString);
050: assertFalse(strategy.map.containsKey("guilherme"));
051: }
052:
053: public void testRemovesAnInvalidObject() {
054: XmlMap map = new XmlMap(this .strategy);
055: String aCuteString = (String) map.remove("guilherme");
056: assertNull(aCuteString);
057: }
058:
059: public void testHasZeroLength() {
060: XmlMap map = new XmlMap(this .strategy);
061: assertEquals(0, map.size());
062: }
063:
064: public void testHasOneItem() {
065: XmlMap map = new XmlMap(this .strategy);
066: map.put("guilherme", "aCuteString");
067: assertEquals(1, map.size());
068: }
069:
070: public void testHasTwoItems() {
071: XmlMap map = new XmlMap(this .strategy);
072: map.put("guilherme", "aCuteString");
073: map.put("silveira", "anotherCuteString");
074: assertEquals(2, map.size());
075: }
076:
077: public void testIsNotEmpty() {
078: XmlMap map = new XmlMap(this .strategy);
079: map.put("guilherme", "aCuteString");
080: assertFalse("Map should not be empty", map.isEmpty());
081: }
082:
083: public void testDoesNotContainKey() {
084: XmlMap map = new XmlMap(this .strategy);
085: assertFalse(map.containsKey("guilherme"));
086: }
087:
088: public void testContainsKey() {
089: XmlMap map = new XmlMap(this .strategy);
090: map.put("guilherme", "aCuteString");
091: assertTrue(map.containsKey("guilherme"));
092: }
093:
094: public void testGetsAnObject() {
095: XmlMap map = new XmlMap(this .strategy);
096: this .strategy.map.put("guilherme", "aCuteString");
097: String aCuteString = (String) map.get("guilherme");
098: assertEquals("aCuteString", aCuteString);
099: }
100:
101: public void testGetsAnInvalidObject() {
102: XmlMap map = new XmlMap(this .strategy);
103: String aCuteString = (String) map.get("guilherme");
104: assertNull(aCuteString);
105: }
106:
107: public void testRewritesASingleObject() {
108: XmlMap map = new XmlMap(this .strategy);
109: map.put("guilherme", "aCuteString");
110: assertEquals("aCuteString", map.get("guilherme"));
111: map.put("guilherme", "anotherCuteString");
112: assertEquals("anotherCuteString", map.get("guilherme"));
113: }
114:
115: public void testIsEmpty() {
116: XmlMap map = new XmlMap(this .strategy);
117: assertTrue("Map should be empty", map.isEmpty());
118: }
119:
120: public void testClearsItsObjects() {
121: XmlMap map = new XmlMap(this .strategy);
122: map.put("guilherme", "aCuteString");
123: map.put("silveira", "anotherCuteString");
124: map.clear();
125: assertEquals(0, map.size());
126: }
127:
128: public void testPutsAllAddsTwoItems() {
129: Map original = new HashMap();
130: original.put("guilherme", "aCuteString");
131: original.put("silveira", "anotherCuteString");
132: XmlMap map = new XmlMap(this .strategy);
133: map.putAll(original);
134: assertEquals(2, map.size());
135: }
136:
137: public void testContainsASpecificValue() {
138: XmlMap map = new XmlMap(this .strategy);
139: String value = "aCuteString";
140: map.put("guilherme", value);
141: assertTrue(map.containsValue(value));
142: }
143:
144: public void testDoesNotContainASpecificValue() {
145: XmlMap map = new XmlMap(this .strategy);
146: assertFalse(map.containsValue("zzzz"));
147: }
148:
149: public void testEntrySetContainsAllItems() {
150: Map original = new HashMap();
151: original.put("guilherme", "aCuteString");
152: original.put("silveira", "anotherCuteString");
153: Set originalSet = original.entrySet();
154: XmlMap map = new XmlMap(this .strategy);
155: map.put("guilherme", "aCuteString");
156: map.put("silveira", "anotherCuteString");
157: Set set = map.entrySet();
158: assertTrue(set.containsAll(originalSet));
159: }
160:
161: // actually an acceptance test?
162: public void testIteratesOverEntryAndChecksItsKeyWithAnotherInstance() {
163: XmlMap map = new XmlMap(this .strategy);
164: map.put("guilherme", "aCuteString");
165: map.put("silveira", "anotherCuteString");
166: XmlMap built = new XmlMap(this .strategy);
167: for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
168: Map.Entry entry = (Map.Entry) iter.next();
169: assertTrue(built.containsKey(entry.getKey()));
170: }
171: }
172:
173: // actually an acceptance test?
174: public void testIteratesOverEntryAndChecksItsValueWithAnotherInstance() {
175: XmlMap map = new XmlMap(this .strategy);
176: map.put("guilherme", "aCuteString");
177: map.put("silveira", "anotherCuteString");
178: XmlMap built = new XmlMap(this .strategy);
179: for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
180: Map.Entry entry = (Map.Entry) iter.next();
181: assertTrue(built.containsValue(entry.getValue()));
182: }
183: }
184:
185: public void testIteratesOverEntrySetContainingTwoItems() {
186: XmlMap map = new XmlMap(this .strategy);
187: map.put("guilherme", "aCuteString");
188: map.put("silveira", "anotherCuteString");
189: Map built = new HashMap();
190: for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
191: Map.Entry entry = (Map.Entry) iter.next();
192: built.put(entry.getKey(), entry.getValue());
193: }
194: assertEquals(map, built);
195: }
196:
197: public void testRemovesAnItemThroughIteration() {
198: XmlMap map = new XmlMap(this .strategy);
199: map.put("guilherme", "aCuteString");
200: map.put("silveira", "anotherCuteString");
201: for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
202: Map.Entry entry = (Map.Entry) iter.next();
203: if (entry.getKey().equals("guilherme")) {
204: iter.remove();
205: }
206: }
207: assertFalse(map.containsKey("guilherme"));
208: }
209:
210: public void testRewritesAObject() {
211: XmlMap map = new XmlMap(this .strategy);
212: map.put("guilherme", "aCuteString");
213: map.put("guilherme", "anotherCuteString");
214: assertEquals("anotherCuteString", map.get("guilherme"));
215: }
216:
217: public void testPutReturnsTheOldValueWhenRewritingAObject() {
218: XmlMap map = new XmlMap(this .strategy);
219: map.put("guilherme", "aCuteString");
220: assertEquals("aCuteString", map.put("guilherme",
221: "anotherCuteString"));
222: }
223:
224: private static class MockedStrategy implements StreamStrategy {
225:
226: private Map map = new HashMap();
227:
228: public Iterator iterator() {
229: return map.entrySet().iterator();
230: }
231:
232: public int size() {
233: return map.size();
234: }
235:
236: public Object get(Object key) {
237: return map.get(key);
238: }
239:
240: public Object put(Object key, Object value) {
241: return map.put(key, value);
242: }
243:
244: public Object remove(Object key) {
245: return map.remove(key);
246: }
247:
248: }
249:
250: }
|