1 17 package org.apache.geronimo.system.repository; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.util.Properties ; 22 import java.util.SortedSet ; 23 24 import junit.framework.TestCase; 25 import org.apache.geronimo.kernel.repository.Artifact; 26 import org.apache.geronimo.kernel.repository.ListableRepository; 27 import org.apache.geronimo.kernel.repository.Version; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 35 public class AbstractRepositoryTest extends TestCase { 36 private static final Log log = LogFactory.getLog(AbstractRepositoryTest.class); 37 38 protected ListableRepository repository; 39 protected File rootRepoDir; 40 41 protected void setUp() throws Exception { 42 super.setUp(); 43 deleteSVN(rootRepoDir); 45 } 46 47 private void deleteSVN(File dir) { 48 if(!dir.isDirectory() || !dir.canRead()) { 49 throw new IllegalStateException ("Invalid dir "+dir.getAbsolutePath()); 50 } 51 File [] children = dir.listFiles(); 52 for (int i = 0; i < children.length; i++) { 53 File child = children[i]; 54 if(child.isDirectory()) { 55 if(child.getName().equals(".svn")) { 56 recursiveDelete(child); 57 } else { 58 deleteSVN(child); 59 } 60 } 61 } 62 } 63 64 private void recursiveDelete(File dir) { 65 if(!dir.isDirectory() || !dir.canRead()) { 66 throw new IllegalStateException ("Invalid dir "+dir.getAbsolutePath()); 67 } 68 File [] children = dir.listFiles(); 69 for (int i = 0; i < children.length; i++) { 70 File child = children[i]; 71 if(child.isDirectory()) { 72 recursiveDelete(child); 73 } else { 74 if(!child.delete()) { 75 throw new IllegalStateException ("Cannot delete "+child.getAbsolutePath()); 76 } 77 } 78 } 79 if(!dir.delete()) { 80 throw new IllegalStateException ("Cannot delete "+dir.getAbsolutePath()); 81 } 82 } 83 84 public void testListAll() { 85 SortedSet artifacts = repository.list(); 86 log.debug("Matched artifacts: "+artifacts); 87 88 assertTrue(artifacts.contains(new Artifact("org.foo", "test", "2.0.1", "properties"))); 89 assertFalse(artifacts.contains(new Artifact("Unknown", "artifact", "2.0.1", "properties"))); 90 assertEquals(4, artifacts.size()); 91 } 92 93 public void testListSpecifyArtifact() { 94 SortedSet artifacts = repository.list(new Artifact(null, "test", (Version)null, null)); 95 96 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.5", "properties"))); 97 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.3", "properties"))); 98 assertTrue(artifacts.contains(new Artifact("org.foo", "test", "2.0.1", "properties"))); 99 assertEquals(3, artifacts.size()); 100 } 101 102 public void testListSpecifyArtifactGroup() { 103 SortedSet artifacts = repository.list(new Artifact("org.bar", "test", (Version)null, null)); 104 105 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.5", "properties"))); 106 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.3", "properties"))); 107 assertEquals(2, artifacts.size()); 108 } 109 110 public void testListSpecifyArtifactType() { 111 SortedSet artifacts = repository.list(new Artifact(null, "test", (Version)null, "properties")); 112 113 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.5", "properties"))); 114 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.3", "properties"))); 115 assertTrue(artifacts.contains(new Artifact("org.foo", "test", "2.0.1", "properties"))); 116 assertEquals(3, artifacts.size()); 117 } 118 119 public void testListSpecifyArtifactVersion() { 120 SortedSet artifacts = repository.list(new Artifact(null, "test", "1.5", null)); 121 122 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.5", "properties"))); 123 assertEquals(1, artifacts.size()); 124 } 125 126 public void testListSpecifyArtifactGroupType() { 127 SortedSet artifacts = repository.list(new Artifact("org.bar", "test", (Version)null, "properties")); 128 129 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.5", "properties"))); 130 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.3", "properties"))); 131 assertEquals(2, artifacts.size()); 132 } 133 134 public void testListSpecifyArtifactGroupVersion() { 135 SortedSet artifacts = repository.list(new Artifact("org.bar", "test", "1.5", null)); 136 137 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.5", "properties"))); 138 assertEquals(1, artifacts.size()); 139 } 140 141 public void testListSpecifyArtifactVersionType() { 142 SortedSet artifacts = repository.list(new Artifact(null, "test", "1.5", "properties")); 143 144 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.5", "properties"))); 145 assertEquals(1, artifacts.size()); 146 } 147 148 public void testListSpecifyAll() { 149 SortedSet artifacts = repository.list(new Artifact("org.bar", "test", "1.5", "properties")); 150 151 assertTrue(artifacts.contains(new Artifact("org.bar", "test", "1.5", "properties"))); 152 assertEquals(1, artifacts.size()); 153 } 154 155 public void testLocation() throws Exception { 156 Artifact artifact = new Artifact("org.foo", "test", "2.0.1", "properties"); 157 assertTrue(repository.contains(artifact)); 158 159 File location = repository.getLocation(artifact); 160 assertTrue(location.exists()); 161 assertTrue(location.isFile()); 162 assertTrue(location.canRead()); 163 164 Properties properties = new Properties (); 165 properties.load(new FileInputStream (location)); 166 assertEquals(artifact.getGroupId(), properties.get("groupId")); 167 assertEquals(artifact.getArtifactId(), properties.get("artifactId")); 168 assertEquals(artifact.getVersion().toString(), properties.get("version")); 169 assertEquals(artifact.getType(), properties.get("type")); 170 } 171 } 172 | Popular Tags |