KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > savant > test > LocalCacheStoreTest


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.savant.test;
8
9
10 import java.io.File JavaDoc;
11 import java.util.Date JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import com.inversoft.savant.Artifact;
16 import com.inversoft.savant.LocalCacheStore;
17
18
19 /**
20  * <p>
21  * This class is the test for the LocalCacheStore.
22  * </p>
23  *
24  * @author Brian Pontarelli
25  */

26 public class LocalCacheStoreTest extends TestCase {
27
28     /**
29      * Constructs a new <code>LocalCacheStoreTest</code>.
30      */

31     public LocalCacheStoreTest(String JavaDoc name) {
32         super(name);
33     }
34
35
36     /**
37      * Tests the construction of the LocalCacheStore.
38      */

39     public void testConstruction() throws Exception JavaDoc {
40         File JavaDoc file = new File JavaDoc("build.xml");
41         try {
42             new LocalCacheStore(file);
43             fail("Should have thrown exception");
44         } catch (Exception JavaDoc e) {
45             // Expected
46
}
47
48         System.setProperty("savant.local.repository", File.separator + "savant");
49         LocalCacheStore store = new LocalCacheStore(null);
50         File JavaDoc location = store.getLocation();
51         assertTrue(location.getAbsolutePath().endsWith(File.separator + "savant"));
52
53         System.getProperties().remove("savant.local.repository");
54         store = new LocalCacheStore(null);
55         location = store.getLocation();
56         assertEquals(System.getProperty("user.home") + File.separator + ".savant_repository",
57             location.getAbsolutePath());
58     }
59
60     /**
61      * Tests the find method.
62      */

63     public void testFind() throws Exception JavaDoc {
64         Artifact artifact = new Artifact();
65         artifact.setGroup("my_group");
66         artifact.setName("my_name");
67         artifact.setProjectname("my_project");
68         artifact.setType("exe");
69         artifact.setVersion("2.0");
70
71         File JavaDoc file = new File JavaDoc("test/savant");
72         LocalCacheStore store = new LocalCacheStore(file);
73         File JavaDoc artFile = store.find(artifact);
74         assertNotNull(artFile);
75         assertTrue(artFile.getAbsolutePath().endsWith(artifact.getArtifactFile()));
76     }
77
78     /**
79      * Tests the expiration using minutes.
80      */

81     public void testFindExpireMinutes() throws Exception JavaDoc {
82         Artifact artifact = new Artifact();
83         artifact.setGroup("my_group");
84         artifact.setName("my_name");
85         artifact.setProjectname("my_project");
86         artifact.setType("exe");
87         artifact.setVersion("2.0");
88
89         // Set the files time to a few hours ago
90
File JavaDoc dir = new File JavaDoc("test/savant");
91         LocalCacheStore store = new LocalCacheStore(dir);
92         final File JavaDoc artFile = store.find(artifact);
93         assertNotNull(artFile);
94         assertTrue(artFile.setLastModified(System.currentTimeMillis() - (3600000 * 3)));
95
96         // Fetch with expire
97
artifact.setExpireminutes(120); // 2 hours
98
File JavaDoc file = store.find(artifact);
99         assertNull(file);
100
101         // Fetch without expire
102
assertTrue(artFile.setLastModified(System.currentTimeMillis() - (3600000 * 1)));
103         file = store.find(artifact);
104         assertNotNull(file);
105     }
106
107     /**
108      * Tests the expiration using time.
109      */

110     public void testFindExpireTime() throws Exception JavaDoc {
111         Artifact artifact = new Artifact();
112         artifact.setGroup("my_group");
113         artifact.setName("my_name");
114         artifact.setProjectname("my_project");
115         artifact.setType("exe");
116         artifact.setVersion("2.0");
117
118         File JavaDoc dir = new File JavaDoc("test/savant");
119         LocalCacheStore store = new LocalCacheStore(dir);
120
121         // Fetch with an expire time
122
long fiveHoursAgo = System.currentTimeMillis() - (3600000 * 5);
123         artifact.setExpiretime(new Date JavaDoc(fiveHoursAgo));
124         File JavaDoc file = store.find(artifact);
125         assertNull(file);
126     }
127
128     /**
129      * Tests the storing of the artifact to the local cache.
130      */

131     public void testStore() throws Exception JavaDoc {
132         Artifact artifact = new Artifact();
133         artifact.setGroup("my_group");
134         artifact.setName("my_name");
135         artifact.setProjectname("my_project");
136         artifact.setType("exe");
137         artifact.setVersion("2.0");
138
139         File JavaDoc artFile = new File JavaDoc("test/savant", artifact.getArtifactFile());
140         File JavaDoc dir = new File JavaDoc("test/cache");
141         LocalCacheStore store = new LocalCacheStore(dir);
142         File JavaDoc remove = store.find(artifact);
143         if (remove != null) {
144             remove.delete();
145         }
146
147         File JavaDoc cached = new File JavaDoc("test/cache", artifact.getArtifactFile());
148         store.store(artifact, artFile);
149         assertTrue(cached.exists() && cached.isFile());
150     }
151
152     /**
153      * Tests the storing of the artifact to the local cache.
154      */

155     public void testStoreCreateDirs() throws Exception JavaDoc {
156         Artifact artifact = new Artifact();
157         artifact.setGroup("my_group");
158         artifact.setName("my_name");
159         artifact.setProjectname("my_project");
160         artifact.setType("exe");
161         artifact.setVersion("2.0");
162
163         // Prune one directory
164
File JavaDoc cacheFile = new File JavaDoc("test/cache", artifact.getArtifactFile());
165         File JavaDoc parent = cacheFile.getParentFile();
166         File JavaDoc[] cacheFiles = parent.listFiles();
167         for (int i = 0; i < cacheFiles.length; i++) {
168             cacheFiles[i].delete();
169         }
170         parent.delete();
171
172         File JavaDoc dir = new File JavaDoc("test/cache");
173         LocalCacheStore store = new LocalCacheStore(dir);
174
175         File JavaDoc artFile = new File JavaDoc("test/savant", artifact.getArtifactFile());
176         store.store(artifact, artFile);
177         assertTrue(cacheFile.exists() && cacheFile.isFile());
178     }
179 }
Popular Tags