KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cvsgrab > LocalRepositoryTest


1 package net.sourceforge.cvsgrab;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.util.Properties JavaDoc;
7
8
9 /**
10  * Test for LocalRepository
11  *
12  * @author <a HREF="mailto:ludovicc@users.sourceforge.net">Ludovic Claude</a>
13  * @version $Revision: 1.1 $ $Date: 2004/05/11 19:20:18 $
14  * @created on 8 mars 2004
15  */

16 public class LocalRepositoryTest extends AbstractTestCase {
17
18     private LocalRepository localRepository;
19     private File JavaDoc localRepoDir;
20     private RemoteRepository remoteRepository;
21     
22     /**
23      * Constructor for LocalRepositoryTest
24      * @param name
25      */

26     public LocalRepositoryTest(String JavaDoc name) {
27         super(name);
28     }
29     
30     /**
31      * {@inheritDoc}
32      * @throws java.lang.Exception
33      */

34     protected void setUp() throws Exception JavaDoc {
35         super.setUp();
36         File JavaDoc localRootDir = new File JavaDoc(basedir, "target/testRepo");
37         CVSGrab cvsGrab = new CVSGrab();
38         cvsGrab.setCvsRoot("testRoot");
39         cvsGrab.setDestDir(localRootDir.getPath());
40         cvsGrab.getWebOptions().setRootUrl("rootUrl");
41         cvsGrab.getWebOptions().setPackagePath("package");
42         cvsGrab.setCleanUpdate(true);
43         localRepoDir = new File JavaDoc(localRootDir, "package");
44         localRepoDir.mkdirs();
45         localRepository = new LocalRepository(cvsGrab);
46         remoteRepository = new RemoteRepository("testRoot", localRepository);
47         File JavaDoc file = new File JavaDoc(basedir, "src/conf/FileTypes.properties");
48         Properties JavaDoc fileTypes = new Properties JavaDoc();
49         InputStream JavaDoc is = new FileInputStream JavaDoc(file);
50         fileTypes.load(is);
51         RemoteFile.setFileTypes(fileTypes);
52     }
53
54     /**
55      * {@inheritDoc}
56      * @throws java.lang.Exception
57      */

58     protected void tearDown() throws Exception JavaDoc {
59         super.tearDown();
60         cleanDir(localRepoDir);
61         localRepoDir.delete();
62     }
63     
64     private void cleanDir(File JavaDoc dir) {
65         final File JavaDoc[] contents = dir.listFiles();
66         if (contents == null) {
67             return;
68         }
69
70         for (int i = 0; i < contents.length; i++) {
71             if (contents[i].isDirectory()) {
72                 cleanDir(contents[i]);
73             }
74             contents[i].delete();
75         }
76     }
77     
78     public void testPruneEmptyDirs() throws Exception JavaDoc {
79         RemoteDirectory dirRootRemote = new RemoteDirectory(remoteRepository, "package", "package");
80         localRepository.add(dirRootRemote);
81         File JavaDoc a = new File JavaDoc(localRepoDir, "a.txt");
82         a.createNewFile();
83         RemoteFile aRemote = new RemoteFile("a.txt", "1.1");
84         aRemote.setDirectory(dirRootRemote);
85         localRepository.updateFileVersion(aRemote);
86         File JavaDoc dir1 = new File JavaDoc(localRepoDir, "dir1");
87         dir1.mkdir();
88         RemoteDirectory dir1Remote = new RemoteDirectory(dirRootRemote, "dir1");
89         localRepository.add(dir1Remote);
90         File JavaDoc b = new File JavaDoc(dir1, "b.txt");
91         b.createNewFile();
92         RemoteFile bRemote = new RemoteFile("b.txt", "1.1");
93         bRemote.setDirectory(dir1Remote);
94         localRepository.updateFileVersion(bRemote);
95         
96         File JavaDoc dir2 = new File JavaDoc(localRepoDir, "dir2");
97         dir2.mkdir();
98         RemoteDirectory dir2Remote = new RemoteDirectory(dirRootRemote, "dir2");
99         localRepository.add(dir2Remote);
100         
101         File JavaDoc dir21 = new File JavaDoc(dir2, "dir21");
102         dir21.mkdir();
103         RemoteDirectory dir21Remote = new RemoteDirectory(dir2Remote, "dir21");
104         localRepository.add(dir21Remote);
105         
106         File JavaDoc c = new File JavaDoc(dir21, "c.txt");
107         c.createNewFile();
108         RemoteFile cRemote = new RemoteFile("c.txt", "1.1");
109         cRemote.setDirectory(dir21Remote);
110         localRepository.updateFileVersion(cRemote);
111         
112         c.delete();
113         localRepository.cleanRemovedFiles(dir21Remote);
114         
115         File JavaDoc dir3 = new File JavaDoc(localRepoDir, "dir3");
116         dir3.mkdir();
117         RemoteDirectory dir3Remote = new RemoteDirectory(dirRootRemote, "dir3");
118         localRepository.add(dir3Remote);
119         
120         // Add a file in dir3 but don't manage it with cvs
121
File JavaDoc d = new File JavaDoc(dir3, "d.txt");
122         d.createNewFile();
123         
124         localRepository.pruneEmptyDirectories();
125         
126         assertFalse(dir21.exists());
127         assertFalse(dir2.exists());
128         assertTrue(dir1.exists());
129         assertTrue(dir3.exists());
130         assertTrue(d.exists());
131     }
132 }
133
Popular Tags