1 17 package org.apache.geronimo.kernel.repository; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.util.Collections ; 24 import java.util.HashSet ; 25 import java.util.LinkedHashSet ; 26 import java.util.Set ; 27 import java.util.SortedSet ; 28 import java.util.TreeSet ; 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 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 { 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 { 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 { 73 ArtifactManager artifactManager = new DefaultArtifactManager(); 74 Set loaded = new HashSet (); 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 { 88 MockRepository mockRepository = new MockRepository(); 89 90 ArtifactManager artifactManager = new DefaultArtifactManager(); 91 Set loaded = new HashSet (); 92 loaded.add(version1); 93 loaded.add(version2); 94 artifactManager.loadArtifacts(loader, loaded); 95 96 ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, mockRepository); 97 98 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 ("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 parents = new LinkedHashSet (); 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 list() { 125 throw new UnsupportedOperationException (); 126 } 127 128 public SortedSet list(Artifact query) { 129 TreeSet set = new TreeSet (); 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 getLocation(Artifact artifact) { 141 return new File ("."); 142 } 143 144 public LinkedHashSet getDependencies(Artifact artifact) { 145 return new LinkedHashSet (); 146 } 147 } 148 149 public static class MockConfigStore extends NullConfigurationStore { 150 URL baseURL; 151 152 public MockConfigStore() { 153 } 154 155 public MockConfigStore(URL baseURL) { 156 this.baseURL = baseURL; 157 } 158 159 public ConfigurationData loadConfiguration(Artifact configId) throws NoSuchConfigException, IOException , 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 resolve(Artifact configId, String moduleName, String pattern) throws NoSuchConfigException, MalformedURLException { 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 |