| 1 7 package com.inversoft.savant.test; 8 9 10 import java.io.File ; 11 import java.util.Date ; 12 13 import junit.framework.TestCase; 14 15 import com.inversoft.savant.Artifact; 16 import com.inversoft.savant.LocalCacheStore; 17 18 19 26 public class LocalCacheStoreTest extends TestCase { 27 28 31 public LocalCacheStoreTest(String name) { 32 super(name); 33 } 34 35 36 39 public void testConstruction() throws Exception { 40 File file = new File ("build.xml"); 41 try { 42 new LocalCacheStore(file); 43 fail("Should have thrown exception"); 44 } catch (Exception e) { 45 } 47 48 System.setProperty("savant.local.repository", File.separator + "savant"); 49 LocalCacheStore store = new LocalCacheStore(null); 50 File 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 63 public void testFind() throws Exception { 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 file = new File ("test/savant"); 72 LocalCacheStore store = new LocalCacheStore(file); 73 File artFile = store.find(artifact); 74 assertNotNull(artFile); 75 assertTrue(artFile.getAbsolutePath().endsWith(artifact.getArtifactFile())); 76 } 77 78 81 public void testFindExpireMinutes() throws Exception { 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 File dir = new File ("test/savant"); 91 LocalCacheStore store = new LocalCacheStore(dir); 92 final File artFile = store.find(artifact); 93 assertNotNull(artFile); 94 assertTrue(artFile.setLastModified(System.currentTimeMillis() - (3600000 * 3))); 95 96 artifact.setExpireminutes(120); File file = store.find(artifact); 99 assertNull(file); 100 101 assertTrue(artFile.setLastModified(System.currentTimeMillis() - (3600000 * 1))); 103 file = store.find(artifact); 104 assertNotNull(file); 105 } 106 107 110 public void testFindExpireTime() throws Exception { 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 dir = new File ("test/savant"); 119 LocalCacheStore store = new LocalCacheStore(dir); 120 121 long fiveHoursAgo = System.currentTimeMillis() - (3600000 * 5); 123 artifact.setExpiretime(new Date (fiveHoursAgo)); 124 File file = store.find(artifact); 125 assertNull(file); 126 } 127 128 131 public void testStore() throws Exception { 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 artFile = new File ("test/savant", artifact.getArtifactFile()); 140 File dir = new File ("test/cache"); 141 LocalCacheStore store = new LocalCacheStore(dir); 142 File remove = store.find(artifact); 143 if (remove != null) { 144 remove.delete(); 145 } 146 147 File cached = new File ("test/cache", artifact.getArtifactFile()); 148 store.store(artifact, artFile); 149 assertTrue(cached.exists() && cached.isFile()); 150 } 151 152 155 public void testStoreCreateDirs() throws Exception { 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 File cacheFile = new File ("test/cache", artifact.getArtifactFile()); 165 File parent = cacheFile.getParentFile(); 166 File [] cacheFiles = parent.listFiles(); 167 for (int i = 0; i < cacheFiles.length; i++) { 168 cacheFiles[i].delete(); 169 } 170 parent.delete(); 171 172 File dir = new File ("test/cache"); 173 LocalCacheStore store = new LocalCacheStore(dir); 174 175 File artFile = new File ("test/savant", artifact.getArtifactFile()); 176 store.store(artifact, artFile); 177 assertTrue(cacheFile.exists() && cacheFile.isFile()); 178 } 179 } | Popular Tags |