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 28. June 2006 by Guilherme Silveira
011: */
012: package com.thoughtworks.xstream.persistence;
013:
014: import java.util.HashMap;
015: import java.util.HashSet;
016: import java.util.Iterator;
017: import java.util.Map;
018: import java.util.Set;
019:
020: import junit.framework.TestCase;
021:
022: public class XmlSetTest extends TestCase {
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: XmlSet set = new XmlSet(this .strategy);
032: set.add("guilherme");
033: assertTrue(strategy.map.containsValue("guilherme"));
034: }
035:
036: public void testWritesTwoObjects() {
037: XmlSet set = new XmlSet(this .strategy);
038: set.add("guilherme");
039: set.add("silveira");
040: assertTrue(strategy.map.containsValue("guilherme"));
041: assertTrue(strategy.map.containsValue("silveira"));
042: }
043:
044: public void testRemovesAWrittenObject() {
045: XmlSet set = new XmlSet(this .strategy);
046: set.add("guilherme");
047: assertTrue(strategy.map.containsValue("guilherme"));
048: boolean changed = set.remove("guilherme");
049: assertTrue(changed);
050: assertFalse(strategy.map.containsValue("guilherme"));
051: }
052:
053: public void testRemovesAnInvalidObject() {
054: XmlSet set = new XmlSet(this .strategy);
055: boolean removed = set.remove("guilherme");
056: assertFalse(removed);
057: }
058:
059: public void testHasZeroLength() {
060: XmlSet set = new XmlSet(this .strategy);
061: assertEquals(0, set.size());
062: }
063:
064: public void testHasOneItem() {
065: XmlSet set = new XmlSet(this .strategy);
066: set.add("guilherme");
067: assertEquals(1, set.size());
068: }
069:
070: public void testHasTwoItems() {
071: XmlSet set = new XmlSet(this .strategy);
072: set.add("guilherme");
073: set.add("silveira");
074: assertEquals(2, set.size());
075: }
076:
077: public void testIsNotEmpty() {
078: XmlSet set = new XmlSet(this .strategy);
079: set.add("guilherme");
080: assertFalse("set should not be empty", set.isEmpty());
081: }
082:
083: public void testDoesNotContainKey() {
084: XmlSet set = new XmlSet(this .strategy);
085: assertFalse(set.contains("guilherme"));
086: }
087:
088: public void testContainsKey() {
089: XmlSet set = new XmlSet(this .strategy);
090: set.add("guilherme");
091: assertTrue(set.contains("guilherme"));
092: }
093:
094: public void testGetsAnObject() {
095: XmlSet set = new XmlSet(this .strategy);
096: set.add("guilherme");
097: Object onlyValue = set.iterator().next();
098: assertEquals("guilherme", onlyValue);
099: }
100:
101: public void testIsEmpty() {
102: XmlSet set = new XmlSet(this .strategy);
103: assertTrue("set should be empty", set.isEmpty());
104: }
105:
106: public void testClearsItsObjects() {
107: XmlSet set = new XmlSet(this .strategy);
108: set.add("guilherme");
109: set.add("silveira");
110: set.clear();
111: assertEquals(0, set.size());
112: }
113:
114: public void testPutsAllAddsTwoItems() {
115: Set original = new HashSet();
116: original.add("guilherme");
117: original.add("silveira");
118: XmlSet set = new XmlSet(this .strategy);
119: set.addAll(original);
120: assertEquals(2, set.size());
121: }
122:
123: public void testContainsASpecificValue() {
124: XmlSet set = new XmlSet(this .strategy);
125: set.add("guilherme");
126: assertTrue(set.contains("guilherme"));
127: }
128:
129: public void testDoesNotContainASpecificValue() {
130: XmlSet set = new XmlSet(this .strategy);
131: assertFalse(set.contains("zzzz"));
132: }
133:
134: public void testEntrySetContainsAllItems() {
135: Set original = new HashSet();
136: original.add("guilherme");
137: original.add("silveira");
138: XmlSet set = new XmlSet(this .strategy);
139: set.add("guilherme");
140: set.add("silveira");
141: assertTrue(set.containsAll(original));
142: }
143:
144: // actually an acceptance test?
145: public void testIteratesOverEntryAndChecksWithAnotherInstance() {
146: XmlSet set = new XmlSet(this .strategy);
147: set.add("guilherme");
148: set.add("silveira");
149: XmlSet built = new XmlSet(this .strategy);
150: for (Iterator iter = set.iterator(); iter.hasNext();) {
151: Object entry = iter.next();
152: assertTrue(built.contains(entry));
153: }
154: }
155:
156: public void testIteratesOverEntrySetContainingTwoItems() {
157: XmlSet set = new XmlSet(this .strategy);
158: set.add("guilherme");
159: set.add("silveira");
160: Set built = new HashSet();
161: for (Iterator iter = set.iterator(); iter.hasNext();) {
162: Object entry = iter.next();
163: built.add(entry);
164: }
165: assertEquals(set, built);
166: }
167:
168: public void testRemovesAnItemThroughIteration() {
169: XmlSet set = new XmlSet(this .strategy);
170: set.add("guilherme");
171: set.add("silveira");
172: for (Iterator iter = set.iterator(); iter.hasNext();) {
173: Object entry = iter.next();
174: if (entry.equals("guilherme")) {
175: iter.remove();
176: }
177: }
178: assertFalse(set.contains("guilherme"));
179: }
180:
181: private static class MockedStrategy implements StreamStrategy {
182:
183: private Map map = new HashMap();
184:
185: public Iterator iterator() {
186: return map.entrySet().iterator();
187: }
188:
189: public int size() {
190: return map.size();
191: }
192:
193: public Object get(Object key) {
194: return map.get(key);
195: }
196:
197: public Object put(Object key, Object value) {
198: return map.put(key, value);
199: }
200:
201: public Object remove(Object key) {
202: return map.remove(key);
203: }
204:
205: }
206:
207: }
|