KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > repository > ArtifactResolverTest


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.kernel.repository;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.LinkedHashSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.SortedSet JavaDoc;
28 import java.util.TreeSet JavaDoc;
29 import junit.framework.TestCase;
30 import org.apache.geronimo.gbean.GBeanInfo;
31 import org.apache.geronimo.gbean.GBeanInfoBuilder;
32 import org.apache.geronimo.kernel.Jsr77Naming;
33 import org.apache.geronimo.kernel.config.Configuration;
34 import org.apache.geronimo.kernel.config.ConfigurationData;
35 import org.apache.geronimo.kernel.config.ConfigurationResolver;
36 import org.apache.geronimo.kernel.config.ConfigurationStore;
37 import org.apache.geronimo.kernel.config.InvalidConfigException;
38 import org.apache.geronimo.kernel.config.NoSuchConfigException;
39 import org.apache.geronimo.kernel.config.NullConfigurationStore;
40
41 /**
42  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
43  */

44 public class ArtifactResolverTest extends TestCase {
45     private final Artifact loader = new Artifact("loader", "loader", "1", "car");
46     private final Artifact version1 = new Artifact("version", "version", "1", "jar");
47     private final Artifact version2 = new Artifact("version", "version", "2", "jar");
48     private final Artifact version3 = new Artifact("version", "version", "3", "jar");
49
50     public void testSelectHighestFromRepo() throws Exception JavaDoc {
51         ArtifactManager artifactManager = new DefaultArtifactManager();
52         MockRepository mockRepository = new MockRepository();
53         ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, mockRepository);
54
55         Artifact unresolvedArtifact = new Artifact("version", "version", (Version) null, "jar");
56         Artifact artifact = artifactResolver.resolveInClassLoader(unresolvedArtifact);
57         assertEquals(version3, artifact);
58     }
59
60     public void testAlreadyLoaded() throws Exception JavaDoc {
61         ArtifactManager artifactManager = new DefaultArtifactManager();
62         artifactManager.loadArtifacts(loader, Collections.singleton(version2));
63
64         MockRepository mockRepository = new MockRepository();
65         ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, mockRepository);
66
67         Artifact unresolvedArtifact = new Artifact("version", "version", (Version) null, "jar");
68         Artifact artifact = artifactResolver.resolveInClassLoader(unresolvedArtifact);
69         assertEquals(version2, artifact);
70     }
71
72     public void testMultipleSelected() throws Exception JavaDoc {
73         ArtifactManager artifactManager = new DefaultArtifactManager();
74         Set JavaDoc loaded = new HashSet JavaDoc();
75         loaded.add(version1);
76         loaded.add(version2);
77         artifactManager.loadArtifacts(loader, loaded);
78
79         MockRepository mockRepository = new MockRepository();
80         ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, mockRepository);
81
82         Artifact unresolvedArtifact = new Artifact("version", "version", (Version) null, "jar");
83         Artifact artifact = artifactResolver.resolveInClassLoader(unresolvedArtifact);
84         assertEquals(version2, artifact);
85     }
86
87     public void testParentLoaded() throws Exception JavaDoc {
88         MockRepository mockRepository = new MockRepository();
89
90         ArtifactManager artifactManager = new DefaultArtifactManager();
91         Set JavaDoc loaded = new HashSet JavaDoc();
92         loaded.add(version1);
93         loaded.add(version2);
94         artifactManager.loadArtifacts(loader, loaded);
95
96         ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, mockRepository);
97
98         // create parent which uses version1 explicitly
99
Environment environment = new Environment();
100         environment.setConfigId(loader);
101         environment.addDependency(version1, ImportType.CLASSES);
102
103         ConfigurationData parentConfigurationData = new ConfigurationData(environment, new Jsr77Naming());
104         parentConfigurationData.setConfigurationStore(new MockConfigStore(new File JavaDoc("foo").toURL()));
105
106         ConfigurationResolver configurationResolver = new ConfigurationResolver(parentConfigurationData,
107                 Collections.singleton(mockRepository),
108                 artifactResolver);
109
110         Configuration parent = new Configuration(null,
111                 parentConfigurationData,
112                 configurationResolver,
113                 null);
114
115         LinkedHashSet JavaDoc parents = new LinkedHashSet JavaDoc();
116         parents.add(parent);
117
118         Artifact unresolvedArtifact = new Artifact("version", "version", (Version) null, "jar");
119         Artifact artifact = artifactResolver.resolveInClassLoader(unresolvedArtifact, parents);
120         assertEquals(version1, artifact);
121     }
122
123     private class MockRepository implements ListableRepository {
124         public SortedSet JavaDoc list() {
125             throw new UnsupportedOperationException JavaDoc();
126         }
127
128         public SortedSet JavaDoc list(Artifact query) {
129             TreeSet JavaDoc set = new TreeSet JavaDoc();
130             set.add(version1);
131             set.add(version2);
132             set.add(version3);
133             return set;
134         }
135
136         public boolean contains(Artifact artifact) {
137             return true;
138         }
139
140         public File JavaDoc getLocation(Artifact artifact) {
141             return new File JavaDoc(".");
142         }
143
144         public LinkedHashSet JavaDoc getDependencies(Artifact artifact) {
145             return new LinkedHashSet JavaDoc();
146         }
147     }
148
149     public static class MockConfigStore extends NullConfigurationStore {
150         URL JavaDoc baseURL;
151
152         public MockConfigStore() {
153         }
154
155         public MockConfigStore(URL JavaDoc baseURL) {
156             this.baseURL = baseURL;
157         }
158
159         public ConfigurationData loadConfiguration(Artifact configId) throws NoSuchConfigException, IOException JavaDoc, InvalidConfigException {
160             ConfigurationData configurationData = new ConfigurationData(configId, new Jsr77Naming());
161             configurationData.setConfigurationStore(this);
162             return configurationData;
163         }
164
165         public boolean containsConfiguration(Artifact configID) {
166             return true;
167         }
168
169         public Set JavaDoc resolve(Artifact configId, String JavaDoc moduleName, String JavaDoc pattern) throws NoSuchConfigException, MalformedURLException JavaDoc {
170             return Collections.singleton(baseURL);
171         }
172
173         public final static GBeanInfo GBEAN_INFO;
174
175         static {
176             GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(MockConfigStore.class, "ConfigurationStore");
177             infoBuilder.addInterface(ConfigurationStore.class);
178             GBEAN_INFO = infoBuilder.getBeanInfo();
179         }
180     }
181 }
182
Popular Tags