001: package com.sun.portal.app.filesharing.repo;
002:
003: import com.sun.portal.app.filesharing.util.LogUtilsSetter;
004: import junit.framework.TestCase;
005:
006: import java.io.ByteArrayInputStream;
007: import java.io.File;
008: import java.io.InputStream;
009: import java.util.HashSet;
010: import java.util.Set;
011:
012: /**
013: * @author Alejandro Abdelnur
014: */
015: public class FileRepositoryTest extends TestCase {
016:
017: private static final String ROOT_TEST = "build/repotest";
018: private File _root;
019:
020: static {
021: new LogUtilsSetter();
022: }
023:
024: public void setUp() throws Exception {
025: _root = new File(ROOT_TEST);
026: if (_root.exists()) {
027: deleteDir(_root);
028: }
029: if (!_root.mkdirs()) {
030: throw new Exception(
031: "FileRepository root could not be created ["
032: + _root.getAbsolutePath() + "]");
033: }
034: System.out.println("FileRepository root at ["
035: + _root.getAbsolutePath() + "]");
036: }
037:
038: public void tearDown() throws Exception {
039: if (_root != null) {
040: // deleteDir(_root);
041: }
042: }
043:
044: private Repository getRepository() throws Exception {
045: return new FileRepository(getFileRoot());
046: }
047:
048: protected File getFileRoot() {
049: return _root;
050: }
051:
052: public static void deleteDir(File file) throws Exception {
053: if (file.isDirectory()) {
054: File[] files = file.listFiles();
055: for (int i = 0; i < files.length; i++) {
056: deleteDir(files[i]);
057: }
058: }
059: if (!file.delete()) {
060: throw new Exception(
061: "File in FileRepository could not be deleted ["
062: + file + "]");
063: }
064: }
065:
066: public void testConstructor() throws Exception {
067: getRepository();
068: assertTrue("FileRepository root dir exists", true);
069:
070: try {
071: new FileRepository(new File(getFileRoot(), "AA"));
072: File f = new File(getFileRoot(), "AA");
073: assertTrue("FileRepository root with anchor exists", f
074: .exists());
075: } catch (RepoException ex) {
076: assertEquals("F000", ex.getId());
077: }
078:
079: try {
080: File f = new File(getFileRoot(), "file");
081: f.createNewFile();
082: new FileRepository(f);
083: assertTrue("FileRepository root cannot be a file", false);
084: } catch (RepoException ex) {
085: assertEquals("F001", ex.getId());
086: }
087:
088: new FileRepository(getFileRoot(), "aa");
089: File f = new File(getFileRoot(), "aa");
090: assertTrue("FileRepository with anchor exists", f.exists());
091: }
092:
093: private static final byte[] PAYLOAD = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
094: .getBytes();
095:
096: public void testAddFile() throws Exception {
097: Repository r = getRepository();
098:
099: InputStream is = new ByteArrayInputStream(PAYLOAD);
100: RepoItem rItem = new RepoItem(RepoItem.ROOT, "f.txt", "owner");
101: r.addFile(rItem, is);
102: File f = new File(getFileRoot(), "f.txt");
103: assertTrue("File f.txt exists in repo", f.exists());
104:
105: is = new ByteArrayInputStream(PAYLOAD);
106: rItem = new RepoItem(RepoItem.ROOT, "f.txt", "owner");
107: try {
108: r.addFile(rItem, is);
109: assertTrue("File f.txt already exists", false);
110: } catch (RepoException ex) {
111: assertEquals("F010", ex.getId());
112: }
113:
114: is = new ByteArrayInputStream(PAYLOAD);
115: rItem = new RepoItem(
116: new RepoItem(RepoItem.ROOT, "ff", "owner"), "f.txt",
117: "owner");
118: try {
119: r.addFile(rItem, is);
120: assertTrue("Parent ff does not exist", false);
121: } catch (RepoException ex) {
122: assertEquals("F012", ex.getId());
123: }
124:
125: }
126:
127: public void testReplaceFile() throws Exception {
128: Repository r = getRepository();
129:
130: InputStream is = new ByteArrayInputStream(PAYLOAD);
131: RepoItem rItem = new RepoItem(RepoItem.ROOT, "f.txt", "owner");
132: r.replaceFile(rItem, is);
133: File f = new File(getFileRoot(), "f.txt");
134: assertTrue("File f.txt exists in repo", f.exists());
135:
136: is = new ByteArrayInputStream(PAYLOAD);
137: rItem = new RepoItem(RepoItem.ROOT, "f.txt", "owner");
138: r.replaceFile(rItem, is);
139:
140: f = new File(getFileRoot(), "folder");
141: f.mkdir();
142: is = new ByteArrayInputStream(PAYLOAD);
143: rItem = new RepoItem(RepoItem.ROOT, "folder", "owner");
144: try {
145: r.replaceFile(rItem, is);
146: assertTrue("File folder is a dir", false);
147: } catch (RepoException ex) {
148: assertEquals("F011", ex.getId());
149: }
150:
151: is = new ByteArrayInputStream(PAYLOAD);
152: rItem = new RepoItem(
153: new RepoItem(RepoItem.ROOT, "ff", "owner"), "f.txt",
154: "owner");
155: try {
156: r.replaceFile(rItem, is);
157: assertTrue("Parent ff does not exist", false);
158: } catch (RepoException ex) {
159: assertEquals("F012", ex.getId());
160: }
161:
162: }
163:
164: public void testGetFile() throws Exception {
165: Repository r = getRepository();
166:
167: InputStream is = new ByteArrayInputStream(PAYLOAD);
168: RepoItem rItem = new RepoItem(RepoItem.ROOT, "f.txt", "owner");
169: r.replaceFile(rItem, is);
170:
171: byte[] buffer = new byte[50];
172: int counter = 0;
173: is = r.getFile(rItem);
174: int c = is.read();
175: while (c > -1) {
176: buffer[counter++] = (byte) c;
177: c = is.read();
178: }
179: is.close();
180: for (int i = 0; i < counter; i++) {
181: assertEquals(PAYLOAD[i], buffer[i]);
182: }
183:
184: rItem = new RepoItem(RepoItem.ROOT, "ff.txt", "owner");
185: try {
186: is = r.getFile(rItem);
187: assertTrue("File ff.txt does not exist", false);
188: } catch (RepoException ex) {
189: assertEquals("F020", ex.getId());
190: }
191:
192: File f = new File(getFileRoot(), "folder");
193: f.mkdir();
194: rItem = new RepoItem(RepoItem.ROOT, "folder", "owner");
195: try {
196: is = r.getFile(rItem);
197: assertTrue("File folder is a dir", false);
198: } catch (RepoException ex) {
199: assertEquals("F021", ex.getId());
200: }
201:
202: }
203:
204: public void testDeleteFile() throws Exception {
205: Repository r = getRepository();
206:
207: InputStream is = new ByteArrayInputStream(PAYLOAD);
208: RepoItem rItem = new RepoItem(RepoItem.ROOT, "f.txt", "owner");
209: r.replaceFile(rItem, is);
210:
211: File f = new File(getFileRoot(), "f.txt");
212: assertTrue("File created", f.exists());
213: r.deleteFile(rItem);
214: assertTrue("File deleted", !f.exists());
215:
216: rItem = new RepoItem(RepoItem.ROOT, "ff.txt", "owner");
217: try {
218: r.deleteFile(rItem);
219: assertTrue("File ff.txt does not exist", false);
220: } catch (RepoException ex) {
221: assertEquals("F030", ex.getId());
222: }
223:
224: f = new File(getFileRoot(), "folder");
225: f.mkdir();
226: rItem = new RepoItem(RepoItem.ROOT, "folder", "owner");
227: try {
228: r.deleteFile(rItem);
229: assertTrue("File folder is a dir", false);
230: } catch (RepoException ex) {
231: assertEquals("F031", ex.getId());
232: }
233:
234: }
235:
236: public void testAddFolder() throws Exception {
237: Repository repo = getRepository();
238: RepoItem folder = new RepoItem(RepoItem.ROOT, "f1", "owner");
239: repo.addFolder(folder);
240: File f1 = new File(getFileRoot(), "/f1");
241: assertTrue("Folder /f1 exists within Repo", f1.exists());
242:
243: try {
244: folder = new RepoItem(RepoItem.ROOT, "f1", "owner");
245: repo.addFolder(folder);
246: } catch (RepoException ex) {
247: assertEquals(ex.getId(), "F040");
248: }
249:
250: folder = new RepoItem(folder, "f2", "owner");
251: repo.addFolder(folder);
252: f1 = new File(getFileRoot(), "/f1/f2");
253: assertTrue("Folder /f1/f2 exists within Repo", f1.exists());
254:
255: try {
256: folder = new RepoItem(folder, "f1", "owner");
257: folder = new RepoItem(folder, "f1", "owner");
258: repo.addFolder(folder);
259: } catch (RepoException ex) {
260: assertEquals(ex.getId(), "F041");
261: }
262:
263: }
264:
265: public void testGetContent() throws Exception {
266: Repository repo = getRepository();
267: RepoItem i1 = new RepoItem(RepoItem.ROOT, "f1", "owner");
268: repo.addFolder(i1);
269: RepoItem i2 = new RepoItem(i1, "f2", "owner");
270: repo.addFolder(i2);
271: RepoItem i3 = new RepoItem(i1, "f3", "owner");
272: repo.addFolder(i3);
273: InputStream is = new ByteArrayInputStream(PAYLOAD);
274: RepoItem i4 = new RepoItem(RepoItem.ROOT, "f.txt", "owner");
275: repo.replaceFile(i4, is);
276:
277: Set root = new HashSet();
278: root.add(i1);
279: root.add(i4);
280: Set f1 = new HashSet();
281: f1.add(i2);
282: f1.add(i3);
283:
284: RepoItem[] content = repo.getContent(RepoItem.ROOT);
285: Set result = new HashSet();
286: for (int i = 0; i < content.length; i++) {
287: result.add(content[i]);
288: }
289: assertEquals(root, result);
290:
291: content = repo.getContent(i1);
292: result = new HashSet();
293: for (int i = 0; i < content.length; i++) {
294: result.add(content[i]);
295: }
296: assertEquals(f1, result);
297:
298: try {
299: content = repo.getContent(new RepoItem(RepoItem.ROOT,
300: "caca", "owner"));
301: } catch (RepoException ex) {
302: assertEquals(ex.getId(), "F050");
303: }
304:
305: try {
306: content = repo.getContent(i4);
307: } catch (RepoException ex) {
308: assertEquals(ex.getId(), "F051");
309: }
310:
311: }
312:
313: public void testDeleteFolder() throws Exception {
314: Repository repo = getRepository();
315: RepoItem folder = new RepoItem(RepoItem.ROOT, "f1", "owner");
316: repo.addFolder(folder);
317: RepoItem f2 = new RepoItem(folder, "f2", "owner");
318: repo.addFolder(f2);
319: f2 = new RepoItem(folder, "f3", "owner");
320: repo.addFolder(f2);
321:
322: File f1 = new File(getFileRoot(), "/f1");
323: assertTrue("Folder /f1 exists within Repo", f1.exists());
324: f1 = new File(getFileRoot(), "/f1/f2");
325: assertTrue("Folder /f1/f2 exists within Repo", f1.exists());
326: f1 = new File(getFileRoot(), "/f1/f3");
327: assertTrue("Folder /f1/f3 exists within Repo", f1.exists());
328: f1 = new File(getFileRoot(), "/f1/f4");
329: assertTrue("Folder /f1/f4 doesn't exist within Repo", !f1
330: .exists());
331:
332: try {
333: f2 = new RepoItem(folder, "f4", "owner");
334: repo.deleteFolder(f2, true);
335: } catch (RepoException ex) {
336: assertEquals(ex.getId(), "F060");
337: }
338:
339: try {
340: repo.deleteFolder(folder, false);
341: } catch (RepoException ex) {
342: assertEquals(ex.getId(), "F062");
343: }
344:
345: File f = new File(getFileRoot(), "file");
346: f.createNewFile();
347: try {
348: f2 = new RepoItem(RepoItem.ROOT, "file", "owner");
349: repo.deleteFolder(f2, true);
350: } catch (RepoException ex) {
351: assertEquals(ex.getId(), "F061");
352: }
353:
354: f1 = new File(getFileRoot(), "/f1/f2");
355: assertTrue("Folder /f1/f2 exists within Repo", f1.exists());
356: f1 = new File(getFileRoot(), "/f1/f3");
357: assertTrue("Folder /f1/f3 exists within Repo", f1.exists());
358: f1 = new File(getFileRoot(), "/f1/f4");
359: assertTrue("Folder /f1/f4 doesn't exist within Repo", !f1
360: .exists());
361:
362: f2 = new RepoItem(folder, "f3", "owner");
363: repo.deleteFolder(f2, true);
364:
365: f1 = new File(getFileRoot(), "/f1/f2");
366: assertTrue("Folder /f1/f2 exists within Repo", f1.exists());
367: f1 = new File(getFileRoot(), "/f1/f3");
368: assertTrue("Folder /f1/f3 exists within Repo", !f1.exists());
369: f1 = new File(getFileRoot(), "/f1/f4");
370: assertTrue("Folder /f1/f4 doesn't exist within Repo", !f1
371: .exists());
372:
373: repo.deleteFolder(folder, true);
374:
375: f1 = new File(getFileRoot(), "/f1");
376: assertTrue("Folder /f1 exists within Repo", !f1.exists());
377: f1 = new File(getFileRoot(), "/f1/f2");
378: assertTrue("Folder /f1/f2 exists within Repo", !f1.exists());
379: f1 = new File(getFileRoot(), "/f1/f3");
380: assertTrue("Folder /f1/f3 exists within Repo", !f1.exists());
381: f1 = new File(getFileRoot(), "/f1/f4");
382: assertTrue("Folder /f1/f4 doesn't exist within Repo", !f1
383: .exists());
384:
385: f1 = getFileRoot();
386: repo.deleteFolder(RepoItem.ROOT, true);
387: assertTrue("Folder ROOT doesn't exist within Repo", !f1
388: .exists());
389:
390: }
391:
392: public void testCopy() throws Exception {
393: Repository repo = getRepository();
394: RepoItem f1 = new RepoItem(RepoItem.ROOT, "f1", "owner");
395: repo.addFolder(f1);
396: RepoItem f2 = new RepoItem(RepoItem.ROOT, "f2", "owner");
397: repo.addFolder(f2);
398: RepoItem f3 = new RepoItem(RepoItem.ROOT, "f3", "owner");
399: repo.addFolder(f3);
400: RepoItem a1 = new RepoItem(f1, "a1", "owner");
401: repo.addFolder(a1);
402: RepoItem a2 = new RepoItem(f1, "a2", "owner");
403: repo.addFolder(a2);
404: RepoItem a3 = new RepoItem(f1, "a3", "owner");
405: repo.addFolder(a3);
406: InputStream is = new ByteArrayInputStream(PAYLOAD);
407: RepoItem b1 = new RepoItem(RepoItem.ROOT, "b1.txt", "owner");
408: repo.replaceFile(b1, is);
409: is = new ByteArrayInputStream(PAYLOAD);
410: RepoItem b2 = new RepoItem(f1, "b2.txt", "owner");
411: repo.replaceFile(b2, is);
412: is = new ByteArrayInputStream(PAYLOAD);
413: RepoItem b4 = new RepoItem(RepoItem.ROOT, "a3", "owner");
414: repo.replaceFile(b4, is);
415:
416: RepoItem b3 = new RepoItem(RepoItem.ROOT, "b3.txt", "owner");
417:
418: try {
419: repo.copy(b3, f1);
420: } catch (RepoException ex) {
421: assertEquals(ex.getId(), "F080");
422: }
423:
424: try {
425: repo.copy(f1, b3);
426: } catch (RepoException ex) {
427: assertEquals(ex.getId(), "F081");
428: }
429:
430: try {
431: repo.copy(f1, b2);
432: } catch (RepoException ex) {
433: assertEquals(ex.getId(), "F082");
434: }
435:
436: try {
437: repo.copy(b4, f1);
438: } catch (RepoException ex) {
439: assertEquals(ex.getId(), "F083");
440: }
441:
442: File f = new File(getFileRoot(), "f1/b1.txt");
443: assertTrue("File doesn't exist", !f.exists());
444: repo.copy(b1, f1);
445: assertTrue("File copied", f.exists());
446:
447: repo.copy(f1, f2);
448:
449: Set s1 = new HashSet();
450: s1.add("/" + f1.getName());
451: flattenDir(repo, f1, "/" + f1.getName(), s1);
452: Set s2 = new HashSet();
453: flattenDir(repo, f2, "", s2);
454: assertEquals(s1, s2);
455:
456: }
457:
458: private void flattenDir(Repository repo, RepoItem f1, String depth,
459: Set set) throws RepoException {
460: RepoItem[] files = repo.getContent(f1);
461: for (int i = 0; i < files.length; i++) {
462: set.add(depth + "/" + files[i].getName());
463: if (files[i].isDirectory()) {
464: flattenDir(repo, files[i], depth + "/"
465: + files[i].getName(), set);
466: }
467: }
468: }
469:
470: public void testMove() throws Exception {
471: Repository repo = getRepository();
472: RepoItem f1 = new RepoItem(RepoItem.ROOT, "f1", "owner");
473: repo.addFolder(f1);
474: RepoItem f2 = new RepoItem(RepoItem.ROOT, "f2", "owner");
475: repo.addFolder(f2);
476: RepoItem f3 = new RepoItem(RepoItem.ROOT, "f3", "owner");
477: repo.addFolder(f3);
478: RepoItem a1 = new RepoItem(f1, "a1", "owner");
479: repo.addFolder(a1);
480: RepoItem a2 = new RepoItem(f1, "a2", "owner");
481: repo.addFolder(a2);
482: RepoItem a3 = new RepoItem(f1, "a3", "owner");
483: repo.addFolder(a3);
484: InputStream is = new ByteArrayInputStream(PAYLOAD);
485: RepoItem b1 = new RepoItem(RepoItem.ROOT, "b1.txt", "owner");
486: repo.replaceFile(b1, is);
487: is = new ByteArrayInputStream(PAYLOAD);
488: RepoItem b2 = new RepoItem(f1, "b2.txt", "owner");
489: repo.replaceFile(b2, is);
490: is = new ByteArrayInputStream(PAYLOAD);
491: RepoItem b4 = new RepoItem(RepoItem.ROOT, "b4.txt", "owner");
492: repo.replaceFile(b4, is);
493:
494: repo.move(f1, f2);
495:
496: File f = new File(getFileRoot(), "f1");
497: assertTrue("Original item is gone", !f.exists());
498: f = new File(getFileRoot(), "f2/f1");
499: assertTrue("Item in new place", f.exists());
500: }
501:
502: public void testRename() throws Exception {
503: Repository repo = getRepository();
504: RepoItem f1 = new RepoItem(RepoItem.ROOT, "f1", "owner");
505: repo.addFolder(f1);
506: RepoItem a1 = new RepoItem(f1, "a1", "owner");
507: repo.addFolder(a1);
508: InputStream is = new ByteArrayInputStream(PAYLOAD);
509: RepoItem b1 = new RepoItem(RepoItem.ROOT, "b1.txt", "owner");
510: repo.addFile(b1, is);
511: repo.rename(b1, "b2.txt");
512: File f = new File(getFileRoot(), "b2.txt");
513: assertTrue("Renamed file exists", f.exists());
514:
515: repo.rename(f1, "f2");
516: f = new File(getFileRoot(), "f2");
517: assertTrue("Renamed folder exists", f.exists());
518: }
519: }
|