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 17. June 2006 by Guilherme Silveira
011: */
012: package com.thoughtworks.xstream.persistence;
013:
014: import java.io.File;
015: import java.util.HashMap;
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 FileStreamStrategyTest extends TestCase {
023:
024: private final File baseDir = new File("tmp-xstream-test");
025:
026: protected void setUp() throws Exception {
027: super .setUp();
028: if (baseDir.exists()) {
029: clear(baseDir);
030: }
031: baseDir.mkdirs();
032: }
033:
034: protected void tearDown() throws Exception {
035: super .tearDown();
036: clear(baseDir);
037: }
038:
039: private void clear(File dir) {
040: File[] files = dir.listFiles();
041: for (int i = 0; i < files.length; i++) {
042: if (files[i].isFile()) {
043: boolean deleted = files[i].delete();
044: if (!deleted) {
045: throw new RuntimeException(
046: "Unable to continue testing: unable to remove file "
047: + files[i].getAbsolutePath());
048: }
049: }
050: }
051: dir.delete();
052: }
053:
054: public void testConcatenatesXmlExtensionWhileGettingAFilename() {
055: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
056: assertEquals("guilherme.xml", strategy.getName("guilherme"));
057: }
058:
059: public void testConcatenatesXmlExtensionWhileExtractingAKey() {
060: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
061: assertEquals("guilherme", strategy.extractKey("guilherme.xml"));
062: }
063:
064: public void testEscapesNonAcceptableCharacterWhileExtractingAKey() {
065: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
066: assertEquals("../guilherme", strategy
067: .extractKey("_2e__2e__2f_guilherme.xml"));
068: }
069:
070: public void testEscapesNonAcceptableCharacterWhileGettingAFilename() {
071: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
072: assertEquals("_2e__2e__2f_guilherme.xml", strategy
073: .getName("../guilherme"));
074: }
075:
076: public void testEscapesUTF8NonAcceptableCharacterWhileGettingAFilename() {
077: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
078: assertEquals("_5377_guilherme.xml", strategy
079: .getName("\u5377guilherme"));
080: }
081:
082: public void testEscapesUTF8NonAcceptableCharacterWhileExtractingAKey() {
083: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
084: assertEquals("\u5377guilherme", strategy
085: .extractKey("_5377_guilherme.xml"));
086: }
087:
088: public void testEscapesUnderlineWhileGettingAFilename() {
089: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
090: assertEquals("__guilherme.xml", strategy.getName("_guilherme"));
091: }
092:
093: public void testEscapesUnderlineWhileExtractingAKey() {
094: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
095: assertEquals("_guilherme", strategy
096: .extractKey("__guilherme.xml"));
097: }
098:
099: public void testWritesASingleFile() {
100: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
101: strategy.put("guilherme", "aCuteString");
102: File file = new File(baseDir, "guilherme.xml");
103: assertTrue(file.exists());
104: }
105:
106: public void testWritesTwoFiles() {
107: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
108: strategy.put("guilherme", "aCuteString");
109: strategy.put("silveira", "anotherCuteString");
110: assertTrue(new File(baseDir, "guilherme.xml").exists());
111: assertTrue(new File(baseDir, "silveira.xml").exists());
112: }
113:
114: public void testRemovesAWrittenFile() {
115: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
116: strategy.put("guilherme", "aCuteString");
117: assertTrue(new File(baseDir, "guilherme.xml").exists());
118: String aCuteString = (String) strategy.remove("guilherme");
119: assertEquals("aCuteString", aCuteString);
120: assertFalse(new File(baseDir, "guilherme.xml").exists());
121: }
122:
123: public void testRemovesAnInvalidFile() {
124: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
125: String aCuteString = (String) strategy.remove("guilherme");
126: assertNull(aCuteString);
127: }
128:
129: public void testHasZeroLength() {
130: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
131: assertEquals(0, strategy.size());
132: }
133:
134: public void testHasOneItem() {
135: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
136: strategy.put("guilherme", "aCuteString");
137: assertEquals(1, strategy.size());
138: }
139:
140: public void testHasTwoItems() {
141: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
142: strategy.put("guilherme", "aCuteString");
143: strategy.put("silveira", "anotherCuteString");
144: assertEquals(2, strategy.size());
145: }
146:
147: public void testIsNotEmpty() {
148: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
149: strategy.put("guilherme", "aCuteString");
150: assertEquals("Map should not be empty", 1, strategy.size());
151: }
152:
153: public void testDoesNotContainKey() {
154: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
155: assertFalse(strategy.containsKey("guilherme"));
156: }
157:
158: public void testContainsKey() {
159: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
160: strategy.put("guilherme", "aCuteString");
161: assertTrue(strategy.containsKey("guilherme"));
162: }
163:
164: public void testGetsAFile() {
165: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
166: strategy.put("guilherme", "aCuteString");
167: assertTrue(new File(baseDir, "guilherme.xml").exists());
168: String aCuteString = (String) strategy.get("guilherme");
169: assertEquals("aCuteString", aCuteString);
170: }
171:
172: public void testGetsAnInvalidFile() {
173: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
174: String aCuteString = (String) strategy.get("guilherme");
175: assertNull(aCuteString);
176: }
177:
178: public void testRewritesASingleFile() {
179: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
180: strategy.put("guilherme", "aCuteString");
181: File file = new File(baseDir, "guilherme.xml");
182: assertTrue(file.exists());
183: strategy.put("guilherme", "anotherCuteString");
184: assertEquals("anotherCuteString", strategy.get("guilherme"));
185: }
186:
187: public void testIsEmpty() {
188: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
189: assertEquals("Map should be empty", 0, strategy.size());
190: }
191:
192: public void testEntrySetContainsAllItems() {
193: Map original = new HashMap();
194: original.put("guilherme", "aCuteString");
195: original.put("silveira", "anotherCuteString");
196: Set originalSet = original.entrySet();
197: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
198: strategy.put("guilherme", "aCuteString");
199: strategy.put("silveira", "anotherCuteString");
200: for (Iterator iter = original.keySet().iterator(); iter
201: .hasNext();) {
202: assertTrue(strategy.containsKey(iter.next()));
203: }
204: }
205:
206: // actually an acceptance test?
207: public void testIteratesOverEntryAndChecksItsKeyWithAnotherInstance() {
208: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
209: strategy.put("guilherme", "aCuteString");
210: strategy.put("silveira", "anotherCuteString");
211: FileStreamStrategy built = new FileStreamStrategy(baseDir);
212: for (Iterator iter = strategy.iterator(); iter.hasNext();) {
213: Map.Entry entry = (Map.Entry) iter.next();
214: assertTrue(built.containsKey(entry.getKey()));
215: }
216: }
217:
218: public void testRemovesAnItemThroughIteration() {
219: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
220: strategy.put("guilherme", "aCuteString");
221: strategy.put("silveira", "anotherCuteString");
222: for (Iterator iter = strategy.iterator(); iter.hasNext();) {
223: Map.Entry entry = (Map.Entry) iter.next();
224: if (entry.getKey().equals("guilherme")) {
225: iter.remove();
226: }
227: }
228: assertFalse(strategy.containsKey("guilherme"));
229: }
230:
231: public void testRewritesAFile() {
232: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
233: strategy.put("guilherme", "aCuteString");
234: strategy.put("guilherme", "anotherCuteString");
235: assertEquals("anotherCuteString", strategy.get("guilherme"));
236: }
237:
238: public void testPutReturnsTheOldValueWhenRewritingAFile() {
239: FileStreamStrategy strategy = new FileStreamStrategy(baseDir);
240: strategy.put("guilherme", "aCuteString");
241: assertEquals("aCuteString", strategy.put("guilherme",
242: "anotherCuteString"));
243: }
244:
245: }
|