001: package org.drools.scm.svn;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.File;
005: import java.io.FileInputStream;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.net.URL;
009: import java.util.ArrayList;
010: import java.util.Arrays;
011: import java.util.Collection;
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.List;
015: import java.util.Map;
016: import java.util.Set;
017:
018: import junit.framework.TestCase;
019:
020: //import org.apache.log4j.Logger;
021: import org.drools.scm.CompositeScmAction;
022: import org.drools.scm.ScmAction;
023: import org.drools.scm.ScmActionFactory;
024: import org.drools.scm.ScmEntry;
025: import org.drools.scm.svn.SvnActionFactory.AddDirectory;
026: import org.drools.scm.svn.SvnActionFactory.AddFile;
027: import org.drools.scm.svn.SvnActionFactory.CopyDirectory;
028: import org.drools.scm.svn.SvnActionFactory.CopyFile;
029: import org.drools.scm.svn.SvnActionFactory.DeleteDirectory;
030: import org.drools.scm.svn.SvnActionFactory.DeleteFile;
031: import org.drools.scm.svn.SvnActionFactory.MoveDirectory;
032: import org.drools.scm.svn.SvnActionFactory.MoveFile;
033: import org.drools.scm.svn.SvnActionFactory.UpdateFile;
034: import org.tmatesoft.svn.core.SVNLogEntry;
035: import org.tmatesoft.svn.core.SVNLogEntryPath;
036:
037: public class SvnActionFactoryTest extends TestCase {
038:
039: //private static Logger logger = Logger.getLogger( SvnActionFactoryTest.class );
040:
041: private static String svnUrl;
042:
043: public void setUp() throws IOException {
044: // First we need to find the absolute path
045: URL url = getClass().getResource("/svn_repo_empty");
046:
047: assertNotNull(url);
048: File src = new File(url.getFile());
049: File dst = new File(src.getParent(), "/copy_svn_repo_empty");
050:
051: // make sure the destination is empty before we copy
052: delete(dst);
053:
054: copy(src, dst);
055:
056: // Now set the two path roots
057: svnUrl = "file:///"
058: + dst.getAbsolutePath().replaceAll("\\\\", "/");
059: }
060:
061: public void tearDown() throws Exception {
062: URL url = getClass().getResource("/copy_svn_repo_empty");
063: delete(new File(url.getFile()));
064: }
065:
066: public void testCannotAddDirectoryWithNoParent() throws Exception {
067: ScmActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
068: "drools");
069:
070: CompositeScmAction actions = new CompositeScmAction();
071:
072: try {
073: ScmAction addDirectory = new AddDirectory("folder1",
074: "folder1_1");
075: actions.addScmAction(addDirectory);
076: svn.execute(actions, "test message");
077: fail("This should fail as 'folder1' has not yet been created");
078: } catch (Exception e) {
079:
080: }
081: }
082:
083: public void testCannotAddDuplicateDirectories() throws Exception {
084: ScmActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
085: "drools");
086:
087: CompositeScmAction actions = new CompositeScmAction();
088:
089: // Correctly add a new directory at root
090: actions = new CompositeScmAction();
091: ScmAction addDirectory = new AddDirectory("", "folder1");
092: actions.addScmAction(addDirectory);
093:
094: svn.execute(actions, "test message");
095:
096: // Check we can't add duplicate Directorys
097: try {
098: actions = new CompositeScmAction();
099: addDirectory = new AddDirectory("", "folder1");
100: actions.addScmAction(addDirectory);
101: svn.execute(actions, "test message");
102: fail("This should fail as 'folder1' already exists");
103: } catch (Exception e) {
104:
105: }
106: }
107:
108: public void testAddDirectories() throws Exception {
109: ScmActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
110: "drools");
111:
112: CompositeScmAction actions = new CompositeScmAction();
113:
114: // Correctly add a new Directory at root
115: actions = new CompositeScmAction();
116: ScmAction addDirectory = new AddDirectory("", "folder1");
117: actions.addScmAction(addDirectory);
118:
119: svn.execute(actions, "test message");
120:
121: // Now check various flat and deep Directory creations
122: actions = new CompositeScmAction();
123:
124: addDirectory = new AddDirectory("folder1", "folder1_1");
125: actions.addScmAction(addDirectory);
126:
127: addDirectory = new AddDirectory("folder1/folder1_1",
128: "folder1_1_1");
129: actions.addScmAction(addDirectory);
130:
131: addDirectory = new AddDirectory("folder1", "folder1_2");
132: actions.addScmAction(addDirectory);
133:
134: addDirectory = new AddDirectory("", "folder2/folder2_1");
135: actions.addScmAction(addDirectory);
136:
137: addDirectory = new AddDirectory("",
138: "folder3/folder3_1/folder3_1_1/folder3_1_1_1");
139: actions.addScmAction(addDirectory);
140:
141: svn.execute(actions, "test message");
142: //------
143: // Now test results
144: //-------
145: List list = convertToStringList(svn.listEntries(""));
146:
147: assertTrue(list.contains("folder1"));
148: assertTrue(list.contains("folder1/folder1_1"));
149: assertTrue(list.contains("folder1/folder1_2"));
150: assertTrue(list.contains("folder2/folder2_1"));
151: assertTrue(list
152: .contains("folder3/folder3_1/folder3_1_1/folder3_1_1_1"));
153: }
154:
155: public void testAddFiles() throws Exception {
156: ScmActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
157: "drools");
158:
159: CompositeScmAction actions = new CompositeScmAction();
160:
161: ScmAction addDirectory = new AddDirectory("", "folder1");
162: actions.addScmAction(addDirectory);
163:
164: ScmAction addFile = new AddFile("folder1", "file1.dat",
165: new byte[] { 1, 1, 1, 1 });
166: actions.addScmAction(addFile);
167:
168: addDirectory = new AddDirectory("folder1", "folder1_1");
169: actions.addScmAction(addDirectory);
170:
171: addFile = new AddFile("folder1/folder1_1", "file1_1.dat",
172: new byte[] { 0, 0, 0, 0 });
173: actions.addScmAction(addFile);
174:
175: svn.execute(actions, "test message");
176:
177: // Check the contents are correct
178: ByteArrayOutputStream baos = new ByteArrayOutputStream();
179: svn.getContent("folder1", "file1.dat", -1, baos);
180: assertTrue(Arrays.equals(new byte[] { 1, 1, 1, 1 }, baos
181: .toByteArray()));
182:
183: baos = new ByteArrayOutputStream();
184: svn.getContent("folder1/folder1_1", "file1_1.dat", -1, baos);
185: assertTrue(Arrays.equals(new byte[] { 0, 0, 0, 0 }, baos
186: .toByteArray()));
187:
188: // Check the directories are correctly created
189: List list = convertToStringList(svn.listEntries(""));
190: assertTrue(list.contains("folder1"));
191: assertTrue(list.contains("folder1/file1.dat"));
192: assertTrue(list.contains("folder1/folder1_1"));
193: assertTrue(list.contains("folder1/folder1_1/file1_1.dat"));
194: }
195:
196: public void testUpdateFile() throws Exception {
197: ScmActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
198: "drools");
199:
200: CompositeScmAction actions = new CompositeScmAction();
201:
202: ScmAction addDirectory = new AddDirectory("", "folder1");
203: actions.addScmAction(addDirectory);
204:
205: byte[] oldContent = new byte[] { 1, 1, 1, 1 };
206: byte[] newContent = new byte[] { 1, 0, 1, 0 };
207:
208: // Add the initial file
209: ScmAction addFile = new AddFile("folder1", "file1.dat",
210: oldContent);
211: actions.addScmAction(addFile);
212: svn.execute(actions, "test message");
213:
214: // Check the contents are correct
215: ByteArrayOutputStream baos = new ByteArrayOutputStream();
216: svn.getContent("folder1", "file1.dat", -1, baos);
217: assertTrue(Arrays.equals(oldContent, baos.toByteArray()));
218:
219: // Update the existing file
220: actions = new CompositeScmAction();
221: ScmAction updateFile = new UpdateFile("folder1", "file1.dat",
222: oldContent, newContent);
223: actions.addScmAction(updateFile);
224: svn.execute(actions, "test message");
225:
226: // Check the contents are correct
227: baos = new ByteArrayOutputStream();
228: svn.getContent("folder1", "file1.dat", -1, baos);
229: assertTrue(Arrays.equals(newContent, baos.toByteArray()));
230:
231: // Check the correct directory structue was created
232: List list = convertToStringList(svn.listEntries(""));
233: assertTrue(list.contains("folder1"));
234: assertTrue(list.contains("folder1/file1.dat"));
235: }
236:
237: public void testCopyFile() throws Exception {
238: ScmActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
239: "drools");
240:
241: CompositeScmAction actions = new CompositeScmAction();
242:
243: ScmAction addDirectory = new AddDirectory("", "folder1");
244: actions.addScmAction(addDirectory);
245: byte[] content = new byte[] { 1, 1, 1, 1 };
246: ScmAction addFile = new AddFile("folder1", "file1.dat", content);
247: actions.addScmAction(addFile);
248: svn.execute(actions, "test message");
249:
250: // Check the contents are correct
251: ByteArrayOutputStream baos = new ByteArrayOutputStream();
252: svn.getContent("folder1", "file1.dat", -1, baos);
253: assertTrue(Arrays.equals(content, baos.toByteArray()));
254:
255: List list = convertToStringList(svn.listEntries(""));
256: assertTrue(list.contains("folder1"));
257: assertTrue(list.contains("folder1/file1.dat"));
258: assertFalse(list.contains("folder2/file2.dat"));
259:
260: // Now copy the file
261: actions = new CompositeScmAction();
262: addDirectory = new AddDirectory("", "folder2");
263: actions.addScmAction(addDirectory);
264: ScmAction copyFile = new CopyFile("folder1", "file1.dat",
265: "folder2", "file2.dat", svn.getLatestRevision());
266: actions.addScmAction(copyFile);
267: svn.execute(actions, "test message");
268:
269: baos = new ByteArrayOutputStream();
270: svn.getContent("folder2", "file2.dat", -1, baos);
271: assertTrue(Arrays.equals(content, baos.toByteArray()));
272:
273: list = convertToStringList(svn.listEntries(""));
274: assertTrue(list.contains("folder1"));
275: assertTrue(list.contains("folder1/file1.dat"));
276: assertTrue(list.contains("folder2/file2.dat"));
277: }
278:
279: public void testCopyDirectory() throws Exception {
280: ScmActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
281: "drools");
282:
283: CompositeScmAction actions = new CompositeScmAction();
284:
285: ScmAction addDirectory = new AddDirectory("", "folder1");
286: actions.addScmAction(addDirectory);
287: byte[] content1 = new byte[] { 1, 1, 1, 1 };
288: ScmAction addFile = new AddFile("folder1", "file1.dat",
289: content1);
290: actions.addScmAction(addFile);
291:
292: addDirectory = new AddDirectory("folder1", "folder1_1");
293: actions.addScmAction(addDirectory);
294: byte[] content2 = new byte[] { 1, 0, 0, 1 };
295: addFile = new AddFile("folder1/folder1_1", "file1.dat",
296: content2);
297: actions.addScmAction(addFile);
298: svn.execute(actions, "test message");
299:
300: ByteArrayOutputStream baos = new ByteArrayOutputStream();
301: svn.getContent("folder1", "file1.dat", -1, baos);
302: assertTrue(Arrays.equals(content1, baos.toByteArray()));
303: baos = new ByteArrayOutputStream();
304: svn.getContent("folder1/folder1_1", "file1.dat", -1, baos);
305: assertTrue(Arrays.equals(content2, baos.toByteArray()));
306:
307: List list = convertToStringList(svn.listEntries(""));
308: assertTrue(list.contains("folder1"));
309: assertTrue(list.contains("folder1/folder1_1/file1.dat"));
310: assertFalse(list.contains("folder2/folder1/file1.dat"));
311:
312: // Now copy the directory
313: actions = new CompositeScmAction();
314: addDirectory = new AddDirectory("", "folder2");
315: actions.addScmAction(addDirectory);
316: ScmAction copyDirectory = new CopyDirectory("folder1",
317: "folder2/folder1", svn.getLatestRevision());
318: actions.addScmAction(copyDirectory);
319: svn.execute(actions, "test message");
320:
321: baos = new ByteArrayOutputStream();
322: svn.getContent("folder1", "file1.dat", -1, baos);
323: assertTrue(Arrays.equals(content1, baos.toByteArray()));
324: baos = new ByteArrayOutputStream();
325: svn.getContent("folder1/folder1_1", "file1.dat", -1, baos);
326: assertTrue(Arrays.equals(content2, baos.toByteArray()));
327: baos = new ByteArrayOutputStream();
328: svn.getContent("folder2/folder1", "file1.dat", -1, baos);
329: assertTrue(Arrays.equals(content1, baos.toByteArray()));
330: baos = new ByteArrayOutputStream();
331: svn.getContent("folder2/folder1/folder1_1", "file1.dat", -1,
332: baos);
333: assertTrue(Arrays.equals(content2, baos.toByteArray()));
334:
335: list = convertToStringList(svn.listEntries(""));
336: assertTrue(list.contains("folder1"));
337: assertTrue(list.contains("folder1/folder1_1/file1.dat"));
338: assertTrue(list.contains("folder2/folder1/file1.dat"));
339: }
340:
341: public void testMoveFile() throws Exception {
342: ScmActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
343: "drools");
344:
345: CompositeScmAction actions = new CompositeScmAction();
346:
347: ScmAction addDirectory = new AddDirectory("", "folder1");
348: actions.addScmAction(addDirectory);
349: byte[] content = new byte[] { 1, 1, 1, 1 };
350: ScmAction addFile = new AddFile("folder1", "file1.dat", content);
351: actions.addScmAction(addFile);
352: svn.execute(actions, "test message");
353:
354: ByteArrayOutputStream baos = new ByteArrayOutputStream();
355: svn.getContent("folder1", "file1.dat", -1, baos);
356: assertTrue(Arrays.equals(content, baos.toByteArray()));
357:
358: List list = convertToStringList(svn.listEntries(""));
359: assertTrue(list.contains("folder1"));
360: assertTrue(list.contains("folder1/file1.dat"));
361: assertFalse(list.contains("folder2/file2.dat"));
362:
363: // No do the file move
364: actions = new CompositeScmAction();
365: addDirectory = new AddDirectory("", "folder2");
366: actions.addScmAction(addDirectory);
367: MoveFile moveFile = new MoveFile("folder1", "file1.dat",
368: "folder2", "file2.dat", svn.getLatestRevision());
369: actions.addScmAction(moveFile);
370: svn.execute(actions, "test message");
371:
372: baos = new ByteArrayOutputStream();
373: svn.getContent("folder2", "file2.dat", -1, baos);
374: assertTrue(Arrays.equals(content, baos.toByteArray()));
375:
376: list = convertToStringList(svn.listEntries(""));
377: assertTrue(list.contains("folder1"));
378: assertFalse(list.contains("folder1/file1.dat"));
379: assertTrue(list.contains("folder2/file2.dat"));
380: }
381:
382: public void testMoveDirectory() throws Exception {
383: ScmActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
384: "drools");
385:
386: CompositeScmAction actions = new CompositeScmAction();
387:
388: ScmAction addDirectory = new AddDirectory("", "folder1");
389: actions.addScmAction(addDirectory);
390: byte[] content = new byte[] { 1, 1, 1, 1 };
391: ScmAction addFile = new AddFile("folder1", "file1.dat", content);
392: actions.addScmAction(addFile);
393: svn.execute(actions, "test message");
394:
395: // check the intial content and dir structure
396: ByteArrayOutputStream baos = new ByteArrayOutputStream();
397: svn.getContent("folder1", "file1.dat", -1, baos);
398: assertTrue(Arrays.equals(content, baos.toByteArray()));
399:
400: actions = new CompositeScmAction();
401: MoveDirectory moveDirectory = new MoveDirectory("folder1",
402: "folder2", svn.getLatestRevision());
403: actions.addScmAction(moveDirectory);
404: svn.execute(actions, "test message");
405:
406: // Check the moved content and dir structure
407: baos = new ByteArrayOutputStream();
408: svn.getContent("folder2", "file1.dat", -1, baos);
409: assertTrue(Arrays.equals(content, baos.toByteArray()));
410:
411: List list = convertToStringList(svn.listEntries(""));
412:
413: assertFalse(list.contains("folder1"));
414: assertFalse(list.contains("folder1/file1.dat"));
415: assertTrue(list.contains("folder2/file1.dat"));
416: }
417:
418: public void testDeleteFile() throws Exception {
419: ScmActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
420: "drools");
421:
422: CompositeScmAction actions = new CompositeScmAction();
423:
424: ScmAction addDirectory = new AddDirectory("", "folder1");
425: actions.addScmAction(addDirectory);
426: byte[] content = new byte[] { 1, 1, 1, 1 };
427: ScmAction addFile = new AddFile("folder1", "file1.dat", content);
428: actions.addScmAction(addFile);
429: svn.execute(actions, "test message");
430: List list = convertToStringList(svn.listEntries(""));
431: assertTrue(list.contains("folder1"));
432: assertTrue(list.contains("folder1/file1.dat"));
433:
434: // Now do the file delete
435: actions = new CompositeScmAction();
436: ScmAction deleteFile = new DeleteFile("folder1", "file1.dat");
437: actions.addScmAction(deleteFile);
438: svn.execute(actions, "test message");
439: list = convertToStringList(svn.listEntries(""));
440: assertTrue(list.contains("folder1"));
441: assertFalse(list.contains("folder1/file1.dat"));
442: }
443:
444: public void testDeleteDirectory() throws Exception {
445: ScmActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
446: "drools");
447:
448: CompositeScmAction actions = new CompositeScmAction();
449:
450: ScmAction addDirectory = new AddDirectory("", "folder1");
451: actions.addScmAction(addDirectory);
452: byte[] content = new byte[] { 1, 1, 1, 1 };
453: ScmAction addFile = new AddFile("folder1", "file1.dat", content);
454: actions.addScmAction(addFile);
455: addDirectory = new AddDirectory("", "folder2");
456: actions.addScmAction(addDirectory);
457: svn.execute(actions, "test message");
458: List list = convertToStringList(svn.listEntries(""));
459: assertTrue(list.contains("folder1"));
460: assertTrue(list.contains("folder1/file1.dat"));
461: assertTrue(list.contains("folder2"));
462:
463: // now do the directory delete
464: actions = new CompositeScmAction();
465: ScmAction deleteDirectory = new DeleteDirectory("folder1");
466: actions.addScmAction(deleteDirectory);
467: svn.execute(actions, "test message");
468: list = convertToStringList(svn.listEntries(""));
469: assertFalse(list.contains("folder1"));
470: assertFalse(list.contains("folder1/file1.dat"));
471: assertTrue(list.contains("folder2"));
472: }
473:
474: public void XXXtestHistory() throws Exception {
475: SvnActionFactory svn = new SvnActionFactory(svnUrl, "mrtrout",
476: "drools");
477:
478: CompositeScmAction actions = new CompositeScmAction();
479:
480: ScmAction addDirectory = new AddDirectory("", "folder1");
481: actions.addScmAction(addDirectory);
482: byte[] content = new byte[] { 1, 1, 1, 1 };
483: ScmAction addFile = new AddFile("folder1", "file1.dat", content);
484: actions.addScmAction(addFile);
485: svn.execute(actions, "test message");
486:
487: actions = new CompositeScmAction();
488: MoveDirectory moveDirectory = new MoveDirectory("folder1",
489: "folder2", svn.getLatestRevision());
490: actions.addScmAction(moveDirectory);
491: svn.execute(actions, "test message");
492:
493: Collection collection = svn.log(new String[] { "" }, 0, -1);
494: for (Iterator it = collection.iterator(); it.hasNext();) {
495: SVNLogEntry logEntry = (SVNLogEntry) it.next();
496: Map map = logEntry.getChangedPaths();
497: Set changePathSet = map.keySet();
498: for (Iterator it2 = changePathSet.iterator(); it2.hasNext();) {
499: SVNLogEntryPath entryPath = (SVNLogEntryPath) map
500: .get(it2.next());
501: System.out.println(entryPath);
502: }
503: }
504:
505: }
506:
507: public static void copy(File src, File dest) throws IOException {
508: if (src.isDirectory()) {
509: dest.mkdirs();
510: String list[] = src.list();
511:
512: for (int i = 0; i < list.length; i++) {
513: String dest1 = dest.getPath() + File.separator
514: + list[i];
515: String src1 = src.getPath() + File.separator + list[i];
516: copy(new File(src1), new File(dest1));
517: }
518: } else {
519:
520: FileInputStream fin = new FileInputStream(src);
521: FileOutputStream fout = new FileOutputStream(dest);
522: int c;
523: while ((c = fin.read()) >= 0)
524: fout.write(c);
525: fin.close();
526: fout.close();
527: }
528: }
529:
530: public static void delete(File src) throws IOException {
531: if (src.isDirectory()) {
532: String list[] = src.list();
533:
534: for (int i = 0; i < list.length; i++) {
535: String src1 = src.getPath() + File.separator + list[i];
536: delete(new File(src1));
537: }
538: src.delete();
539: } else {
540: src.delete();
541: }
542: }
543:
544: public static List convertToStringList(List list) {
545: List files = new ArrayList(list.size());
546:
547: for (Iterator it = list.iterator(); it.hasNext();) {
548: ScmEntry entry = (ScmEntry) it.next();
549: files.add(entry.getPath().equals("") ? entry.getName()
550: : entry.getPath() + "/" + entry.getName());
551: }
552: return files;
553: }
554:
555: public static Map convertToMap(List list) {
556: Map map = new HashMap(list.size());
557:
558: for (Iterator it = list.iterator(); it.hasNext();) {
559: ScmEntry entry = (ScmEntry) it.next();
560: if (entry.isDirectory()) {
561: map.put(entry.getPath().equals("") ? entry.getName()
562: : entry.getPath() + "/" + entry.getName(),
563: entry);
564: } else {
565: List files = (List) map.get(entry.getPath());
566: if (files == null) {
567: files = new ArrayList();
568: map.put(entry.getPath(), files);
569: }
570:
571: files.add(entry);
572: }
573: }
574: return map;
575: }
576: }
|