KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > repository > AbstractRepositoryTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.system.repository;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.SortedSet JavaDoc;
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 /**
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  */

35 public class AbstractRepositoryTest extends TestCase {
36     private static final Log log = LogFactory.getLog(AbstractRepositoryTest.class);
37     
38     protected ListableRepository repository;
39     protected File JavaDoc rootRepoDir;
40
41     protected void setUp() throws Exception JavaDoc {
42         super.setUp();
43         // Don't want .svn dirs messing up my count!
44
deleteSVN(rootRepoDir);
45     }
46
47     private void deleteSVN(File JavaDoc dir) {
48         if(!dir.isDirectory() || !dir.canRead()) {
49             throw new IllegalStateException JavaDoc("Invalid dir "+dir.getAbsolutePath());
50         }
51         File JavaDoc[] children = dir.listFiles();
52         for (int i = 0; i < children.length; i++) {
53             File JavaDoc 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 JavaDoc dir) {
65         if(!dir.isDirectory() || !dir.canRead()) {
66             throw new IllegalStateException JavaDoc("Invalid dir "+dir.getAbsolutePath());
67         }
68         File JavaDoc[] children = dir.listFiles();
69         for (int i = 0; i < children.length; i++) {
70             File JavaDoc child = children[i];
71             if(child.isDirectory()) {
72                 recursiveDelete(child);
73             } else {
74                 if(!child.delete()) {
75                     throw new IllegalStateException JavaDoc("Cannot delete "+child.getAbsolutePath());
76                 }
77             }
78         }
79         if(!dir.delete()) {
80             throw new IllegalStateException JavaDoc("Cannot delete "+dir.getAbsolutePath());
81         }
82     }
83
84     public void testListAll() {
85         SortedSet JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc {
156         Artifact artifact = new Artifact("org.foo", "test", "2.0.1", "properties");
157         assertTrue(repository.contains(artifact));
158
159         File JavaDoc location = repository.getLocation(artifact);
160         assertTrue(location.exists());
161         assertTrue(location.isFile());
162         assertTrue(location.canRead());
163
164         Properties JavaDoc properties = new Properties JavaDoc();
165         properties.load(new FileInputStream JavaDoc(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